| 1 |
context("lm objects") |
| 2 |
set.seed(20190513) |
| 3 |
|
| 4 |
m <- 8 |
| 5 |
cluster <- factor(rep(LETTERS[1:m], 3 + rpois(m, 5))) |
| 6 |
n <- length(cluster) |
| 7 |
const <- rep("whuzzup.", n) |
| 8 |
X <- matrix(rnorm(3 * n), n, 3) |
| 9 |
nu <- rnorm(m)[cluster] |
| 10 |
e <- rnorm(n) |
| 11 |
w <- rgamma(n, shape = 3, scale = 3) |
| 12 |
y <- X %*% c(.4, .3, -.3) + nu + e |
| 13 |
|
| 14 |
dat <- data.frame(y, X, cluster, const, w, row = 1:n) |
| 15 |
|
| 16 |
lm_fit <- lm(y ~ X1 + X2 + X3, data = dat) |
| 17 |
WLS_fit <- lm(y ~ X1 + X2 + X3, data = dat, weights = w) |
| 18 |
CR_types <- paste0("CR",0:4) |
| 19 |
|
| 20 |
# obj <- WLS_fit |
| 21 |
# y <- dat$y |
| 22 |
# type <- "CR2" |
| 23 |
# vcov <- vcovCR(obj, cluster = cluster, type = type) |
| 24 |
# target = NULL |
| 25 |
# inverse_var = FALSE |
| 26 |
|
| 27 |
test_that("bread works", { |
| 28 |
|
| 29 |
expect_true(check_bread(lm_fit, cluster = dat$cluster, y = dat$y)) |
| 30 |
lm_vcov <- bread(lm_fit) * summary(lm_fit)$sigma^2 / v_scale(lm_fit) |
| 31 |
expect_equal(vcov(lm_fit), lm_vcov) |
| 32 |
|
| 33 |
expect_true(check_bread(WLS_fit, cluster = dat$cluster, y = dat$y)) |
| 34 |
wls_vcov <- bread(WLS_fit) * summary(WLS_fit)$sigma^2 / v_scale(WLS_fit) |
| 35 |
expect_equal(vcov(WLS_fit), wls_vcov) |
| 36 |
}) |
| 37 |
|
| 38 |
test_that("vcovCR options don't matter for CR0", { |
| 39 |
expect_error(vcovCR(lm_fit, type = "CR0")) |
| 40 |
CR0 <- vcovCR(lm_fit, cluster = dat$cluster, type = "CR0") |
| 41 |
expect_output(print(CR0)) |
| 42 |
attr(CR0, "target") <- NULL |
| 43 |
attr(CR0, "inverse_var") <- NULL |
| 44 |
CR0_A <- vcovCR(lm_fit, cluster = dat$cluster, type = "CR0", target = 1 / dat$w) |
| 45 |
attr(CR0_A, "target") <- NULL |
| 46 |
attr(CR0_A, "inverse_var") <- NULL |
| 47 |
expect_identical(CR0_A, CR0) |
| 48 |
CR0_B <- vcovCR(lm_fit, cluster = dat$cluster, type = "CR0", target = 1 / dat$w, inverse_var = FALSE) |
| 49 |
attr(CR0_B, "target") <- NULL |
| 50 |
attr(CR0_B, "inverse_var") <- NULL |
| 51 |
expect_identical(CR0_A, CR0) |
| 52 |
CR0_C <- vcovCR(lm_fit, cluster = dat$cluster, type = "CR0", target = 1 / dat$w, inverse_var = TRUE) |
| 53 |
attr(CR0_C, "target") <- NULL |
| 54 |
attr(CR0_C, "inverse_var") <- NULL |
| 55 |
expect_identical(CR0_C, CR0) |
| 56 |
|
| 57 |
wCR0 <- vcovCR(WLS_fit, cluster = dat$cluster, type = "CR0") |
| 58 |
attr(wCR0, "target") <- NULL |
| 59 |
attr(wCR0, "inverse_var") <- NULL |
| 60 |
wCR0_A <- vcovCR(WLS_fit, cluster = dat$cluster, type = "CR0", target = 1 / dat$w) |
| 61 |
attr(wCR0_A, "target") <- NULL |
| 62 |
attr(wCR0_A, "inverse_var") <- NULL |
| 63 |
expect_identical(wCR0_A, wCR0) |
| 64 |
wCR0_B <- vcovCR(WLS_fit, cluster = dat$cluster, type = "CR0", target = 1 / dat$w, inverse_var = FALSE) |
| 65 |
attr(wCR0_B, "target") <- NULL |
| 66 |
attr(wCR0_B, "inverse_var") <- NULL |
| 67 |
expect_identical(wCR0_B, wCR0) |
| 68 |
wCR0_C <- vcovCR(WLS_fit, cluster = dat$cluster, type = "CR0", target = 1 / dat$w, inverse_var = TRUE) |
| 69 |
attr(wCR0_C, "target") <- NULL |
| 70 |
attr(wCR0_C, "inverse_var") <- NULL |
| 71 |
expect_identical(wCR0_C, wCR0) |
| 72 |
}) |
| 73 |
|
| 74 |
test_that("vcovCR options work for CR2", { |
| 75 |
CR2_iv <- vcovCR(lm_fit, cluster = dat$cluster, type = "CR2") |
| 76 |
expect_equal(vcovCR(lm_fit, cluster = dat$cluster, type = "CR2", inverse_var = TRUE), CR2_iv) |
| 77 |
expect_equal(vcovCR(lm_fit, cluster = dat$cluster, type = "CR2", target = rep(1, n), inverse_var = TRUE), CR2_iv) |
| 78 |
|
| 79 |
attr(CR2_iv, "inverse_var") <- FALSE |
| 80 |
CR2_not <- vcovCR(lm_fit, cluster = dat$cluster, type = "CR2", inverse_var = FALSE) |
| 81 |
expect_equal(CR2_not, CR2_iv) |
| 82 |
expect_equal(vcovCR(lm_fit, cluster = dat$cluster, type = "CR2", target = rep(1, n)), CR2_not) |
| 83 |
expect_equal(vcovCR(lm_fit, cluster = dat$cluster, type = "CR2", target = rep(1, n), inverse_var = FALSE), CR2_not) |
| 84 |
expect_false(identical(vcovCR(lm_fit, cluster = dat$cluster, type = "CR2", target = 1 / dat$w), CR2_not)) |
| 85 |
|
| 86 |
wCR2_id <- vcovCR(WLS_fit, cluster = dat$cluster, type = "CR2") |
| 87 |
expect_equal(vcovCR(WLS_fit, cluster = dat$cluster, type = "CR2", inverse_var = FALSE), wCR2_id) |
| 88 |
expect_equal(vcovCR(WLS_fit, cluster = dat$cluster, type = "CR2", target = rep(1, n)), wCR2_id) |
| 89 |
expect_equal(vcovCR(WLS_fit, cluster = dat$cluster, type = "CR2", target = rep(1, n), inverse_var = FALSE), wCR2_id) |
| 90 |
|
| 91 |
wCR2_iv <- vcovCR(WLS_fit, cluster = dat$cluster, type = "CR2", inverse_var = TRUE) |
| 92 |
wCR2_target <- vcovCR(WLS_fit, cluster = dat$cluster, type = "CR2", target = 1 / dat$w, inverse_var = TRUE) |
| 93 |
expect_false(identical(wCR2_target, wCR2_id)) |
| 94 |
expect_equal(matrix(wCR2_target, dim(wCR2_target)), matrix(wCR2_iv, dim(wCR2_iv))) |
| 95 |
expect_equal(vcovCR(WLS_fit, cluster = dat$cluster, type = "CR2", target = 1 / dat$w, inverse_var = TRUE), wCR2_target) |
| 96 |
}) |
| 97 |
|
| 98 |
test_that("vcovCR options work for CR4", { |
| 99 |
CR4_iv <- vcovCR(lm_fit, cluster = dat$cluster, type = "CR4") |
| 100 |
expect_equal(vcovCR(lm_fit, cluster = dat$cluster, type = "CR4", inverse_var = TRUE), CR4_iv) |
| 101 |
expect_equal(vcovCR(lm_fit, cluster = dat$cluster, type = "CR4", target = rep(1, n), inverse_var = TRUE), CR4_iv) |
| 102 |
|
| 103 |
attr(CR4_iv, "inverse_var") <- FALSE |
| 104 |
CR4_not <- vcovCR(lm_fit, cluster = dat$cluster, type = "CR4", inverse_var = FALSE) |
| 105 |
expect_equal(CR4_not, CR4_iv) |
| 106 |
expect_equal(vcovCR(lm_fit, cluster = dat$cluster, type = "CR4", target = rep(1, n)), CR4_not) |
| 107 |
expect_equal(vcovCR(lm_fit, cluster = dat$cluster, type = "CR4", target = rep(1, n), inverse_var = FALSE), CR4_not) |
| 108 |
expect_false(identical(vcovCR(lm_fit, cluster = dat$cluster, type = "CR4", target = 1 / dat$w), CR4_not)) |
| 109 |
|
| 110 |
wCR4_id <- vcovCR(WLS_fit, cluster = dat$cluster, type = "CR4") |
| 111 |
expect_equal(vcovCR(WLS_fit, cluster = dat$cluster, type = "CR4", inverse_var = FALSE), wCR4_id) |
| 112 |
expect_equal(vcovCR(WLS_fit, cluster = dat$cluster, type = "CR4", target = rep(1, n)), wCR4_id) |
| 113 |
expect_equal(vcovCR(WLS_fit, cluster = dat$cluster, type = "CR4", target = rep(1, n), inverse_var = FALSE), wCR4_id) |
| 114 |
|
| 115 |
wCR4_iv <- vcovCR(WLS_fit, cluster = dat$cluster, type = "CR4", inverse_var = TRUE) |
| 116 |
wCR4_target <- vcovCR(WLS_fit, cluster = dat$cluster, type = "CR4", target = 1 / dat$w, inverse_var = TRUE) |
| 117 |
expect_false(identical(wCR4_target, wCR4_id)) |
| 118 |
expect_equal(matrix(wCR4_target, dim(wCR4_target)), matrix(wCR4_iv, dim(wCR4_iv))) |
| 119 |
expect_equal(vcovCR(WLS_fit, cluster = dat$cluster, type = "CR4", target = 1 / dat$w, inverse_var = TRUE), wCR4_target) |
| 120 |
|
| 121 |
}) |
| 122 |
|
| 123 |
|
| 124 |
test_that("CR2 and CR4 are target-unbiased", { |
| 125 |
expect_true(check_CR(lm_fit, vcov = "CR2", cluster = dat$cluster)) |
| 126 |
expect_true(check_CR(WLS_fit, vcov = "CR2", cluster = dat$cluster)) |
| 127 |
expect_true(check_CR(lm_fit, vcov = "CR4", cluster = dat$cluster)) |
| 128 |
expect_true(check_CR(WLS_fit, vcov = "CR4", cluster = dat$cluster)) |
| 129 |
}) |
| 130 |
|
| 131 |
test_that("vcovCR is equivalent to vcovHC when clusters are all of size 1", { |
| 132 |
library(sandwich, quietly=TRUE) |
| 133 |
CR0 <- vcovCR(lm_fit, cluster = dat$row, type = "CR0") |
| 134 |
expect_equal(vcovHC(lm_fit, type = "HC0"), as.matrix(CR0)) |
| 135 |
CR1 <- vcovCR(lm_fit, cluster = dat$row, type = "CR1S") |
| 136 |
expect_equal(vcovHC(lm_fit, type = "HC1"), as.matrix(CR1)) |
| 137 |
CR2 <- vcovCR(lm_fit, cluster = dat$row, type = "CR2") |
| 138 |
expect_equal(vcovHC(lm_fit, type = "HC2"), as.matrix(CR2)) |
| 139 |
CR3 <- vcovCR(lm_fit, cluster = dat$row, type = "CR3") |
| 140 |
expect_equal(vcovHC(lm_fit, type = "HC3"), as.matrix(CR3)) |
| 141 |
}) |
| 142 |
|
| 143 |
test_that("CR2 is equivalent to Welch t-test for DiD design", { |
| 144 |
m0 <- 4 |
| 145 |
m1 <- 9 |
| 146 |
m <- m0 + m1 |
| 147 |
cluster <- factor(rep(LETTERS[1:m], each = 2)) |
| 148 |
n <- length(cluster) |
| 149 |
time <- rep(c(1,2), m) |
| 150 |
trt_clusters <- c(rep(0,m0), rep(1,m1)) |
| 151 |
trt <- (time - 1) * rep(trt_clusters, each = 2) |
| 152 |
nu <- rnorm(m)[cluster] |
| 153 |
e <- rnorm(n) |
| 154 |
y <- 0.4 * trt + nu + e |
| 155 |
|
| 156 |
dat <- data.frame(y, time, trt, cluster) |
| 157 |
lm_DID <- lm(y ~ cluster + factor(time) + trt, data = dat) |
| 158 |
t_Satt <- coef_test(lm_DID, vcov = "CR2", cluster = dat$cluster)["trt",] |
| 159 |
|
| 160 |
y_diff <- apply(matrix(y, nrow = 2), 2, diff) |
| 161 |
t_Welch <- t.test(y_diff ~ trt_clusters) |
| 162 |
|
| 163 |
expect_equal(with(t_Welch, estimate[[2]] - estimate[[1]]), t_Satt$beta) |
| 164 |
expect_equal(as.numeric(-t_Welch$statistic), with(t_Satt, beta / SE)) |
| 165 |
expect_is(all.equal(as.numeric(t_Welch$parameter), t_Satt$df), "character") |
| 166 |
|
| 167 |
df <- m^2 * (m0 - 1) * (m1 - 1) / (m0^2 * (m0 - 1) + m1^2 * (m1 - 1)) |
| 168 |
expect_equal(t_Satt$df, df) |
| 169 |
}) |
| 170 |
|
| 171 |
test_that("Order doesn't matter.",{ |
| 172 |
|
| 173 |
check_sort_order(WLS_fit, dat, "cluster") |
| 174 |
|
| 175 |
}) |
| 176 |
|
| 177 |
test_that("clubSandwich works with dropped observations", { |
| 178 |
dat_miss <- dat |
| 179 |
miss_indicator <- sample.int(n, size = round(n / 10)) |
| 180 |
dat_miss$X1[miss_indicator] <- NA |
| 181 |
dat_miss$cluster[miss_indicator] <- NA |
| 182 |
|
| 183 |
lm_dropped <- lm(y ~ X1 + X2 + X3, data = dat_miss) |
| 184 |
dat_complete <- subset(dat_miss, !is.na(X1)) |
| 185 |
lm_complete <- lm(y ~ X1 + X2 + X3, data = dat_complete) |
| 186 |
|
| 187 |
CR_drop <- lapply(CR_types, function(x) vcovCR(lm_dropped, cluster = dat_miss$cluster, type = x)) |
| 188 |
CR_complete <- lapply(CR_types, function(x) vcovCR(lm_complete, cluster = dat_complete$cluster, type = x)) |
| 189 |
expect_equal(CR_drop, CR_complete) |
| 190 |
|
| 191 |
test_drop <- lapply(CR_types, function(x) coef_test(lm_dropped, vcov = x, cluster = dat_miss$cluster, test = "All", p_values = FALSE)) |
| 192 |
test_complete <- lapply(CR_types, function(x) coef_test(lm_complete, vcov = x, cluster = dat_complete$cluster, test = "All", p_values = FALSE)) |
| 193 |
expect_equal(test_drop, test_complete) |
| 194 |
}) |
| 195 |
|
| 196 |
|
| 197 |
test_that("clubSandwich requires no missing values on the clustering variable", { |
| 198 |
dat_miss <- dat |
| 199 |
miss_indicator <- sample.int(n, size = round(n / 10)) |
| 200 |
dat_miss$cluster[miss_indicator] <- NA |
| 201 |
|
| 202 |
lm_dropped <- lm(y ~ X1 + X2 + X3, data = dat_miss) |
| 203 |
|
| 204 |
expect_error(vcovCR(lm_dropped, cluster = dat_miss$cluster, type = "CR0"), |
| 205 |
"Clustering variable cannot have missing values.") |
| 206 |
expect_error(coef_test(lm_dropped, vcov = "CR0", cluster = dat_miss$cluster, test = "All"), |
| 207 |
"Clustering variable cannot have missing values.") |
| 208 |
}) |
| 209 |
|
| 210 |
|
| 211 |
test_that("clubSandwich works with aliased predictors", { |
| 212 |
|
| 213 |
data(npk, package = "datasets") |
| 214 |
npk_alias <- lm(yield ~ block + N*P*K, npk) |
| 215 |
npk_drop <- lm(yield ~ block + N + P + K + N:P + N:K + P:K, npk) |
| 216 |
|
| 217 |
CR_alias <- lapply(CR_types[-4], function(x) vcovCR(npk_alias, cluster = npk$block, type = x)) |
| 218 |
CR_drop <- lapply(CR_types[-4], function(x) vcovCR(npk_drop, cluster = npk$block, type = x)) |
| 219 |
expect_equal(CR_alias, CR_drop) |
| 220 |
|
| 221 |
test_drop <- lapply(CR_types[-4], function(x) coef_test(npk_alias, vcov = x, cluster = npk$block, test = c("z","naive-t","Satterthwaite"), coefs = 7:12, p_values = FALSE)) |
| 222 |
test_complete <- lapply(CR_types[-4], function(x) coef_test(npk_drop, vcov = x, cluster = npk$block, test = c("z","naive-t","Satterthwaite"), coefs = 7:12, p_values = FALSE)) |
| 223 |
expect_equal(test_drop, test_complete) |
| 224 |
}) |
| 225 |
|
| 226 |
|
| 227 |
test_that("weight scale doesn't matter", { |
| 228 |
|
| 229 |
lm_fit_w <- lm(y ~ X1 + X2 + X3, data = dat, weights = rep(4, nrow(dat))) |
| 230 |
|
| 231 |
unweighted_fit <- lapply(CR_types, function(x) vcovCR(lm_fit, cluster = cluster, type = x)) |
| 232 |
weighted_fit <- lapply(CR_types, function(x) vcovCR(lm_fit_w, cluster = cluster, type = x)) |
| 233 |
|
| 234 |
expect_equal(lapply(unweighted_fit, as.matrix), |
| 235 |
lapply(weighted_fit, as.matrix), |
| 236 |
tol = 5 * 10^-7) |
| 237 |
|
| 238 |
target <- 1 + rpois(nrow(dat), lambda = 8) |
| 239 |
unweighted_fit <- lapply(CR_types, function(x) vcovCR(lm_fit, cluster = cluster, type = x, target = target)) |
| 240 |
weighted_fit <- lapply(CR_types, function(x) vcovCR(lm_fit_w, cluster = cluster, type = x, target = target * 15)) |
| 241 |
|
| 242 |
expect_equal(lapply(unweighted_fit, as.matrix), |
| 243 |
lapply(weighted_fit, as.matrix), |
| 244 |
tol = 5 * 10^-7) |
| 245 |
|
| 246 |
}) |
| 247 |
|
| 248 |
test_that("clubSandwich works with weights of zero.", { |
| 249 |
|
| 250 |
data("LifeCycleSavings", package = "datasets") |
| 251 |
|
| 252 |
n_life <- nrow(LifeCycleSavings) |
| 253 |
LifeCycleSavings$cl <- substr(rownames(LifeCycleSavings), 1, 1) |
| 254 |
table(LifeCycleSavings$cl) |
| 255 |
LifeCycleSavings$wt <- rpois(n_life, lambda = 0.8) |
| 256 |
table(LifeCycleSavings$wt) |
| 257 |
|
| 258 |
lm_full <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings, weights = wt) |
| 259 |
LCS_sub <- subset(LifeCycleSavings, wt > 0) |
| 260 |
lm_sub <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LCS_sub, weights = wt) |
| 261 |
|
| 262 |
CR_full <- lapply(CR_types, function(x) vcovCR(lm_full, cluster = LifeCycleSavings$cl, type = x)) |
| 263 |
CR_sub <- lapply(CR_types, function(x) vcovCR(lm_sub, cluster = LCS_sub$cl, type = x)) |
| 264 |
expect_equal(CR_full, CR_sub, check.attributes = FALSE) |
| 265 |
|
| 266 |
test_full <- lapply(CR_types, function(x) coef_test(lm_full, vcov = x, cluster = LifeCycleSavings$cl, test = c("z","naive-t","Satterthwaite"), p_values = TRUE)) |
| 267 |
test_sub <- lapply(CR_types, function(x) coef_test(lm_sub, vcov = x, cluster = LCS_sub$cl, test = c("z","naive-t","Satterthwaite"), p_values = TRUE)) |
| 268 |
expect_equal(test_full, test_sub, check.attributes = FALSE) |
| 269 |
|
| 270 |
dat_miss <- LifeCycleSavings |
| 271 |
miss_indicator <- sample.int(n_life, size = round(n_life / 5)) |
| 272 |
dat_miss$pop15[miss_indicator] <- NA |
| 273 |
dat_miss$cl[miss_indicator] <- NA |
| 274 |
with(dat_miss, table(wt, is.na(pop15))) |
| 275 |
|
| 276 |
lm_dropped <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = dat_miss, weights = wt) |
| 277 |
dat_complete <- subset(dat_miss, !is.na(pop15)) |
| 278 |
lm_complete <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = dat_complete, weights = wt) |
| 279 |
|
| 280 |
CR_drop <- lapply(CR_types, function(x) vcovCR(lm_dropped, cluster = dat_miss$cl, type = x)) |
| 281 |
CR_complete <- lapply(CR_types, function(x) vcovCR(lm_complete, cluster = dat_complete$cl, type = x)) |
| 282 |
expect_equal(CR_drop, CR_complete) |
| 283 |
|
| 284 |
test_drop <- lapply(CR_types, function(x) coef_test(lm_dropped, vcov = x, cluster = dat_miss$cl, test = "All", p_values = FALSE)) |
| 285 |
test_complete <- lapply(CR_types, function(x) coef_test(lm_complete, vcov = x, cluster = dat_complete$cl, test = "All", p_values = FALSE)) |
| 286 |
expect_equal(test_drop, test_complete) |
| 287 |
|
| 288 |
}) |
| 289 |
|
| 290 |
test_that("vcovCR errors when there is only one cluster.", { |
| 291 |
|
| 292 |
single_cluster_error_msg <- "Cluster-robust variance estimation will not work when the data only includes a single cluster." |
| 293 |
|
| 294 |
expect_error( |
| 295 |
vcovCR(lm_fit, cluster = dat$const, type = "CR0"), |
| 296 |
single_cluster_error_msg |
| 297 |
) |
| 298 |
|
| 299 |
expect_error( |
| 300 |
conf_int(WLS_fit, vcov = "CR1", cluster = dat$const), single_cluster_error_msg |
| 301 |
) |
| 302 |
|
| 303 |
expect_error( |
| 304 |
coef_test(lm_fit, vcov = "CR2", cluster = dat$const), single_cluster_error_msg |
| 305 |
) |
| 306 |
|
| 307 |
expect_error( |
| 308 |
Wald_test(WLS_fit, constraints = constrain_zero(2:4), |
| 309 |
vcov = "CR3", cluster = dat$const), |
| 310 |
single_cluster_error_msg |
| 311 |
) |
| 312 |
|
| 313 |
}) |
| 314 |
|
| 315 |
|
| 316 |
test_that("vcovCR works with intercept-only model and user-specified weights.", { |
| 317 |
|
| 318 |
lm_int <- lm(y ~ 1, data = dat) |
| 319 |
HC_un <- coef_test(lm_int, vcov="CR2", cluster=dat$row, test = "All") |
| 320 |
|
| 321 |
# Unweighted, HC-robust |
| 322 |
N <- nobs(lm_int) |
| 323 |
yi <- dat$y |
| 324 |
wi <- rep(1, N) |
| 325 |
W <- sum(wi) |
| 326 |
wi <- wi / W |
| 327 |
vi <- rep(1, N) |
| 328 |
V <- sum(vi) |
| 329 |
ei <- residuals_CS(lm_int) |
| 330 |
M <- sum(wi^2 * vi) |
| 331 |
ai <- 1 / sqrt(1 - 2 * wi + M / vi) |
| 332 |
V_hand <- sum(wi^2 * ai^2 * ei^2) |
| 333 |
pi_theta_pj <- diag(vi) - tcrossprod(rep(1,N), wi * vi) - tcrossprod(wi * vi, rep(1, N)) + M |
| 334 |
df <- M^2 / sum(tcrossprod(ai^2 * wi^2) * (pi_theta_pj^2)) |
| 335 |
|
| 336 |
expect_true(check_bread(lm_int, cluster = dat$row, y = dat$y)) |
| 337 |
expect_true(check_CR(lm_int, vcov = "CR2", cluster = dat$row)) |
| 338 |
expect_equal(sqrt(V_hand), HC_un$SE) |
| 339 |
expect_equal(df, HC_un$df_Satt) |
| 340 |
expect_equal(Inf, HC_un$df_z) |
| 341 |
expect_equal(N - 1, HC_un$df_t) |
| 342 |
|
| 343 |
|
| 344 |
# Unweighted, cluster-robust |
| 345 |
CR_un <- coef_test(lm_int, vcov="CR2", cluster=dat$cluster, test = "All") |
| 346 |
|
| 347 |
J <- nlevels(dat$cluster) |
| 348 |
w_j <- tapply(wi, dat$cluster, sum) |
| 349 |
e_j <- tapply(wi * ei, dat$cluster, sum) / w_j |
| 350 |
v_j <- tapply(wi^2 * vi, dat$cluster, sum) / w_j^2 |
| 351 |
a_j <- 1 / sqrt(1 - 2 * w_j + M / v_j) |
| 352 |
V_hand <- sum(w_j^2 * a_j^2 * e_j^2) |
| 353 |
pi_theta_pj <- diag(v_j) - tcrossprod(rep(1,J), w_j * v_j) - tcrossprod(w_j * v_j, rep(1, J)) + M |
| 354 |
df <- M^2 / sum(tcrossprod(a_j^2 * w_j^2) * (pi_theta_pj^2)) |
| 355 |
|
| 356 |
expect_true(check_bread(lm_int, cluster = dat$cluster, y = dat$y)) |
| 357 |
expect_true(check_CR(lm_int, vcov = "CR2", cluster = dat$cluster)) |
| 358 |
expect_equal(sqrt(V_hand), CR_un$SE) |
| 359 |
expect_equal(df, CR_un$df_Satt) |
| 360 |
expect_equal(Inf, CR_un$df_z) |
| 361 |
expect_equal(J - 1, CR_un$df_t) |
| 362 |
|
| 363 |
|
| 364 |
# Weighted, HC-robust |
| 365 |
WLS_int <- lm(y ~ 1, data = dat, weights = w) |
| 366 |
HC_wt <- coef_test(WLS_int, vcov="CR2", cluster=dat$row, test = "All") |
| 367 |
|
| 368 |
N <- nobs(WLS_int) |
| 369 |
yi <- dat$y |
| 370 |
wi <- WLS_int$weights |
| 371 |
W <- sum(wi) |
| 372 |
wi <- wi / W |
| 373 |
vi <- rep(1, N) |
| 374 |
V <- sum(vi) |
| 375 |
ei <- residuals_CS(WLS_int) |
| 376 |
M <- sum(wi^2 * vi) |
| 377 |
ai <- 1 / sqrt(1 - 2 * wi + M / vi) |
| 378 |
V_hand <- sum(wi^2 * ai^2 * ei^2) |
| 379 |
pi_theta_pj <- diag(vi) - tcrossprod(rep(1,N), wi * vi) - tcrossprod(wi * vi, rep(1, N)) + M |
| 380 |
df <- M^2 / sum(tcrossprod(ai^2 * wi^2) * (pi_theta_pj^2)) |
| 381 |
|
| 382 |
expect_true(check_bread(WLS_int, cluster = dat$row, y = dat$y)) |
| 383 |
expect_true(check_CR(WLS_int, vcov = "CR2", cluster = dat$row)) |
| 384 |
expect_equal(sqrt(V_hand), HC_wt$SE) |
| 385 |
expect_equal(df, HC_wt$df_Satt) |
| 386 |
expect_equal(Inf, HC_wt$df_z) |
| 387 |
expect_equal(N - 1, HC_wt$df_t) |
| 388 |
|
| 389 |
# Weighted, cluster-robust |
| 390 |
CR_wt <- coef_test(WLS_int, vcov="CR2", cluster=dat$cluster, test = "All") |
| 391 |
|
| 392 |
J <- nlevels(dat$cluster) |
| 393 |
w_j <- tapply(wi, dat$cluster, sum) |
| 394 |
e_j <- tapply(wi * ei, dat$cluster, sum) / w_j |
| 395 |
v_j <- tapply(wi^2 * vi, dat$cluster, sum) / w_j^2 |
| 396 |
a_j <- 1 / sqrt(1 - 2 * w_j + M / v_j) |
| 397 |
V_hand <- sum(w_j^2 * a_j^2 * e_j^2) |
| 398 |
pi_theta_pj <- diag(v_j) - tcrossprod(rep(1,J), w_j * v_j) - tcrossprod(w_j * v_j, rep(1, J)) + M |
| 399 |
df <- M^2 / sum(tcrossprod(a_j^2 * w_j^2) * (pi_theta_pj^2)) |
| 400 |
|
| 401 |
expect_true(check_bread(WLS_int, cluster = dat$cluster, y = dat$y)) |
| 402 |
expect_true(check_CR(WLS_int, vcov = "CR2", cluster = dat$cluster)) |
| 403 |
expect_equal(sqrt(V_hand), CR_wt$SE, tolerance = 10^-3) |
| 404 |
expect_equal(df, CR_wt$df_Satt, tolerance = 10^-3) |
| 405 |
expect_equal(Inf, CR_wt$df_z) |
| 406 |
expect_equal(J - 1, CR_wt$df_t) |
| 407 |
|
| 408 |
}) |