| 1 |
skip_on_cran() |
| 2 |
|
| 3 |
skip_if_not_installed("modeltests") |
| 4 |
library(modeltests) |
| 5 |
|
| 6 |
skip_if_not_installed("fixest") |
| 7 |
fixest::setFixest_nthreads(1) # avoid warnings about requesting more threads |
| 8 |
|
| 9 |
set.seed(27) |
| 10 |
n <- 100 |
| 11 |
df <- data.frame( |
| 12 |
id = sample(5, n, TRUE), |
| 13 |
v1 = sample(5, n, TRUE), |
| 14 |
v2 = sample(1e6, n, TRUE), |
| 15 |
v3 = sample(round(runif(100, max = 100), 4), n, TRUE), |
| 16 |
v4 = sample(round(runif(100, max = 100), 4), n, TRUE) |
| 17 |
) |
| 18 |
|
| 19 |
fit <- fixest::feols(v2 ~ v3, df) |
| 20 |
fit2 <- fixest::feols(v2 ~ v3 | id + v1, df) |
| 21 |
|
| 22 |
form <- v2 ~ v4 |
| 23 |
fit_form <- fixest::feols(form, df) # part of a regression test |
| 24 |
|
| 25 |
test_that("fixest tidier arguments", { |
| 26 |
check_arguments(tidy.fixest) |
| 27 |
check_arguments(glance.fixest) |
| 28 |
check_arguments(augment.fixest) |
| 29 |
}) |
| 30 |
|
| 31 |
test_that("bug #1018: fixed effects but no regressors", { |
| 32 |
mod <- fixest::feols(Sepal.Length ~ 1 | Species, data = iris) |
| 33 |
check_tidy_output(tidy(mod)) |
| 34 |
check_glance_outputs(glance(mod)) |
| 35 |
}) |
| 36 |
|
| 37 |
test_that("tidy.fixest", { |
| 38 |
td <- tidy(fit) |
| 39 |
td2 <- tidy(fit2, conf.int = TRUE) |
| 40 |
td3 <- tidy(fit2, conf.int = TRUE, se = "hetero") |
| 41 |
td4 <- tidy(fit_form) |
| 42 |
|
| 43 |
check_tidy_output(td) |
| 44 |
check_tidy_output(td2) |
| 45 |
check_tidy_output(td3) |
| 46 |
check_tidy_output(td4) |
| 47 |
|
| 48 |
check_dims(td, 2, 5) |
| 49 |
}) |
| 50 |
|
| 51 |
test_that("glance.fixest", { |
| 52 |
gl <- glance(fit) |
| 53 |
gl2 <- glance(fit2) |
| 54 |
|
| 55 |
check_glance_outputs(gl, gl2) |
| 56 |
check_dims(gl, expected_cols = 9) |
| 57 |
}) |
| 58 |
|
| 59 |
test_that("augment.fixest", { |
| 60 |
check_augment_function( |
| 61 |
aug = augment.fixest, |
| 62 |
model = fit, |
| 63 |
data = df, |
| 64 |
newdata = df, |
| 65 |
strict = FALSE |
| 66 |
) |
| 67 |
|
| 68 |
check_augment_function( |
| 69 |
aug = augment.fixest, |
| 70 |
model = fit2, |
| 71 |
data = df, |
| 72 |
newdata = df, |
| 73 |
strict = FALSE |
| 74 |
) |
| 75 |
|
| 76 |
check_augment_function( |
| 77 |
aug = augment.fixest, |
| 78 |
model = fit_form, |
| 79 |
data = df, |
| 80 |
newdata = df, |
| 81 |
strict = FALSE |
| 82 |
) |
| 83 |
}) |
| 84 |
|
| 85 |
test_that("all other fixest estimators run", { |
| 86 |
skip_on_cran() |
| 87 |
skip_on_ci() |
| 88 |
|
| 89 |
form <- v2 ~ v4 | id |
| 90 |
res_feglm <- fixest::feglm(form, data = df, family = "poisson") |
| 91 |
res_fenegbin <- fixest::fenegbin(form, data = df) |
| 92 |
res_feNmlm <- fixest::feNmlm(form, data = df) |
| 93 |
res_femlm <- fixest::femlm(form, data = df) |
| 94 |
res_fepois <- fixest::fepois(form, data = df) |
| 95 |
|
| 96 |
# Tidy |
| 97 |
check_tidy_output(tidy(res_feglm)) |
| 98 |
check_tidy_output(tidy(res_fenegbin)) |
| 99 |
check_tidy_output(tidy(res_feNmlm)) |
| 100 |
check_tidy_output(tidy(res_femlm)) |
| 101 |
check_tidy_output(tidy(res_fepois)) |
| 102 |
|
| 103 |
# Glance |
| 104 |
check_glance_outputs( |
| 105 |
glance(res_feglm), |
| 106 |
glance(res_fenegbin), |
| 107 |
glance(res_feNmlm), |
| 108 |
glance(res_femlm), |
| 109 |
glance(res_fepois) |
| 110 |
) |
| 111 |
|
| 112 |
# augment |
| 113 |
# Note this this causes warnings with strict=TRUE because |
| 114 |
# modeltests:::acceptable_augment_colnames calls model.frame, which doesn't |
| 115 |
# work for fixest models. |
| 116 |
check_augment_function( |
| 117 |
aug = augment.fixest, |
| 118 |
model = res_feglm, |
| 119 |
data = df, |
| 120 |
newdata = df, |
| 121 |
strict = FALSE |
| 122 |
) |
| 123 |
check_augment_function( |
| 124 |
aug = augment.fixest, |
| 125 |
model = res_femlm, |
| 126 |
data = df, |
| 127 |
newdata = df, |
| 128 |
strict = FALSE |
| 129 |
) |
| 130 |
|
| 131 |
expect_snapshot(error = TRUE, augment(res_fenegbin)) |
| 132 |
|
| 133 |
expect_snapshot(error = TRUE, augment(res_fenegbin, df)) |
| 134 |
expect_snapshot(error = TRUE, augment(res_feNmlm, df)) |
| 135 |
expect_snapshot(error = TRUE, augment(res_fepois, df)) |
| 136 |
}) |
| 137 |
|
| 138 |
|
| 139 |
test_that("tidiers work with model results or summary of model results", { |
| 140 |
skip_on_cran() |
| 141 |
skip_on_ci() |
| 142 |
|
| 143 |
# Default standard errors are clustered by `id`. Test against non-default |
| 144 |
# independent, heteroskedastic ("hetero") standard errors. |
| 145 |
fit2_summ <- summary(fit2, se = "hetero") |
| 146 |
expect_equal(tidy(fit2, se = "hetero"), tidy(fit2_summ)) |
| 147 |
expect_equal( |
| 148 |
tidy(fit2, se = "hetero", conf.int = TRUE), |
| 149 |
tidy(fit2_summ, conf.int = TRUE) |
| 150 |
) |
| 151 |
expect_equal(glance(fit2, se = "hetero"), glance(fit2_summ)) |
| 152 |
expect_equal(augment(fit2, df, se = "hetero"), augment(fit2_summ, df)) |
| 153 |
|
| 154 |
# Repeat for feglm |
| 155 |
res_glm <- fixest::feglm(v2 ~ v4 | id, data = df, family = "poisson") |
| 156 |
res_glm_summ <- summary( |
| 157 |
fixest::feglm(v2 ~ v4 | id, data = df, family = "poisson"), |
| 158 |
se = "hetero" |
| 159 |
) |
| 160 |
expect_equal(tidy(res_glm, se = "hetero"), tidy(res_glm_summ)) |
| 161 |
expect_equal( |
| 162 |
as.data.frame(tidy(res_glm, se = "hetero", conf.int = TRUE)), |
| 163 |
as.data.frame(tidy(res_glm_summ, conf.int = TRUE)) |
| 164 |
) |
| 165 |
expect_equal(glance(res_glm, se = "hetero"), glance(res_glm_summ)) |
| 166 |
expect_equal(augment(res_glm, df, se = "hetero"), augment(res_glm_summ, df)) |
| 167 |
|
| 168 |
# We rely on behavior from fixest where summary.fixest() doesn't change the |
| 169 |
# `se` or `dof` arguments if they've already been set. |
| 170 |
# Test that claim here. (Calling summary again does change other things, like |
| 171 |
# the `summary_from_fit` flag, so we can't just expect_equal the whole object) |
| 172 |
expect_false(is.null(fit2_summ$coeftable)) |
| 173 |
expect_equal(fit2_summ$coeftable, summary(fit2_summ)$coeftable) |
| 174 |
expect_equal(summary(fit)$coeftable, summary(summary(fit))$coeftable) |
| 175 |
}) |