Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Dec 15, 2024
1 parent 02ad2da commit 7c4f4d4
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/testthat/test-estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ test_that("estimate_means() - lm", {


test_that("estimate_means() - glm", {
data(iris)
# GLM
dat <- iris
dat$y <- as.numeric(as.factor(ifelse(dat$Sepal.Width > 3, "A", "B"))) - 1
Expand Down Expand Up @@ -286,6 +287,8 @@ test_that("estimate_means() - mixed models", {
skip_if_not_installed("emmeans")
skip_if_not_installed("lme4")
skip_if_not_installed("glmmTMB")

data(iris)
dat <- iris
dat$Petal.Length_factor <- as.factor(ifelse(dat$Petal.Length < 4.2, "A", "B"))
dat <<- dat
Expand Down Expand Up @@ -324,4 +327,27 @@ test_that("estimate_means() - mixed models", {
out1 <- estimate_means(m, c("mined", "spp"), backend = "marginaleffects")
out2 <- estimate_means(m, c("mined", "spp"))
expect_equal(out1$Mean[order(out1$spp)], out2$rate, tolerance = 1e-3)

data(sleepstudy, package = "lme4")
model <- lme4::lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)

estim1 <- suppressMessages(estimate_means(model, by = "Days"))
estim2 <- suppressMessages(estimate_means(model, by = "Days", backend = "marginaleffects"))
expect_identical(dim(estim1), c(10L, 5L))
expect_identical(dim(estim2), c(10L, 5L))
expect_equal(estim1$Mean, estim2$Mean, tolerance = 1e-4)
expect_equal(estim1$CI_low, estim2$CI_low, tolerance = 1e-1)

data(cbpp, package = "lme4")
gm1 <- lme4::glmer(
cbind(incidence, size - incidence) ~ period + (1 | herd),
data = cbpp,
family = "binomial"
)
estim1 <- suppressMessages(estimate_means(gm1))
estim2 <- suppressMessages(estimate_means(gm1, backend = "marginaleffects"))
expect_identical(dim(estim1), c(4L, 5L))
expect_identical(dim(estim2), c(4L, 5L))
expect_equal(estim2$Probability, c(0.21521, 0.0954, 0.08453, 0.05599), tolerance = 1e-3)
expect_equal(estim2$CI_low, c(0.14056, 0.04352, 0.0349, 0.0116), tolerance = 1e-3)
})

0 comments on commit 7c4f4d4

Please sign in to comment.