Debian Codegraph @

tests/testthat/test_rma-mv.R

raw
1 context("rma.mv objects")
2 set.seed(20190513)
3
4 skip_if_not_installed("robumeta")
5 skip_if_not_installed("metafor")
6
7 CR_types <- paste0("CR",0:4)
8
9 library(robumeta, quietly=TRUE)
10 suppressMessages(library(metafor, quietly=TRUE))
11
12 data(corrdat)
13 corr_robu <- robu(effectsize ~ males + college + binge, data = corrdat,
14 modelweights = "CORR", studynum = studyid,
15 var.eff.size = var)
16 corrdat$wt <- corr_robu$data.full$r.weights
17 corr_meta <- rma.mv(effectsize ~ males + college + binge, data = corrdat,
18 V = var, W = wt, method = "FE", sparse = TRUE)
19
20 test_that("CR2 t-tests agree with robumeta for correlated effects", {
21
22 robu_CR2 <- vcovCR(corr_meta, cluster = corrdat$studyid, target = 1 / corrdat$wt, type = "CR2")
23 expect_true(check_CR(corr_meta, vcov = robu_CR2))
24 # expect_true(check_CR(corr_meta, vcov = "CR4", cluster = corrdat$studyid))
25 expect_equivalent(as.matrix(robu_CR2), corr_robu$VR.r)
26 expect_that(all.equal(as.matrix(vcovCR(corr_meta, cluster = corrdat$studyid,
27 inverse_var = TRUE, type = "CR2")), corr_robu$VR.r),
28 is_a("character"))
29
30 CR2_ttests <- coef_test(corr_meta, vcov = robu_CR2, test = "Satterthwaite")
31 expect_equal(corr_robu$dfs, CR2_ttests$df)
32 expect_equal(corr_robu$reg_table$prob, CR2_ttests$p_Satt)
33 })
34
35 data(hierdat)
36 hier_meta <- rma.mv(effectsize ~ binge + followup + sreport + age, data = hierdat,
37 random = list(~ 1 | esid, ~ 1 | studyid),
38 V = var, method = "REML", sparse = TRUE)
39 hier_robu <- robu(effectsize ~ binge + followup + sreport + age,
40 data = hierdat, studynum = studyid,
41 var.eff.size = var, modelweights = "HIER")
42
43 test_that("CR2 t-tests do not exactly agree with robumeta for hierarchical weighting", {
44
45 robu_CR2_iv <- vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid)
46 robu_CR2_not <- vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid,
47 target = hier_robu$data.full$avg.var.eff.size)
48 expect_true(check_CR(hier_meta, vcov = robu_CR2_iv))
49 # expect_true(check_CR(hier_meta, vcov = "CR4"))
50 expect_true(check_CR(hier_meta, vcov = robu_CR2_not))
51 # expect_true(check_CR(hier_meta, vcov = "CR4",
52 # target = hier_robu$data.full$avg.var.eff.size))
53
54 expect_that(all.equal(hier_robu$VR.r, as.matrix(robu_CR2_iv), check.attributes=FALSE), is_a("character"))
55 expect_that(all.equal(hier_robu$VR.r, as.matrix(robu_CR2_not), check.attributes=FALSE), is_a("character"))
56
57 CR2_ttests <- coef_test(hier_meta, vcov = robu_CR2_not, test = "Satterthwaite")
58 expect_that(all.equal(hier_robu$dfs, CR2_ttests$df), is_a("character"))
59 expect_that(all.equal(hier_robu$reg_table$prob, CR2_ttests$p_Satt), is_a("character"))
60 })
61
62 dat_long <- to.long(measure="OR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg)
63 levels(dat_long$group) <- c("exp", "con")
64 dat_long$group <- relevel(dat_long$group, ref="con")
65 dat_long$esid <- factor(1:nrow(dat_long))
66 dat_long <- escalc(measure="PLO", xi=out1, mi=out2, data=dat_long)
67 rma_G <- rma.mv(yi, vi, mods = ~ group, random = ~ group | study, struct="CS", data=dat_long, sparse = TRUE)
68 rma_S <- rma.mv(yi, vi, mods = ~ group, random = list(~ 1 | esid, ~ 1 | study), data=dat_long, sparse = TRUE)
69
70 test_that("withS and withG model specifications agree.", {
71 CR_G <- lapply(CR_types, function(x) vcovCR(rma_G, type = x))
72 CR_S <- lapply(CR_types, function(x) vcovCR(rma_S, type = x))
73 expect_equivalent(CR_G, CR_S)
74
75 tests_G <- lapply(CR_types, function(x) coef_test(rma_G, vcov = x, test = "All", p_values = FALSE))
76 tests_S <- lapply(CR_types, function(x) coef_test(rma_S, vcov = x, test = "All", p_values = FALSE))
77 expect_equal(tests_G, tests_S, tolerance = 1e-6)
78 })
79
80 test_that("bread works", {
81 expect_true(check_bread(corr_meta, cluster = corrdat$studyid, y = corrdat$effectsize))
82 X <- model_matrix(corr_meta)
83 W <- corr_meta$W
84 V <- corr_meta$vi
85 vcov_corr <- bread(corr_meta) %*% t(X) %*% W %*% (V * W) %*% X %*% bread(corr_meta) / nobs(corr_meta)^2
86 vcov_corr <- as.matrix(vcov_corr)
87 attr(vcov_corr, "dimnames") <- attr(vcov(corr_meta), "dimnames")
88 expect_equal(vcov(corr_meta), vcov_corr)
89
90 expect_true(check_bread(hier_meta, cluster = hierdat$studyid, y = hierdat$effectsize))
91 expect_equal(vcov(hier_meta), bread(hier_meta) / nobs(hier_meta))
92
93 expect_true(check_bread(rma_G, cluster = dat_long$study, y = dat_long$yi))
94 expect_equal(vcov(rma_G), bread(rma_G) / nobs(rma_G))
95
96 expect_true(check_bread(rma_S, cluster = dat_long$study, y = dat_long$yi))
97 expect_equal(vcov(rma_S), bread(rma_S) / nobs(rma_S))
98 })
99
100 test_that("order doesn't matter", {
101
102 skip_on_cran()
103
104 check_sort_order(hier_meta, hierdat)
105
106 })
107
108 test_that("clubSandwich works with dropped covariates", {
109 dat_miss <- hierdat
110 dat_miss$binge[sample.int(nrow(hierdat), size = round(nrow(hierdat) / 10))] <- NA
111 dat_miss$followup[sample.int(nrow(hierdat), size = round(nrow(hierdat) / 20))] <- NA
112 expect_warning(hier_drop <- rma.mv(effectsize ~ binge + followup + sreport + age,
113 random = list(~ 1 | esid, ~ 1 | studyid),
114 data = dat_miss, V = var, method = "REML", sparse = TRUE))
115
116 hier_complete <- rma.mv(effectsize ~ binge + followup + sreport + age,
117 random = list(~ 1 | esid, ~ 1 | studyid),
118 subset = !is.na(binge) & !is.na(followup),
119 data = dat_miss, V = var, method = "REML", sparse = TRUE)
120
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))
124 CR_drop_B <- lapply(CR_types, function(x) vcovCR(hier_drop, type = x, cluster = dat_miss$studyid))
125 CR_complete <- lapply(CR_types, function(x) vcovCR(hier_complete, type = x))
126 expect_equal(CR_drop_A, CR_complete)
127 expect_equal(CR_drop_B, CR_complete)
128
129 test_drop_A <- lapply(CR_types, function(x) coef_test(hier_drop, vcov = x, test = "All", p_values = FALSE))
130 test_drop_B <- lapply(CR_types, function(x) coef_test(hier_drop, vcov = x, cluster = dat_miss$studyid, test = "All", p_values = FALSE))
131 test_complete <- lapply(CR_types, function(x) coef_test(hier_complete, vcov = x, test = "All", p_values = FALSE))
132 compare_ttests(test_drop_A, test_complete)
133 compare_ttests(test_drop_B, test_complete)
134
135 })
136
137 test_that("clubSandwich works with missing diagonal variances", {
138
139 dat_miss <- hierdat
140 dat_miss$var[sample.int(nrow(hierdat), size = round(nrow(hierdat) / 10))] <- NA
141 expect_warning(hier_drop <- rma.mv(effectsize ~ binge + followup + sreport + age,
142 random = list(~ 1 | esid, ~ 1 | studyid),
143 data = dat_miss, V = var, method = "REML", sparse = TRUE))
144
145 hier_complete <- rma.mv(effectsize ~ binge + followup + sreport + age,
146 random = list(~ 1 | esid, ~ 1 | studyid),
147 subset = !is.na(var),
148 data = dat_miss, V = var, method = "REML", sparse = TRUE)
149
150 expect_error(vcovCR(hier_complete, type = "CR0", cluster = dat_miss$studyid))
151
152 CR_drop_A <- lapply(CR_types, function(x) vcovCR(hier_drop, type = x))
153 CR_drop_B <- lapply(CR_types, function(x) vcovCR(hier_drop, type = x, cluster = dat_miss$studyid))
154 CR_complete <- lapply(CR_types, function(x) vcovCR(hier_complete, type = x))
155 expect_equal(CR_drop_A, CR_complete)
156 expect_equal(CR_drop_B, CR_complete)
157
158 })
159
160 test_that("clubSandwich works with missing vcov matrix", {
161
162 skip_if(packageVersion("metafor") < "2.1")
163
164 dat_miss <- corrdat
165 dat_miss$var[sample.int(nrow(corrdat), size = round(nrow(corrdat) / 10))] <- NA
166
167 suppressWarnings(
168 V_missing <- impute_covariance_matrix(dat_miss$var, cluster = dat_miss$studyid, r = 0.8)
169 )
170
171 expect_warning(corr_drop <- rma.mv(effectsize ~ males + college + binge,
172 random = ~ 1 | studyid,
173 V = V_missing, data = dat_miss,
174 sparse = TRUE))
175
176 corr_complete <- rma.mv(effectsize ~ males + college + binge,
177 random = ~ 1 | studyid,
178 subset = !is.na(var),
179 data = dat_miss, V = V_missing,
180 sparse = TRUE)
181
182 expect_error(vcovCR(corr_complete, type = "CR0", cluster = dat_miss$studyid))
183
184 CR_drop <- lapply(CR_types, function(x) vcovCR(corr_drop, cluster = dat_miss$studyid, type = x))
185 CR_complete <- lapply(CR_types, function(x) vcovCR(corr_complete, type = x))
186 expect_equal(CR_drop, CR_complete, tolerance = 1e-5)
187
188 })
189
190
191 test_that("vcovCR options work for CR2", {
192 RE_var <- targetVariance(hier_meta, cluster = factor(hierdat$studyid))
193 CR2_iv <- vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid)
194 expect_equal(vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid, inverse_var = TRUE), CR2_iv)
195
196 CR2_not <- vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid, inverse_var = FALSE)
197 expect_equal(CR2_not, CR2_iv)
198 expect_equivalent(vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid, target = RE_var), CR2_not)
199 expect_equivalent(vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid, target = RE_var, inverse_var = FALSE), CR2_not)
200 expect_false(identical(vcovCR(hier_meta, type = "CR2", cluster = hierdat$studyid, target = hierdat$var), CR2_not))
201 })
202
203 test_that("clubSandwich works with complicated random effects specifications.", {
204
205 skip_on_cran()
206
207 data(oswald2013, package = "robumeta")
208
209 oswald2013 <- within(oswald2013, {
210 V = (1 - R^2)^2 / (N - 3)
211 SSID = paste(Study, "sample",Sample.ID)
212 ESID = 1:nrow(oswald2013)
213 })
214
215 SS_lab <- unique(oswald2013$SSID)
216 n_SS <- length(SS_lab)
217 R_mat <- 0.4 + 0.6 * diag(nrow = n_SS)
218 colnames(R_mat) <- rownames(R_mat) <- SS_lab
219
220 m1 <- rma.mv(
221 R ~ 0 + IAT.Focus + Crit.Cat, V = V,
222 data = oswald2013,
223 random = list(~ 1 | Study, ~ 1 | SSID, ~ 1 | ESID),
224 sparse = TRUE
225 )
226
227 m2 <- update(m1,
228 random = list(~ IAT.Focus | Study, ~ 1 | SSID, ~ 1 | ESID),
229 struct = c("UN"))
230
231 m3 <- update(m1,
232 random = list(~ 1 | Study, ~ IAT.Focus | SSID, ~ 1 | ESID),
233 struct = c("UN","UN"))
234
235 m4 <- update(m1,
236 random = list(~ 1 | Study, ~ 1 | SSID, ~ IAT.Focus | ESID),
237 struct = c("DIAG"))
238
239 m5 <- update(m1,
240 random = list(~ IAT.Focus | Study, ~ IAT.Focus | SSID),
241 struct = c("UN","UN"))
242
243 m6 <- update(m1,
244 random = list(~ IAT.Focus | Study, ~ IAT.Focus | SSID, ~ 1 | ESID),
245 struct = c("UN","UN"))
246
247 m7 <- update(m5, struct = c("CS","CS"))
248 m8 <- update(m5, struct = c("HCS","HCS"))
249 m9 <- update(m5, struct = c("UN","CS"))
250 m10 <- update(m5, struct = c("CS","UN"))
251
252 m11 <- rma.mv(
253 R ~ 0 + IAT.Focus + Crit.ID, V = V,
254 data = oswald2013,
255 random = list(~ 1 + Crit.ID | Study),
256 struct = c("GEN"),
257 sparse = TRUE
258 )
259
260 m12 <- update(m11, random = list(~ 1 + Crit.ID | Study, ~ 1 | SSID))
261 m13 <- update(m11, random = list(~ 1 + Crit.ID | Study, ~ IAT.Focus | SSID),
262 struct = c("GEN","UN"))
263 m14 <- update(m11, random = list(~ IAT.Focus | Study, ~ 1 + Crit.ID | SSID),
264 struct = c("UN","GEN"))
265
266 mod_list <- list(m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14)
267 os_cluster <- factor(oswald2013$Study)
268
269 obj <- m6
270 struct <- parse_structure(obj)
271 findCluster.rma.mv(obj)
272
273 cluster_list <- lapply(mod_list, findCluster.rma.mv)
274 lapply(cluster_list, expect_equal, expected = os_cluster)
275
276 bread_checks <- sapply(mod_list, check_bread, cluster = oswald2013$Study, y = oswald2013$R)
277 expect_true(all(bread_checks))
278
279 CR_checks <- sapply(mod_list, check_CR, vcov = "CR2")
280 expect_true(all(CR_checks))
281
282 m11 <- update(m1, R = list(SSID = R_mat))
283 expect_error(findCluster.rma.mv(m11))
284
285 })
286
287 test_that("clubSandwich works for random slopes model.", {
288
289 # example from https://wviechtb.github.io/metadat/reference/dat.obrien2003.html
290 dat <- dat.obrien2003
291 dat$bmicent <- dat$bmi - ave(dat$bmi, dat$study)
292 dat <- escalc(measure="PR", xi=cases, ni=total, data=dat)
293 dat$yi <- dat$yi*100
294 dat$vi <- dat$vi*100^2
295 res <- rma.mv(yi, vi, mods = ~ bmicent,
296 random = ~ bmicent | study, struct="GEN",
297 data=dat,
298 sparse = TRUE)
299
300 cl <- findCluster.rma.mv(res)
301
302 expect_true(check_bread(res, cluster = cl, y = dat$yi))
303 expect_true(check_CR(res, vcov = "CR2"))
304
305 })
306
307 test_that("clubSandwich works for correlated hierarchical effects model.", {
308
309 skip_on_cran()
310
311 V_mat <- impute_covariance_matrix(vi = corrdat$var,
312 cluster = corrdat$studyid,
313 r = 0.7,
314 smooth_vi = TRUE)
315
316 CHE_es <- rma.mv(effectsize ~ males + college + binge, data = corrdat,
317 V = V_mat, random = ~ 1 | esid,
318 sparse = TRUE)
319 CHE_study <- rma.mv(effectsize ~ males + college + binge, data = corrdat,
320 V = V_mat, random = ~ 1 | studyid,
321 sparse = TRUE)
322 CHE_studyes <- rma.mv(effectsize ~ males + college + binge, data = corrdat,
323 V = V_mat, random = ~ 1 | studyid / esid,
324 sparse = TRUE)
325 CHE_esstudy <- rma.mv(effectsize ~ males + college + binge, data = corrdat,
326 V = V_mat, random = ~ 1 | esid/ studyid,
327 sparse = TRUE)
328 CHE_study_es <- rma.mv(effectsize ~ males + college + binge, data = corrdat,
329 V = V_mat, random = list(~ 1 | studyid, ~ 1 | esid),
330 sparse = TRUE)
331 CHE_es_study <- rma.mv(effectsize ~ males + college + binge, data = corrdat,
332 V = V_mat, random = list(~ 1 | esid, ~ 1 | studyid),
333 sparse = TRUE)
334
335 mods <- list(es = CHE_es, study = CHE_study,
336 studyes = CHE_studyes, esstudy = CHE_esstudy,
337 study_es = CHE_study_es, es_study = CHE_es_study)
338
339 clusters <- lapply(mods, findCluster.rma.mv)
340
341 expect_equal(clusters$study, clusters$studyes)
342 expect_equal(clusters$es, clusters$esstudy)
343 expect_equal(clusters$studyes, clusters$study_es)
344 expect_equal(clusters$study_es, clusters$es_study)
345
346 V_CR2s <- lapply(mods, vcovCR, type = "CR2")
347 V_CR2s_clust <- mapply(vcovCR, mods, clusters, type = "CR2", SIMPLIFY = FALSE)
348
349 expect_equal(V_CR2s, V_CR2s_clust)
350 expect_equal(V_CR2s$studyes, V_CR2s$study_es)
351 expect_equal(V_CR2s$studyes, V_CR2s$es_study)
352
353 })
354
355 test_that("vcovCR errors when there is only one cluster.", {
356
357 dat <- data.frame(
358 study = "study1", # study number
359 est = runif(5, 0.1, 0.6), # R-squared values
360 se = runif(5, 0.005, 0.025), # standard errors of R-squared values
361 es_id = 1:5 # effect size ID
362 )
363
364 v_mat <- impute_covariance_matrix(dat$se^2, cluster = dat$study, r = 0.8)
365
366 # working model in metafor
367 expect_warning(
368 res <- rma.mv(yi = est, V = v_mat, random = ~ 1 | study / es_id, data = dat)
369 )
370
371 single_cluster_error_msg <- "Cluster-robust variance estimation will not work when the data only includes a single cluster."
372
373 expect_error(
374 vcovCR(res, type = "CR0"), single_cluster_error_msg
375 )
376
377 expect_error(
378 conf_int(res, vcov = "CR1"), single_cluster_error_msg
379 )
380
381 expect_error(
382 coef_test(res, vcov = "CR2"), single_cluster_error_msg
383 )
384
385 expect_error(
386 Wald_test(res, constraints = constrain_zero(1), vcov = "CR3"),
387 single_cluster_error_msg
388 )
389
390 expect_error(
391 vcovCR(res, cluster = dat$es_id),
392 "Random effects are not nested within clustering variable."
393 )
394
395 })
396
397
398 test_that("clubSandwich works when random effects variable has missing levels.",{
399
400 dat <- dat.konstantopoulos2011
401 dat$district_fac <- factor(dat$district)
402 dat$district_fac_plus <- factor(dat$district, levels = c(levels(dat$district_fac), 1000, 10000))
403
404 mlma_fac <- rma.mv(yi ~ year, V = vi,
405 random = ~ 1 | district_fac / study,
406 data = dat,
407 sparse = TRUE)
408
409 implicit_fac <- coef_test(mlma_fac, vcov = "CR2")
410 explicit_fac <- coef_test(mlma_fac, vcov = "CR2", cluster = dat$district_fac)
411 expect_equal(implicit_fac, explicit_fac)
412
413 mlma_plus <- rma.mv(yi ~ year, V = vi,
414 random = ~ 1 | district_fac_plus / study,
415 data = dat,
416 sparse = TRUE)
417
418 implicit_plus <- coef_test(mlma_plus, vcov = "CR2")
419 explicit_plus <- coef_test(mlma_plus, vcov = "CR2", cluster = dat$district_fac_plus)
420 expect_equal(implicit_plus, explicit_plus)
421 expect_equal(implicit_fac, implicit_plus)
422 expect_equal(implicit_fac, explicit_plus)
423
424 mlma_num <- rma.mv(yi ~ year, V = vi,
425 random = ~ 1 | district / study,
426 data = dat,
427 sparse = TRUE)
428
429 implicit_num <- coef_test(mlma_num, vcov = "CR2")
430 explicit_num <- coef_test(mlma_num, vcov = "CR2", cluster = dat$district)
431 expect_equal(implicit_num, explicit_num)
432 expect_equal(implicit_fac, implicit_num)
433 expect_equal(implicit_fac, explicit_num)
434
435 })
436
437 Vmat <- with(corrdat, impute_covariance_matrix(vi = var, cluster = studyid, r = 0.8))
438 corr_meta <- rma.mv(effectsize ~ males + college + binge, data = corrdat,
439 V = Vmat, random = ~ 1 | studyid, sparse = TRUE)
440
441 test_that("clubSandwich agrees with metafor::robust() for CR0.", {
442
443 test_CR0 <- coef_test(corr_meta, vcov = "CR0", test = "All")
444 meta_CR0 <- robust(corr_meta, cluster = corrdat$studyid, adjust = FALSE)
445 rob_CR0 <- coef_test(meta_CR0, vcov = "CR0", test = "All")
446 expect_equal(test_CR0$SE, meta_CR0$se)
447 expect_equal(test_CR0$df_tp, rep(meta_CR0$df, length(test_CR0$df_tp)))
448 expect_equal(test_CR0$p_tp, meta_CR0$pval, tolerance = 1e-5)
449 compare_ttests(rob_CR0, test_CR0, tol = 1e-6)
450
451 club_F_CR0 <- Wald_test(corr_meta, constraints = constrain_zero(2:4),
452 vcov = "CR0", test = "Naive-Fp")
453 rob_F_CR0 <- Wald_test(meta_CR0, constraints = constrain_zero(2:4),
454 vcov = "CR0", test = "Naive-Fp")
455 expect_equal(club_F_CR0$Fstat, meta_CR0$QM)
456 expect_equal(club_F_CR0$df_num, meta_CR0$QMdf[1])
457 expect_equal(club_F_CR0$df_denom, meta_CR0$QMdf[2])
458 expect_equal(club_F_CR0$p_val, meta_CR0$QMp, tolerance = 1e-5)
459 compare_Waldtests(club_F_CR0, rob_F_CR0, tol = 1e-5)
460
461 })
462
463 test_that("clubSandwich agrees with metafor::robust() for CR1p.", {
464
465 test_CR1 <- coef_test(corr_meta, vcov = "CR1p", test = "All")
466 meta_CR1 <- robust(corr_meta, cluster = corrdat$studyid, adjust = TRUE)
467 rob_CR1 <- coef_test(meta_CR1, vcov = "CR1p", test = "All")
468 expect_equal(test_CR1$SE, meta_CR1$se)
469 expect_equal(test_CR1$df_tp, rep(meta_CR1$df, length(test_CR1$df_tp)))
470 expect_equal(test_CR1$p_tp, meta_CR1$pval, tolerance = 1e-5)
471 compare_ttests(rob_CR1, test_CR1, tol = 1e-5)
472
473 club_F_CR1 <- Wald_test(corr_meta, constraints = constrain_zero(2:4),
474 vcov = "CR1p", test = "Naive-Fp")
475 rob_F_CR1 <- Wald_test(meta_CR1, constraints = constrain_zero(2:4),
476 vcov = "CR1p", test = "Naive-Fp")
477 expect_equal(club_F_CR1$Fstat, meta_CR1$QM)
478 expect_equal(club_F_CR1$df_num, meta_CR1$QMdf[1])
479 expect_equal(club_F_CR1$df_denom, meta_CR1$QMdf[2])
480 expect_equal(club_F_CR1$p_val, meta_CR1$QMp, tolerance = 1e-5)
481 compare_Waldtests(club_F_CR1, rob_F_CR1, tol = 1e-5)
482
483 })
484
485 test_that("clubSandwich agrees with metafor::robust() for CR2.", {
486
487 skip_if(packageVersion('metafor') < "3.1.31")
488 test_CR2 <- coef_test(corr_meta, vcov = "CR2", test = "All")
489 meta_CR2 <- robust(corr_meta, cluster = corrdat$studyid, clubSandwich = TRUE)
490 rob_CR2 <- coef_test(meta_CR2, vcov = "CR2", test = "All")
491 expect_equal(test_CR2$SE, meta_CR2$se)
492 compare_ttests(rob_CR2, test_CR2, tol = 1e-5)
493
494 club_F_CR2 <- Wald_test(corr_meta, constraints = constrain_zero(2:4),
495 vcov = "CR2", test = "All")
496 rob_F_CR2 <- Wald_test(meta_CR2, constraints = constrain_zero(2:4),
497 vcov = "CR2", test = "All")
498 expect_equal(subset(club_F_CR2, test == "HTZ")$Fstat, meta_CR2$QM)
499 expect_equal(subset(club_F_CR2, test == "HTZ")$df_num, meta_CR2$QMdf[1])
500 expect_equal(subset(club_F_CR2, test == "HTZ")$df_denom, meta_CR2$QMdf[2])
501 expect_equal(subset(club_F_CR2, test == "HTZ")$p_val, meta_CR2$QMp, tolerance = 1e-5)
502 compare_Waldtests(club_F_CR2, rob_F_CR2, tol = 1e-5)
503 })
504
505 test_that("clubSandwich methods work on robust.rma objects.", {
506
507 hier_robust <- robust(hier_meta, cluster = hierdat$studyid, adjust = TRUE)
508
509 expect_equal(residuals_CS(hier_meta), residuals_CS(hier_robust))
510 expect_equal(coef_CS(hier_meta), coef_CS(hier_robust))
511 expect_equal(model_matrix(hier_meta), model_matrix(hier_robust))
512 expect_equal(bread(hier_meta), bread(hier_robust))
513 expect_equal(v_scale(hier_meta), v_scale(hier_robust))
514 expect_equal(targetVariance(hier_meta, cluster = hierdat$studyid),
515 targetVariance(hier_robust, cluster = hierdat$studyid))
516 expect_equal(weightMatrix(hier_meta, cluster = hierdat$studyid),
517 weightMatrix(hier_robust, cluster = hierdat$studyid))
518
519 hier_club <- robust(hier_meta, cluster = hierdat$studyid, adjust = FALSE, clubSandwich = TRUE)
520
521 expect_equal(residuals_CS(hier_meta), residuals_CS(hier_club))
522 expect_equal(coef_CS(hier_meta), coef_CS(hier_club))
523 expect_equal(model_matrix(hier_meta), model_matrix(hier_club))
524 expect_equal(bread(hier_meta), bread(hier_club))
525 expect_equal(v_scale(hier_meta), v_scale(hier_club))
526 expect_equal(targetVariance(hier_meta, cluster = hierdat$studyid),
527 targetVariance(hier_club, cluster = hierdat$studyid))
528 expect_equal(weightMatrix(hier_meta, cluster = hierdat$studyid),
529 weightMatrix(hier_club, cluster = hierdat$studyid))
530
531 })
532
533 test_that("clubSandwich works with test = 'knha'.", {
534
535 data("oswald2013", package = "robumeta")
536 oswald2013$yi <- atanh(oswald2013$R)
537 oswald2013$vi <- 1 / (oswald2013$N - 3)
538
539 mod1 <- rma.mv(yi ~ 0 + Crit.Cat + Crit.Domain + IAT.Focus + Scoring,
540 V = vi,
541 data = oswald2013,
542 sparse = TRUE)
543
544 suppressWarnings(
545 mod2 <- update(mod1, test = "knha")
546 )
547
548 vcovs_1 <- lapply(CR_types, function(x) vcovCR(mod1, type = x, cluster = oswald2013$Study))
549 vcovs_2 <- lapply(CR_types, function(x) vcovCR(mod2, type = x, cluster = oswald2013$Study))
550
551 expect_equal(vcovs_1, vcovs_2, tolerance = 1e-5)
552
553 })