Debian Codegraph @

tests/testthat/test_lm_lin.R

raw
1 context("lm_lin robust objects")
2
3 skip_if_not_installed("estimatr")
4
5 suppressMessages(library(estimatr, quietly=TRUE))
6
7 set.seed(20250630)
8 data("CO2")
9 N <- nrow(CO2)
10 CO2$wt <- 1 + rpois(N, 3)
11 CO2$Plant_[!is.na(CO2$uptake), ] # remove rows with NA in uptakeordered <- CO2$Plant
12 CO2$Plant <- factor(CO2$Plant, ordered = FALSE)
13 CO2$Plant_int <- as.integer(CO2$Plant)
14 CO2$rando <- "Drop"
15 CO2$rando[sample(1:N, size = round(0.8 * N))] <- "Keep"
16 table(CO2$rando)
17
18 # make a copy of CO2 for weighted clustered models
19 wcCO2 <- CO2
20
21 CO2$conc_c <- CO2$conc - mean(CO2$conc)
22 CO2$`(factor(Type)Mississippi)_c` <- ifelse(CO2$Type == "Mississippi", 0.5, -0.5)
23
24 lin <- lm_lin(
25 uptake ~ Treatment,
26 covariates = ~ conc,
27 data = CO2
28 )
29
30 lin_exp <- lm_lin(
31 uptake ~ Treatment,
32 covariates = ~ conc + factor(Type),
33 data = CO2
34 )
35
36 rob <- lm_robust(
37 uptake ~ Treatment +
38 conc_c + Treatment:conc_c,
39 data = CO2
40 )
41
42 rob_exp <- lm_robust(
43 uptake ~ Treatment + conc_c + `(factor(Type)Mississippi)_c` + Treatment:conc_c + Treatment:`(factor(Type)Mississippi)_c`,
44 data = CO2
45 )
46
47 # weighted and clustered
48
49 wclin <- lm_lin(
50 uptake ~ Treatment,
51 covariates = ~ conc,
52 data = wcCO2,
53 weights = wt,
54 clusters = Plant
55 )
56
57 wcCO2$conc_c <- wcCO2$conc - wclin$scaled_center[["conc"]]
58
59 wcrob <- lm_robust(
60 uptake ~ Treatment +
61 conc_c + Treatment:conc_c,
62 data = wcCO2,
63 weights = wt,
64 clusters = Plant
65 )
66
67 test_that("model.frame() works", {
68
69 # basic models
70 mf_lin <- model.frame(lin)
71 mf_rob <- model.frame(rob)
72 expect_equivalent(mf_lin, mf_rob)
73
74 # covariates with an expression
75 mf_exp <- model.frame(lin_exp)
76 mf_rexp <- model.frame(rob_exp)
77 expect_equivalent(mf_exp, mf_rexp)
78
79 # weighted models
80 mf_wclin <- model.frame(wclin)
81 mf_wcrob <- model.frame(wcrob)
82 expect_equivalent(mf_wclin, mf_wcrob[names(mf_wclin)])
83
84 })
85
86 test_that("model_matrix() works", {
87
88 # basic models
89 mm_lin <- model_matrix(lin)
90 mm_rob <- model_matrix(rob)
91 expect_equivalent(mm_lin, mm_rob)
92
93 # covariates with an expression
94 mm_exp <- model_matrix(lin_exp)
95 mm_rexp <- model_matrix(rob_exp)
96 expect_equivalent(mm_exp, mm_rexp)
97
98 # weighted models
99 mm_wclin <- model_matrix(wclin)
100 mm_wcrob <- model_matrix(wcrob)
101 expect_equivalent(mm_wclin, mm_wcrob)
102
103 })
104
105 test_that("targetVariance() works", {
106
107 # unweighted tests
108
109 tV_lin <- targetVariance(lin, CO2$Plant)
110 tV_rob <- targetVariance(rob, CO2$Plant)
111
112 expect_equal(tV_lin, tV_rob)
113
114 # weighted tests
115
116 tV_wclin <- targetVariance(wclin, wcCO2$Plant)
117 tV_wcrob <- targetVariance(wcrob, wcCO2$Plant)
118
119 expect_equal(tV_wclin, tV_wcrob)
120
121 })
122
123
124 test_that("weightMatrix() works", {
125
126 # unweighted tests
127
128 wM_fit <- weightMatrix(lin, CO2$Plant)
129 wM_rob <- weightMatrix(rob, CO2$Plant)
130
131 expect_equal(wM_fit, wM_rob)
132
133 # weighted tests
134
135 wM_wcfit <- weightMatrix(wclin, wcCO2$Plant)
136 wM_wcrob <- weightMatrix(wcrob, wcCO2$Plant)
137
138 expect_equal(wM_wcfit, wM_wcrob)
139
140 })
141
142 test_that("sandwich::bread works", {
143
144 # unweighted tests
145
146 bread_lin <- bread(lin)
147 bread_rob <- bread(rob)
148
149 expect_equivalent(bread_lin, bread_rob) # changed from expect_equal
150
151 # weighted tests
152
153 bread_wclin <- bread(wclin)
154 bread_wcrob <- bread(wcrob)
155
156 expect_equivalent(bread_wclin, bread_wcrob) # changed from expect_equal
157
158 })
159
160
161 test_that("residuals_CS() works", {
162
163 # unweighted tests
164
165 rcs_lin <- residuals_CS(lin)
166 rcs_rob <- residuals_CS(rob)
167
168 expect_equal(rcs_lin, rcs_rob)
169
170 # weighted tests
171
172 rcs_wclin <- residuals_CS(wclin)
173 rcs_wcrob <- residuals_CS(wcrob)
174
175 expect_equal(rcs_wclin, rcs_wcrob)
176
177 })
178
179
180 test_that("coef() works", {
181
182 # unweighted tests
183
184 coef_lin <- coef(lin)
185 coef_rob <- coef(rob)
186
187 expect_equal(coef_lin, coef_rob)
188
189 # weighted tests
190
191 coef_wclin <- coef(wclin)
192 coef_wcrob <- coef(wcrob)
193
194 expect_equal(coef_wclin, coef_wcrob)
195
196 })
197
198
199 test_that("nobs() works", {
200
201 # unweighted tests
202
203 nobs_lin <- nobs(lin)
204 nobs_rob <- nobs(rob)
205
206 expect_equal(nobs_lin, nobs_rob)
207
208 # weighted tests
209
210 nobs_wclin <- nobs(wclin)
211 nobs_wcrob <- nobs(wcrob)
212
213 expect_equal(nobs_wclin, nobs_wcrob)
214
215 })
216
217
218
219 test_that("v_scale() works", {
220
221 # unweighted tests
222
223 vs_lin <- v_scale(lin)
224 vs_rob <- v_scale(rob)
225
226 expect_equal(vs_lin, vs_rob)
227
228 # weighted tests
229
230 vs_wclin <- v_scale(wclin)
231 vs_wcrob <- v_scale(wcrob)
232
233 expect_equal(vs_wclin, vs_wcrob)
234
235 })
236
237
238 test_that("vcovCR works", {
239
240 types <- c("CR0", "CR1", "CR1p", "CR1S", "CR2", "CR3")
241
242 for (type in types) {
243
244 # unweighted tests
245
246 vcov_lin <- vcovCR(lin, CO2$Plant, type = type)
247 vcov_rob <- vcovCR(rob, CO2$Plant, type = type)
248
249 # changed from equal b/c of attributes
250 expect_equivalent(vcov_lin, vcov_rob,
251 label = paste0("When type = ", type, ", ", "vcov_lin"))
252
253 # weighted tests
254
255 vcov_wclin <- vcovCR(wclin, wcCO2$Plant, type = type)
256 vcov_wcrob <- vcovCR(wcrob, wcCO2$Plant, type = type)
257
258 # changed from equal b/c of attributes
259 expect_equivalent(vcov_wclin, vcov_wcrob,
260 label = paste0("When type = ", type, ", ", "vcov_wclin"))
261
262 }
263
264 })
265
266
267 test_that("vovCR properly pulls cluster specified for lm_lin objects", {
268
269 # weighted tests
270
271 w_clust <- vcovCR(wclin, wcCO2$Plant, "CR2")
272 w_no_clust <- vcovCR(wclin, type = "CR2")
273 w_lin <- vcovCR(wclin, wcCO2$Plant, "CR2")
274
275 expect_equal(w_clust, w_no_clust)
276 expect_equal(w_no_clust, w_lin)
277
278 # create an lm_robust object that draws in data differently
279 lin_fact <- lm_lin(uptake ~ Treatment,
280 covariates = ~ conc,
281 data = CO2,
282 weights = wt,
283 clusters = factor(CO2$Plant, ordered = FALSE))
284
285 # perform vcovCR
286 w_fact_cr <- vcovCR(lin_fact, type = "CR2")
287
288 # check they are the same
289 expect_equivalent(w_clust, w_fact_cr)
290
291 # put cluster data in a variable
292 # fact <- factor(ChickWeight$Chick_ordered, ordered = FALSE)
293 fact <- CO2$Plant
294
295 # pass variable to robust
296 lin_var <- lm_lin(uptake ~ Treatment,
297 covariates = ~ conc,
298 data = CO2,
299 weights = wt,
300 clusters = fact)
301
302 # perform vcovCR
303 w_fact_var <- vcovCR(lin_var, type = "CR2")
304
305 # check they are the same
306 expect_equivalent(w_clust, w_fact_var)
307
308 })
309
310
311 test_that("vcovCR works with se_type inherited from lm_lin().", {
312
313 types <- c("CR0", "CR2", "stata")
314
315 data("AchievementAwardsRCT")
316 AchievementAwardsRCT$sub <- with(
317 AchievementAwardsRCT,
318 unsplit(tapply(Bagrut_status, school_id, \(x) seq(0, 1, length.out = length(x))), school_id)
319 )
320 AchievementAwardsRCT <- subset(AchievementAwardsRCT, school_id != 1 & sub < 0.2)
321 AchievementAwardsRCT$father_ed <- ifelse(AchievementAwardsRCT$father_ed <= 5, "GS", ifelse(AchievementAwardsRCT$father_ed <=12, "HS","Coll"))
322
323 for (type in types) {
324
325 # basic
326 rob_fit <- lm_lin(
327 achv_math ~ treated,
328 covariates = ~ school_type + sex + father_ed,
329 data = AchievementAwardsRCT,
330 cluster = school_id,
331 se_type = type
332 )
333
334 expect_equal(as.matrix(vcovCR(rob_fit)), vcov(rob_fit))
335
336 # with expression in the covariates
337 rob_exp <- lm_lin(
338 achv_math ~ treated,
339 covariates = ~ school_type + sex + factor(father_ed),
340 data = AchievementAwardsRCT,
341 cluster = school_id,
342 se_type = type
343 )
344
345 expect_equivalent(as.matrix(vcovCR(rob_exp)), vcov(rob_exp))
346
347 # weighted
348 wt_fit <- lm_lin(
349 achv_math ~ treated,
350 covariates = ~ school_type + sex + father_ed,
351 data = AchievementAwardsRCT,
352 weights = siblings + 1,
353 cluster = school_id,
354 se_type = type
355 )
356
357 expect_equal(as.matrix(vcovCR(wt_fit)), vcov(wt_fit))
358
359 }
360
361 })
362
363 test_that("na.action.robust() works correctly", {
364
365 compare_na_actions <- function(i) {
366
367 # generate random data
368 n <- 100
369 df <- data.frame(
370 y = rnorm(n),
371 x1 = sample(letters[1:2], size = n, replace = TRUE),
372 x2 = rnorm(n)
373 )
374
375 # add random NA values to y and x2
376 miss_rows <- sample.int(n, size = n/10)
377 df$y[miss_rows] <- NA
378 df$x2[miss_rows] <- NA
379
380 # fit models
381 lin_fit <- lm_lin(y ~ x1,
382 covariates = ~ x2,
383 data = df)
384 rob_fit <- lm_robust(y ~ x1 + x2 + x1:x2,
385 data = df)
386
387 # get na.action() of models
388 na_lin <- na.action(lin_fit)
389 na_rob <- names(na.action(rob_fit))
390
391 # compare
392 expect_equal(na_lin, na_rob)
393 }
394
395 # compare 10 times with different random data
396 lapply(1:10, compare_na_actions)
397
398 })
399
400
401 test_that("try_cholesky argument does not interfere with vcovCR functionality", {
402
403 lin_chole <- lm_lin(
404 uptake ~ Treatment,
405 covariates = ~ conc,
406 data = CO2,
407 try_cholesky = TRUE
408 )
409
410 expect_equal(
411 vcovCR(lin, cluster = CO2$Plant, type = "CR2"),
412 vcovCR(lin_chole, cluster = CO2$Plant, type = "CR2")
413 )
414
415 wclin_chole <- wclin <- lm_lin(
416 uptake ~ Treatment,
417 covariates = ~ conc,
418 data = wcCO2,
419 weights = wt,
420 clusters = Plant,
421 try_cholesky = TRUE
422 )
423
424 expect_equal(vcovCR(wclin, type = "CR2"), vcovCR(wclin_chole, type = "CR2"))
425
426 })
427
428
429 test_that("subset argument does not interfere with vcovCR functionality", {
430
431 lin_sub <- lm_lin(uptake ~ Treatment,
432 covariates = ~ conc,
433 data = CO2,
434 subset = rando == "Keep")
435
436 # CO2$conc_c_no_subset <- CO2$conc_c
437 CO2$conc_c <- CO2$conc - lin_sub$scaled_center
438
439 rob_sub <- lm_robust(uptake ~ Treatment + conc_c + Treatment:conc_c,
440 data = CO2,
441 subset = rando == "Keep")
442
443 # changed from equal b/c of attributes
444 expect_equivalent(vcovCR(lin_sub, CO2$Plant[CO2$rando == "Keep"], type = "CR2"),
445 vcovCR(rob_sub, CO2$Plant[CO2$rando == "Keep"], type = "CR2"))
446
447 wclin_sub <- lm_lin(uptake ~ Treatment,
448 covariates = ~ conc,
449 data = wcCO2,
450 weights = wt,
451 clusters = Plant,
452 subset = rando == "Keep")
453
454 # wcCO2$conc_c_no_subset <- wcCO2$conc_c
455 wcCO2$conc_c <- wcCO2$conc - wclin_sub$scaled_center
456
457 wcrob_sub <- lm_robust(uptake ~ Treatment + conc_c + Treatment:conc_c,
458 data = wcCO2,
459 weights = wt,
460 clusters = Plant,
461 subset = rando == "Keep")
462
463 # changed from equal b/c of attributes
464 expect_equivalent(vcovCR(wclin_sub, type = "CR2"),
465 vcovCR(wcrob_sub, type = "CR2"))
466
467 })
468
469
470 # =============== Higher level Tests ===============
471
472
473 data("Seatbelts", package = "datasets")
474
475 # Convert Seatbelts time series to data frame
476 belts <- as.data.frame(Seatbelts)
477
478 # Extract the time index and convert to Date
479 time_index <- time(Seatbelts)
480 year <- floor(time_index)
481 month <- cycle(Seatbelts)
482
483 # Add the time columns
484 belts$kms <- belts$kms - mean(belts$kms)
485 belts$year <- year - mean(year)
486 belts$month <- month
487
488 # Create identical lm and robust models
489 belts_fit <- lm(DriversKilled ~ kms + PetrolPrice + law + year, data = belts)
490 belts_rob <- lm_robust(DriversKilled ~ kms + PetrolPrice + law + year, data = belts)
491
492
493 test_that("Wald_test() works with robust", {
494
495 Wald_FIT <- Wald_test(
496 belts_fit,
497 constraints = constrain_zero("year", reg_ex = TRUE),
498 vcov = "CR2",
499 cluster = belts$month
500 )
501
502 Wald_ROB <- Wald_test(
503 belts_rob,
504 constraints = constrain_zero("year", reg_ex = TRUE),
505 vcov = "CR2",
506 cluster = belts$month
507 )
508
509 expect_equal(Wald_FIT, Wald_ROB)
510
511 })
512
513
514 test_that("conf_int() works with robust", {
515
516 conf_FIT <- conf_int(belts_fit, vcov = "CR2", cluster = belts$month)
517 conf_ROB <- conf_int(belts_rob, vcov = "CR2", cluster = belts$month)
518
519 expect_equal(conf_FIT, conf_ROB)
520
521 })
522
523
524 test_that("coef_test() works with robust", {
525
526 coef_FIT <- coef_test(belts_fit, vcov = "CR2", cluster = belts$month)
527 coef_ROB <- coef_test(belts_rob, vcov = "CR2", cluster = belts$month)
528
529 expect_equal(coef_FIT, coef_ROB)
530
531 })
532
533
534 # =============== Tests Based on test_lm.R ===============
535
536
537 test_that("Order doesn't matter.",{
538
539 check_sort_order(belts_rob, belts, "month", tol = 1e-5)
540
541 })
542
543
544 test_that("clubSandwich works with dropped observations", {
545 belts_miss <- belts
546 miss_indicator <- sample.int(nrow(belts), size = round(nrow(belts) / 10))
547 belts_miss$law[miss_indicator] <- NA
548 belts_miss$kms[miss_indicator] <- NA
549
550 rob_dropped <- lm_robust(DriversKilled ~ kms + PetrolPrice + law + year, data = belts_miss, cluster = month)
551 belts_complete <- subset(belts_miss, !is.na(law))
552 rob_complete <- lm_robust(DriversKilled ~ kms + PetrolPrice + law + year, data = belts_complete, cluster = month)
553
554 CR_types <- paste0("CR",0:4)
555
556 CR_drop <- lapply(CR_types, function(x) vcovCR(rob_dropped, type = x))
557 CR_complete <- lapply(CR_types, function(x) vcovCR(rob_complete, type = x))
558 expect_equal(CR_drop, CR_complete)
559
560 test_drop <- lapply(CR_types, function(x) coef_test(rob_dropped, vcov = x, test = "All", p_values = FALSE))
561 test_complete <- lapply(CR_types, function(x) coef_test(rob_complete, vcov = x, test = "All", p_values = FALSE))
562 expect_equal(test_drop, test_complete)
563 })
564
565
566
567 test_that("clubSandwich requires no missing values on the clustering variable", {
568
569 belts_miss <- belts
570 miss_indicator <- sample.int(nrow(belts), size = round(nrow(belts) / 10))
571 belts_miss$month[miss_indicator] <- NA
572
573 rob_dropped <- lm_robust(DriversKilled ~ kms + PetrolPrice + law + year, data = belts_miss)
574 expect_error(vcovCR(rob_dropped, cluster = belts_miss$month, type = "CR0"),
575 "Clustering variable cannot have missing values.")
576 expect_error(coef_test(rob_dropped, vcov = "CR0", cluster = belts_miss$month, test = "All"),
577 "Clustering variable cannot have missing values.")
578 })