| 1 |
context("lm_robust objects") |
| 2 |
|
| 3 |
skip_if_not_installed("estimatr") |
| 4 |
|
| 5 |
suppressMessages(library(estimatr, quietly=TRUE)) |
| 6 |
|
| 7 |
|
| 8 |
set.seed(20250630) |
| 9 |
data("ChickWeight", package = "datasets") |
| 10 |
N <- nrow(ChickWeight) |
| 11 |
ChickWeight$wt <- 1 + rpois(N, 3) |
| 12 |
ChickWeight$Chick_ordered <- ChickWeight$Chick # James' suggestion 4/16 |
| 13 |
ChickWeight$Chick <- factor(ChickWeight$Chick, ordered = FALSE) |
| 14 |
ChickWeight$Chick_int <- as.integer(ChickWeight$Chick) |
| 15 |
ChickWeight$rando <- "Drop" |
| 16 |
ChickWeight$rando[sample(1:N, size = round(0.8 * N))] <- "Keep" |
| 17 |
table(ChickWeight$rando) |
| 18 |
|
| 19 |
lm_fit <- lm(weight ~ 0 + Diet + Time:Diet, data = ChickWeight) |
| 20 |
lm_rob <- lm_robust( |
| 21 |
weight ~ 0 + Diet + Time:Diet, |
| 22 |
data = ChickWeight, |
| 23 |
clusters = Chick |
| 24 |
) |
| 25 |
|
| 26 |
lm_fit_fe <- lm(weight ~ 0 + Time:Diet + Chick, data = ChickWeight) |
| 27 |
lm_rob_fe <- lm_robust( |
| 28 |
weight ~ Time:Diet, |
| 29 |
data = ChickWeight, |
| 30 |
clusters = Chick, |
| 31 |
fixed_effects = ~ Chick |
| 32 |
) |
| 33 |
|
| 34 |
wlm_fit <- lm(weight ~ 0 + Diet + Time:Diet, weights = wt, data = ChickWeight) |
| 35 |
wlm_rob <- lm_robust( |
| 36 |
weight ~ 0 + Diet + Time:Diet, |
| 37 |
weights = wt, |
| 38 |
data = ChickWeight, |
| 39 |
clusters = Chick |
| 40 |
) |
| 41 |
|
| 42 |
# Two-way fixed effects models |
| 43 |
data("MortalityRates") |
| 44 |
MortalityRates <- subset(MortalityRates, cause == "Motor Vehicle") |
| 45 |
MortalityRates$state <- factor(MortalityRates$state) |
| 46 |
MortalityRates$year <- factor(MortalityRates$year) |
| 47 |
|
| 48 |
lm_fit_fe2 <- lm( |
| 49 |
mrate ~ 0 + year + state + legal + beertaxa + beerpercap + winepercap + spiritpercap, |
| 50 |
data = MortalityRates |
| 51 |
) |
| 52 |
lm_rob_fe2 <- lm_robust( |
| 53 |
mrate ~ legal + beertaxa + beerpercap + winepercap + spiritpercap, |
| 54 |
fixed_effects = ~ year + state, |
| 55 |
data = MortalityRates, |
| 56 |
cluster = state |
| 57 |
) |
| 58 |
|
| 59 |
test_that("model.frame() works", { |
| 60 |
|
| 61 |
# unweighted models |
| 62 |
mf_fit <- model.frame(lm_fit) |
| 63 |
mf_rob <- model.frame(lm_rob) |
| 64 |
mf_rob$`(clusters)` <- NULL |
| 65 |
expect_equal(mf_fit, mf_rob) |
| 66 |
|
| 67 |
# fixed effects models |
| 68 |
mf_fit_fe <- model.frame(lm_fit_fe) |
| 69 |
mf_rob_fe <- model.frame(lm_rob_fe) |
| 70 |
mf_rob_fe$`(clusters)` <- NULL |
| 71 |
expect_equivalent(mf_fit_fe, mf_rob_fe) |
| 72 |
|
| 73 |
# two-way fixed effects models |
| 74 |
mf_fit_fe2 <- model.frame(lm_fit_fe2) |
| 75 |
mf_rob_fe2 <- model.frame(lm_rob_fe2) |
| 76 |
mf_rob_fe2$`(clusters)` <- NULL |
| 77 |
expect_equivalent(mf_fit_fe2[names(mf_rob_fe2)], mf_rob_fe2) |
| 78 |
|
| 79 |
# weighted models |
| 80 |
mf_wlm <- model.frame(wlm_fit) |
| 81 |
mf_wrob <- model.frame(wlm_rob) |
| 82 |
mf_wrob$`(clusters)` <- NULL |
| 83 |
|
| 84 |
expect_equal(mf_wlm, mf_wrob) |
| 85 |
|
| 86 |
# set up tests with missing values |
| 87 |
set.seed(20250629) |
| 88 |
|
| 89 |
dat_miss <- ChickWeight |
| 90 |
i <- 1:nrow(ChickWeight) |
| 91 |
miss1 <- 3:8 |
| 92 |
miss2 <- sample(i, 9L) |
| 93 |
dat_miss$Diet_miss <- dat_miss$Diet |
| 94 |
dat_miss$Diet_miss[miss1] <- NA |
| 95 |
dat_miss$wt_miss <- ifelse(i %in% miss1, NA, dat_miss$wt) |
| 96 |
dat_miss$Chick_miss1 <- dat_miss$Chick |
| 97 |
dat_miss$Chick_miss1[miss1] <- NA |
| 98 |
dat_miss$Chick_miss2 <- dat_miss$Chick |
| 99 |
dat_miss$Chick_miss2[miss2] <- NA |
| 100 |
|
| 101 |
dat_complete1 <- droplevels(dat_miss[setdiff(i, miss1),]) |
| 102 |
dat_complete2 <- droplevels(dat_miss[setdiff(i, miss2),]) |
| 103 |
dat_complete <- droplevels(dat_miss[setdiff(i, c(miss1, miss2)),]) |
| 104 |
|
| 105 |
compare_model_frames <- function(data1, data2, ...) { |
| 106 |
cl <- match.call() |
| 107 |
|
| 108 |
cl1 <- cl |
| 109 |
cl1$data2 <- NULL |
| 110 |
names(cl1)[2] <- "data" |
| 111 |
cl1[[1]] <- quote(estimatr::lm_robust) |
| 112 |
m1 <- suppressWarnings(eval(cl1, parent.frame())) |
| 113 |
mf1 <- model.frame(m1) |
| 114 |
|
| 115 |
cl2 <- cl |
| 116 |
cl2$data1 <- NULL |
| 117 |
names(cl2)[2] <- "data" |
| 118 |
cl2[[1]] <- quote(estimatr::lm_robust) |
| 119 |
m2 <- suppressWarnings(eval(cl2, parent.frame())) |
| 120 |
mf2 <- model.frame(m2) |
| 121 |
|
| 122 |
expect_equivalent(mf1, mf2) |
| 123 |
} |
| 124 |
|
| 125 |
# X missing |
| 126 |
compare_model_frames( |
| 127 |
data1 = dat_miss, data2 = dat_complete1, |
| 128 |
formula = weight ~ Time:Diet_miss, |
| 129 |
fixed_effects = ~ Chick + Diet, |
| 130 |
clusters = Chick_int, |
| 131 |
se_type = "CR0" |
| 132 |
) |
| 133 |
|
| 134 |
# clusters missing |
| 135 |
compare_model_frames( |
| 136 |
data1 = dat_miss, data2 = dat_complete1, |
| 137 |
formula = weight ~ Time:Diet, |
| 138 |
fixed_effects = ~ Chick, |
| 139 |
clusters = Chick_miss1, |
| 140 |
se_type = "CR0" |
| 141 |
) |
| 142 |
|
| 143 |
# weights missing |
| 144 |
compare_model_frames( |
| 145 |
data1 = dat_miss, data2 = dat_complete1, |
| 146 |
formula = weight ~ Time:Diet, |
| 147 |
weights = wt_miss, |
| 148 |
fixed_effects = ~ Chick, |
| 149 |
se_type = "HC0" |
| 150 |
) |
| 151 |
|
| 152 |
# FE missing |
| 153 |
compare_model_frames( |
| 154 |
data1 = dat_miss, data2 = dat_complete1, |
| 155 |
formula = weight ~ Time:Diet, |
| 156 |
fixed_effects = ~ Chick + Diet_miss, |
| 157 |
clusters = Chick_int, |
| 158 |
se_type = "CR0" |
| 159 |
) |
| 160 |
compare_model_frames( |
| 161 |
data1 = dat_miss, data2 = dat_complete2, |
| 162 |
formula = weight ~ Time:Diet, |
| 163 |
fixed_effects = ~ Chick_miss2 + Diet, |
| 164 |
se_type = "HC0" |
| 165 |
) |
| 166 |
|
| 167 |
# X and FE missing |
| 168 |
compare_model_frames( |
| 169 |
data1 = dat_miss, data2 = dat_complete1, |
| 170 |
formula = weight ~ Time:Diet_miss, |
| 171 |
fixed_effects = ~ Chick_miss1, |
| 172 |
clusters = Chick_int, |
| 173 |
se_type = "CR0" |
| 174 |
) |
| 175 |
compare_model_frames( |
| 176 |
data1 = dat_miss, data2 = dat_complete, |
| 177 |
formula = weight ~ Time:Diet_miss, |
| 178 |
fixed_effects = ~ Chick_miss2, |
| 179 |
clusters = Chick_int, |
| 180 |
se_type = "CR0" |
| 181 |
) |
| 182 |
|
| 183 |
# clusters and FE missing |
| 184 |
compare_model_frames( |
| 185 |
data1 = dat_miss, data2 = dat_complete1, |
| 186 |
formula = weight ~ Time:Diet, |
| 187 |
fixed_effects = ~ Chick_miss1, |
| 188 |
clusters = Chick_miss1, |
| 189 |
se_type = "CR0" |
| 190 |
) |
| 191 |
compare_model_frames( |
| 192 |
data1 = dat_miss, data2 = dat_complete, |
| 193 |
formula = weight ~ Time:Diet, |
| 194 |
fixed_effects = ~ Chick_miss1, |
| 195 |
clusters = Chick_miss2, |
| 196 |
se_type = "CR0" |
| 197 |
) |
| 198 |
|
| 199 |
# weights and FE missing |
| 200 |
compare_model_frames( |
| 201 |
data1 = dat_miss, data2 = dat_complete1, |
| 202 |
formula = weight ~ Time:Diet, |
| 203 |
weights = wt_miss, |
| 204 |
fixed_effects = ~ Chick_miss1, |
| 205 |
clusters = Chick_int, |
| 206 |
se_type = "CR0" |
| 207 |
) |
| 208 |
compare_model_frames( |
| 209 |
data1 = dat_miss, data2 = dat_complete, |
| 210 |
formula = weight ~ Time:Diet, |
| 211 |
weights = wt_miss, |
| 212 |
fixed_effects = ~ Chick_miss2, |
| 213 |
se_type = "HC0" |
| 214 |
) |
| 215 |
}) |
| 216 |
|
| 217 |
test_that("model_matrix() works", { |
| 218 |
|
| 219 |
# basic models |
| 220 |
|
| 221 |
mm_fit <- model_matrix(lm_fit) |
| 222 |
mm_rob <- model_matrix(lm_rob) |
| 223 |
expect_equivalent(mm_fit, mm_rob) |
| 224 |
|
| 225 |
|
| 226 |
# fixed effects models |
| 227 |
|
| 228 |
mm_fit_fe <- model_matrix(lm_fit_fe) |
| 229 |
mm_rob_fe <- model_matrix(lm_rob_fe) |
| 230 |
amm_rob_fe <- augmented_model_matrix(lm_rob_fe) |
| 231 |
|
| 232 |
# Check that fixed effects are the same |
| 233 |
expect_equivalent(mm_fit_fe[,colnames(amm_rob_fe)], amm_rob_fe) |
| 234 |
# Core predictor matrices are different |
| 235 |
expect_false(identical(mm_rob_fe, mm_fit_fe[,colnames(mm_rob_fe)])) |
| 236 |
# But one can be computed by residualizing |
| 237 |
expect_equivalent( |
| 238 |
mm_rob_fe, |
| 239 |
residuals(lm.fit(amm_rob_fe, mm_fit_fe[,colnames(mm_rob_fe)])) |
| 240 |
) |
| 241 |
# model matrix is centered by chick so means are zero |
| 242 |
expect_lt(max(abs(apply(mm_rob_fe, 2, \(x) tapply(x, ChickWeight$Chick, mean)))), 1e-12) |
| 243 |
|
| 244 |
|
| 245 |
# two-way fixed effects models |
| 246 |
|
| 247 |
mm_fit_fe2 <- model_matrix(lm_fit_fe2) |
| 248 |
mm_rob_fe2 <- model_matrix(lm_rob_fe2) |
| 249 |
amm_rob_fe2 <- augmented_model_matrix(lm_rob_fe2) |
| 250 |
|
| 251 |
# Check that fixed effects are the same |
| 252 |
expect_equivalent(mm_fit_fe2[,colnames(amm_rob_fe2)], amm_rob_fe2) |
| 253 |
|
| 254 |
# Core predictor matrices are different |
| 255 |
expect_false(identical(mm_rob_fe, mm_fit_fe[,colnames(mm_rob_fe)])) |
| 256 |
# But one can be computed by residualizing |
| 257 |
expect_equivalent( |
| 258 |
mm_rob_fe2, |
| 259 |
residuals(lm.fit(amm_rob_fe2, mm_fit_fe2[,colnames(mm_rob_fe2)])) |
| 260 |
) |
| 261 |
# model matrix is centered by state and year |
| 262 |
MortalityRates_full <- subset(MortalityRates, !is.na(beertaxa)) |
| 263 |
expect_lt(max(abs(apply(mm_rob_fe2, 2, \(x) tapply(x, MortalityRates_full$state, mean)))), 1e-12) |
| 264 |
expect_lt(max(abs(apply(mm_rob_fe2, 2, \(x) tapply(x, MortalityRates_full$year, mean)))), 1e-12) |
| 265 |
|
| 266 |
|
| 267 |
# weighted models |
| 268 |
|
| 269 |
mm_wlm <- model_matrix(wlm_fit) |
| 270 |
mm_wrob <- model_matrix(wlm_rob) |
| 271 |
expect_equivalent(mm_wlm, mm_wrob) |
| 272 |
|
| 273 |
}) |
| 274 |
|
| 275 |
test_that("model_matrix() works without fixest", { |
| 276 |
|
| 277 |
local_mocked_bindings( |
| 278 |
requireNamespace = function(...) FALSE |
| 279 |
) |
| 280 |
|
| 281 |
expect_false(requireNamespace("fixest", quietly = TRUE)) |
| 282 |
|
| 283 |
# basic models |
| 284 |
|
| 285 |
mm_fit <- model_matrix(lm_fit) |
| 286 |
mm_rob <- model_matrix(lm_rob) |
| 287 |
amm_rob_fe <- augmented_model_matrix(lm_rob_fe) |
| 288 |
expect_equivalent(mm_fit, mm_rob) |
| 289 |
|
| 290 |
|
| 291 |
# fixed effects models |
| 292 |
|
| 293 |
mm_fit_fe <- model_matrix(lm_fit_fe) |
| 294 |
mm_rob_fe <- model_matrix(lm_rob_fe) |
| 295 |
|
| 296 |
# Check that fixed effects are the same |
| 297 |
expect_equivalent(mm_fit_fe[,colnames(amm_rob_fe)], amm_rob_fe) |
| 298 |
# Core predictor matrices are different |
| 299 |
expect_false(identical(mm_rob_fe, mm_fit_fe[,colnames(mm_rob_fe)])) |
| 300 |
# But one can be computed by residualizing |
| 301 |
expect_equivalent( |
| 302 |
mm_rob_fe, |
| 303 |
residuals(lm.fit(amm_rob_fe, mm_fit_fe[,colnames(mm_rob_fe)])) |
| 304 |
) |
| 305 |
# model matrix is centered by chick so means are zero |
| 306 |
expect_lt(max(abs(apply(mm_rob_fe, 2, \(x) tapply(x, ChickWeight$Chick, mean)))), 1e-12) |
| 307 |
|
| 308 |
expect_message( |
| 309 |
vcovCR(lm_rob_fe,type = "CR0"), |
| 310 |
"For improved performance in models with fixed effects, install the package \\{fixest\\}\\." |
| 311 |
) |
| 312 |
|
| 313 |
}) |
| 314 |
|
| 315 |
test_that("targetVariance() works", { |
| 316 |
|
| 317 |
# unweighted tests |
| 318 |
|
| 319 |
tV_fit <- targetVariance(lm_fit, ChickWeight$Chick) |
| 320 |
tV_rob <- targetVariance(lm_rob, ChickWeight$Chick) |
| 321 |
|
| 322 |
expect_equal(tV_fit, tV_rob) |
| 323 |
|
| 324 |
tV_rob_fe <- targetVariance(lm_rob_fe, ChickWeight$Chick) |
| 325 |
|
| 326 |
expect_equal(tV_fit, tV_rob_fe) |
| 327 |
|
| 328 |
# weighted tests |
| 329 |
|
| 330 |
tV_wlm <- targetVariance(wlm_fit, ChickWeight$Chick) |
| 331 |
tV_wrob <- targetVariance(wlm_rob, ChickWeight$Chick) |
| 332 |
|
| 333 |
expect_equal(tV_wlm, tV_wrob) |
| 334 |
|
| 335 |
}) |
| 336 |
|
| 337 |
|
| 338 |
test_that("weightMatrix() works", { |
| 339 |
|
| 340 |
# unweighted tests |
| 341 |
|
| 342 |
wM_fit <- weightMatrix(lm_fit, ChickWeight$Chick) |
| 343 |
wM_rob <- weightMatrix(lm_rob, ChickWeight$Chick) |
| 344 |
|
| 345 |
expect_equal(wM_fit, wM_rob) |
| 346 |
|
| 347 |
wM_rob_fe <- weightMatrix(lm_rob_fe, ChickWeight$Chick) |
| 348 |
|
| 349 |
expect_equal(wM_fit, wM_rob_fe) |
| 350 |
|
| 351 |
# weighted tests |
| 352 |
|
| 353 |
wM_wlm <- weightMatrix(wlm_fit, ChickWeight$Chick) |
| 354 |
wM_wrob <- weightMatrix(wlm_rob, ChickWeight$Chick) |
| 355 |
|
| 356 |
expect_equal(wM_wlm, wM_wrob) |
| 357 |
|
| 358 |
}) |
| 359 |
|
| 360 |
test_that("sandwich::bread works", { |
| 361 |
|
| 362 |
# basic models |
| 363 |
bread_lm <- bread(lm_fit) |
| 364 |
bread_rob <- bread(lm_rob) |
| 365 |
expect_equal(bread_lm, bread_rob) |
| 366 |
|
| 367 |
# fixed effects models |
| 368 |
bread_lm_fe <- bread(lm_fit_fe) |
| 369 |
bread_rob_fe <- bread(lm_rob_fe) |
| 370 |
focal_coefs <- names(coef(lm_rob_fe)) |
| 371 |
expect_equal(bread_lm_fe[focal_coefs,focal_coefs], as.matrix(bread_rob_fe)) |
| 372 |
|
| 373 |
# two-way fixed effects models |
| 374 |
bread_lm_fe2 <- bread(lm_fit_fe2) |
| 375 |
bread_rob_fe2 <- bread(lm_rob_fe2) |
| 376 |
focal_coefs <- names(coef(lm_rob_fe2)) |
| 377 |
expect_equal(bread_lm_fe2[focal_coefs,focal_coefs], as.matrix(bread_rob_fe2)) |
| 378 |
|
| 379 |
# weighted models |
| 380 |
bread_wlm <- bread(wlm_fit) |
| 381 |
bread_wrob <- bread(wlm_rob) |
| 382 |
expect_equal(bread_wlm, bread_wrob) |
| 383 |
|
| 384 |
}) |
| 385 |
|
| 386 |
|
| 387 |
test_that("residuals_CS() works", { |
| 388 |
|
| 389 |
# basic models |
| 390 |
rcs_fit <- residuals_CS(lm_fit) |
| 391 |
rcs_rob <- residuals_CS(lm_rob) |
| 392 |
expect_equal(rcs_fit, rcs_rob) |
| 393 |
|
| 394 |
# fixed effects models |
| 395 |
rcs_fit_fe <- residuals_CS(lm_fit_fe) |
| 396 |
rcs_rob_fe <- residuals_CS(lm_rob_fe) |
| 397 |
expect_equal(rcs_fit_fe, rcs_rob_fe) |
| 398 |
|
| 399 |
# two-way fixed effects models |
| 400 |
rcs_fit_fe2 <- residuals_CS(lm_fit_fe2) |
| 401 |
rcs_rob_fe2 <- residuals_CS(lm_rob_fe2) |
| 402 |
expect_equal(rcs_fit_fe2, rcs_rob_fe2) |
| 403 |
|
| 404 |
# weighted models |
| 405 |
rcs_wlm <- residuals_CS(wlm_fit) |
| 406 |
rcs_wrob <- residuals_CS(wlm_rob) |
| 407 |
expect_equal(rcs_wlm, rcs_wrob) |
| 408 |
|
| 409 |
}) |
| 410 |
|
| 411 |
|
| 412 |
test_that("coef() works", { |
| 413 |
|
| 414 |
# basic models |
| 415 |
coef_fit <- coef(lm_fit) |
| 416 |
coef_rob <- coef(lm_rob) |
| 417 |
expect_equal(coef_fit, coef_rob) |
| 418 |
|
| 419 |
# fixed effects models |
| 420 |
coef_fit_fe <- coef(lm_fit_fe) |
| 421 |
coef_rob_fe <- coef(lm_rob_fe) |
| 422 |
expect_equal(coef_fit_fe[names(coef_rob_fe)], coef_rob_fe) |
| 423 |
|
| 424 |
# two-way fixed effects models |
| 425 |
coef_fit_fe2 <- coef(lm_fit_fe2) |
| 426 |
coef_rob_fe2 <- coef(lm_rob_fe2) |
| 427 |
expect_equal(coef_fit_fe2[names(coef_rob_fe2)], coef_rob_fe2) |
| 428 |
|
| 429 |
# weighted models |
| 430 |
coef_wlm <- coef(wlm_fit) |
| 431 |
coef_wrob <- coef(wlm_rob) |
| 432 |
expect_equal(coef_wlm, coef_wrob) |
| 433 |
|
| 434 |
}) |
| 435 |
|
| 436 |
|
| 437 |
test_that("nobs() works", { |
| 438 |
|
| 439 |
# unweighted tests |
| 440 |
|
| 441 |
nobs_fit <- nobs(lm_fit) |
| 442 |
nobs_rob <- nobs(lm_rob) |
| 443 |
|
| 444 |
expect_equal(nobs_fit, nobs_rob) |
| 445 |
|
| 446 |
nobs_rob_fe <- nobs(lm_rob_fe) |
| 447 |
|
| 448 |
expect_equal(nobs_fit, nobs_rob_fe) |
| 449 |
|
| 450 |
# weighted tests |
| 451 |
|
| 452 |
nobs_wlm <- nobs(wlm_fit) |
| 453 |
nobs_wrob <- nobs(wlm_rob) |
| 454 |
|
| 455 |
expect_equal(nobs_wlm, nobs_wrob) |
| 456 |
|
| 457 |
}) |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
test_that("v_scale() works", { |
| 462 |
|
| 463 |
# unweighted tests |
| 464 |
|
| 465 |
vs_fit <- v_scale(lm_fit) |
| 466 |
vs_rob <- v_scale(lm_rob) |
| 467 |
vs_rob_fe <- v_scale(lm_rob_fe) |
| 468 |
|
| 469 |
expect_equal(vs_fit, vs_rob) |
| 470 |
expect_equal(vs_fit, vs_rob_fe) |
| 471 |
|
| 472 |
# weighted tests |
| 473 |
|
| 474 |
vs_wlm <- v_scale(wlm_fit) |
| 475 |
vs_wrob <- v_scale(wlm_rob) |
| 476 |
|
| 477 |
expect_equal(vs_wlm, vs_wrob) |
| 478 |
|
| 479 |
}) |
| 480 |
|
| 481 |
|
| 482 |
test_that("vcovCR works", { |
| 483 |
|
| 484 |
types <- c("CR0", "CR1", "CR1p", "CR1S", "CR2", "CR3") |
| 485 |
|
| 486 |
|
| 487 |
focal_coefs <- names(coef(lm_rob_fe)) |
| 488 |
|
| 489 |
for (type in types) { |
| 490 |
|
| 491 |
# basic models |
| 492 |
vcov_lm <- vcovCR(lm_fit, ChickWeight$Chick, type = type) |
| 493 |
vcov_lmr <- vcovCR(lm_rob, ChickWeight$Chick, type = type) |
| 494 |
expect_equal(vcov_lm, vcov_lmr, |
| 495 |
label = paste0("When type = ", type, ", ", "vcov_lm")) |
| 496 |
|
| 497 |
|
| 498 |
# fixed effects models |
| 499 |
|
| 500 |
if (type %in% c("CR0", "CR1", "CR2")) { |
| 501 |
|
| 502 |
vcov_lm_fe <- vcovCR(lm_fit_fe, ChickWeight$Chick, type = type) |
| 503 |
vcov_lmr_fe <- vcovCR(lm_rob_fe, ChickWeight$Chick, type = type) |
| 504 |
|
| 505 |
expect_equal(vcov_lm_fe[focal_coefs,focal_coefs], as.matrix(vcov_lmr_fe), |
| 506 |
label = paste0("When type = ", type, ", ", "vcov_lm_fe[focal_coefs,focal_coefs]")) |
| 507 |
|
| 508 |
} |
| 509 |
|
| 510 |
# weighted models |
| 511 |
vcov_wlm <- vcovCR(wlm_fit, ChickWeight$Chick, type = type) |
| 512 |
vcov_wlmr <- vcovCR(wlm_rob, ChickWeight$Chick, type = type) |
| 513 |
expect_equal(vcov_wlm, vcov_wlmr) |
| 514 |
|
| 515 |
|
| 516 |
if (type %in% c("CR0","CR2")) { |
| 517 |
|
| 518 |
# basic models |
| 519 |
lm_rob_type <- lm_robust( |
| 520 |
weight ~ 0 + Diet + Time:Diet, data = ChickWeight, |
| 521 |
clusters = Chick, |
| 522 |
se_type = type |
| 523 |
) |
| 524 |
|
| 525 |
expect_equal(as.matrix(vcov_lmr), vcov(lm_rob_type), |
| 526 |
label = paste0("When type = ", type, ", ", "as.matrix(vcov_lmr)")) |
| 527 |
|
| 528 |
# fixed effects models |
| 529 |
lm_rob_fe_type <- lm_robust( |
| 530 |
weight ~ Time:Diet, data = ChickWeight, |
| 531 |
clusters = Chick, fixed_effects = ~Chick, |
| 532 |
se_type = type |
| 533 |
) |
| 534 |
|
| 535 |
expect_equal(as.matrix(vcov_lmr_fe), vcov(lm_rob_fe_type), |
| 536 |
label = paste0("When type = ", type, ", ", "as.matrix(vcov_lmr_fe)")) |
| 537 |
|
| 538 |
|
| 539 |
# weighted models |
| 540 |
|
| 541 |
wlm_rob_type <- lm_robust( |
| 542 |
weight ~ 0 + Diet + Time:Diet, data = ChickWeight, |
| 543 |
clusters = Chick, weights = wt, |
| 544 |
se_type = type |
| 545 |
) |
| 546 |
|
| 547 |
expect_equal(as.matrix(vcov_wlmr), vcov(wlm_rob_type), |
| 548 |
label = paste0("When type = ", type, ", ", "as.matrix(vcov_wlmr)")) |
| 549 |
|
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
}) |
| 554 |
|
| 555 |
|
| 556 |
test_that("vcovCR works with se_type inherited from lm_robust().", { |
| 557 |
|
| 558 |
types <- c("CR0", "CR2", "stata") |
| 559 |
|
| 560 |
data("OrchardSprays", package = "datasets") |
| 561 |
OrchardSprays$wt <- 1 + rpois(nrow(OrchardSprays), lambda = 3) |
| 562 |
|
| 563 |
for (type in types) { |
| 564 |
|
| 565 |
# unweighted |
| 566 |
rob_fit <- lm_robust( |
| 567 |
decrease ~ 0 + factor(rowpos) + treatment, |
| 568 |
data = OrchardSprays, |
| 569 |
cluster = colpos, |
| 570 |
se_type = type |
| 571 |
) |
| 572 |
|
| 573 |
expect_equal(as.matrix(vcovCR(rob_fit)), vcov(rob_fit)) |
| 574 |
|
| 575 |
# weighted |
| 576 |
wt_fit <- lm_robust( |
| 577 |
decrease ~ 0 + factor(rowpos) + treatment, |
| 578 |
weights = wt, |
| 579 |
data = OrchardSprays, |
| 580 |
cluster = colpos, |
| 581 |
se_type = type |
| 582 |
) |
| 583 |
|
| 584 |
expect_equal(as.matrix(vcovCR(wt_fit)), vcov(wt_fit)) |
| 585 |
|
| 586 |
|
| 587 |
if (type != "stata") { |
| 588 |
|
| 589 |
# fixed effects |
| 590 |
fe_fit <- lm_robust( |
| 591 |
decrease ~ treatment, |
| 592 |
fixed_effects = ~ rowpos, |
| 593 |
data = OrchardSprays, |
| 594 |
cluster = colpos, |
| 595 |
se_type = type |
| 596 |
) |
| 597 |
expect_equal(as.matrix(vcovCR(fe_fit)), vcov(fe_fit)) |
| 598 |
|
| 599 |
# two-way fixed effects |
| 600 |
fe2_fit <- lm_robust( |
| 601 |
mrate ~ legal + beertaxa + beerpercap + winepercap + spiritpercap, |
| 602 |
fixed_effects = ~ year + state, |
| 603 |
data = MortalityRates, |
| 604 |
cluster = state, |
| 605 |
se_type = type |
| 606 |
) |
| 607 |
expect_equal(as.matrix(vcovCR(fe2_fit)), vcov(fe2_fit)) |
| 608 |
|
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
} |
| 613 |
|
| 614 |
}) |
| 615 |
|
| 616 |
|
| 617 |
test_that("vovCR properly pulls cluster specified for lm_robust", { |
| 618 |
|
| 619 |
# basic models |
| 620 |
uw_clust <- vcovCR(lm_rob, ChickWeight$Chick, "CR2") |
| 621 |
uw_no_clust <- vcovCR(lm_rob, type = "CR2") |
| 622 |
uw_lm <- vcovCR(lm_fit, ChickWeight$Chick, "CR2") |
| 623 |
|
| 624 |
expect_equal(uw_clust, uw_no_clust) |
| 625 |
expect_equal(uw_no_clust, uw_lm) |
| 626 |
|
| 627 |
# create an lm_robust that draws in data differently |
| 628 |
lm_rob_fact <- lm_robust(weight ~ 0 + Diet + Time:Diet, data = ChickWeight, |
| 629 |
clusters = factor(ChickWeight$Chick_ordered, ordered = FALSE)) |
| 630 |
# perform vcovCR |
| 631 |
uw_fact_cr <- vcovCR(lm_rob_fact, type = "CR2") |
| 632 |
|
| 633 |
# check they are the same |
| 634 |
expect_equivalent(uw_clust, uw_fact_cr) |
| 635 |
|
| 636 |
# put cluster data in a variable |
| 637 |
# fact <- factor(ChickWeight$Chick_ordered, ordered = FALSE) |
| 638 |
fact <- ChickWeight$Chick |
| 639 |
|
| 640 |
# pass variable to lm_robust |
| 641 |
lm_rob_var <- lm_robust(weight ~ 0 + Diet + Time:Diet, data = ChickWeight, |
| 642 |
clusters = fact) |
| 643 |
|
| 644 |
# perform vcovCR |
| 645 |
uw_fact_var <- vcovCR(lm_rob_var, type = "CR2") |
| 646 |
|
| 647 |
# check they are the same |
| 648 |
expect_equivalent(uw_clust, uw_fact_var) |
| 649 |
|
| 650 |
# weighted tests |
| 651 |
|
| 652 |
w_clust <- vcovCR(wlm_rob, ChickWeight$Chick, "CR2") |
| 653 |
w_no_clust <- vcovCR(wlm_rob, type = "CR2") |
| 654 |
w_lm <- vcovCR(wlm_fit, ChickWeight$Chick, "CR2") |
| 655 |
|
| 656 |
expect_equal(w_clust, w_no_clust) |
| 657 |
expect_equal(w_no_clust, w_lm) |
| 658 |
|
| 659 |
# create an lm_robust that draws in data differently |
| 660 |
lm_rob_fact_w <- lm_robust(weight ~ 0 + Diet + Time:Diet, weights = wt, |
| 661 |
data = ChickWeight, |
| 662 |
clusters = factor(ChickWeight$Chick_ordered, ordered = FALSE)) |
| 663 |
# perform vcovCR |
| 664 |
w_fact_cr <- vcovCR(lm_rob_fact_w, type = "CR2") |
| 665 |
|
| 666 |
expect_equal(w_clust, w_fact_cr) |
| 667 |
|
| 668 |
# pass variable to lm_robust |
| 669 |
lm_rob_var_w <- lm_robust(weight ~ 0 + Diet + Time:Diet, weights = wt, |
| 670 |
data = ChickWeight, clusters = fact) |
| 671 |
|
| 672 |
# perform vcovCR |
| 673 |
w_fact_var <- vcovCR(lm_rob_var_w, type = "CR2") |
| 674 |
|
| 675 |
# check they are the same |
| 676 |
expect_equal(w_clust, w_fact_var) |
| 677 |
|
| 678 |
}) |
| 679 |
|
| 680 |
|
| 681 |
test_that("na.action.lm_robust() works correctly", { |
| 682 |
|
| 683 |
compare_na_actions <- function(i) { |
| 684 |
|
| 685 |
# generate random data |
| 686 |
n <- 100 |
| 687 |
df <- data.frame( |
| 688 |
y = rnorm(n), |
| 689 |
x1 = rnorm(n), |
| 690 |
x2 = rnorm(n) |
| 691 |
) |
| 692 |
|
| 693 |
# add random NA values to y and x2 |
| 694 |
miss_rows <- sample.int(n, size = n/10) |
| 695 |
df$y[miss_rows] <- NA |
| 696 |
df$x2[miss_rows] <- NA |
| 697 |
|
| 698 |
# fit models |
| 699 |
linear <- lm(y ~ x1 + x2 + x1:x2, data = df) |
| 700 |
robust <- lm_robust(y ~ x1 + x2 + x1:x2, data = df) |
| 701 |
|
| 702 |
# get na.action() of models |
| 703 |
na_lm <- na.action(linear) |
| 704 |
na_rob <- na.action(robust) |
| 705 |
|
| 706 |
# compare |
| 707 |
expect_equal(na_lm, na_rob) |
| 708 |
} |
| 709 |
|
| 710 |
# compare 10 times with different random data |
| 711 |
lapply(1:10, compare_na_actions) |
| 712 |
|
| 713 |
}) |
| 714 |
|
| 715 |
|
| 716 |
test_that("try_cholesky argument does not interfere with vcovCR functionality", { |
| 717 |
|
| 718 |
lm_rob_chole <- lm_robust( |
| 719 |
weight ~ 0 + Diet + Time:Diet, |
| 720 |
data = ChickWeight, |
| 721 |
clusters = Chick, |
| 722 |
try_cholesky = TRUE |
| 723 |
) |
| 724 |
|
| 725 |
expect_equal( |
| 726 |
vcovCR(lm_rob, type = "CR2"), |
| 727 |
vcovCR(lm_rob_chole, type = "CR2") |
| 728 |
) |
| 729 |
|
| 730 |
lm_rob_fe_chole <- lm_robust( |
| 731 |
weight ~ Time:Diet, data = ChickWeight, |
| 732 |
clusters = Chick, |
| 733 |
fixed_effects = ~Chick, |
| 734 |
try_cholesky = TRUE |
| 735 |
) |
| 736 |
|
| 737 |
expect_equal(vcovCR(lm_rob_fe, type = "CR2"), vcovCR(lm_rob_fe_chole, type = "CR2")) |
| 738 |
|
| 739 |
wlm_rob_chole <- lm_robust( |
| 740 |
weight ~ 0 + Diet + Time:Diet, |
| 741 |
weights = wt, |
| 742 |
data = ChickWeight, |
| 743 |
clusters = Chick, |
| 744 |
try_cholesky = TRUE |
| 745 |
) |
| 746 |
|
| 747 |
expect_equal(vcovCR(wlm_rob, type = "CR2"), vcovCR(wlm_rob_chole, type = "CR2")) |
| 748 |
|
| 749 |
}) |
| 750 |
|
| 751 |
|
| 752 |
test_that("subset argument does not interfere with vcovCR functionality", { |
| 753 |
|
| 754 |
# basic models |
| 755 |
lm_fit_sub <- lm(weight ~ 0 + Diet + Time:Diet, data = ChickWeight, |
| 756 |
subset = ChickWeight$rando == "Keep") |
| 757 |
lm_rob_sub <- lm_robust(weight ~ 0 + Diet + Time:Diet, data = ChickWeight, |
| 758 |
clusters = Chick, subset = ChickWeight$rando == "Keep") |
| 759 |
|
| 760 |
expect_equal(vcovCR(lm_fit_sub, ChickWeight$Chick[ChickWeight$rando == "Keep"], type = "CR2"), |
| 761 |
vcovCR(lm_rob_sub, type = "CR2")) |
| 762 |
|
| 763 |
|
| 764 |
# fixed effects models |
| 765 |
|
| 766 |
lm_fit_fe_sub <- lm( |
| 767 |
weight ~ 0 + Time:Diet + Chick, |
| 768 |
data = ChickWeight, |
| 769 |
subset = rando == "Keep" |
| 770 |
) |
| 771 |
lm_rob_fe_sub <- lm_robust( |
| 772 |
weight ~ Time:Diet, |
| 773 |
data = ChickWeight, |
| 774 |
clusters = Chick, |
| 775 |
fixed_effects = ~Chick, |
| 776 |
subset = rando == "Keep" |
| 777 |
) |
| 778 |
|
| 779 |
sub_coef <- names(coef(lm_rob_fe_sub)) |
| 780 |
expect_equivalent( |
| 781 |
vcovCR(lm_fit_fe_sub, ChickWeight$Chick[ChickWeight$rando == "Keep"], type = "CR2")[sub_coef, sub_coef], |
| 782 |
as.matrix(vcovCR(lm_rob_fe_sub, type = "CR2")) |
| 783 |
) |
| 784 |
|
| 785 |
# weighted models |
| 786 |
|
| 787 |
wlm_fit_sub <- lm(weight ~ 0 + Diet + Time:Diet, weights = wt, |
| 788 |
data = ChickWeight, subset = rando == "Keep") |
| 789 |
wlm_rob_sub <- lm_robust(weight ~ 0 + Diet + Time:Diet, weights = wt, |
| 790 |
data = ChickWeight, clusters = Chick, |
| 791 |
subset = rando == "Keep") |
| 792 |
|
| 793 |
expect_equal(vcovCR(wlm_fit_sub, ChickWeight$Chick[ChickWeight$rando == "Keep"], type = "CR2"), |
| 794 |
vcovCR(wlm_rob_sub, type = "CR2")) |
| 795 |
|
| 796 |
}) |
| 797 |
|
| 798 |
|
| 799 |
# =============== Higher level Tests =============== |
| 800 |
|
| 801 |
|
| 802 |
data("Seatbelts", package = "datasets") |
| 803 |
|
| 804 |
# Convert Seatbelts time series to data frame |
| 805 |
belts <- as.data.frame(Seatbelts) |
| 806 |
|
| 807 |
# Extract the time index and convert to Date |
| 808 |
time_index <- time(Seatbelts) |
| 809 |
year <- floor(time_index) |
| 810 |
month <- cycle(Seatbelts) |
| 811 |
|
| 812 |
# Add the time columns |
| 813 |
belts$kms <- belts$kms - mean(belts$kms) |
| 814 |
belts$year <- year - mean(year) |
| 815 |
belts$month <- month |
| 816 |
|
| 817 |
# Create identical lm and lm_robust models |
| 818 |
belts_fit <- lm(DriversKilled ~ kms + PetrolPrice + law + year, data = belts) |
| 819 |
belts_rob <- lm_robust(DriversKilled ~ kms + PetrolPrice + law + year, data = belts) |
| 820 |
|
| 821 |
|
| 822 |
test_that("Wald_test() works with lm_robust", { |
| 823 |
|
| 824 |
Wald_FIT <- Wald_test( |
| 825 |
belts_fit, |
| 826 |
constraints = constrain_zero("year", reg_ex = TRUE), |
| 827 |
vcov = "CR2", |
| 828 |
cluster = belts$month |
| 829 |
) |
| 830 |
|
| 831 |
Wald_ROB <- Wald_test( |
| 832 |
belts_rob, |
| 833 |
constraints = constrain_zero("year", reg_ex = TRUE), |
| 834 |
vcov = "CR2", |
| 835 |
cluster = belts$month |
| 836 |
) |
| 837 |
|
| 838 |
expect_equal(Wald_FIT, Wald_ROB) |
| 839 |
|
| 840 |
}) |
| 841 |
|
| 842 |
|
| 843 |
test_that("conf_int() works with lm_robust", { |
| 844 |
|
| 845 |
conf_FIT <- conf_int(belts_fit, vcov = "CR2", cluster = belts$month) |
| 846 |
conf_ROB <- conf_int(belts_rob, vcov = "CR2", cluster = belts$month) |
| 847 |
|
| 848 |
expect_equal(conf_FIT, conf_ROB) |
| 849 |
|
| 850 |
}) |
| 851 |
|
| 852 |
|
| 853 |
test_that("coef_test() works with lm_robust", { |
| 854 |
|
| 855 |
coef_FIT <- coef_test(belts_fit, vcov = "CR2", cluster = belts$month) |
| 856 |
coef_ROB <- coef_test(belts_rob, vcov = "CR2", cluster = belts$month) |
| 857 |
|
| 858 |
expect_equal(coef_FIT, coef_ROB) |
| 859 |
|
| 860 |
}) |
| 861 |
|
| 862 |
|
| 863 |
# =============== Tests Based on test_lm.R =============== |
| 864 |
|
| 865 |
|
| 866 |
test_that("Order doesn't matter.",{ |
| 867 |
|
| 868 |
check_sort_order(belts_rob, belts, "month", tol = 1e-5) |
| 869 |
|
| 870 |
}) |
| 871 |
|
| 872 |
|
| 873 |
test_that("clubSandwich works with dropped observations", { |
| 874 |
belts_miss <- belts |
| 875 |
miss_indicator <- sample.int(nrow(belts), size = round(nrow(belts) / 10)) |
| 876 |
belts_miss$law[miss_indicator] <- NA |
| 877 |
belts_miss$kms[miss_indicator] <- NA |
| 878 |
|
| 879 |
rob_dropped <- lm_robust(DriversKilled ~ kms + PetrolPrice + law + year, data = belts_miss, cluster = month) |
| 880 |
belts_complete <- subset(belts_miss, !is.na(law)) |
| 881 |
rob_complete <- lm_robust(DriversKilled ~ kms + PetrolPrice + law + year, data = belts_complete, cluster = month) |
| 882 |
|
| 883 |
CR_types <- paste0("CR",0:4) |
| 884 |
|
| 885 |
CR_drop <- lapply(CR_types, function(x) vcovCR(rob_dropped, type = x)) |
| 886 |
CR_complete <- lapply(CR_types, function(x) vcovCR(rob_complete, type = x)) |
| 887 |
expect_equal(CR_drop, CR_complete) |
| 888 |
|
| 889 |
test_drop <- lapply(CR_types, function(x) coef_test(rob_dropped, vcov = x, test = "All", p_values = FALSE)) |
| 890 |
test_complete <- lapply(CR_types, function(x) coef_test(rob_complete, vcov = x, test = "All", p_values = FALSE)) |
| 891 |
expect_equal(test_drop, test_complete) |
| 892 |
}) |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
test_that("clubSandwich requires no missing values on the clustering variable", { |
| 897 |
|
| 898 |
belts_miss <- belts |
| 899 |
miss_indicator <- sample.int(nrow(belts), size = round(nrow(belts) / 10)) |
| 900 |
belts_miss$month[miss_indicator] <- NA |
| 901 |
|
| 902 |
rob_dropped <- lm_robust(DriversKilled ~ kms + PetrolPrice + law + year, data = belts_miss) |
| 903 |
expect_error(vcovCR(rob_dropped, cluster = belts_miss$month, type = "CR0"), |
| 904 |
"Clustering variable cannot have missing values.") |
| 905 |
expect_error(coef_test(rob_dropped, vcov = "CR0", cluster = belts_miss$month, test = "All"), |
| 906 |
"Clustering variable cannot have missing values.") |
| 907 |
}) |