Debian Codegraph @

tests/testthat/test_linear_contrast.R

raw
1 set.seed(20210110)
2
3 skip_if_not_installed("carData")
4
5
6 # Duncan example
7 data(Duncan, package = "carData")
8 Duncan$cluster <- sample(LETTERS[1:8], size = nrow(Duncan), replace = TRUE)
9 Duncan_int <- lm(prestige ~ type * (income + education), data=Duncan)
10 coefs_int <- coef(Duncan_int)
11 coef_names_int <- names(coefs_int)
12 Duncan_int_CR2 <- vcovCR(Duncan_int, type = "CR2", cluster = Duncan$cluster)
13
14 Duncan_sep <- lm(prestige ~ 0 + type + type:(income + education), data=Duncan)
15 coefs_sep <- coef(Duncan_sep)
16 coef_names_sep <- names(coefs_sep)
17 Duncan_sep_CR2 <- vcovCR(Duncan_sep, type = "CR2", cluster = Duncan$cluster)
18
19
20 # STAR example
21 skip_if_not_installed("AER")
22 data(STAR, package = "AER")
23 levels(STAR$stark)[3] <- "aide"
24 levels(STAR$schoolk)[1] <- "urban"
25 STAR <- subset(STAR,
26 !is.na(schoolidk),
27 select = c(schoolidk, schoolk, stark, gender, ethnicity, math1, lunchk))
28
29 lm_urbanicity <- lm(math1 ~ schoolk * stark + gender + ethnicity + lunchk,
30 data = STAR)
31
32
33 CRs <- paste0("CR", 0:4)
34
35 test_that("vcov arguments work", {
36 VCR <- lapply(CRs, function(t) vcovCR(Duncan_sep, type = t, cluster = Duncan$cluster))
37 CI_A <- lapply(VCR, function(v) linear_contrast(Duncan_sep,
38 vcov = v, cluster = Duncan$cluster,
39 contrast = constrain_pairwise(1:3),
40 level = .98))
41 CI_B <- lapply(CRs, function(t) linear_contrast(Duncan_sep,
42 vcov = t, cluster = Duncan$cluster,
43 contrast = constrain_pairwise(1:3),
44 level = .98))
45 expect_equal(CI_A, CI_B, check.attributes = FALSE)
46
47 })
48
49 test_that("constrain_() functions work.", {
50
51 # Not worrying about CR4 here
52
53 CI_A <- lapply(CRs[1:4], function(t)
54 linear_contrast(Duncan_sep, vcov = t, cluster = Duncan$cluster,
55 contrast = constrain_pairwise(1:3))
56 )
57
58 CI_B <- lapply(CRs[1:4], function(t)
59 linear_contrast(Duncan_int, vcov = t, cluster = Duncan$cluster,
60 contrast = constrain_pairwise(2:3, with_zero = TRUE))
61 )
62
63 CI_A <- lapply(CI_A, subset, select = -Coef)
64 CI_B <- lapply(CI_B, subset, select = -Coef)
65
66 expect_equal(CI_A, CI_B, check.attributes = FALSE)
67 })
68
69 test_that("linear_contrast() works with lists.", {
70
71
72 CIs <- linear_contrast(lm_urbanicity,
73 vcov = "CR2", cluster = STAR$schoolidk,
74 contrast = list(
75 A = constrain_zero(2:3),
76 B = constrain_pairwise(2:3, with_zero = TRUE),
77 C = constrain_equal("ethnicity", reg_ex = TRUE),
78 D = constrain_pairwise("ethnicity", reg_ex = TRUE)
79 ))
80
81 CI_A <- as.data.frame(subset(CIs, grepl("A\\.", Coef), select = -Coef))
82 CI_B <- as.data.frame(subset(CIs, grepl("B\\.[a-z]+$", Coef), select = -Coef))
83 expect_equal(CI_A, CI_B, check.attributes = FALSE)
84
85 CI_C <- as.data.frame(subset(CIs, grepl("C\\.", Coef), select = -Coef))
86 CI_D <- as.data.frame(subset(CIs, grepl("D\\..+ethnicityafam$", Coef), select = -Coef))
87 expect_equal(CI_C, CI_D, check.attributes = FALSE)
88
89 })
90
91 test_that("printing works", {
92 CIs <- linear_contrast(Duncan_sep, vcov = "CR0", cluster = Duncan$cluster,
93 constrain_pairwise(":education", reg_ex = TRUE))
94 expect_output(print(CIs))
95
96 CIs <- linear_contrast(Duncan_sep, vcov = "CR0", cluster = Duncan$cluster,
97 constrain_pairwise(":education", reg_ex = TRUE),
98 p_values = TRUE)
99 expect_output(x <- print(CIs))
100 expect_true(all(c("p-value","Sig.") %in% names(x)))
101 })
102
103 test_that("level checks work", {
104 expect_error(linear_contrast(Duncan_sep, vcov = "CR0", cluster = Duncan$cluster,
105 constrain_pairwise(":education", reg_ex = TRUE),
106 level = -0.01))
107 expect_error(linear_contrast(Duncan_sep, vcov = "CR0", cluster = Duncan$cluster,
108 constrain_pairwise(":education", reg_ex = TRUE),
109 level = 95))
110 expect_output(print(
111 linear_contrast(Duncan_sep, vcov = "CR0", cluster = Duncan$cluster,
112 constrain_pairwise(":education", reg_ex = TRUE),
113 level = runif(1))
114 ))
115 })
116
117 test_that("CI boundaries are ordered", {
118 lev <- runif(1)
119 CI_z <- linear_contrast(Duncan_sep, vcov = "CR0", cluster = Duncan$cluster,
120 constrain_pairwise(":education", reg_ex = TRUE),
121 test = "z", level = lev)
122 CI_t <- linear_contrast(Duncan_sep, vcov = "CR0", cluster = Duncan$cluster,
123 constrain_pairwise(":education", reg_ex = TRUE),
124 test = "naive-t", level = lev)
125 CI_Satt <- linear_contrast(Duncan_sep, vcov = "CR0", cluster = Duncan$cluster,
126 constrain_pairwise(":education", reg_ex = TRUE),
127 test = "Satterthwaite", level = lev)
128 expect_true(all(CI_t$CI_L < CI_z$CI_L))
129 expect_true(all(CI_t$CI_U > CI_z$CI_U))
130 expect_true(all(CI_Satt$CI_L < CI_z$CI_L))
131 expect_true(all(CI_Satt$CI_U > CI_z$CI_U))
132 })
133
134 test_that("linear_contrast() is consistent with Wald_test()", {
135
136 skip_on_cran()
137
138 lev <- runif(1)
139 CIs <- lapply(CRs, function(v)
140 linear_contrast(lm_urbanicity, vcov = v, cluster = STAR$schoolidk,
141 contrasts = list(
142 A = constrain_zero("ethnicity", reg_ex = TRUE),
143 B = constrain_equal("ethnicity", reg_ex = TRUE)
144 ),
145 test = "Satterthwaite", level = lev, p_values = TRUE))
146 Wald_tests <- lapply(CRs, function(v)
147 Wald_test(lm_urbanicity, vcov = v, cluster = STAR$schoolidk,
148 constraints = constrain_pairwise("ethnicity", reg_ex = TRUE, with_zero = TRUE),
149 test = "HTZ", tidy = TRUE))
150
151 CI_pvals <- lapply(CIs, function(x) x$p_val)
152 Wald_pvals <- lapply(Wald_tests, function(x) x$p_val[1:7])
153 expect_equal(CI_pvals, Wald_pvals, tolerance = 1e-6)
154
155 })
156
157 test_that("linear_contrast() has informative error messages.", {
158 expect_error(
159 linear_contrast(Duncan_sep, vcov = "CR0", cluster = Duncan$cluster,
160 constrain_pairwise(":education", reg_ex = TRUE),
161 test = "all")
162 )
163
164 expect_error(
165 linear_contrast(Duncan_sep, vcov = "CR0", cluster = Duncan$cluster,
166 constrain_pairwise(":education", reg_ex = TRUE),
167 test = "saddlepoint")
168 )
169
170 })
171
172 test_that("linear_contrast() works with scalar (length-1) contrasts.", {
173
174 lm_int <- lm(math1 ~ 1, data = STAR)
175 x0 <- linear_contrast(lm_int, vcov = "CR0", cluster = STAR$schoolidk, contrasts = 1)
176 expect_s3_class(x0, "conf_int_clubSandwich")
177
178 x2 <- linear_contrast(lm_int, vcov = "CR2", cluster = STAR$schoolidk, contrasts = 1)
179 expect_s3_class(x2, "conf_int_clubSandwich")
180
181 y0 <- linear_contrast(lm_int, vcov = "CR0", cluster = STAR$schoolidk, contrasts = matrix(1))
182 y0$Coef <- row.names(y0) <- "Contrast"
183 expect_equal(x0, y0)
184
185 y2 <- linear_contrast(lm_int, vcov = "CR2", cluster = STAR$schoolidk, contrasts = 1)
186 y2$Coef <- row.names(y2) <- "Contrast"
187 expect_equal(x2, y2)
188
189 STAR$wt <- 1L
190 wls_int <- lm(math1 ~ 1, weights = wt, data = STAR)
191 z0 <- linear_contrast(wls_int, vcov = "CR0", cluster = STAR$schoolidk, contrasts = 1)
192 expect_s3_class(z0, "conf_int_clubSandwich")
193
194 z2 <- linear_contrast(wls_int, vcov = "CR2", cluster = STAR$schoolidk, contrasts = 1)
195 expect_s3_class(z2, "conf_int_clubSandwich")
196
197 })
198
199 test_that("linear_contrast() works with scalar (length-1) contrasts in metafor.", {
200
201 skip_if_not_installed("metafor")
202 suppressPackageStartupMessages(library(metafor))
203
204 dat <- dat.bangertdrowns2004
205 res_uni <- rma(yi, vi, weights = 1, data=dat)
206
207 x0 <- linear_contrast(res_uni, vcov = "CR0", cluster = dat$id, contrasts = 1)
208 expect_s3_class(x0, "conf_int_clubSandwich")
209 y0 <- linear_contrast(res_uni, vcov = "CR0", cluster = dat$id, contrasts = matrix(1))
210 y0$Coef <- row.names(y0) <- "Contrast"
211 expect_equal(x0, y0)
212
213 x2 <- linear_contrast(res_uni, vcov = "CR2", cluster = dat$id, contrasts = 1)
214 expect_s3_class(x2, "conf_int_clubSandwich")
215 y2 <- linear_contrast(res_uni, vcov = "CR2", cluster = dat$id, contrasts = 1)
216 y2$Coef <- row.names(y2) <- "Contrast"
217 expect_equal(x2, y2)
218
219
220 })
221
222 test_that("linear_contrast() works with multiple contrasts in metafor.", {
223
224 skip_if_not_installed("metafor")
225 suppressPackageStartupMessages(library(metafor))
226
227 dat <- dat.bangertdrowns2004
228 dat$grade_f <- factor(dat$grade)
229
230 res_uni <- rma.mv(
231 yi = yi,
232 mods = ~ 0 + grade_f,
233 V = vi,
234 W = 1,
235 data=dat,
236 sparse = TRUE
237 )
238
239 contrast_mat <- constrain_pairwise("grade_f", coefs = coef(res_uni), reg_ex = TRUE)
240
241 pairwise_contrasts <- linear_contrast(
242 res_uni,
243 vcov = "CR2", cluster = dat$id,
244 contrasts = contrast_mat,
245 p_values = TRUE
246 )
247
248 expect_s3_class(pairwise_contrasts, "conf_int_clubSandwich")
249
250 pairwise_tests <- Wald_test(
251 res_uni,
252 vcov = "CR2", cluster = dat$id,
253 constraints = contrast_mat,
254 tidy = TRUE
255 )
256
257 expect_equal(pairwise_contrasts$df, pairwise_tests$df_denom)
258 expect_equal(pairwise_contrasts$p_val, pairwise_tests$p_val)
259
260 })