Skip to content

Commit

Permalink
Merge branch 'coef_plot'
Browse files Browse the repository at this point in the history
  • Loading branch information
m-clark committed Jun 18, 2018
2 parents c55a67e + 1857595 commit 93047b6
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions R/plot_fixefs.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ plot_fixefs.merMod <- function(model,
}
}

# plot_fixefs.glmerMod <- plot_fixefs.merMod
1 change: 1 addition & 0 deletions tests/testthat/helper_load_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
load('brms_res.RData')
136 changes: 136 additions & 0 deletions tests/testthat/test_plot_coefficients.R
Original file line number Diff line number Diff line change
@@ -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')
})

0 comments on commit 93047b6

Please sign in to comment.