library(magrittr)
context("model_tools.R")
### makeCI() -------------------------------------------------------
# lm()
data(mtcars)
mtcars$cyl %<>% as.factor()
lm_model <- lm(log2(mpg) ~ 0 + cyl, data = mtcars)
ci <- suppressMessages(make_cis(lm_model))
ci2 <- suppressMessages(make_cis(lm_model, restore = FALSE))
stats_ci <- confint(lm_model)
test_that("confidence intervals work for lm()s", {
expect_equal(names(ci), c("cyl", "est", "lwr", "upr"))
expect_equal(ci$lwr, 2^unname(stats_ci[, 1]))
expect_equal(ci2$lwr, unname(stats_ci[, 1]))
expect_true(is.data.frame(ci))
})
# lmer()
data(iris)
iris %<>% dplyr::group_by(Species) %>% dplyr::mutate(site = rep(1:10, each = 5))
lmer_model <- lme4::lmer(log2(Sepal.Length) ~ 0 + Species + (1 | site), data = iris)
ci <- suppressMessages(make_cis(lmer_model))
ci2 <- suppressMessages(make_cis(lmer_model, restore = FALSE))
stats_ci <- suppressMessages(confint(lmer_model))
test_that("confidence intervals work for lm()s", {
expect_equal(names(ci), c("Species", "est", "lwr", "upr"))
expect_equal(ci$lwr, 2^unname(stats_ci[3:5, 1]))
expect_equal(ci2$lwr, unname(stats_ci[3:5, 1]))
expect_true(is.data.frame(ci))
})
# makeFC() -------------------------------------------------------------------
# lm()
fc <- suppressWarnings(make_fcs(lm_model))
fc2 <- suppressWarnings(make_fcs(lm_model, restore = FALSE))
test_that("fold changes work for lm()s", {
expect_true(is.data.frame(fc))
expect_equal(names(fc), c("cyl", "contrast", "baseline", "fc_est", "fc_lwr", "fc_upr", "pval", "pct_change", "log2_fc", "label"))
expect_equal(fc$cyl, c("6", "8"))
expect_equal(fc$fc_est, c(0.7482528, 0.5653657), tolerance = 0.001)
expect_equal(fc2$fc_est, c(-0.4184022, -0.8227438), tolerance = 0.001)
expect_equal(fc$pct_change, c(-25, -43))
})
# lmer()
data(iris)
iris %<>% dplyr::group_by(Species) %>% dplyr::mutate(site = rep(1:10, each = 5))
lmer_model <- lme4::lmer(log2(Sepal.Length) ~ 0 + Species + (1 | site), data = iris)
fc <- suppressWarnings(make_fcs(lmer_model))
fc2 <- suppressWarnings(make_fcs(lmer_model, restore = FALSE))
test_that("fold changes work for lmer()s", {
expect_true(is.data.frame(fc))
expect_equal(names(fc), c("Species", "contrast", "baseline", "fc_est", "fc_lwr", "fc_upr", "pval", "pct_change", "log2_fc", "label"))
expect_equal(fc$Species, c("versicolor", "virginica"))
expect_equal(fc$fc_est, c(1.184255, 1.313176), tolerance = 0.001)
expect_equal(fc2$fc_est, c(0.2439793, 0.3930608), tolerance = 0.001)
expect_equal(fc$pct_change, c(18, 31))
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.