| 1 |
skip_if_not_installed("carData") |
| 2 |
|
| 3 |
data(Baumann, package = "carData") |
| 4 |
Baumann$id <- 1:nrow(Baumann) |
| 5 |
|
| 6 |
test_that("Wald_test words with multiple comparisons adjustment and a single test type (Baumann data).", { |
| 7 |
|
| 8 |
Baumann_fit <- lm(post.test.1 ~ 0 + group + pretest.1, data=Baumann) |
| 9 |
|
| 10 |
Wald5 <- Wald_test( |
| 11 |
Baumann_fit, |
| 12 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 13 |
vcov = "CR2", |
| 14 |
cluster = Baumann$id, |
| 15 |
test = "HTZ" |
| 16 |
) |
| 17 |
|
| 18 |
Wald6 <- Wald_test( |
| 19 |
Baumann_fit, |
| 20 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 21 |
vcov = "CR2", |
| 22 |
cluster = Baumann$id, |
| 23 |
test = "HTZ", |
| 24 |
adjustment_method = "none" |
| 25 |
) |
| 26 |
|
| 27 |
# check that explicitly stating default does not affect functionality |
| 28 |
lapply(Wald5, expect_s3_class, class = "Wald_test_clubSandwich") |
| 29 |
lapply(Wald6, expect_s3_class, class = "Wald_test_clubSandwich") |
| 30 |
expect_equal(Wald5, Wald6) |
| 31 |
|
| 32 |
# change Wald6 to have hochberg adjustment |
| 33 |
Wald6 <- Wald_test( |
| 34 |
Baumann_fit, |
| 35 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 36 |
vcov = "CR2", |
| 37 |
cluster = Baumann$id, |
| 38 |
test = "HTZ", |
| 39 |
adjustment_method = "hochberg" |
| 40 |
) |
| 41 |
|
| 42 |
Wald5_p_values <- sapply(Wald5, function(x) x$p_val) # extract p-values of Wald5 |
| 43 |
Wald6_p_values <- sapply(Wald6, function(x) x$p_val) # extract p-values of Wald6 |
| 44 |
|
| 45 |
expect_false(all(Wald5_p_values == Wald6_p_values)) |
| 46 |
|
| 47 |
# get adjusted_p_values for Wald5, formatted the same as extracted p-values from Wald6 |
| 48 |
Wald5_adjusted_p <- p.adjust(Wald5_p_values, method = "hochberg") |
| 49 |
expect_equal(Wald5_adjusted_p, Wald6_p_values) |
| 50 |
|
| 51 |
# Now using tidy = TRUE |
| 52 |
Wald7 <- Wald_test( |
| 53 |
Baumann_fit, |
| 54 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 55 |
vcov = "CR2", |
| 56 |
cluster = Baumann$id, |
| 57 |
test = "EDF", |
| 58 |
tidy = TRUE |
| 59 |
) |
| 60 |
|
| 61 |
Wald8 <- Wald_test( |
| 62 |
Baumann_fit, |
| 63 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 64 |
vcov = "CR2", |
| 65 |
cluster = Baumann$id, |
| 66 |
test = "EDF", |
| 67 |
adjustment_method = "none", |
| 68 |
tidy = TRUE |
| 69 |
) |
| 70 |
|
| 71 |
expect_equal(Wald7,Wald8) |
| 72 |
|
| 73 |
Wald9 <- Wald_test( |
| 74 |
Baumann_fit, |
| 75 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 76 |
vcov = "CR2", |
| 77 |
cluster = Baumann$id, |
| 78 |
test = "EDF", |
| 79 |
adjustment_method = "holm", |
| 80 |
tidy = TRUE |
| 81 |
) |
| 82 |
|
| 83 |
Wald8_p_adjusted <- tapply(Wald8$p_val, Wald8$test, p.adjust, method = "holm", simplify = FALSE) |
| 84 |
Wald8_adjusted <- Wald8 |
| 85 |
Wald8_adjusted$p_val <- unsplit(Wald8_p_adjusted, Wald8$test) |
| 86 |
|
| 87 |
expect_equal(Wald8_adjusted, Wald9) |
| 88 |
|
| 89 |
}) |
| 90 |
|
| 91 |
test_that("Wald_test words with multiple comparisons adjustment and multiple test types (Baumann data).", { |
| 92 |
|
| 93 |
Baumann_fit <- lm(post.test.1 ~ 0 + group + pretest.1, data=Baumann) |
| 94 |
|
| 95 |
Wald5 <- Wald_test( |
| 96 |
Baumann_fit, |
| 97 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 98 |
vcov = "CR2", |
| 99 |
cluster = Baumann$id, |
| 100 |
test = c("HTZ","chi-sq") |
| 101 |
) |
| 102 |
|
| 103 |
Wald6 <- Wald_test( |
| 104 |
Baumann_fit, |
| 105 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 106 |
vcov = "CR2", |
| 107 |
cluster = Baumann$id, |
| 108 |
test = c("HTZ","chi-sq"), |
| 109 |
adjustment_method = "none" |
| 110 |
) |
| 111 |
|
| 112 |
# check that explicitly stating default does not affect functionality |
| 113 |
lapply(Wald5, expect_s3_class, class = "Wald_test_clubSandwich") |
| 114 |
lapply(Wald6, expect_s3_class, class = "Wald_test_clubSandwich") |
| 115 |
expect_equal(Wald5, Wald6) |
| 116 |
|
| 117 |
# change Wald6 to have hochberg adjustment |
| 118 |
Wald6 <- Wald_test( |
| 119 |
Baumann_fit, |
| 120 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 121 |
vcov = "CR2", |
| 122 |
cluster = Baumann$id, |
| 123 |
test = c("HTZ","chi-sq"), |
| 124 |
adjustment_method = "hochberg" |
| 125 |
) |
| 126 |
|
| 127 |
Wald5_p_values <- sapply(Wald5, function(x) x$p_val) # extract p-values of Wald5 |
| 128 |
Wald6_p_values <- sapply(Wald6, function(x) x$p_val) # extract p-values of Wald6 |
| 129 |
|
| 130 |
expect_false(all(Wald5_p_values == Wald6_p_values)) |
| 131 |
|
| 132 |
# get adjusted_p_values for Wald5, formatted the same as extracted p-values from Wald6 |
| 133 |
Wald5_adjusted_p <- apply(Wald5_p_values, 1, p.adjust, method = "hochberg", simplify = FALSE) |
| 134 |
Wald5_adjusted_p <- do.call(rbind, Wald5_adjusted_p) |
| 135 |
expect_equal(Wald5_adjusted_p, Wald6_p_values) |
| 136 |
|
| 137 |
# Now using tidy = TRUE |
| 138 |
Wald7 <- Wald_test( |
| 139 |
Baumann_fit, |
| 140 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 141 |
vcov = "CR2", |
| 142 |
cluster = Baumann$id, |
| 143 |
test = c("HTA","EDF","EDT"), |
| 144 |
tidy = TRUE |
| 145 |
) |
| 146 |
|
| 147 |
Wald8 <- Wald_test( |
| 148 |
Baumann_fit, |
| 149 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 150 |
vcov = "CR2", |
| 151 |
cluster = Baumann$id, |
| 152 |
test = c("HTA","EDF","EDT"), |
| 153 |
adjustment_method = "none", |
| 154 |
tidy = TRUE |
| 155 |
) |
| 156 |
|
| 157 |
expect_equal(Wald7,Wald8) |
| 158 |
|
| 159 |
Wald9 <- Wald_test( |
| 160 |
Baumann_fit, |
| 161 |
constraints = constrain_pairwise("group", reg_ex = TRUE), |
| 162 |
vcov = "CR2", |
| 163 |
cluster = Baumann$id, |
| 164 |
test = c("HTA","EDF","EDT"), |
| 165 |
adjustment_method = "bonferroni", |
| 166 |
tidy = TRUE |
| 167 |
) |
| 168 |
|
| 169 |
Wald8_p_adjusted <- tapply(Wald8$p_val, Wald8$test, p.adjust, method = "bonferroni", simplify = FALSE) |
| 170 |
Wald8_adjusted <- Wald8 |
| 171 |
Wald8_adjusted$p_val <- unsplit(Wald8_p_adjusted, Wald8$test) |
| 172 |
|
| 173 |
expect_equal(Wald8_adjusted, Wald9) |
| 174 |
|
| 175 |
}) |
| 176 |
|
| 177 |
data(Duncan, package = "carData") |
| 178 |
Duncan$cluster <- sample(LETTERS[1:8], size = nrow(Duncan), replace = TRUE) |
| 179 |
|
| 180 |
|
| 181 |
test_that("Wald_test words with multiple comparisons adjustment and a single test type (Duncan data).", { |
| 182 |
|
| 183 |
Duncan_fit <- lm(prestige ~ 0 + type + income + type:income + type:education, data=Duncan) |
| 184 |
|
| 185 |
Wald5 <- Wald_test( |
| 186 |
Duncan_fit, |
| 187 |
constraints = constrain_pairwise(":education", reg_ex = TRUE), |
| 188 |
vcov = "CR0", |
| 189 |
cluster = Duncan$cluster, |
| 190 |
test = "HTZ" |
| 191 |
) |
| 192 |
|
| 193 |
Wald6 <- Wald_test( |
| 194 |
Duncan_fit, |
| 195 |
constraints = constrain_pairwise(":education", reg_ex = TRUE), |
| 196 |
vcov = "CR0", |
| 197 |
cluster = Duncan$cluster, |
| 198 |
test = "HTZ", |
| 199 |
adjustment_method = "holm" |
| 200 |
) |
| 201 |
|
| 202 |
Wald5_p_values <- sapply(Wald5, function(x) x$p_val) # extract p-values of Wald5 |
| 203 |
Wald6_p_values <- sapply(Wald6, function(x) x$p_val) # extract p-values of Wald6 |
| 204 |
|
| 205 |
expect_false(all(Wald5_p_values == Wald6_p_values)) |
| 206 |
|
| 207 |
# get adjusted_p_values for Wald5, formatted the same as extracted p-values from Wald6 |
| 208 |
Wald5_adjusted_p <- p.adjust(Wald5_p_values, method = "BY") |
| 209 |
expect_equal(Wald5_adjusted_p, Wald6_p_values) |
| 210 |
|
| 211 |
# Now using tidy = TRUE |
| 212 |
Wald8 <- Wald_test( |
| 213 |
Duncan_fit, |
| 214 |
constraints = constrain_pairwise(":education", reg_ex = TRUE), |
| 215 |
vcov = "CR0", |
| 216 |
cluster = Duncan$cluster, |
| 217 |
test = "EDF", |
| 218 |
adjustment_method = "none", |
| 219 |
tidy = TRUE |
| 220 |
) |
| 221 |
|
| 222 |
Wald9 <- Wald_test( |
| 223 |
Duncan_fit, |
| 224 |
constraints = constrain_pairwise(":education", reg_ex = TRUE), |
| 225 |
vcov = "CR0", |
| 226 |
cluster = Duncan$cluster, |
| 227 |
test = "EDF", |
| 228 |
adjustment_method = "fdr", |
| 229 |
tidy = TRUE |
| 230 |
) |
| 231 |
|
| 232 |
Wald8_p_adjusted <- tapply(Wald8$p_val, Wald8$test, p.adjust, method = "fdr", simplify = FALSE) |
| 233 |
Wald8_adjusted <- Wald8 |
| 234 |
Wald8_adjusted$p_val <- unsplit(Wald8_p_adjusted, Wald8$test) |
| 235 |
|
| 236 |
expect_equal(Wald8_adjusted, Wald9) |
| 237 |
|
| 238 |
}) |
| 239 |
|
| 240 |
test_that("Wald_test words with multiple comparisons adjustment and multiple test types (Duncan data).", { |
| 241 |
|
| 242 |
Duncan_fit <- lm(prestige ~ 0 + type + income + type:income + type:education, data=Duncan) |
| 243 |
|
| 244 |
Wald5 <- Wald_test( |
| 245 |
Duncan_fit, |
| 246 |
constraints = constrain_pairwise(":education", reg_ex = TRUE), |
| 247 |
vcov = "CR0", |
| 248 |
cluster = Duncan$cluster, |
| 249 |
test = c("HTZ","chi-sq") |
| 250 |
) |
| 251 |
|
| 252 |
Wald6 <- Wald_test( |
| 253 |
Duncan_fit, |
| 254 |
constraints = constrain_pairwise(":education", reg_ex = TRUE), |
| 255 |
vcov = "CR0", |
| 256 |
cluster = Duncan$cluster, |
| 257 |
test = c("HTZ","chi-sq"), |
| 258 |
adjustment_method = "BY" |
| 259 |
) |
| 260 |
|
| 261 |
Wald5_p_values <- sapply(Wald5, function(x) x$p_val) # extract p-values of Wald5 |
| 262 |
Wald6_p_values <- sapply(Wald6, function(x) x$p_val) # extract p-values of Wald6 |
| 263 |
|
| 264 |
expect_false(all(Wald5_p_values == Wald6_p_values)) |
| 265 |
|
| 266 |
# get adjusted_p_values for Wald5, formatted the same as extracted p-values from Wald6 |
| 267 |
Wald5_adjusted_p <- apply(Wald5_p_values, 1, p.adjust, method = "BY", simplify = FALSE) |
| 268 |
Wald5_adjusted_p <- do.call(rbind, Wald5_adjusted_p) |
| 269 |
expect_equal(Wald5_adjusted_p, Wald6_p_values) |
| 270 |
|
| 271 |
# Now using tidy = TRUE |
| 272 |
|
| 273 |
Wald8 <- Wald_test( |
| 274 |
Duncan_fit, |
| 275 |
constraints = constrain_pairwise(":education", reg_ex = TRUE), |
| 276 |
vcov = "CR0", |
| 277 |
cluster = Duncan$cluster, |
| 278 |
test = c("HTA","EDF","EDT"), |
| 279 |
tidy = TRUE |
| 280 |
) |
| 281 |
|
| 282 |
Wald9 <- Wald_test( |
| 283 |
Duncan_fit, |
| 284 |
constraints = constrain_pairwise(":education", reg_ex = TRUE), |
| 285 |
vcov = "CR0", |
| 286 |
cluster = Duncan$cluster, |
| 287 |
test = c("HTA","EDF","EDT"), |
| 288 |
adjustment_method = "hommel", |
| 289 |
tidy = TRUE |
| 290 |
) |
| 291 |
|
| 292 |
Wald8_p_adjusted <- tapply(Wald8$p_val, Wald8$test, p.adjust, method = "hommel", simplify = FALSE) |
| 293 |
Wald8_adjusted <- Wald8 |
| 294 |
Wald8_adjusted$p_val <- unsplit(Wald8_p_adjusted, Wald8$test) |
| 295 |
|
| 296 |
expect_equal(Wald8_adjusted, Wald9) |
| 297 |
|
| 298 |
}) |
| 299 |
|
| 300 |
|