diff --git a/R/plot_fixefs.R b/R/plot_fixefs.R index fcd5e04..25a1eb0 100644 --- a/R/plot_fixefs.R +++ b/R/plot_fixefs.R @@ -132,3 +132,4 @@ plot_fixefs.merMod <- function(model, } } +# plot_fixefs.glmerMod <- plot_fixefs.merMod diff --git a/tests/testthat/helper_load_data.R b/tests/testthat/helper_load_data.R new file mode 100644 index 0000000..67b43ac --- /dev/null +++ b/tests/testthat/helper_load_data.R @@ -0,0 +1 @@ +load('brms_res.RData') diff --git a/tests/testthat/test_plot_coefficients.R b/tests/testthat/test_plot_coefficients.R new file mode 100644 index 0000000..1c184c5 --- /dev/null +++ b/tests/testthat/test_plot_coefficients.R @@ -0,0 +1,136 @@ +context('test coefficient plots') + + +test_lm = lm(mpg ~ ., mtcars) + +x = rnorm(100) +q = rnorm(100) +z = rnorm(100) +y = rpois(100, exp(.25*x + .1*q - .5*z)) +test_glm = glm(y ~ x + q + z, family = poisson) + +fit_mer = lme4::lmer(Reaction ~ Days + (Days|Subject), lme4::sleepstudy) +fit_mer2 = lme4::lmer(count ~ log_Age_c + log_Base4_c * Trt + (1 | patient), brms::epilepsy) +mer_re = plot_coefficients(fit_mer, ranef = T, which_ranef = 'Subject') + +test_that('It works', { + expect_s3_class(mer_re[[1]], 'ggplot') +}) + +test_that('It works', { + expect_s3_class(plot_coefficients(test_lm, plot = FALSE), 'data.frame') +}) + + + +# test lm glm coefficients ------------------------------------------------ + +test_that('test plot_coefficients.lm', { + expect_s3_class(plot_coefficients(test_lm), 'ggplot') +}) + +test_that('test palette', { + expect_s3_class(plot_coefficients(test_lm, palette = 'oslo'), 'ggplot') +}) + +test_that('test order numeric', { + expect_s3_class(plot_coefficients(test_lm, order = sample(1:10)), 'ggplot') +}) + +test_that('test order increasing', { + expect_s3_class(plot_coefficients(test_lm, order = 'increasing'), 'ggplot') +}) + + +test_that('test plot_coefficients.glm and trans', { + expect_s3_class(plot_coefficients(test_glm, trans = exp, ref_line = 1), 'ggplot') +}) + + + + + +# test lme4 --------------------------------------------------------------- + +# test fixed effects plots ------------------------------------------------ +test_that('test fixef options', { + test_fe = plot_coefficients(fit_mer, ranef = F) + expect_s3_class(test_fe, 'ggplot') +}) + +test_that('test fixef options order increasing', { + test_fe = plot_coefficients(fit_mer, order = 'increasing') + expect_s3_class(test_fe, 'ggplot') +}) + +test_that('test fixef options order numeric', { + test_fe = plot_coefficients(fit_mer2, order = 4:1) + expect_s3_class(test_fe, 'ggplot') +}) + +test_that('no plot works', { + expect_s3_class(plot_coefficients(fit_mer, plot = FALSE), 'data.frame') +}) + + +# test random effect plots ------------------------------------------------ + +test_that('test ranef options', { + expect_s3_class(mer_re[[1]], 'ggplot') +}) + +test_that('test error with wrong ranef', { + expect_error(plot_coefficients(fit_mer, ranef = T, which_ranef = 'blah')) +}) + +test_that('test error with null ranef', { + expect_error(plot_coefficients(fit_mer, ranef = T, which_ranef = NULL)) +}) + +test_that('test can return data', { + expect_s3_class(plot_coefficients(fit_mer, ranef = T, which_ranef = 'Subject', plot=F)[[1]], 'data.frame') +}) + +# test brms --------------------------------------------------------------- + + +# test fixed effects plots ------------------------------------------------ + +test_that('test fixef options', { + test_fe = plot_coefficients(fit1, ranef = F) + expect_s3_class(test_fe, 'ggplot') +}) + +test_that('test fixef options order increasing', { + test_fe = plot_coefficients(fit1, order = 'increasing') + expect_s3_class(test_fe, 'ggplot') +}) + +test_that('test fixef options order numeric', { + test_fe = plot_coefficients(fit1, order = 4:1) + expect_s3_class(test_fe, 'ggplot') +}) + +test_that('no plot works', { + expect_s3_class(plot_coefficients(fit1, plot = FALSE), 'data.frame') +}) + + +# test random effect plots ------------------------------------------------ + + +test_that('test ranef options', { + expect_s3_class(plot_coefficients(fit1, ranef = T, which_ranef='patient')[[1]], 'ggplot') +}) + +test_that('test error with wrong ranef', { + expect_error(plot_coefficients(fit1, ranef = T, which_ranef = 'blah')) +}) + +test_that('test error with null ranef', { + expect_error(plot_coefficients(fit1, ranef = T, which_ranef = NULL)) +}) + +test_that('test can return data', { + expect_s3_class(plot_coefficients(fit1, ranef = T, which_ranef = 'patient', plot=F)[[1]], 'data.frame') +})