| 1 |
context("robu objects") |
| 2 |
set.seed(20190513) |
| 3 |
|
| 4 |
skip_if_not_installed("robumeta") |
| 5 |
|
| 6 |
library(robumeta, quietly=TRUE) |
| 7 |
data(corrdat) |
| 8 |
|
| 9 |
|
| 10 |
test_that("methods work with intercept-only model.", { |
| 11 |
|
| 12 |
obj <- robu(effectsize ~ 1, |
| 13 |
studynum = studyid, |
| 14 |
var.eff.size = var, |
| 15 |
small = FALSE, |
| 16 |
data = corrdat) |
| 17 |
N <- obj$M |
| 18 |
k <- obj$N |
| 19 |
|
| 20 |
expect_equal(as.numeric(vcovCR(obj, type = "CR0")), as.numeric(obj$VR.r)) |
| 21 |
|
| 22 |
expect_identical(as.numeric(clubSandwich:::coef_CS.robu(obj)), as.numeric(obj$b.r)) |
| 23 |
expect_identical(length(clubSandwich:::residuals_CS.robu(obj)), N) |
| 24 |
expect_identical(dim(clubSandwich:::model_matrix.robu(obj)), c(N, 1L)) |
| 25 |
expect_identical(dim(clubSandwich:::bread.robu(obj)), c(1L, 1L)) |
| 26 |
|
| 27 |
V_list <- clubSandwich:::targetVariance.robu(obj, cluster = obj$study_orig_id) |
| 28 |
expect_identical(as.integer(sapply(V_list, nrow)), obj$k) |
| 29 |
|
| 30 |
W_list <- clubSandwich:::weightMatrix.robu(obj, cluster = obj$study_orig_id) |
| 31 |
expect_identical(as.integer(sapply(W_list, nrow)), obj$k) |
| 32 |
|
| 33 |
}) |
| 34 |
|
| 35 |
corr_large <- robu(effectsize ~ males + college + binge, data = corrdat, |
| 36 |
modelweights = "CORR", studynum = studyid, |
| 37 |
var.eff.size = var, small = FALSE) |
| 38 |
|
| 39 |
test_that("CR0 z-tests agree with robumeta for correlated effects", { |
| 40 |
p <- length(coef_CS(corr_large)) |
| 41 |
N <- corr_large$N |
| 42 |
robu_CR0 <- vcovCR(corr_large, type = "CR0") |
| 43 |
ztests <- coef_test(corr_large, vcov = robu_CR0 * N / (N - p), test = "z") |
| 44 |
|
| 45 |
expect_equivalent(corr_large$VR.r, as.matrix(robu_CR0)) |
| 46 |
expect_equivalent(corr_large$reg_table$SE, ztests$SE) |
| 47 |
expect_equal(with(corr_large$reg_table, 2 * pnorm(abs(b.r / SE),lower.tail=FALSE)), |
| 48 |
ztests$p_z) |
| 49 |
}) |
| 50 |
|
| 51 |
corr_small <- robu(effectsize ~ males + college + binge, data = corrdat, |
| 52 |
modelweights = "CORR", studynum = studyid, |
| 53 |
var.eff.size = var) |
| 54 |
|
| 55 |
test_that("CR2 t-tests agree with robumeta for correlated effects", { |
| 56 |
robu_CR2 <- vcovCR(corr_small, type = "CR2") |
| 57 |
expect_true(check_CR(corr_small, vcov = robu_CR2)) |
| 58 |
# expect_true(check_CR(corr_small, vcov = "CR4")) |
| 59 |
|
| 60 |
CR2_ttests <- coef_test(corr_small, vcov = robu_CR2, test = "Satterthwaite") |
| 61 |
expect_equivalent(corr_small$VR.r, as.matrix(robu_CR2)) |
| 62 |
expect_equal(corr_small$dfs, CR2_ttests$df) |
| 63 |
expect_equal(corr_small$reg_table$prob, CR2_ttests$p_Satt) |
| 64 |
}) |
| 65 |
|
| 66 |
data(hierdat) |
| 67 |
|
| 68 |
hier_large <- robu(effectsize ~ binge + followup + sreport + age, |
| 69 |
data = hierdat, studynum = studyid, |
| 70 |
var.eff.size = var, modelweights = "HIER", small = FALSE) |
| 71 |
|
| 72 |
test_that("CR0 z-tests agree with robumeta for hierarchical effects", { |
| 73 |
p <- length(coef_CS(hier_large)) |
| 74 |
N <- hier_large$N |
| 75 |
robu_CR0 <- vcovCR(hier_large, type = "CR0") |
| 76 |
ztests <- coef_test(hier_large, vcov = robu_CR0 * N / (N - p), test = "z") |
| 77 |
|
| 78 |
expect_equivalent(hier_large$VR.r, as.matrix(robu_CR0)) |
| 79 |
expect_equivalent(hier_large$reg_table$SE, ztests$SE) |
| 80 |
expect_equal(with(hier_large$reg_table, 2 * pnorm(abs(b.r / SE),lower.tail=FALSE)), |
| 81 |
ztests$p_z) |
| 82 |
}) |
| 83 |
|
| 84 |
hier_small <- robu(effectsize ~ binge + followup + sreport + age, |
| 85 |
data = hierdat, studynum = studyid, |
| 86 |
var.eff.size = var, modelweights = "HIER") |
| 87 |
|
| 88 |
test_that("CR2 t-tests agree with robumeta for hierarchical effects", { |
| 89 |
robu_CR2 <- vcovCR(hier_small, type = "CR2") |
| 90 |
expect_true(check_CR(hier_small, vcov = robu_CR2)) |
| 91 |
# expect_true(check_CR(hier_small, vcov = "CR4")) |
| 92 |
|
| 93 |
CR2_ttests <- coef_test(hier_small, vcov = robu_CR2, test = "Satterthwaite") |
| 94 |
|
| 95 |
expect_equivalent(hier_small$VR.r, as.matrix(robu_CR2)) |
| 96 |
expect_equal(hier_small$dfs, CR2_ttests$df) |
| 97 |
expect_equal(hier_small$reg_table$prob, CR2_ttests$p_Satt) |
| 98 |
}) |
| 99 |
|
| 100 |
hierdat$user_wt <- 1 + rpois(nrow(hierdat), lambda = 1.2) |
| 101 |
|
| 102 |
user_large <- robu(effectsize ~ binge + followup + sreport + age, |
| 103 |
data = hierdat, studynum = studyid, |
| 104 |
var.eff.size = var, userweights = user_wt, small = FALSE) |
| 105 |
|
| 106 |
test_that("CR0 z-tests agree with robumeta for user weighting", { |
| 107 |
p <- length(coef_CS(user_large)) |
| 108 |
N <- user_large$N |
| 109 |
robu_CR0 <- vcovCR(user_large, type = "CR0") |
| 110 |
ztests <- coef_test(user_large, vcov = robu_CR0 * N / (N - p), test = "z") |
| 111 |
|
| 112 |
expect_equivalent(user_large$VR.r, as.matrix(robu_CR0)) |
| 113 |
expect_equivalent(user_large$reg_table$SE, ztests$SE) |
| 114 |
expect_equal(with(user_large$reg_table, 2 * pnorm(abs(b.r / SE),lower.tail=FALSE)), |
| 115 |
ztests$p_z) |
| 116 |
}) |
| 117 |
|
| 118 |
user_small <- robu(effectsize ~ binge + followup + sreport + age, |
| 119 |
data = hierdat, studynum = studyid, |
| 120 |
var.eff.size = var, userweights = user_wt) |
| 121 |
|
| 122 |
test_that("CR2 t-tests agree with robumeta for user weighting", { |
| 123 |
|
| 124 |
user_lm <- lm(effectsize ~ binge + followup + sreport + age, data = hierdat, |
| 125 |
weights = user_wt) |
| 126 |
expect_equivalent(coef_CS(user_lm), coef(user_lm)) |
| 127 |
|
| 128 |
robu_CR2 <- vcovCR(user_small, type = "CR2") |
| 129 |
expect_true(check_CR(user_small, vcov = robu_CR2)) |
| 130 |
|
| 131 |
expect_equivalent(user_small$VR.r, as.matrix(robu_CR2)) |
| 132 |
|
| 133 |
target <- user_small$data.full$avg.var.eff.size |
| 134 |
|
| 135 |
lm_CR2 <- vcovCR(user_lm, cluster = hierdat$studyid, type = "CR2", target = target) |
| 136 |
expect_equivalent(robu_CR2, lm_CR2) |
| 137 |
|
| 138 |
CR2_ttests <- coef_test(user_small, vcov = robu_CR2, test = "Satterthwaite", p_values = FALSE) |
| 139 |
# expect_equal(user_small$dfs, CR2_ttests$df) |
| 140 |
# expect_equal(user_small$reg_table$prob, CR2_ttests$p_Satt) |
| 141 |
|
| 142 |
lm_CR2_ttests <- coef_test(user_lm, vcov = "CR2", |
| 143 |
cluster = hierdat$studyid, |
| 144 |
target = user_small$data.full$avg.var.eff.size, |
| 145 |
test = "Satterthwaite", p_values = FALSE) |
| 146 |
compare_ttests(CR2_ttests, lm_CR2_ttests) |
| 147 |
}) |
| 148 |
|
| 149 |
test_that("bread works", { |
| 150 |
vcov_corr_large <- with(corr_large, chol2inv(chol(crossprod(Xreg, data.full$r.weights * Xreg)))) |
| 151 |
expect_equal(vcov_corr_large, bread(corr_large) / v_scale(corr_large)) |
| 152 |
|
| 153 |
vcov_corr_small <- with(corr_small, chol2inv(chol(crossprod(Xreg, data.full$r.weights * Xreg)))) |
| 154 |
expect_equal(vcov_corr_small, bread(corr_small) / v_scale(corr_small)) |
| 155 |
|
| 156 |
vcov_hier_large <- with(hier_large, chol2inv(chol(crossprod(Xreg, data.full$r.weights * Xreg)))) |
| 157 |
expect_equal(vcov_hier_large, bread(hier_large) / v_scale(hier_large)) |
| 158 |
|
| 159 |
vcov_hier_small <- with(hier_small, chol2inv(chol(crossprod(Xreg, data.full$r.weights * Xreg)))) |
| 160 |
expect_equal(vcov_hier_small, bread(hier_small) / v_scale(hier_small)) |
| 161 |
|
| 162 |
vcov_user_large <- with(user_large, chol2inv(chol(crossprod(Xreg, data.full$userweights * Xreg)))) |
| 163 |
expect_equal(vcov_user_large, bread(user_large) / v_scale(user_large)) |
| 164 |
|
| 165 |
vcov_user_small <- with(user_small, chol2inv(chol(crossprod(Xreg, data.full$userweights * Xreg)))) |
| 166 |
expect_equal(vcov_user_small, bread(user_small) / v_scale(user_small)) |
| 167 |
}) |
| 168 |
|
| 169 |
|
| 170 |
data(dropoutPrevention) |
| 171 |
|
| 172 |
test_that("dropoutPrevention tests replicate Tipton & Pustejovsky (2015) - full sample", { |
| 173 |
skip_on_cran() |
| 174 |
m3_hier <- robu(LOR1 ~ study_design + attrition + group_equivalence + adjusted |
| 175 |
+ outcome + evaluator_independence |
| 176 |
+ male_pct + white_pct + average_age |
| 177 |
+ implementation_quality + program_site + duration + service_hrs, |
| 178 |
data = dropoutPrevention, studynum = studyID, var.eff.size = varLOR, modelweights = "HIER") |
| 179 |
|
| 180 |
m3_hier_CR2 <- vcovCR(m3_hier, cluster = dropoutPrevention$studyID, type = "CR2") |
| 181 |
expect_true(check_CR(m3_hier, vcov = m3_hier_CR2)) |
| 182 |
# expect_true(check_CR(m3_hier, vcov = "CR4")) |
| 183 |
CR2_ttests <- coef_test(m3_hier, vcov = m3_hier_CR2, test = "Satterthwaite") |
| 184 |
|
| 185 |
expect_equivalent(m3_hier$VR.r, as.matrix(m3_hier_CR2)) |
| 186 |
expect_equal(m3_hier$dfs, CR2_ttests$df) |
| 187 |
expect_equal(m3_hier$reg_table$prob, CR2_ttests$p_Satt) |
| 188 |
|
| 189 |
contrast_list <- list("Study design" = 2:3, |
| 190 |
"Outcome measure" = 7:9, |
| 191 |
"Evaluator independence" = 10:12, |
| 192 |
"Implmentation quality" = 16:17, |
| 193 |
"Program format" = 18:20) |
| 194 |
|
| 195 |
dropout_tests <- Wald_test(m3_hier, constraints = constrain_zero(contrast_list), |
| 196 |
vcov = m3_hier_CR2, test = c("Naive-F","HTZ")) |
| 197 |
|
| 198 |
Fstat_club <- sapply(dropout_tests, function(x) x$Fstat) |
| 199 |
attr(Fstat_club, "dimnames") <- NULL |
| 200 |
Fstat_paper <- matrix(c(0.23, 0.22, 0.91, 0.84, 3.11, 2.78, 14.15, 13.78, 3.85, 3.65), nrow = 2) |
| 201 |
expect_equivalent(Fstat_paper, round(Fstat_club, 2)) |
| 202 |
|
| 203 |
df_club <- sapply(dropout_tests, function(x) x$df_denom[2]) |
| 204 |
df_paper <- c(42.9, 21.5, 16.8, 36.9, 37.5) |
| 205 |
attr(df_club, "names") <- NULL |
| 206 |
expect_equivalent(df_paper, round(df_club, 1)) |
| 207 |
}) |
| 208 |
|
| 209 |
test_that("dropoutPrevention tests replicate Tipton & Pustejovsky (2015) - reduced sample", { |
| 210 |
skip_on_cran() |
| 211 |
dp_subset <- subset(dropoutPrevention, big_study==TRUE) |
| 212 |
m3_hier <- robu(LOR1 ~ study_design + attrition + group_equivalence + adjusted |
| 213 |
+ outcome + evaluator_independence |
| 214 |
+ male_pct + white_pct + average_age |
| 215 |
+ implementation_quality + program_site + duration + service_hrs, |
| 216 |
data = dp_subset, studynum = studyID, var.eff.size = varLOR, modelweights = "HIER") |
| 217 |
|
| 218 |
|
| 219 |
m3_hier_CR2 <- vcovCR(m3_hier, cluster = dp_subset$studyID, type = "CR2") |
| 220 |
expect_true(check_CR(m3_hier, vcov = m3_hier_CR2)) |
| 221 |
|
| 222 |
CR2_ttests <- coef_test(m3_hier, vcov = m3_hier_CR2, test = "Satterthwaite") |
| 223 |
|
| 224 |
expect_equivalent(m3_hier$VR.r, as.matrix(m3_hier_CR2)) |
| 225 |
expect_equal(m3_hier$dfs, CR2_ttests$df) |
| 226 |
expect_equal(m3_hier$reg_table$prob, CR2_ttests$p_Satt) |
| 227 |
|
| 228 |
contrast_list <- list("Study design" = 2:3, |
| 229 |
"Outcome measure" = 7:9, |
| 230 |
"Evaluator independence" = 10:11, |
| 231 |
"Implmentation quality" = 15:16, |
| 232 |
"Program format" = 17:19) |
| 233 |
|
| 234 |
dropout_tests <- Wald_test(m3_hier, constraints = constrain_zero(contrast_list), |
| 235 |
vcov = "CR2", test = c("Naive-F","HTZ")) |
| 236 |
|
| 237 |
Fstat_club <- sapply(dropout_tests, function(x) x$Fstat) |
| 238 |
Fstat_paper <- matrix(c(3.19, 2.93, 1.05, 0.84, 0.32, 0.26, 4.02, 3.69, 1.19, 0.98), nrow = 2) |
| 239 |
attr(Fstat_club, "dimnames") <- NULL |
| 240 |
expect_equivalent(Fstat_paper, round(Fstat_club, 2)) |
| 241 |
|
| 242 |
df_club <- sapply(dropout_tests, function(x) x$df_denom[2]) |
| 243 |
df_paper <- c(11.0, 7.7, 4.6, 11.0, 9.1) |
| 244 |
attr(df_club, "names") <- NULL |
| 245 |
expect_equivalent(df_paper, round(df_club, 1)) |
| 246 |
}) |
| 247 |
|
| 248 |
CR_types <- paste0("CR",0:4) |
| 249 |
|
| 250 |
test_that("order doesn't matter", { |
| 251 |
|
| 252 |
skip_on_cran() |
| 253 |
|
| 254 |
dat_scramble <- corrdat[sample(nrow(corrdat)),] |
| 255 |
corr_scramble <- robu(effectsize ~ males + college + binge, data = dat_scramble, |
| 256 |
modelweights = "CORR", studynum = studyid, |
| 257 |
var.eff.size = var) |
| 258 |
|
| 259 |
CR_fit <- lapply(CR_types, function(x) vcovCR(corr_small, type = x)) |
| 260 |
CR_scramble <- lapply(CR_types, function(x) vcovCR(corr_scramble, type = x)) |
| 261 |
expect_equivalent(CR_fit, CR_scramble) |
| 262 |
|
| 263 |
test_fit <- lapply(CR_types, function(x) coef_test(corr_small, vcov = x, test = "All", p_values = FALSE)) |
| 264 |
test_scramble <- lapply(CR_types, function(x) coef_test(corr_scramble, vcov = x, test = "All", p_values = FALSE)) |
| 265 |
compare_ttests(test_fit, test_scramble) |
| 266 |
|
| 267 |
constraints <- combn(length(coef_CS(corr_small)), 2, simplify = FALSE) |
| 268 |
Wald_fit <- Wald_test(corr_small, constraints = constrain_zero(constraints), vcov = "CR2", test = "All") |
| 269 |
Wald_scramble <- Wald_test(corr_scramble, constraints = constrain_zero(constraints), vcov = "CR2", test = "All") |
| 270 |
compare_Waldtests(Wald_fit, Wald_scramble) |
| 271 |
}) |
| 272 |
|
| 273 |
test_that("clubSandwich works with dropped observations", { |
| 274 |
dat_miss <- hierdat |
| 275 |
dat_miss$binge[sample.int(nrow(hierdat), size = round(nrow(hierdat) / 10))] <- NA |
| 276 |
dat_miss$followup[sample.int(nrow(hierdat), size = round(nrow(hierdat) / 20))] <- NA |
| 277 |
hier_drop <- robu(effectsize ~ binge + followup + sreport + age, |
| 278 |
data = dat_miss, studynum = studyid, |
| 279 |
var.eff.size = var, modelweights = "HIER") |
| 280 |
|
| 281 |
dat_complete <- subset(dat_miss, !is.na(binge) & !is.na(followup)) |
| 282 |
hier_complete <- robu(effectsize ~ binge + followup + sreport + age, |
| 283 |
data = dat_complete, studynum = studyid, |
| 284 |
var.eff.size = var, modelweights = "HIER") |
| 285 |
|
| 286 |
CR_drop <- lapply(CR_types, function(x) vcovCR(hier_drop, type = x)) |
| 287 |
CR_complete <- lapply(CR_types, function(x) vcovCR(hier_complete, type = x)) |
| 288 |
expect_equal(CR_drop, CR_complete) |
| 289 |
|
| 290 |
test_drop <- lapply(CR_types, function(x) coef_test(hier_drop, vcov = x, test = "All", p_values = FALSE)) |
| 291 |
test_complete <- lapply(CR_types, function(x) coef_test(hier_complete, vcov = x, test = "All", p_values = FALSE)) |
| 292 |
expect_equal(test_drop, test_complete) |
| 293 |
}) |
| 294 |
|
| 295 |
test_that("vcovCR options work for CR2", { |
| 296 |
dp_subset <- subset(dropoutPrevention, big_study==TRUE) |
| 297 |
m3_hier <- robu(LOR1 ~ study_design + attrition + group_equivalence + adjusted |
| 298 |
+ outcome + evaluator_independence |
| 299 |
+ male_pct + white_pct + average_age |
| 300 |
+ implementation_quality + program_site + duration + service_hrs, |
| 301 |
data = dp_subset, studynum = studyID, var.eff.size = varLOR, modelweights = "HIER") |
| 302 |
|
| 303 |
iv <- mean(m3_hier$data.full$r.weights) / m3_hier$data.full$r.weights |
| 304 |
CR2_iv <- vcovCR(m3_hier, type = "CR2") |
| 305 |
expect_equal(vcovCR(m3_hier, type = "CR2", inverse_var = TRUE), CR2_iv) |
| 306 |
expect_equal(vcovCR(m3_hier, type = "CR2", target = iv, inverse_var = TRUE), CR2_iv) |
| 307 |
|
| 308 |
CR2_not <- vcovCR(m3_hier, type = "CR2", inverse_var = FALSE) |
| 309 |
attr(CR2_iv, "inverse_var") <- FALSE |
| 310 |
attr(CR2_iv, "target") <- attr(CR2_not, "target") |
| 311 |
expect_equal(CR2_not, CR2_iv) |
| 312 |
|
| 313 |
expect_equal(vcovCR(m3_hier, type = "CR2", target = iv), CR2_not) |
| 314 |
expect_equal(vcovCR(m3_hier, type = "CR2", target = iv, inverse_var = FALSE), CR2_not) |
| 315 |
expect_false(identical(vcovCR(m3_hier, type = "CR2", target = m3_hier$data.full$var.eff.size), CR2_not)) |
| 316 |
}) |
| 317 |
|
| 318 |
|
| 319 |
test_that("Wald test problem.", { |
| 320 |
|
| 321 |
mod0 <- robu(formula = effectsize ~ 0 + factor(binge), |
| 322 |
data = hierdat, |
| 323 |
var.eff.size = var, |
| 324 |
studynum = studyid, |
| 325 |
modelweights = "HIER", small = TRUE) |
| 326 |
Wald0 <- Wald_test(mod0, constraints = constrain_equal(1:2), vcov = "CR2", test = "All") |
| 327 |
|
| 328 |
mod1 <- robu(formula = effectsize ~ binge, |
| 329 |
data = hierdat, |
| 330 |
var.eff.size = var, |
| 331 |
studynum = studyid, |
| 332 |
modelweights = "HIER", small = TRUE) |
| 333 |
Wald1 <- Wald_test(mod1, constraints = constrain_zero(2), vcov = "CR2", test = "All") |
| 334 |
|
| 335 |
compare_Waldtests(Wald0, Wald1) |
| 336 |
}) |
| 337 |
|
| 338 |
|
| 339 |
test_that("CR0 and CR2 agree with robumeta for user weighting with some zeros.", { |
| 340 |
|
| 341 |
hierdat$user_wt <- rpois(nrow(hierdat), lambda = 1.2) |
| 342 |
table(hierdat$user_wt) |
| 343 |
|
| 344 |
user_large <- robu(effectsize ~ binge + followup + sreport + age, |
| 345 |
data = hierdat, studynum = studyid, |
| 346 |
var.eff.size = var, userweights = user_wt, small = FALSE) |
| 347 |
|
| 348 |
p <- length(coef_CS(user_large)) |
| 349 |
N <- user_large$N |
| 350 |
robu_CR0 <- vcovCR(user_large, type = "CR0") |
| 351 |
ztests <- coef_test(user_large, vcov = robu_CR0 * N / (N - p), test = "z") |
| 352 |
|
| 353 |
expect_equivalent(user_large$VR.r, as.matrix(robu_CR0)) |
| 354 |
expect_equivalent(user_large$reg_table$SE, ztests$SE) |
| 355 |
expect_equal(with(user_large$reg_table, 2 * pnorm(abs(b.r / SE),lower.tail=FALSE)), |
| 356 |
ztests$p_z) |
| 357 |
|
| 358 |
user_small <- robu(effectsize ~ binge + followup + sreport + age, |
| 359 |
data = hierdat, studynum = studyid, |
| 360 |
var.eff.size = var, userweights = user_wt) |
| 361 |
|
| 362 |
user_lm <- lm(effectsize ~ binge + followup + sreport + age, data = hierdat, |
| 363 |
weights = user_wt) |
| 364 |
expect_equivalent(coef_CS(user_lm), coef(user_lm)) |
| 365 |
|
| 366 |
robu_CR2 <- vcovCR(user_small, type = "CR2") |
| 367 |
expect_true(check_CR(user_small, vcov = robu_CR2)) |
| 368 |
|
| 369 |
target <- user_small$data.full$avg.var.eff.size |
| 370 |
|
| 371 |
lm_CR2 <- vcovCR(user_lm, cluster = hierdat$studyid, type = "CR2", target = target) |
| 372 |
expect_equivalent(robu_CR2, lm_CR2) |
| 373 |
|
| 374 |
CR2_ttests <- coef_test(user_small, vcov = robu_CR2, test = "Satterthwaite", p_values = FALSE) |
| 375 |
|
| 376 |
lm_CR2_ttests <- coef_test(user_lm, vcov = "CR2", |
| 377 |
cluster = hierdat$studyid, |
| 378 |
target = user_small$data.full$avg.var.eff.size, |
| 379 |
test = "Satterthwaite", p_values = FALSE) |
| 380 |
compare_ttests(CR2_ttests, lm_CR2_ttests) |
| 381 |
|
| 382 |
}) |
| 383 |
|
| 384 |
test_that("clubSandwich works with weights of zero (kind of).", { |
| 385 |
|
| 386 |
hierdat$user_wt <- rpois(nrow(hierdat), lambda = 1.2) |
| 387 |
table(hierdat$user_wt) |
| 388 |
|
| 389 |
user_full <- robu(effectsize ~ binge + followup + sreport + age, |
| 390 |
data = hierdat, studynum = studyid, |
| 391 |
var.eff.size = var, userweights = user_wt, small = FALSE) |
| 392 |
|
| 393 |
hierdat_sub <- subset(hierdat, user_wt > 0) |
| 394 |
user_sub <- robu(effectsize ~ binge + followup + sreport + age, |
| 395 |
data = hierdat_sub, studynum = studyid, |
| 396 |
var.eff.size = var, userweights = user_wt) |
| 397 |
|
| 398 |
CR_full <- lapply(CR_types, function(x) vcovCR(user_full, type = x)) |
| 399 |
CR_sub <- lapply(CR_types, function(x) vcovCR(user_sub, type = x)) |
| 400 |
expect_equal(CR_full[c(1,2,4)], CR_sub[c(1,2,4)], check.attributes = FALSE) |
| 401 |
|
| 402 |
dat_miss <- hierdat |
| 403 |
miss_indicator <- sample.int(nrow(hierdat), size = round(nrow(hierdat) / 10)) |
| 404 |
dat_miss$binge[miss_indicator] <- NA |
| 405 |
with(dat_miss, table(user_wt, is.na(binge))) |
| 406 |
|
| 407 |
user_dropped <- robu(effectsize ~ binge + followup + sreport + age, |
| 408 |
data = dat_miss, studynum = studyid, |
| 409 |
var.eff.size = var, userweights = user_wt) |
| 410 |
dat_complete <- subset(dat_miss, !is.na(binge)) |
| 411 |
user_complete <- robu(effectsize ~ binge + followup + sreport + age, |
| 412 |
data = dat_complete, studynum = studyid, |
| 413 |
var.eff.size = var, userweights = user_wt) |
| 414 |
|
| 415 |
CR_drop <- lapply(CR_types, function(x) vcovCR(user_dropped, type = x)) |
| 416 |
CR_complete <- lapply(CR_types, function(x) vcovCR(user_complete, type = x)) |
| 417 |
expect_equal(CR_drop, CR_complete) |
| 418 |
|
| 419 |
test_drop <- lapply(CR_types, function(x) coef_test(user_dropped, vcov = x, test = "All", p_values = FALSE)) |
| 420 |
test_complete <- lapply(CR_types, function(x) coef_test(user_complete, vcov = x, test = "All", p_values = FALSE)) |
| 421 |
expect_equal(test_drop, test_complete, tolerance = 1e-6) |
| 422 |
|
| 423 |
}) |