Debian Codegraph @

tests/testthat/test_lmerMod.R

raw
1 context("lmerMod objects")
2 set.seed(20191217)
3
4 skip_if_not_installed("lme4")
5 skip_if_not_installed("nlme")
6 skip_if_not_installed("mlmRev")
7
8 suppressMessages(library(lme4, quietly=TRUE))
9 library(nlme, quietly=TRUE, warn.conflicts=FALSE)
10
11 obj_A1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
12 obj_A2 <- lmer(Reaction ~ Days + (Days || Subject), sleepstudy)
13
14 data(Orthodont, package="nlme")
15
16 obj_B1 <- lmer(distance ~ age + (1 | Subject), data=Orthodont)
17 obj_B2 <- lmer(distance ~ age + (age || Subject), data=Orthodont)
18
19 data(egsingle, package = "mlmRev")
20 egsingle <- within(egsingle, {
21 size <- (size - mean(size)) / sd(size)
22 })
23
24 obj_C1 <- lmer(math ~ year * size + female + black + hispanic + (1 | schoolid) + (1 | childid),
25 data = egsingle)
26 obj_C2 <- lmer(math ~ year * size + female + black + hispanic + (year | schoolid) + (1 | childid),
27 data = egsingle, control = lmerControl(check.conv.grad = .makeCC("ignore", tol = 2e-3, relTol = NULL)))
28
29 test_that("bread works", {
30
31 expect_true(check_bread(obj_A1, cluster = sleepstudy$Subject, y = sleepstudy$Reaction))
32 expect_true(check_bread(obj_A2, cluster = sleepstudy$Subject, y = sleepstudy$Reaction))
33 expect_true(check_bread(obj_B1, cluster = Orthodont$Subject, y = Orthodont$distance))
34 expect_true(check_bread(obj_B2, cluster = Orthodont$Subject, y = Orthodont$distance))
35 expect_true(check_bread(obj_C1, cluster = egsingle$schoolid, y = egsingle$math))
36 expect_true(check_bread(obj_C2, cluster = egsingle$schoolid, y = egsingle$math))
37
38 expect_equal(as.matrix(vcov(obj_A1)), bread(obj_A1) * getME(obj_A1, "sigma")^2 / v_scale(obj_A1))
39 expect_equal(as.matrix(vcov(obj_A2)), bread(obj_A2) * getME(obj_A2, "sigma")^2 / v_scale(obj_A2))
40 expect_equal(as.matrix(vcov(obj_B1)), bread(obj_B1) * getME(obj_B1, "sigma")^2 / v_scale(obj_B1))
41 expect_equal(as.matrix(vcov(obj_B2)), bread(obj_B2) * getME(obj_B2, "sigma")^2 / v_scale(obj_B2))
42 expect_equal(as.matrix(vcov(obj_C1)), bread(obj_C1) * getME(obj_C1, "sigma")^2 / v_scale(obj_C1))
43 expect_equal(as.matrix(vcov(obj_C2)), bread(obj_C2) * getME(obj_C2, "sigma")^2 / v_scale(obj_C2))
44
45 })
46
47 test_that("vcovCR options work for CR2", {
48
49 CR2_A <- vcovCR(obj_A1, type = "CR2")
50 expect_equal(vcovCR(obj_A1, cluster = sleepstudy$Subject, type = "CR2"), CR2_A)
51 expect_equal(vcovCR(obj_A1, type = "CR2", inverse_var = TRUE), CR2_A)
52 expect_false(identical(vcovCR(obj_A1, type = "CR2", inverse_var = FALSE), CR2_A))
53 target <- targetVariance(obj_A1)
54 expect_equal(vcovCR(obj_A1, type = "CR2", target = target, inverse_var = TRUE), CR2_A, check.attributes = FALSE)
55 expect_equal(vcovCR(obj_A1, type = "CR2", target = target, inverse_var = FALSE), CR2_A, check.attributes = FALSE)
56
57 CR2_B <- vcovCR(obj_B1, type = "CR2")
58 expect_equal(vcovCR(obj_B1, cluster = Orthodont$Subject, type = "CR2"), CR2_B)
59 expect_equal(vcovCR(obj_B1, type = "CR2", inverse_var = TRUE), CR2_B)
60 expect_false(identical(vcovCR(obj_B1, type = "CR2", inverse_var = FALSE), CR2_B))
61 target <- targetVariance(obj_B1)
62 expect_equal(vcovCR(obj_B1, type = "CR2", target = target, inverse_var = TRUE), CR2_B, check.attributes = FALSE)
63 expect_equal(vcovCR(obj_B1, type = "CR2", target = target, inverse_var = FALSE), CR2_B, check.attributes = FALSE)
64
65 CR2_C <- vcovCR(obj_C1, type = "CR2")
66 expect_equal(vcovCR(obj_C1, cluster = egsingle$schoolid, type = "CR2"), CR2_C)
67 expect_equal(vcovCR(obj_C1, type = "CR2", inverse_var = TRUE), CR2_C)
68 expect_false(identical(vcovCR(obj_C1, type = "CR2", inverse_var = FALSE), CR2_C))
69 target <- targetVariance(obj_C1)
70 expect_equal(vcovCR(obj_C1, type = "CR2", target = target, inverse_var = TRUE), CR2_C, check.attributes = FALSE)
71 expect_equal(vcovCR(obj_C1, type = "CR2", target = target, inverse_var = FALSE), CR2_C, check.attributes = FALSE)
72
73 })
74
75 test_that("vcovCR options work for CR4", {
76
77 CR4_A <- vcovCR(obj_A1, type = "CR4")
78 expect_equal(vcovCR(obj_A1, cluster = sleepstudy$Subject, type = "CR4"), CR4_A)
79 expect_equal(vcovCR(obj_A1, type = "CR4", inverse_var = TRUE), CR4_A)
80 expect_false(identical(vcovCR(obj_A1, type = "CR4", inverse_var = FALSE), CR4_A))
81 target <- targetVariance(obj_A1)
82 expect_equal(vcovCR(obj_A1, type = "CR4", target = target, inverse_var = TRUE), CR4_A, check.attributes = FALSE)
83 expect_equal(vcovCR(obj_A1, type = "CR4", target = target, inverse_var = FALSE), CR4_A, check.attributes = FALSE)
84
85 CR4_B <- vcovCR(obj_B1, type = "CR4")
86 expect_equal(vcovCR(obj_B1, cluster = Orthodont$Subject, type = "CR4"), CR4_B)
87 expect_equal(vcovCR(obj_B1, type = "CR4", inverse_var = TRUE), CR4_B)
88 expect_false(identical(vcovCR(obj_B1, type = "CR4", inverse_var = FALSE), CR4_B))
89 target <- targetVariance(obj_B1)
90 expect_equal(vcovCR(obj_B1, type = "CR4", target = target, inverse_var = TRUE), CR4_B, check.attributes = FALSE)
91 expect_equal(vcovCR(obj_B1, type = "CR4", target = target, inverse_var = FALSE), CR4_B, check.attributes = FALSE)
92
93 CR4_C <- vcovCR(obj_C1, type = "CR4")
94 expect_equal(vcovCR(obj_C1, cluster = egsingle$schoolid, type = "CR4"), CR4_C)
95 expect_equal(vcovCR(obj_C1, type = "CR4", inverse_var = TRUE), CR4_C)
96 expect_false(identical(vcovCR(obj_C1, type = "CR4", inverse_var = FALSE), CR4_C))
97 target <- targetVariance(obj_C1)
98 expect_equal(vcovCR(obj_C1, type = "CR4", target = target, inverse_var = TRUE), CR4_C, check.attributes = FALSE)
99 expect_equal(vcovCR(obj_C1, type = "CR4", target = target, inverse_var = FALSE), CR4_C, check.attributes = FALSE)
100
101 })
102
103
104 test_that("CR2 and CR4 are target-unbiased", {
105 expect_true(check_CR(obj_A1, vcov = "CR2"))
106 expect_true(check_CR(obj_A2, vcov = "CR2"))
107 expect_true(check_CR(obj_B1, vcov = "CR2"))
108 expect_true(check_CR(obj_B2, vcov = "CR2"))
109 expect_true(check_CR(obj_C1, vcov = "CR2"))
110 expect_true(check_CR(obj_C2, vcov = "CR2"))
111 expect_true(check_CR(obj_A1, vcov = "CR4"))
112 expect_true(check_CR(obj_A2, vcov = "CR4"))
113 expect_true(check_CR(obj_B1, vcov = "CR4"))
114 expect_true(check_CR(obj_B2, vcov = "CR4"))
115 expect_true(check_CR(obj_C1, vcov = "CR4"))
116 expect_true(check_CR(obj_C2, vcov = "CR4"))
117 })
118
119
120 CR_types <- paste0("CR",0:4)
121
122 test_that("Order doesn't matter.", {
123
124 skip_on_cran()
125
126 # Model A1
127 check_sort_order(obj = obj_A1, dat = sleepstudy)
128
129 # Model C1
130 check_sort_order(obj = obj_C1, dat = egsingle)
131 })
132
133
134 test_that("clubSandwich works with dropped observations", {
135
136 dat_miss <- sleepstudy
137 dat_miss$Reaction[sample.int(nrow(sleepstudy), size = round(nrow(sleepstudy) / 10))] <- NA
138 obj_dropped <- update(obj_A1, data = dat_miss, na.action = na.omit)
139 obj_complete <- update(obj_A1, data = dat_miss, subset = !is.na(Reaction))
140
141 CR_drop <- lapply(CR_types, function(x) vcovCR(obj_dropped, type = x))
142 CR_complete <- lapply(CR_types, function(x) vcovCR(obj_complete, type = x))
143 expect_equal(CR_drop, CR_complete)
144
145 test_drop <- lapply(CR_types, function(x) coef_test(obj_dropped, vcov = x, test = "All", p_values = FALSE))
146 test_complete <- lapply(CR_types, function(x) coef_test(obj_complete, vcov = x, test = "All", p_values = FALSE))
147 expect_equal(test_drop, test_complete)
148 })
149
150
151
152 test_that("lmer agrees with lme", {
153
154 data(BodyWeight, package="nlme")
155
156 lmer_fit <- lmer(weight ~ Time * Diet + (1 | Rat), data=BodyWeight)
157 lme_fit <- lme(weight ~ Time * Diet, data=BodyWeight, ~ 1 | Rat)
158
159 expect_equal(coef_CS(lmer_fit), coef_CS(lme_fit))
160 expect_equal(nobs(lmer_fit), nobs(lme_fit))
161 expect_equal(model_matrix(lmer_fit), model_matrix(lme_fit), check.attributes = FALSE)
162 expect_equal(residuals_CS(lmer_fit), residuals_CS(lme_fit), check.attributes = FALSE)
163 expect_equal(v_scale(lmer_fit), v_scale(lme_fit))
164
165 p <- length(coef_CS(lmer_fit))
166 expect_equal(bread(lmer_fit) / bread(lme_fit), matrix(1, p, p), check.attributes = FALSE, tol = 10^-6)
167 expect_equal(targetVariance(lmer_fit), targetVariance(lme_fit), check.attributes = FALSE, tol = 10^-6)
168 expect_equal(weightMatrix(lmer_fit), weightMatrix(lme_fit), check.attributes = FALSE, tol = 10^-6)
169
170 CR_lmer <- lapply(CR_types, function(x) vcovCR(lmer_fit, type = x))
171 CR_lme <- lapply(CR_types, function(x) vcovCR(lme_fit, type = x))
172 expect_equivalent(CR_lmer, CR_lme, tolerance = 10^-6)
173
174 test_lmer <- lapply(CR_types, function(x) coef_test(lmer_fit, vcov = x, test = "All", p_values = FALSE))
175 test_lme <- lapply(CR_types, function(x) coef_test(lme_fit, vcov = x, test = "All", p_values = FALSE))
176 compare_ttests(test_lmer, test_lme, tol = 10^-10)
177
178 constraints <- c(combn(length(coef_CS(lmer_fit)), 2, simplify = FALSE),
179 combn(length(coef_CS(lmer_fit)), 3, simplify = FALSE))
180 Wald_lmer <- Wald_test(lmer_fit, constraints = constrain_zero(constraints), vcov = "CR2", test = "All")
181 Wald_lme <- Wald_test(lme_fit, constraints = constrain_zero(constraints), vcov = "CR2", test = "All")
182 compare_Waldtests(Wald_lmer, Wald_lme)
183
184 })
185
186
187 test_that("Emply levels are dropped in model_matrix", {
188
189 data(AchievementAwardsRCT)
190
191 AA_RCT_females <- subset(AchievementAwardsRCT, sex=="Girl" & year != "1999")
192
193 AA_RCT_females <- within(AA_RCT_females, {
194 sibs_4 <- siblings >= 4
195 treated2001 <- treated * (year=="2001")
196 })
197
198 lmer_fit <- lmer(Bagrut_status ~ year * school_type +
199 father_ed + mother_ed + immigrant + sibs_4 +
200 qrtl + treated2001:half + (1 | school_id),
201 data = AA_RCT_females)
202
203 betas <- fixef(lmer_fit)
204 X <- model_matrix(lmer_fit)
205 expect_identical(names(betas), colnames(X))
206
207 })
208
209
210
211 test_that("Possible to cluster at higher level than random effects", {
212
213 n_districts <- 10
214 n_schools_per <- rnbinom(n_districts, size = 4, prob = 0.3)
215 n_schools <- sum(n_schools_per)
216 n_students_per <- 10
217 n_students <- n_schools * n_students_per
218
219 # identifiers for each level
220 district_id <- factor(rep(1:n_districts, n_schools_per * n_students_per))
221 school_id <- factor(rep(1:sum(n_schools_per), each = n_students_per))
222 student_id <- 1:n_students
223
224 # simulated outcome
225 Y <- rnorm(n_districts)[district_id] + rnorm(n_schools)[school_id] + rnorm(n_students)
226 X <- rnorm(n_students)
227 dat <- data.frame(district_id, school_id, student_id, Y, X)
228 dat_scramble <- dat[sample(nrow(dat)),]
229
230 # fit two-level model
231 lme_2level <- lmer(Y ~ X + (1 | school_id), data = dat)
232
233 # cluster at level 3
234 V <- vcovCR(lme_2level, type = "CR2", cluster = dat$district_id)
235 expect_is(V, "vcovCR")
236 expect_error(vcovCR(lme_2level, type = "CR2", cluster = dat_scramble$district_id))
237
238 # check that result does not depend on sort-order
239 V_scramble <- vcovCR(lmer(Y ~ X + (1 | school_id), data = dat_scramble),
240 type = "CR2", cluster = dat_scramble$district_id)
241 expect_equal(as.matrix(V), as.matrix(V_scramble))
242 })