Skip to content

Commit c390c1f

Browse files
committed
add tests
1 parent 28435c4 commit c390c1f

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tests/testthat/test-estimate_grouplevel.R

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,64 @@ withr::with_environment(
175175
expect_identical(dim(out), c(203L, 5L))
176176
})
177177
)
178+
179+
test_that("estimate_grouplevel type='marginal'", {
180+
skip_on_cran()
181+
skip_if_not_installed("lme4")
182+
data(mtcars)
183+
184+
model <- lme4::lmer(mpg ~ hp + (1 | carb), data = mtcars)
185+
m3 <- estimate_grouplevel(model, type = "marginal")
186+
expect_s3_class(m3, "estimate_grouplevel")
187+
expect_equal(dim(m3), c(6, 6))
188+
expect_equal(colnames(m3), c("Group", "Level", "Parameter", "Coefficient", "CI_low", "CI_high"))
189+
190+
model <- lme4::lmer(mpg ~ hp + (1 + hp | carb), data = mtcars)
191+
m3 <- estimate_grouplevel(model, type = "marginal")
192+
expect_s3_class(m3, "estimate_grouplevel")
193+
expect_equal(dim(m3), c(12, 6))
194+
expect_equal(colnames(m3), c("Group", "Level", "Parameter", "Coefficient", "CI_low", "CI_high"))
195+
196+
model <- lme4::lmer(mpg ~ hp + (1 + hp | carb) + (1 | gear), data = mtcars)
197+
m3 <- estimate_grouplevel(model, type = "marginal")
198+
expect_s3_class(m3, "estimate_grouplevel")
199+
expect_equal(dim(m3), c(15, 6))
200+
expect_equal(colnames(m3), c("Group", "Level", "Parameter", "Coefficient", "CI_low", "CI_high"))
201+
})
202+
203+
test_that("estimate_grouplevel type='marginal' correlations", {
204+
skip_on_cran()
205+
skip_if_not_installed("lme4")
206+
data(mtcars)
207+
208+
model <- lme4::lmer(mpg ~ hp + (1 + hp | carb) + (1 | gear), data = mtcars)
209+
m1 <- estimate_grouplevel(model, type = "random")
210+
m2 <- estimate_grouplevel(model, type = "total")
211+
m3 <- estimate_grouplevel(model, type = "marginal")
212+
213+
# merge m1 and m3 to compare
214+
m1_intercept <- m1[m1$Parameter == "(Intercept)", ]
215+
m3_intercept <- m3[m3$Parameter == "(Intercept)", ]
216+
merged_intercepts <- merge(m1_intercept, m3_intercept, by = c("Group", "Level"))
217+
expect_gt(cor(merged_intercepts$Coefficient.x, merged_intercepts$Coefficient.y), 0.89)
218+
219+
m1_hp <- m1[m1$Parameter == "hp", ]
220+
m3_hp <- m3[m3$Parameter == "hp", ]
221+
merged_hp <- merge(m1_hp, m3_hp, by = c("Group", "Level"))
222+
expect_gt(cor(merged_hp$Coefficient.x, merged_hp$Coefficient.y), 0.99)
223+
224+
# merge m2 and m3 to compare
225+
m2_intercept <- m2[m2$Parameter == "(Intercept)", ]
226+
m3_intercept <- m3[m3$Parameter == "(Intercept)", ]
227+
m2_intercept$Level <- as.character(m2_intercept$Level)
228+
m3_intercept$Level <- as.character(m3_intercept$Level)
229+
merged_intercepts <- merge(m2_intercept, m3_intercept, by = c("Group", "Level"))
230+
expect_gt(cor(merged_intercepts$Coefficient.x, merged_intercepts$Coefficient.y), 0.89)
231+
232+
m2_hp <- m2[m2$Parameter == "hp", ]
233+
m3_hp <- m3[m3$Parameter == "hp", ]
234+
m2_hp$Level <- as.character(m2_hp$Level)
235+
m3_hp$Level <- as.character(m3_hp$Level)
236+
merged_hp <- merge(m2_hp, m3_hp, by = c("Group", "Level"))
237+
expect_gt(cor(merged_hp$Coefficient.x, merged_hp$Coefficient.y), 0.99)
238+
})

0 commit comments

Comments
 (0)