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