Debian Codegraph @

tests/testthat/test_rma-uni.R

raw
1 context("rma.uni objects")
2 set.seed(20190513)
3
4 skip_if_not_installed("robumeta")
5 skip_if_not_installed("metafor")
6
7 library(robumeta, quietly=TRUE)
8 suppressMessages(library(metafor, quietly=TRUE))
9
10 data(corrdat)
11 corr_robu <- robu(effectsize ~ males + college + binge, data = corrdat,
12 modelweights = "CORR", studynum = studyid,
13 var.eff.size = var)
14 corrdat$wt <- corr_robu$data.full$r.weights
15
16 corr_meta <- rma(effectsize ~ males + college + binge, data = corrdat,
17 weights = wt, vi = var, method = "FE")
18
19 test_that("CR2 t-tests agree with robumeta for correlated effects", {
20 robu_CR2 <- vcovCR(corr_meta, cluster = corrdat$studyid, target = 1 / corrdat$wt, type = "CR2")
21 expect_true(check_CR(corr_meta, vcov = robu_CR2))
22 # expect_true(check_CR(corr_meta, vcov = "CR4", cluster = corrdat$studyid))
23 expect_equivalent(as.matrix(robu_CR2), corr_robu$VR.r)
24 expect_equivalent(as.matrix(vcovCR(corr_meta, cluster = corrdat$studyid,
25 inverse_var = TRUE, type = "CR2")), corr_robu$VR.r)
26
27 CR2_ttests <- coef_test(corr_meta, vcov = robu_CR2, test = "Satterthwaite")
28 expect_equal(corr_robu$dfs, CR2_ttests$df)
29 expect_equal(corr_robu$reg_table$prob, CR2_ttests$p_Satt)
30 })
31
32 data(hierdat)
33 hier_meta <- rma(effectsize ~ binge + followup + sreport + age, data = hierdat,
34 vi = var, method = "REML")
35 hierdat$wt <- with(hier_meta, 1 / (vi + tau2))
36 hier_robu <- robu(effectsize ~ binge + followup + sreport + age,
37 data = hierdat, studynum = studyid,
38 var.eff.size = var, userweights = wt)
39
40 test_that("CR2 t-tests agree with robumeta for user weighting", {
41
42 robu_CR2_iv <- vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid)
43 robu_CR2_not <- vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid,
44 target = hier_robu$data.full$avg.var.eff.size)
45 expect_true(check_CR(hier_meta, vcov = robu_CR2_iv))
46 # expect_true(check_CR(hier_meta, vcov = "CR4", cluster = hierdat$studyid))
47 expect_true(check_CR(hier_meta, vcov = robu_CR2_not))
48 # expect_true(check_CR(hier_meta, vcov = "CR4", cluster = hierdat$studyid,
49 # target = hier_robu$data.full$avg.var.eff.size))
50
51 expect_that(all.equal(hier_robu$VR.r, as.matrix(robu_CR2_iv)), is_a("character"))
52 expect_equivalent(hier_robu$VR.r, as.matrix(robu_CR2_not))
53
54 # CR2_ttests <- coef_test(hier_meta, vcov = robu_CR2_not, test = "Satterthwaite")
55 # expect_equal(hier_robu$dfs, CR2_ttests$df)
56 # expect_equal(hier_robu$reg_table$prob, CR2_ttests$p_Satt)
57 })
58
59 test_that("bread works", {
60 expect_true(check_bread(corr_meta, cluster = corrdat$studyid, y = corrdat$effectsize))
61 X <- model_matrix(corr_meta)
62 W <- corr_meta$weights
63 V <- corr_meta$vi
64 vcov_corr <- crossprod((sqrt(V) * W * X) %*% bread(corr_meta) / nobs(corr_meta))
65 attr(vcov_corr, "dimnames") <- attr(vcov(corr_meta), "dimnames")
66 expect_equal(vcov(corr_meta), vcov_corr)
67
68 expect_true(check_bread(hier_meta, cluster = hierdat$studyid, y = hierdat$effectsize))
69 expect_equal(vcov(hier_meta), bread(hier_meta) / nobs(hier_meta))
70 })
71
72 CR_types <- paste0("CR",0:4)
73
74 test_that("order doesn't matter", {
75
76 skip_on_cran()
77
78 check_sort_order(hier_meta, hierdat, cluster = "studyid")
79
80 })
81
82 test_that("clubSandwich works with dropped covariates", {
83 dat_miss <- hierdat
84 dat_miss$binge[sample.int(nrow(hierdat), size = round(nrow(hierdat) / 10))] <- NA
85 dat_miss$followup[sample.int(nrow(hierdat), size = round(nrow(hierdat) / 20))] <- NA
86 expect_warning(hier_drop <- rma(effectsize ~ binge + followup + sreport + age,
87 data = dat_miss, vi = var, method = "REML"))
88
89
90 subset_ind <- with(dat_miss, !is.na(binge) & !is.na(followup))
91 hier_complete <- rma(effectsize ~ binge + followup + sreport + age,
92 subset = !is.na(binge) & !is.na(followup),
93 data = dat_miss, vi = var, method = "REML")
94 expect_error(vcovCR(hier_complete, type = "CR0", cluster = dat_miss$studyid))
95
96 CR_drop_A <- lapply(CR_types, function(x) vcovCR(hier_drop, type = x, cluster = dat_miss$studyid))
97 CR_drop_B <- lapply(CR_types, function(x) vcovCR(hier_drop, type = x, cluster = hierdat$studyid))
98 CR_complete <- lapply(CR_types, function(x) vcovCR(hier_complete, type = x, cluster = dat_miss$studyid[subset_ind]))
99 expect_equal(CR_drop_A, CR_complete)
100 expect_equal(CR_drop_B, CR_complete)
101
102 test_drop_A <- lapply(CR_types, function(x) coef_test(hier_drop, vcov = x, cluster = dat_miss$studyid, test = "All", p_values = FALSE))
103 test_drop_B <- lapply(CR_types, function(x) coef_test(hier_drop, vcov = x, cluster = hierdat$studyid, test = "All", p_values = FALSE))
104 test_complete <- lapply(CR_types, function(x) coef_test(hier_complete, vcov = x, cluster = dat_miss$studyid[subset_ind], test = "All", p_values = FALSE))
105 compare_ttests(test_drop_A, test_complete)
106 compare_ttests(test_drop_B, test_complete)
107 })
108
109 test_that("clubSandwich works with missing variances", {
110
111 dat_miss <- hierdat
112 dat_miss$var[sample.int(nrow(hierdat), size = round(nrow(hierdat) / 10))] <- NA
113 expect_warning(hier_drop <- rma(effectsize ~ binge + followup + sreport + age,
114 data = dat_miss, vi = var, method = "REML"))
115
116
117 subset_ind <- with(dat_miss, !is.na(var))
118 hier_complete <- rma(effectsize ~ binge + followup + sreport + age,
119 subset = !is.na(var),
120 data = dat_miss, vi = var, method = "REML")
121 expect_error(vcovCR(hier_complete, type = "CR0", cluster = dat_miss$studyid))
122
123 CR_drop_A <- lapply(CR_types, function(x) vcovCR(hier_drop, type = x, cluster = dat_miss$studyid))
124 CR_drop_B <- lapply(CR_types, function(x) vcovCR(hier_drop, type = x, cluster = hierdat$studyid))
125 CR_complete <- lapply(CR_types, function(x) vcovCR(hier_complete, type = x, cluster = dat_miss$studyid[subset_ind]))
126 expect_equal(CR_drop_A, CR_complete)
127 expect_equal(CR_drop_B, CR_complete)
128
129 })
130
131
132 test_that("vcovCR options work for CR2", {
133 RE_var <- hier_meta$tau2 + hierdat$var
134 CR2_iv <- vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid)
135 expect_equal(vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid, inverse_var = TRUE), CR2_iv)
136
137 CR2_not <- vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid, inverse_var = FALSE)
138 attr(CR2_iv, "inverse_var") <- FALSE
139 attr(CR2_iv, "target") <- attr(CR2_not, "target")
140 expect_equal(CR2_not, CR2_iv)
141 expect_equal(vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid, target = RE_var), CR2_not)
142 expect_equal(vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid, target = RE_var, inverse_var = FALSE), CR2_not)
143 expect_false(identical(vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid, target = hierdat$var), CR2_not))
144 })
145
146
147 test_that("vcovCR works with intercept-only model and user-specified weights.", {
148
149 dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg)
150 dat$wt <- sample(1:3, size = nrow(dat), replace = TRUE)
151
152 res <- rma(yi, vi, weights = wt, data=dat)
153
154 meta_rob <- robust(res, cluster=dat$trial)
155 club_rob <- coef_test(res, vcov="CR1", cluster=dat$trial, test = "naive-t")
156 expect_equal(meta_rob$se, club_rob$SE)
157 expect_equal(meta_rob$zval, club_rob$tstat)
158 expect_equal(meta_rob$dfs, club_rob$df_t)
159 expect_equal(meta_rob$pval, club_rob$p_t)
160
161 expect_true(check_CR(res, vcov = "CR2", cluster = dat$trial))
162 test_uni <- coef_test(res, vcov="CR2", cluster=dat$trial, test = "All")
163
164
165 res <- rma.mv(yi, vi, W = wt, random = ~ 1 | trial, data=dat)
166 meta_rob <- robust(res, cluster=dat$trial)
167 club_rob <- coef_test(res, vcov="CR1", test = "naive-t")
168 expect_equal(meta_rob$se, club_rob$SE)
169 expect_equal(meta_rob$zval, club_rob$tstat)
170 expect_equal(meta_rob$dfs, club_rob$df_t)
171 expect_equal(meta_rob$pval, club_rob$p_t)
172
173 expect_true(check_CR(res, vcov = "CR2"))
174 test_mv <- coef_test(res, vcov="CR2", test = "All")
175
176 expect_equal(test_uni, test_mv, tolerance = 10^-5)
177
178 V_club <- vcovCR(res, type = "CR2")
179 k <- res$k
180 yi <- res$yi
181 wi <- diag(res$W)
182 W <- sum(wi)
183 wi <- wi / W
184 vi <- diag(res$M)
185 V <- sum(vi)
186 ei <- residuals_CS(res)
187 M <- sum(wi^2 * vi)
188 ai <- 1 / sqrt(1 - 2 * wi + M / vi)
189 V_hand <- sum(wi^2 * ai^2 * ei^2)
190 expect_equal(V_hand, as.numeric(V_club))
191
192 pi_theta_pj <- diag(vi) - tcrossprod(rep(1,k), wi * vi) - tcrossprod(wi * vi, rep(1, k)) + M
193 df <- M^2 / sum(tcrossprod(ai^2 * wi^2) * (pi_theta_pj^2))
194
195 expect_equal(Inf, test_uni$df_z)
196 expect_equal(k - 1, test_uni$df_t)
197 expect_equal(df, test_uni$df_Satt, tolerance = 10^-5)
198
199 })
200
201 test_that("clubSandwich agrees with metafor::robust() for CR0.", {
202
203 test_CR0 <- conf_int(corr_meta, vcov = "CR0", cluster = corrdat$studyid, test = "naive-tp", p_values = TRUE)
204 meta_CR0 <- robust(corr_meta, cluster = corrdat$studyid, adjust = FALSE)
205 rob_CR0 <- conf_int(meta_CR0, vcov = "CR0", cluster = corrdat$studyid, test = "naive-tp", p_values = TRUE)
206 expect_equal(test_CR0$SE, meta_CR0$se)
207 expect_equal(test_CR0$df, rep(meta_CR0$df, length(test_CR0$df)))
208 expect_equal(rob_CR0, test_CR0)
209
210 club_F_CR0 <- Wald_test(corr_meta, constraints = constrain_zero(2:4),
211 vcov = "CR0", cluster = corrdat$studyid, test = "Naive-Fp")
212 rob_F_CR0 <- Wald_test(meta_CR0, constraints = constrain_zero(2:4),
213 vcov = "CR0", cluster = corrdat$studyid, test = "Naive-Fp")
214 expect_equal(club_F_CR0$Fstat, meta_CR0$QM)
215 expect_equal(club_F_CR0$df_num, meta_CR0$QMdf[1])
216 expect_equal(club_F_CR0$df_denom, meta_CR0$QMdf[2])
217 expect_equal(club_F_CR0$p_val, meta_CR0$QMp)
218 expect_equal(club_F_CR0, rob_F_CR0)
219
220 })
221
222 test_that("clubSandwich agrees with metafor::robust() for CR1p.", {
223
224 test_CR1 <- conf_int(corr_meta, vcov = "CR1p", cluster = corrdat$studyid, test = "naive-tp", p_values = TRUE)
225 meta_CR1 <- robust(corr_meta, cluster = corrdat$studyid, adjust = TRUE)
226 rob_CR1 <- conf_int(meta_CR1, vcov = "CR1p", cluster = corrdat$studyid, test = "naive-tp", p_values = TRUE)
227 expect_equal(test_CR1$SE, meta_CR1$se)
228 expect_equal(test_CR1$df, rep(meta_CR1$df, length(test_CR1$df)))
229 expect_equal(rob_CR1, test_CR1)
230
231 club_F_CR1 <- Wald_test(corr_meta, constraints = constrain_zero(2:4),
232 vcov = "CR1p", cluster = corrdat$studyid, test = "Naive-Fp")
233 rob_F_CR1 <- Wald_test(meta_CR1, constraints = constrain_zero(2:4),
234 vcov = "CR1p", cluster = corrdat$studyid, test = "Naive-Fp")
235 expect_equal(club_F_CR1$Fstat, meta_CR1$QM)
236 expect_equal(club_F_CR1$df_num, meta_CR1$QMdf[1])
237 expect_equal(club_F_CR1$df_denom, meta_CR1$QMdf[2])
238 expect_equal(club_F_CR1$p_val, meta_CR1$QMp)
239 expect_equal(club_F_CR1, rob_F_CR1)
240 })
241
242 test_that("clubSandwich agrees with metafor::robust() for CR2.", {
243
244 skip_if(packageVersion('metafor') < "3.1.31")
245 test_CR2 <- conf_int(corr_meta, vcov = "CR2", cluster = corrdat$studyid, p_values = TRUE)
246 meta_CR2 <- robust(corr_meta, cluster = corrdat$studyid, clubSandwich = TRUE)
247 rob_CR2 <- conf_int(meta_CR2, vcov = "CR2", cluster = corrdat$studyid, p_values = TRUE)
248 expect_equal(test_CR2$SE, meta_CR2$se)
249 expect_equal(rob_CR2, test_CR2)
250
251 club_F_CR2 <- Wald_test(corr_meta, constraints = constrain_zero(2:4),
252 vcov = "CR2", cluster = corrdat$studyid, test = "All")
253 rob_F_CR2 <- Wald_test(meta_CR2, constraints = constrain_zero(2:4),
254 vcov = "CR2", cluster = corrdat$studyid, test = "All")
255 expect_equal(subset(club_F_CR2, test == "HTZ")$Fstat, meta_CR2$QM)
256 expect_equal(subset(club_F_CR2, test == "HTZ")$df_num, meta_CR2$QMdf[1])
257 expect_equal(subset(club_F_CR2, test == "HTZ")$df_denom, meta_CR2$QMdf[2])
258 expect_equal(subset(club_F_CR2, test == "HTZ")$p_val, meta_CR2$QMp)
259 expect_equal(club_F_CR2, rob_F_CR2)
260 })
261
262 test_that("clubSandwich methods work on robust.rma objects.", {
263
264 hier_robust <- robust(hier_meta, cluster = hierdat$studyid, adjust = TRUE)
265
266 expect_equal(residuals_CS(hier_meta), residuals_CS(hier_robust))
267 expect_equal(coef_CS(hier_meta), coef_CS(hier_robust))
268 expect_equal(model_matrix(hier_meta), model_matrix(hier_robust))
269 expect_equal(bread(hier_meta), bread(hier_robust))
270 expect_equal(v_scale(hier_meta), v_scale(hier_robust))
271 expect_equal(targetVariance(hier_meta, cluster = hierdat$studyid),
272 targetVariance(hier_robust, cluster = hierdat$studyid))
273 expect_equal(weightMatrix(hier_meta, cluster = hierdat$studyid),
274 weightMatrix(hier_robust, cluster = hierdat$studyid))
275
276 hier_club <- robust(hier_meta, cluster = hierdat$studyid, adjust = FALSE, clubSandwich = TRUE)
277
278 expect_equal(residuals_CS(hier_meta), residuals_CS(hier_club))
279 expect_equal(coef_CS(hier_meta), coef_CS(hier_club))
280 expect_equal(model_matrix(hier_meta), model_matrix(hier_club))
281 expect_equal(bread(hier_meta), bread(hier_club))
282 expect_equal(v_scale(hier_meta), v_scale(hier_club))
283 expect_equal(targetVariance(hier_meta, cluster = hierdat$studyid),
284 targetVariance(hier_club, cluster = hierdat$studyid))
285 expect_equal(weightMatrix(hier_meta, cluster = hierdat$studyid),
286 weightMatrix(hier_club, cluster = hierdat$studyid))
287
288 })
289
290
291 test_that("clubSandwich works with weights of zero.", {
292
293 data("SATcoaching")
294 n_SAT <- nrow(SATcoaching)
295 SATcoaching$wt <- rpois(n_SAT, lambda = 0.8)
296 table(SATcoaching$wt)
297
298 rma_full <- rma.uni(yi = d, vi = V, weights = wt,
299 mods = ~ year + test,
300 data = SATcoaching, method = "FE")
301
302 SAT_sub <- subset(SATcoaching, wt > 0)
303 rma_sub <- rma.uni(yi = d, vi = V, weights = wt,
304 mods = ~ year + test,
305 data = SAT_sub, method = "FE")
306 # Note that this only works for method = "FE" because
307 # tau.sq estimators differ between rma_full and rma_sub
308
309 CR_full <- lapply(CR_types, function(x) vcovCR(rma_full, cluster = SATcoaching$study, type = x))
310 CR_sub <- lapply(CR_types, function(x) vcovCR(rma_sub, cluster = SAT_sub$study, type = x))
311 expect_equal(CR_full, CR_sub, check.attributes = FALSE)
312
313 test_full <- lapply(CR_types, function(x) coef_test(rma_full, vcov = x, cluster = SATcoaching$study, test = c("z","naive-t","Satterthwaite"), p_values = TRUE))
314 test_sub <- lapply(CR_types, function(x) coef_test(rma_sub, vcov = x, cluster = SAT_sub$study, test = c("z","naive-t","Satterthwaite"), p_values = TRUE))
315 expect_equal(test_full, test_sub, check.attributes = FALSE)
316
317 dat_miss <- SATcoaching
318 miss_indicator <- sample.int(n_SAT, size = round(n_SAT / 10))
319 dat_miss$year[miss_indicator] <- NA
320 with(dat_miss, table(wt, is.na(year)))
321
322 expect_warning(
323 rma_dropped <- rma.uni(yi = d, vi = V, weights = wt,
324 mods = ~ year + test,
325 data = dat_miss, method = "FE")
326 )
327 dat_complete <- subset(dat_miss, !is.na(year))
328 rma_complete <- rma.uni(yi = d, vi = V, weights = wt,
329 mods = ~ year + test,
330 data = dat_complete, method = "FE")
331
332 CR_drop <- lapply(CR_types, function(x) vcovCR(rma_dropped, cluster = dat_miss$study, type = x))
333 CR_complete <- lapply(CR_types, function(x) vcovCR(rma_complete, cluster = dat_complete$study, type = x))
334 expect_equal(CR_drop, CR_complete)
335
336 test_drop <- lapply(CR_types, function(x) coef_test(rma_dropped, vcov = x, cluster = dat_miss$study, test = "All", p_values = FALSE))
337 test_complete <- lapply(CR_types, function(x) coef_test(rma_complete, vcov = x, cluster = dat_complete$study, test = "All", p_values = FALSE))
338 expect_equal(test_drop, test_complete)
339
340 })