Debian Codegraph @

tests/testthat/test_rma-ls.R

raw
1 context("rma.uni location-scale models")
2
3 skip_if_not_installed("metadat")
4 skip_if_not_installed("metafor", minimum_version = "3.4-0")
5
6 library(metadat)
7 suppressMessages(library(metafor, quietly=TRUE))
8
9 dat <- dat.bangertdrowns2004
10 dat$ni100 <- dat$ni/100
11 dat$meta[is.na(dat$meta)] <- 0
12 res <- rma(yi, vi, mods = ~ ni100 + meta, scale = ~ ni100 + imag, data = dat)
13
14
15 test_that("bread works", {
16 expect_true(check_bread(res, cluster = dat$id, y = dat$yi))
17 vcov_mat <- bread(res) / nobs(res)
18 attr(vcov_mat, "dimnames") <- attr(vcov(res)$beta, "dimnames")
19 expect_equal(vcov(res)$beta, vcov_mat)
20 })
21
22 CR_types <- paste0("CR",0:4)
23
24 test_that("order doesn't matter", {
25
26 skip_on_cran()
27
28 check_sort_order(res, dat, cluster = "id")
29
30 })
31
32 test_that("clubSandwich works with dropped covariates", {
33
34 dat_miss <- dat.bangertdrowns2004
35 expect_warning(res_drop <- rma(yi, vi, mods = ~ length + feedback + info, scale = ~ wic, data = dat))
36
37 subset_ind <- with(dat_miss, complete.cases(length, feedback, info, wic, yi, vi))
38 res_complete <- rma(yi, vi, mods = ~ length + feedback + info, scale = ~ wic, data = dat_miss[subset_ind,])
39 expect_error(vcovCR(res_complete, type = "CR0", cluster = dat_miss$id))
40
41 CR_drop <- lapply(CR_types, function(x) vcovCR(res_drop, type = x, cluster = dat_miss$id))
42 CR_complete <- lapply(CR_types, function(x) vcovCR(res_complete, type = x, cluster = dat_miss$id[subset_ind]))
43 expect_equal(CR_drop, CR_complete)
44
45 test_drop <- lapply(CR_types, function(x) coef_test(res_drop, vcov = x, cluster = dat_miss$id, test = "All", p_values = FALSE))
46 test_complete <- lapply(CR_types, function(x) coef_test(res_complete, vcov = x, cluster = dat_miss$id[subset_ind], test = "All", p_values = FALSE))
47 compare_ttests(test_drop, test_complete)
48 })
49
50 test_that("clubSandwich works with missing variances", {
51
52 dat_miss <- dat
53 dat_miss$vi[sample.int(nrow(dat_miss), size = round(nrow(dat_miss) / 10))] <- NA
54 expect_warning(res_drop <- rma(yi, vi, mods = ~ ni100 + meta, scale = ~ ni100 + imag, data = dat_miss))
55
56
57 subset_ind <- with(dat_miss, !is.na(vi))
58 res_complete <- rma(yi, vi, mods = ~ ni100 + meta, scale = ~ ni100 + imag, data = dat_miss, subset = !is.na(vi))
59
60 expect_error(vcovCR(res_complete, type = "CR0", cluster = dat_miss$id))
61
62 CR_drop <- lapply(CR_types, function(x) vcovCR(res_drop, type = x, cluster = dat_miss$id))
63 CR_complete <- lapply(CR_types, function(x) vcovCR(res_complete, type = x, cluster = dat_miss$id[subset_ind]))
64 expect_equal(CR_drop, CR_complete)
65
66 })
67
68
69 test_that("vcovCR options work for CR2", {
70 RE_var <- res$tau2 + dat$vi
71 CR2_iv <- vcovCR(res, type = "CR2", cluster = dat$id)
72 expect_equal(vcovCR(res, type = "CR2", cluster = dat$id, inverse_var = TRUE), CR2_iv)
73
74 CR2_not <- vcovCR(res, type = "CR2", cluster = dat$id, inverse_var = FALSE)
75 attr(CR2_iv, "inverse_var") <- FALSE
76 attr(CR2_iv, "target") <- attr(CR2_not, "target")
77 expect_equal(CR2_not, CR2_iv)
78 expect_equal(vcovCR(res, type = "CR2", cluster = dat$id, target = RE_var), CR2_not)
79 expect_equal(vcovCR(res, type = "CR2", cluster = dat$id, target = RE_var, inverse_var = FALSE), CR2_not)
80 expect_false(identical(vcovCR(res, type = "CR2", cluster = dat$id, target = dat$vi), CR2_not))
81 })