Debian Codegraph @

tests/testthat/test-lfe.R

raw
1 skip_on_cran()
2
3 skip_if_not_installed("modeltests")
4 library(modeltests)
5
6 skip_if_not_installed("lfe")
7
8 set.seed(27)
9 n <- 100
10 df <- data.frame(
11 id = sample(5, n, TRUE),
12 v1 = sample(5, n, TRUE),
13 v2 = sample(1e6, n, TRUE),
14 v3 = sample(round(runif(100, max = 100), 4), n, TRUE),
15 v4 = sample(round(runif(100, max = 100), 4), n, TRUE)
16 )
17
18 # no FE or clus
19 fit <- lfe::felm(v2 ~ v3, df)
20 # with FE
21 fit2 <- lfe::felm(v2 ~ v3 | id + v1, df, na.action = na.exclude)
22
23 # with clus
24 fit3 <- lfe::felm(v2 ~ v3 | 0 | 0 | id + v1, df, na.action = na.exclude)
25
26 # with multiple outcomes
27 fit_multi <- lfe::felm(v1 + v2 ~ v3, df)
28 fit_Y2 <- lfe::felm(v1 ~ v3, df)
29
30 form <- v2 ~ v4
31 fit_form <- lfe::felm(form, df) # part of a regression test
32
33 test_that("felm tidier arguments", {
34 check_arguments(tidy.felm)
35 check_arguments(glance.felm)
36 check_arguments(augment.felm)
37 })
38
39 test_that("tidy.felm", {
40 td1 <- tidy(fit)
41 td2 <- tidy(fit2, conf.int = TRUE, fe = TRUE, fe.error = FALSE)
42 td3 <- tidy(fit2, conf.int = TRUE, fe = TRUE)
43 td4 <- tidy(fit_form)
44 td5 <- tidy(fit, se = "robust")
45 td6 <- tidy(fit2, se = "robust")
46 td7 <- tidy(fit2, se = "robust", fe = TRUE)
47 td8 <- tidy(fit3)
48 td9 <- tidy(fit3, se = "iid")
49
50 td_multi <- tidy(fit_multi)
51 td_multi_CI <- tidy(fit_multi, conf.int = TRUE)
52
53 check_tidy_output(td1)
54 check_tidy_output(td2)
55 check_tidy_output(td3)
56 check_tidy_output(td4)
57 check_tidy_output(td5)
58 check_tidy_output(td6)
59 check_tidy_output(td7)
60 check_tidy_output(td8)
61 check_tidy_output(td9)
62 check_tidy_output(td_multi)
63 check_tidy_output(td_multi_CI)
64
65 check_dims(td1, 2, 5)
66 check_dims(td_multi_CI, 4, 8)
67
68 expect_equal(
69 tidy(fit_multi)[3:4, -1],
70 tidy(fit)
71 )
72 expect_equal(
73 tidy(fit_multi, conf.int = TRUE)[3:4, -1],
74 tidy(fit, conf.int = TRUE)
75 )
76 expect_equal(
77 tidy(fit_multi, conf.int = TRUE)[1:2, -1],
78 tidy(fit_Y2, conf.int = TRUE)
79 )
80
81 expect_equal(
82 dplyr::pull(td5, std.error),
83 as.numeric(lfe:::summary.felm(fit, robust = TRUE)$coef[, "Robust s.e"])
84 )
85 expect_equal(
86 dplyr::pull(td6, std.error),
87 as.numeric(lfe:::summary.felm(fit2, robust = TRUE)$coef[, "Robust s.e"])
88 )
89 expect_equal(
90 dplyr::pull(td8, std.error),
91 as.numeric(lfe:::summary.felm(fit3)$coef[, "Cluster s.e."])
92 )
93 expect_equal(
94 dplyr::pull(td9, std.error),
95 as.numeric(lfe:::summary.felm(fit3, robust = FALSE)$coef[, "Std. Error"])
96 )
97
98 # check for deprecation warning from 0.7.0.9001
99 expect_snapshot(.res <- tidy(fit, robust = TRUE))
100 })
101
102 test_that("glance.felm", {
103 gl <- glance(fit)
104 gl2 <- glance(fit2)
105
106 check_glance_outputs(gl, gl2)
107 check_dims(gl, expected_cols = 8)
108
109 expect_snapshot(error = TRUE, glance(fit_multi))
110 })
111
112 test_that("augment.felm", {
113 suppressWarnings(
114 check_augment_function(
115 aug = augment.felm,
116 model = fit,
117 data = df
118 )
119 )
120
121 suppressWarnings(
122 check_augment_function(
123 aug = augment.felm,
124 model = fit2,
125 data = df
126 )
127 )
128
129 suppressWarnings(
130 check_augment_function(
131 aug = augment.felm,
132 model = fit_form,
133 data = df
134 )
135 )
136
137 expect_snapshot(error = TRUE, augment(fit_multi))
138
139 # Ensure that the .resid and .fitted columns are basic columns, not matrix
140 aug <- augment(fit)
141 expect_false(inherits(aug$.resid, "matrix"))
142 expect_false(inherits(aug$.fitted, "matrix"))
143 expect_null(c(colnames(aug$.resid), colnames(aug$.fitted)))
144 })
145
146 test_that("tidy.felm errors informatively", {
147 fit <- lfe::felm(v2 ~ v3, df)
148
149 expect_snapshot(
150 .res <- tidy.felm(fit, se.type = "cluster")
151 )
152 })