| 1 |
context("estimatr::iv_robust objects") |
| 2 |
|
| 3 |
skip_if_not_installed("estimatr") |
| 4 |
|
| 5 |
suppressMessages(library(estimatr, quietly = TRUE)) |
| 6 |
|
| 7 |
set.seed(20190513) |
| 8 |
|
| 9 |
# ------------------------------------------------- |
| 10 |
# Set up test data with IV structure |
| 11 |
# ------------------------------------------------- |
| 12 |
|
| 13 |
N <- 200L |
| 14 |
J <- 20L |
| 15 |
cluster <- rep(1:J, each = N / J) |
| 16 |
|
| 17 |
# Instruments |
| 18 |
z1 <- rnorm(N) |
| 19 |
z2 <- rnorm(N) |
| 20 |
|
| 21 |
# Exogenous regressor |
| 22 |
x_exog <- rnorm(N) |
| 23 |
|
| 24 |
# Endogenous regressor (correlated with instruments) |
| 25 |
x_endog <- 0.5 * z1 + 0.3 * z2 + rnorm(N) |
| 26 |
|
| 27 |
# Outcome |
| 28 |
y <- 1 + 2 * x_endog + 0.5 * x_exog + rnorm(N) |
| 29 |
|
| 30 |
# Weights |
| 31 |
w <- 1 + rpois(N, lambda = 3) |
| 32 |
|
| 33 |
dat <- data.frame( |
| 34 |
y = y, x_endog = x_endog, x_exog = x_exog, |
| 35 |
z1 = z1, z2 = z2, w = w, cluster = cluster |
| 36 |
) |
| 37 |
|
| 38 |
CR_types <- paste0("CR", 0:4) |
| 39 |
|
| 40 |
# ------------------------------------------------- |
| 41 |
# Fit models |
| 42 |
# ------------------------------------------------- |
| 43 |
|
| 44 |
# Unweighted, no clusters in call |
| 45 |
iv_un <- iv_robust( |
| 46 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 47 |
data = dat |
| 48 |
) |
| 49 |
|
| 50 |
# Weighted, no clusters in call |
| 51 |
iv_wt <- iv_robust( |
| 52 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 53 |
data = dat, weights = w |
| 54 |
) |
| 55 |
|
| 56 |
# Clustered with CR2 |
| 57 |
iv_cl <- iv_robust( |
| 58 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 59 |
data = dat, clusters = cluster, se_type = "CR2" |
| 60 |
) |
| 61 |
|
| 62 |
# Clustered with CR0 |
| 63 |
iv_cl_cr0 <- iv_robust( |
| 64 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 65 |
data = dat, clusters = cluster, se_type = "CR0" |
| 66 |
) |
| 67 |
|
| 68 |
# Clustered with stata |
| 69 |
iv_cl_stata <- iv_robust( |
| 70 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 71 |
data = dat, clusters = cluster, se_type = "stata" |
| 72 |
) |
| 73 |
|
| 74 |
# Weighted, no clusters in call |
| 75 |
iv_cl_wt <- iv_robust( |
| 76 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 77 |
data = dat, |
| 78 |
weights = w, |
| 79 |
clusters = cluster |
| 80 |
) |
| 81 |
|
| 82 |
|
| 83 |
# Build manual matrices for verification |
| 84 |
mf <- model.frame(iv_un$terms, data = dat) |
| 85 |
X <- model.matrix(iv_un$terms_regressors, data = mf) |
| 86 |
inst_formula <- as.formula(paste("~", deparse(iv_un$formula[[3]][[3]]))) |
| 87 |
Z <- model.matrix(inst_formula, data = mf) |
| 88 |
y_vec <- mf$y |
| 89 |
|
| 90 |
|
| 91 |
# ------------------------------------------------- |
| 92 |
# Tests: model.frame |
| 93 |
# ------------------------------------------------- |
| 94 |
|
| 95 |
test_that("model.frame() works for iv_robust.", { |
| 96 |
|
| 97 |
mf_un <- model.frame(iv_un) |
| 98 |
expect_equal(ncol(mf_un), 5L) # y, x_endog, x_exog, z1, z2 |
| 99 |
expect_true(all(c("y", "x_endog", "x_exog", "z1", "z2") %in% names(mf_un))) |
| 100 |
expect_equal(nrow(mf_un), N) |
| 101 |
|
| 102 |
# Clustered model has (clusters) column |
| 103 |
mf_cl <- model.frame(iv_cl) |
| 104 |
expect_true("(clusters)" %in% names(mf_cl)) |
| 105 |
expect_equal(mf_cl[["(clusters)"]], dat$cluster) |
| 106 |
|
| 107 |
# Weighted model has (weights) column |
| 108 |
mf_wt <- model.frame(iv_wt) |
| 109 |
expect_true("(weights)" %in% names(mf_wt)) |
| 110 |
|
| 111 |
# Clustered and weighted model has (clusters) and (weights) columns |
| 112 |
mf_cl_wt <- model.frame(iv_cl_wt) |
| 113 |
expect_true("(weights)" %in% names(mf_cl_wt)) |
| 114 |
expect_true("(clusters)" %in% names(mf_cl_wt)) |
| 115 |
expect_equal(mf_cl[["(clusters)"]], dat$cluster) |
| 116 |
|
| 117 |
}) |
| 118 |
|
| 119 |
|
| 120 |
# ------------------------------------------------- |
| 121 |
# Tests: model_matrix (projected X) |
| 122 |
# ------------------------------------------------- |
| 123 |
|
| 124 |
test_that("model_matrix() returns projected matrix with correct dimensions.", { |
| 125 |
|
| 126 |
mm <- model_matrix(iv_un) |
| 127 |
expect_identical(dim(mm), c(N,ncol(X))) |
| 128 |
expect_equal(colnames(mm), colnames(X)) |
| 129 |
}) |
| 130 |
|
| 131 |
test_that("model_matrix() agrees with manual projection.", { |
| 132 |
|
| 133 |
# Unweighted model |
| 134 |
XZ <- model_matrix(iv_un) |
| 135 |
ZtZ_inv <- chol2inv(chol(crossprod(Z))) |
| 136 |
XZ_check <- Z %*% ZtZ_inv %*% crossprod(Z, X) |
| 137 |
|
| 138 |
expect_equal(XZ, XZ_check, check.attributes = FALSE) |
| 139 |
|
| 140 |
# Weighted model |
| 141 |
XZ_wt <- model_matrix(iv_wt) |
| 142 |
ZwZ_inv <- chol2inv(chol(crossprod(Z, w * Z))) |
| 143 |
XZ_check <- Z %*% ZwZ_inv %*% crossprod(Z, w * X) |
| 144 |
|
| 145 |
expect_equal(XZ_wt, XZ_check, check.attributes = FALSE) |
| 146 |
|
| 147 |
}) |
| 148 |
|
| 149 |
# ------------------------------------------------- |
| 150 |
# Tests: residuals |
| 151 |
# ------------------------------------------------- |
| 152 |
|
| 153 |
test_that("residuals_CS() matches y - X * beta.", { |
| 154 |
|
| 155 |
r_un <- residuals_CS(iv_un) |
| 156 |
r_check <- as.vector(y_vec - X %*% coef_CS(iv_un)) |
| 157 |
expect_equal(as.vector(r_un), r_check) |
| 158 |
|
| 159 |
r_wt <- residuals_CS(iv_wt) |
| 160 |
r_check_wt <- as.vector(y_vec - X %*% coef_CS(iv_wt)) |
| 161 |
expect_equal(as.vector(r_wt), r_check_wt) |
| 162 |
}) |
| 163 |
|
| 164 |
|
| 165 |
# ------------------------------------------------- |
| 166 |
# Tests: bread |
| 167 |
# ------------------------------------------------- |
| 168 |
|
| 169 |
test_that("bread() agrees with manual computation.", { |
| 170 |
|
| 171 |
# Unweighted model |
| 172 |
XZ <- model_matrix(iv_un) |
| 173 |
bread_check <- chol2inv(chol(crossprod(XZ))) * nobs(iv_un) |
| 174 |
expect_equal(bread(iv_un), bread_check, check.attributes = FALSE) |
| 175 |
expect_true(check_bread(iv_un, cluster = dat$cluster, y = dat$y)) |
| 176 |
|
| 177 |
# Weighted model |
| 178 |
XZ <- model_matrix(iv_wt) |
| 179 |
bread_check <- chol2inv(chol(crossprod(XZ, w * XZ))) * nobs(iv_wt) |
| 180 |
expect_equal(bread(iv_wt), bread_check, check.attributes = FALSE) |
| 181 |
expect_true(check_bread(iv_wt, cluster = dat$cluster, y = dat$y)) |
| 182 |
}) |
| 183 |
|
| 184 |
# ------------------------------------------------- |
| 185 |
# Tests: vcovCR runs with explicit args |
| 186 |
# ------------------------------------------------- |
| 187 |
|
| 188 |
test_that("vcovCR works with explicit cluster and type.", { |
| 189 |
|
| 190 |
for (cr in CR_types) { |
| 191 |
V <- vcovCR(iv_un, cluster = dat$cluster, type = cr) |
| 192 |
expect_s3_class(V, "clubSandwich") |
| 193 |
expect_identical(dim(V), rep(length(coef(iv_un)), 2L)) |
| 194 |
} |
| 195 |
}) |
| 196 |
|
| 197 |
test_that("vcovCR works for weighted model.", { |
| 198 |
|
| 199 |
for (cr in CR_types) { |
| 200 |
V <- vcovCR(iv_wt, cluster = dat$cluster, type = cr) |
| 201 |
expect_s3_class(V, "clubSandwich") |
| 202 |
} |
| 203 |
}) |
| 204 |
|
| 205 |
|
| 206 |
# ------------------------------------------------- |
| 207 |
# Tests: cluster auto-detection |
| 208 |
# ------------------------------------------------- |
| 209 |
|
| 210 |
test_that("vcovCR auto-detects cluster from iv_robust object.", { |
| 211 |
|
| 212 |
V_auto <- vcovCR(iv_cl) |
| 213 |
V_explicit <- vcovCR(iv_cl, cluster = dat$cluster, type = "CR2") |
| 214 |
expect_equal(V_auto, V_explicit, check.attributes = FALSE) |
| 215 |
}) |
| 216 |
|
| 217 |
test_that("vcovCR errors when cluster is missing on unclustered model.", { |
| 218 |
|
| 219 |
expect_error( |
| 220 |
vcovCR(iv_un, type = "CR2"), |
| 221 |
"You must specify a clustering variable" |
| 222 |
) |
| 223 |
}) |
| 224 |
|
| 225 |
|
| 226 |
# ------------------------------------------------- |
| 227 |
# Tests: SE type inference |
| 228 |
# ------------------------------------------------- |
| 229 |
|
| 230 |
test_that("vcovCR inherits se_type from iv_robust object.", { |
| 231 |
|
| 232 |
# CR2 |
| 233 |
V_auto_cr2 <- vcovCR(iv_cl) |
| 234 |
V_explicit_cr2 <- vcovCR(iv_cl, cluster = dat$cluster, type = "CR2") |
| 235 |
expect_equal(V_auto_cr2, V_explicit_cr2, check.attributes = FALSE) |
| 236 |
|
| 237 |
V_auto_wt_cr2 <- vcovCR(iv_cl_wt) |
| 238 |
V_explicit_wt_cr2 <- vcovCR(iv_cl_wt, cluster = dat$cluster, type = "CR2") |
| 239 |
expect_equal(V_auto_wt_cr2, V_explicit_wt_cr2, check.attributes = FALSE) |
| 240 |
|
| 241 |
# CR0 |
| 242 |
V_auto_cr0 <- vcovCR(iv_cl_cr0) |
| 243 |
V_explicit_cr0 <- vcovCR(iv_cl_cr0, cluster = dat$cluster, type = "CR0") |
| 244 |
expect_equal(V_auto_cr0, V_explicit_cr0, check.attributes = FALSE) |
| 245 |
|
| 246 |
# stata -> CR1S |
| 247 |
V_auto_stata <- vcovCR(iv_cl_stata) |
| 248 |
V_explicit_stata <- vcovCR(iv_cl_stata, cluster = dat$cluster, type = "CR1S") |
| 249 |
expect_equal(V_auto_stata, V_explicit_stata, check.attributes = FALSE) |
| 250 |
}) |
| 251 |
|
| 252 |
test_that("vcovCR errors when se_type cannot be mapped.", { |
| 253 |
|
| 254 |
expect_error( |
| 255 |
vcovCR(iv_un, cluster = dat$cluster), |
| 256 |
"You must specify a `type`" |
| 257 |
) |
| 258 |
}) |
| 259 |
|
| 260 |
|
| 261 |
# ------------------------------------------------- |
| 262 |
# Tests: inverse_var rejection |
| 263 |
# ------------------------------------------------- |
| 264 |
|
| 265 |
test_that("vcovCR rejects inverse_var = TRUE.", { |
| 266 |
|
| 267 |
expect_error( |
| 268 |
vcovCR(iv_un, cluster = dat$cluster, type = "CR2", inverse_var = TRUE), |
| 269 |
"inverse_var option is not available" |
| 270 |
) |
| 271 |
}) |
| 272 |
|
| 273 |
|
| 274 |
# ------------------------------------------------- |
| 275 |
# Tests: na.action |
| 276 |
# ------------------------------------------------- |
| 277 |
|
| 278 |
test_that("na.action() works correctly.", { |
| 279 |
|
| 280 |
dat_na <- dat |
| 281 |
dat_na$y[c(5, 15, 25)] <- NA |
| 282 |
iv_na <- iv_robust( |
| 283 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 284 |
data = dat_na, clusters = cluster, se_type = "CR2" |
| 285 |
) |
| 286 |
|
| 287 |
na_act <- na.action(iv_na) |
| 288 |
expect_s3_class(na_act, "omit") |
| 289 |
expect_equal(as.integer(na_act), c(5L, 15L, 25L)) |
| 290 |
expect_equal(nobs(iv_na), N - 3L) |
| 291 |
}) |
| 292 |
|
| 293 |
|
| 294 |
# ------------------------------------------------- |
| 295 |
# Tests: vcovCR options |
| 296 |
# ------------------------------------------------- |
| 297 |
|
| 298 |
test_that("vcovCR options don't matter for CR0.", { |
| 299 |
|
| 300 |
CR0 <- vcovCR(iv_un, cluster = dat$cluster, type = "CR0") |
| 301 |
expect_output(print(CR0)) |
| 302 |
|
| 303 |
attr(CR0, "target") <- NULL |
| 304 |
attr(CR0, "inverse_var") <- NULL |
| 305 |
|
| 306 |
CR0_A <- vcovCR(iv_un, cluster = dat$cluster, type = "CR0", target = 1 / dat$w) |
| 307 |
attr(CR0_A, "target") <- NULL |
| 308 |
attr(CR0_A, "inverse_var") <- NULL |
| 309 |
expect_identical(CR0_A, CR0) |
| 310 |
|
| 311 |
CR0_B <- vcovCR(iv_un, cluster = dat$cluster, type = "CR0", |
| 312 |
target = 1 / dat$w, inverse_var = FALSE) |
| 313 |
attr(CR0_B, "target") <- NULL |
| 314 |
attr(CR0_B, "inverse_var") <- NULL |
| 315 |
expect_identical(CR0_B, CR0) |
| 316 |
|
| 317 |
expect_error( |
| 318 |
vcovCR(iv_un, cluster = dat$cluster, type = "CR0", |
| 319 |
target = 1 / dat$w, inverse_var = TRUE) |
| 320 |
) |
| 321 |
}) |
| 322 |
|
| 323 |
test_that("vcovCR options work for CR2.", { |
| 324 |
|
| 325 |
CR2 <- vcovCR(iv_un, cluster = dat$cluster, type = "CR2") |
| 326 |
expect_equal( |
| 327 |
vcovCR(iv_un, cluster = dat$cluster, type = "CR2", |
| 328 |
target = rep(1, nobs(iv_un))), |
| 329 |
CR2 |
| 330 |
) |
| 331 |
expect_false(identical( |
| 332 |
vcovCR(iv_un, cluster = dat$cluster, type = "CR2", |
| 333 |
target = 1 / dat$w), |
| 334 |
CR2 |
| 335 |
)) |
| 336 |
}) |
| 337 |
|
| 338 |
test_that("vcovCR options work for CR4.", { |
| 339 |
|
| 340 |
CR4 <- vcovCR(iv_un, cluster = dat$cluster, type = "CR4") |
| 341 |
expect_identical( |
| 342 |
vcovCR(iv_un, cluster = dat$cluster, type = "CR4", |
| 343 |
target = rep(1, nobs(iv_un))), |
| 344 |
CR4 |
| 345 |
) |
| 346 |
expect_false(identical( |
| 347 |
vcovCR(iv_un, cluster = dat$cluster, type = "CR4", |
| 348 |
target = 1 / dat$w), |
| 349 |
CR4 |
| 350 |
)) |
| 351 |
}) |
| 352 |
|
| 353 |
|
| 354 |
# ------------------------------------------------- |
| 355 |
# Tests: CR2 target-unbiasedness |
| 356 |
# ------------------------------------------------- |
| 357 |
|
| 358 |
test_that("CR2 is target-unbiased.", { |
| 359 |
|
| 360 |
expect_true(check_CR(iv_un, vcov = "CR2", cluster = dat$cluster)) |
| 361 |
expect_true(check_CR(iv_wt, vcov = "CR2", cluster = dat$cluster)) |
| 362 |
}) |
| 363 |
|
| 364 |
test_that("CR4 is target-unbiased.", { |
| 365 |
skip("Need to understand target-unbiasedness for iv_robust objects.") |
| 366 |
expect_true(check_CR(iv_un, vcov = "CR4", cluster = dat$cluster)) |
| 367 |
expect_true(check_CR(iv_wt, vcov = "CR4", cluster = dat$cluster)) |
| 368 |
}) |
| 369 |
|
| 370 |
|
| 371 |
# ------------------------------------------------- |
| 372 |
# Tests: equivalence to vcovHC with size-1 clusters |
| 373 |
# ------------------------------------------------- |
| 374 |
|
| 375 |
test_that("vcovCR is equivalent to vcovHC (with HC0-HC3) when clusters are all of size 1.", { |
| 376 |
suppressMessages(library(sandwich, quietly = TRUE)) |
| 377 |
iv_fit <- iv_robust( |
| 378 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 379 |
data = dat, se_type = "HC0" |
| 380 |
) |
| 381 |
CR0 <- vcovCR(iv_fit, cluster = 1:nobs(iv_fit), type = "CR0") |
| 382 |
expect_equal(as.matrix(CR0), vcov(iv_fit), check.attributes = FALSE) |
| 383 |
|
| 384 |
CR1 <- vcovCR(iv_fit, cluster = 1:nobs(iv_fit), type = "CR1p") |
| 385 |
expect_equal(as.matrix(CR1), vcov(update(iv_fit, se_type = "HC1")), check.attributes = FALSE) |
| 386 |
|
| 387 |
CR2 <- vcovCR(iv_fit, cluster = 1:nobs(iv_fit), type = "CR2") |
| 388 |
expect_equal(as.matrix(CR2), vcov(update(iv_fit, se_type = "HC2")), check.attributes = FALSE) |
| 389 |
|
| 390 |
CR3 <- vcovCR(iv_fit, cluster = 1:nobs(iv_fit), type = "CR3") |
| 391 |
expect_equal(as.matrix(CR3), vcov(update(iv_fit, se_type = "HC3")), check.attributes = FALSE) |
| 392 |
|
| 393 |
}) |
| 394 |
|
| 395 |
|
| 396 |
# ------------------------------------------------- |
| 397 |
# Tests: sort-order invariance |
| 398 |
# ------------------------------------------------- |
| 399 |
|
| 400 |
test_that("Order doesn't matter.", { |
| 401 |
|
| 402 |
check_sort_order(iv_un, dat, "cluster") |
| 403 |
check_sort_order(iv_wt, dat, "cluster") |
| 404 |
check_sort_order(iv_cl, dat, "cluster") |
| 405 |
check_sort_order(iv_cl_wt, dat, "cluster") |
| 406 |
|
| 407 |
}) |
| 408 |
|
| 409 |
|
| 410 |
# ------------------------------------------------- |
| 411 |
# Tests: dropped observations |
| 412 |
# ------------------------------------------------- |
| 413 |
|
| 414 |
test_that("clubSandwich works with dropped observations", { |
| 415 |
dat_miss <- dat |
| 416 |
dat_miss$x_exog[sample.int(N, size = round(N / 10))] <- NA |
| 417 |
iv_dropped <- update(iv_un, data = dat_miss) |
| 418 |
dat_complete <- subset(dat_miss, !is.na(x_exog)) |
| 419 |
iv_complete <- update(iv_un, data = dat_complete) |
| 420 |
|
| 421 |
CR_drop <- lapply(CR_types, function(x) vcovCR(iv_dropped, cluster = dat_miss$cluster, type = x)) |
| 422 |
CR_complete <- lapply(CR_types, function(x) vcovCR(iv_complete, cluster = dat_complete$cluster, type = x)) |
| 423 |
expect_equal(CR_drop, CR_complete) |
| 424 |
|
| 425 |
test_drop <- lapply(CR_types, function(x) coef_test(iv_dropped, vcov = x, cluster = dat_miss$cluster, test = "All", p_values = FALSE)) |
| 426 |
test_complete <- lapply(CR_types, function(x) coef_test(iv_complete, vcov = x, cluster = dat_complete$cluster, test = "All", p_values = FALSE)) |
| 427 |
expect_equal(test_drop, test_complete) |
| 428 |
}) |
| 429 |
|
| 430 |
|
| 431 |
# ------------------------------------------------- |
| 432 |
# Tests: weight scale invariance |
| 433 |
# ------------------------------------------------- |
| 434 |
|
| 435 |
test_that("weight scale doesn't matter", { |
| 436 |
|
| 437 |
iv_fit_w <- update(iv_un, weights = rep(4, nobs(iv_un))) |
| 438 |
|
| 439 |
unweighted_fit <- lapply(CR_types, function(x) vcovCR(iv_un, cluster = dat$cluster, type = x)) |
| 440 |
weighted_fit <- lapply(CR_types, function(x) vcovCR(iv_fit_w, cluster = dat$cluster, type = x)) |
| 441 |
|
| 442 |
expect_equal(lapply(unweighted_fit, as.matrix), |
| 443 |
lapply(weighted_fit, as.matrix), |
| 444 |
tol = 5 * 10^-7) |
| 445 |
|
| 446 |
target <- 1 + rpois(N, lambda = 8) |
| 447 |
unweighted_fit <- lapply(CR_types, function(x) vcovCR(iv_un, cluster = dat$cluster, type = x, target = target)) |
| 448 |
weighted_fit <- lapply(CR_types, function(x) vcovCR(iv_fit_w, cluster = dat$cluster, type = x, target = target * 15)) |
| 449 |
|
| 450 |
expect_equal(lapply(unweighted_fit, as.matrix), |
| 451 |
lapply(weighted_fit, as.matrix), |
| 452 |
tol = 5 * 10^-7) |
| 453 |
|
| 454 |
}) |
| 455 |
|
| 456 |
|
| 457 |
# ------------------------------------------------- |
| 458 |
# Tests: weights of zero |
| 459 |
# ------------------------------------------------- |
| 460 |
|
| 461 |
test_that("clubSandwich works with weights of zero.", { |
| 462 |
|
| 463 |
dat$awt <- rpois(N, lambda = 1.4) |
| 464 |
|
| 465 |
iv_full <- update(iv_un, weights = awt) |
| 466 |
dat_sub <- subset(dat, awt > 0) |
| 467 |
iv_sub <- update(iv_full, data = dat_sub) |
| 468 |
|
| 469 |
CR_full <- lapply(CR_types, function(x) vcovCR(iv_full, cluster = dat$cluster, type = x)) |
| 470 |
CR_sub <- lapply(CR_types, function(x) vcovCR(iv_sub, cluster = dat_sub$cluster, type = x)) |
| 471 |
expect_equal(CR_full, CR_sub, check.attributes = FALSE) |
| 472 |
|
| 473 |
test_full <- lapply(CR_types, function(x) coef_test(iv_full, vcov = x, cluster = dat$cluster, test = c("z", "naive-t", "Satterthwaite"), p_values = TRUE)) |
| 474 |
test_sub <- lapply(CR_types, function(x) coef_test(iv_sub, vcov = x, cluster = dat_sub$cluster, test = c("z", "naive-t", "Satterthwaite"), p_values = TRUE)) |
| 475 |
expect_equal(test_full, test_sub, check.attributes = FALSE) |
| 476 |
|
| 477 |
}) |
| 478 |
|
| 479 |
|
| 480 |
# ------------------------------------------------- |
| 481 |
# Tests: fixed_effects |
| 482 |
# ------------------------------------------------- |
| 483 |
|
| 484 |
iv_fe <- iv_robust( |
| 485 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 486 |
data = dat, clusters = cluster, |
| 487 |
fixed_effects = ~ cluster, se_type = "CR2" |
| 488 |
) |
| 489 |
|
| 490 |
test_that("model.frame() includes fixed-effects variables", { |
| 491 |
mf_fe <- model.frame(iv_fe) |
| 492 |
expect_true("cluster" %in% names(mf_fe)) |
| 493 |
}) |
| 494 |
|
| 495 |
test_that("augmented_model_matrix() returns the FE design matrix", { |
| 496 |
amm <- augmented_model_matrix(iv_fe, cluster = dat$cluster, |
| 497 |
inverse_var = FALSE, ignore_FE = FALSE) |
| 498 |
expect_equal(nrow(amm), N) |
| 499 |
expect_equal(ncol(amm), J) |
| 500 |
expect_equal(colnames(amm), paste0("cluster", levels(as.factor(dat$cluster)))) |
| 501 |
|
| 502 |
# For a non-FE model, augmented_model_matrix() returns NULL |
| 503 |
expect_null(augmented_model_matrix(iv_un, cluster = dat$cluster, |
| 504 |
inverse_var = FALSE, ignore_FE = FALSE)) |
| 505 |
}) |
| 506 |
|
| 507 |
test_that("model_matrix() returns FE-residualized projected matrix", { |
| 508 |
XP <- model_matrix(iv_fe) |
| 509 |
expect_equal(nrow(XP), N) |
| 510 |
expect_equal(ncol(XP), length(coef(iv_fe))) |
| 511 |
|
| 512 |
# Coefficients should be recoverable from X_proj and FE-residualized outcome |
| 513 |
F_mat <- model.matrix(~ 0 + cluster, data = dat) |
| 514 |
y_demean <- lm.fit(F_mat, dat$y)$residuals |
| 515 |
expect_equal(lm.fit(XP, y_demean)$coefficients, coef(iv_fe), |
| 516 |
check.attributes = FALSE) |
| 517 |
|
| 518 |
# Projected columns should be centered within each cluster |
| 519 |
within_means <- apply(XP, 2, function(x) tapply(x, dat$cluster, mean)) |
| 520 |
expect_lt(max(abs(within_means)), 1e-12) |
| 521 |
}) |
| 522 |
|
| 523 |
test_that("model_matrix() FE path agrees without fixest", { |
| 524 |
local_mocked_bindings( |
| 525 |
requireNamespace = function(...) FALSE |
| 526 |
) |
| 527 |
expect_false(requireNamespace("fixest", quietly = TRUE)) |
| 528 |
|
| 529 |
XP <- model_matrix(iv_fe) |
| 530 |
F_mat <- model.matrix(~ 0 + cluster, data = dat) |
| 531 |
y_demean <- lm.fit(F_mat, dat$y)$residuals |
| 532 |
expect_equal(lm.fit(XP, y_demean)$coefficients, coef(iv_fe), |
| 533 |
check.attributes = FALSE) |
| 534 |
|
| 535 |
expect_message( |
| 536 |
vcovCR(iv_fe, type = "CR0"), |
| 537 |
"For improved performance in models with fixed effects, install the \\{fixest\\} package." |
| 538 |
) |
| 539 |
}) |
| 540 |
|
| 541 |
test_that("vcovCR works with fixed effects", { |
| 542 |
|
| 543 |
for (cr in CR_types) { |
| 544 |
V <- vcovCR(iv_fe, cluster = dat$cluster, type = cr) |
| 545 |
expect_s3_class(V, "clubSandwich") |
| 546 |
expect_equal(nrow(V), length(coef(iv_fe))) |
| 547 |
expect_equal(ncol(V), length(coef(iv_fe))) |
| 548 |
} |
| 549 |
}) |
| 550 |
|
| 551 |
test_that("bread() works with fixed effects", { |
| 552 |
expect_true(check_bread(iv_fe, cluster = dat$cluster, y = dat$y)) |
| 553 |
}) |
| 554 |
|
| 555 |
test_that("CR2 is target-unbiased with fixed effects", { |
| 556 |
expect_true(check_CR(iv_fe, vcov = "CR2")) |
| 557 |
}) |
| 558 |
|
| 559 |
test_that("vcovCR matches iv_robust$vcov for FE model with se_type='CR0'", { |
| 560 |
iv_fe_cr0 <- iv_robust( |
| 561 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 562 |
data = dat, clusters = cluster, |
| 563 |
fixed_effects = ~ cluster, se_type = "CR0" |
| 564 |
) |
| 565 |
V_cr0 <- vcovCR(iv_fe_cr0, type = "CR0") |
| 566 |
expect_equal(as.matrix(V_cr0), vcov(iv_fe_cr0), check.attributes = FALSE) |
| 567 |
}) |
| 568 |
|
| 569 |
test_that("model_matrix() weighted FE path agrees without fixest", { |
| 570 |
iv_fe_wt <- iv_robust( |
| 571 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 572 |
data = dat, weights = w, clusters = cluster, |
| 573 |
fixed_effects = ~ cluster, se_type = "CR0" |
| 574 |
) |
| 575 |
|
| 576 |
local_mocked_bindings( |
| 577 |
requireNamespace = function(...) FALSE |
| 578 |
) |
| 579 |
expect_false(requireNamespace("fixest", quietly = TRUE)) |
| 580 |
|
| 581 |
XP <- model_matrix(iv_fe_wt) |
| 582 |
F_mat <- model.matrix(~ 0 + cluster, data = dat) |
| 583 |
y_demean <- stats::lm.wfit(F_mat, dat$y, w)$residuals |
| 584 |
expect_equal(stats::lm.wfit(XP, y_demean, w)$coefficients, |
| 585 |
coef(iv_fe_wt), check.attributes = FALSE) |
| 586 |
|
| 587 |
# Projected columns should be weighted-centered within each cluster |
| 588 |
within_means <- apply(XP, 2, function(x) |
| 589 |
tapply(x * w, dat$cluster, sum) / tapply(w, dat$cluster, sum) |
| 590 |
) |
| 591 |
expect_lt(max(abs(within_means)), 1e-12) |
| 592 |
}) |
| 593 |
|
| 594 |
test_that("model.frame() handles misaligned NAs between regressor frame and FE frame", { |
| 595 |
dat_mis <- dat |
| 596 |
dat_mis$fe_var <- factor(rep(1:10, each = N / 10)) |
| 597 |
# NAs in regressor on rows {5, 15, 25}; NAs in FE on rows {7, 17, 27}. |
| 598 |
# The two model frames drop different rows, exercising the reconciliation else-branch. |
| 599 |
dat_mis$x_exog[c(5, 15, 25)] <- NA |
| 600 |
dat_mis$fe_var[c(7, 17, 27)] <- NA |
| 601 |
|
| 602 |
iv_mis <- suppressWarnings(iv_robust( |
| 603 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 604 |
data = dat_mis, clusters = cluster, |
| 605 |
fixed_effects = ~ fe_var, se_type = "CR0" |
| 606 |
)) |
| 607 |
|
| 608 |
mf <- model.frame(iv_mis) |
| 609 |
|
| 610 |
expect_equal(nrow(mf), N - 6L) |
| 611 |
expect_true(all(c("x_endog", "x_exog", "fe_var") %in% names(mf))) |
| 612 |
|
| 613 |
na_act <- attr(mf, "na.action") |
| 614 |
expect_s3_class(na_act, "omit") |
| 615 |
expect_equal(length(na_act), 6L) |
| 616 |
dropped <- as.integer(as.character(na_act)) |
| 617 |
expect_setequal(dropped, c(5L, 7L, 15L, 17L, 25L, 27L)) |
| 618 |
|
| 619 |
expect_s3_class(vcovCR(iv_mis, type = "CR0"), "clubSandwich") |
| 620 |
}) |
| 621 |
|
| 622 |
|
| 623 |
# ------------------------------------------------- |
| 624 |
# Tests: missing cluster |
| 625 |
# ------------------------------------------------- |
| 626 |
|
| 627 |
test_that("clubSandwich requires no missing values on the clustering variable", { |
| 628 |
|
| 629 |
dat_miss <- dat |
| 630 |
miss_indicator <- sample.int(N, size = round(N / 10)) |
| 631 |
dat_miss$cluster[miss_indicator] <- NA |
| 632 |
|
| 633 |
iv_dropped <- iv_robust(y ~ x_endog + x_exog | x_exog + z1 + z2, data = dat_miss) |
| 634 |
expect_error(vcovCR(iv_dropped, cluster = dat_miss$cluster, type = "CR0"), |
| 635 |
"Clustering variable cannot have missing values.") |
| 636 |
expect_error(coef_test(iv_dropped, vcov = "CR0", cluster = dat_miss$cluster, test = "All"), |
| 637 |
"Clustering variable cannot have missing values.") |
| 638 |
}) |
| 639 |
|
| 640 |
|
| 641 |
# ------------------------------------------------- |
| 642 |
# Higher-level inference tests: compare against ivreg::ivreg |
| 643 |
# ------------------------------------------------- |
| 644 |
|
| 645 |
skip_if_not_installed("ivreg") |
| 646 |
|
| 647 |
iv_reg <- ivreg::ivreg( |
| 648 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 649 |
data = dat |
| 650 |
) |
| 651 |
iv_est <- iv_robust( |
| 652 |
y ~ x_endog + x_exog | x_exog + z1 + z2, |
| 653 |
data = dat |
| 654 |
) |
| 655 |
|
| 656 |
test_that("Wald_test() agrees between iv_robust and ivreg", { |
| 657 |
|
| 658 |
W_reg <- Wald_test(iv_reg, constraints = constrain_zero("x_endog"), |
| 659 |
vcov = "CR2", cluster = dat$cluster) |
| 660 |
W_est <- Wald_test(iv_est, constraints = constrain_zero("x_endog"), |
| 661 |
vcov = "CR2", cluster = dat$cluster) |
| 662 |
expect_equal(W_reg, W_est) |
| 663 |
|
| 664 |
}) |
| 665 |
|
| 666 |
|
| 667 |
test_that("conf_int() agrees between iv_robust and ivreg", { |
| 668 |
|
| 669 |
ci_reg <- conf_int(iv_reg, vcov = "CR2", cluster = dat$cluster) |
| 670 |
ci_est <- conf_int(iv_est, vcov = "CR2", cluster = dat$cluster) |
| 671 |
expect_equal(ci_reg, ci_est) |
| 672 |
|
| 673 |
}) |
| 674 |
|
| 675 |
|
| 676 |
test_that("coef_test() agrees between iv_robust and ivreg", { |
| 677 |
|
| 678 |
ct_reg <- coef_test(iv_reg, vcov = "CR2", cluster = dat$cluster) |
| 679 |
ct_est <- coef_test(iv_est, vcov = "CR2", cluster = dat$cluster) |
| 680 |
expect_equal(ct_reg, ct_est) |
| 681 |
|
| 682 |
}) |