| 1 |
#' @templateVar class fixest |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `fixest` object returned from any of the `fixest` estimators |
| 5 |
#' @template param_confint |
| 6 |
#' @param ... Additional arguments passed to `summary` and `confint`. Important |
| 7 |
#' arguments are `se` and `cluster`. Other arguments are `dof`, `exact_dof`, |
| 8 |
#' `forceCovariance`, and `keepBounded`. |
| 9 |
#' See [`summary.fixest`][fixest::summary.fixest()]. |
| 10 |
#' @evalRd return_tidy(regression = TRUE) |
| 11 |
#' |
| 12 |
#' @details The `fixest` package provides a family of functions for estimating |
| 13 |
#' models with arbitrary numbers of fixed-effects, in both an OLS and a GLM |
| 14 |
#' context. The package also supports robust (i.e. White) and clustered |
| 15 |
#' standard error reporting via the generic `summary.fixest()` command. In a |
| 16 |
#' similar vein, the `tidy()` method for these models allows users to specify |
| 17 |
#' a desired standard error correction either 1) implicitly via the supplied |
| 18 |
#' fixest object, or 2) explicitly as part of the tidy call. See examples |
| 19 |
#' below. |
| 20 |
#' |
| 21 |
#' Note that fixest confidence intervals are calculated assuming a normal |
| 22 |
#' distribution -- this assumes infinite degrees of freedom for the CI. |
| 23 |
#' (This assumption is distinct from the degrees of freedom used to calculate |
| 24 |
#' the standard errors. For more on degrees of freedom with clusters and |
| 25 |
#' fixed effects, see \url{https://github.com/lrberge/fixest/issues/6} and |
| 26 |
#' \url{https://github.com/sgaure/lfe/issues/1#issuecomment-530646990}) |
| 27 |
#' |
| 28 |
#' @examplesIf rlang::is_installed("fixest") & !broom:::is_cran_check() |
| 29 |
#' |
| 30 |
#' # load libraries for models and data |
| 31 |
#' library(fixest) |
| 32 |
#' |
| 33 |
#' gravity <- |
| 34 |
#' feols( |
| 35 |
#' log(Euros) ~ log(dist_km) | Origin + Destination + Product + Year, trade |
| 36 |
#' ) |
| 37 |
#' |
| 38 |
#' tidy(gravity) |
| 39 |
#' glance(gravity) |
| 40 |
#' augment(gravity, trade) |
| 41 |
#' |
| 42 |
#' # to get robust or clustered SEs, users can either: |
| 43 |
#' |
| 44 |
#' # 1) specify the arguments directly in the `tidy()` call |
| 45 |
#' |
| 46 |
#' tidy(gravity, conf.int = TRUE, cluster = c("Product", "Year")) |
| 47 |
#' |
| 48 |
#' tidy(gravity, conf.int = TRUE, se = "threeway") |
| 49 |
#' |
| 50 |
#' # 2) or, feed tidy() a summary.fixest object that has already accepted |
| 51 |
#' # these arguments |
| 52 |
#' |
| 53 |
#' gravity_summ <- summary(gravity, cluster = c("Product", "Year")) |
| 54 |
#' |
| 55 |
#' tidy(gravity_summ, conf.int = TRUE) |
| 56 |
#' |
| 57 |
#' # approach (1) is preferred. |
| 58 |
#' |
| 59 |
#' @export |
| 60 |
#' @family fixest tidiers |
| 61 |
#' @seealso [tidy()], [fixest::feglm()], [fixest::fenegbin()], |
| 62 |
#' [fixest::feNmlm()], [fixest::femlm()], [fixest::feols()], [fixest::fepois()] |
| 63 |
tidy.fixest <- function(x, conf.int = FALSE, conf.level = 0.95, ...) { |
| 64 |
check_ellipses("exponentiate", "tidy", "fixest", ...) |
| 65 |
|
| 66 |
coeftable <- summary(x, ...)$coeftable |
| 67 |
ret <- as_tibble(coeftable, rownames = "term") |
| 68 |
colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value") |
| 69 |
if (conf.int) { |
| 70 |
CI <- stats::confint(x, level = conf.level, ...) |
| 71 |
# Bind to rest of tibble |
| 72 |
colnames(CI) <- c("conf.low", "conf.high") |
| 73 |
ret <- bind_cols(ret, unrowname(CI)) |
| 74 |
} |
| 75 |
as_tibble(ret) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
#' @templateVar class fixest |
| 80 |
#' @template title_desc_augment |
| 81 |
#' |
| 82 |
#' @inherit tidy.fixest params examples |
| 83 |
#' @template param_data |
| 84 |
#' @template param_newdata |
| 85 |
#' @param type.predict Passed to [`predict.fixest`][fixest::predict.fixest()] |
| 86 |
#' `type` argument. Defaults to `"link"` (like `predict.glm`). |
| 87 |
#' @param type.residuals Passed to [`predict.fixest`][fixest::residuals.fixest()] |
| 88 |
#' `type` argument. Defaults to `"response"` (like `residuals.lm`, but unlike |
| 89 |
#' `residuals.glm`). |
| 90 |
#' @evalRd return_augment() |
| 91 |
#' |
| 92 |
#' @note Important note: `fixest` models do not include a copy of the input |
| 93 |
#' data, so you must provide it manually. |
| 94 |
#' |
| 95 |
#' augment.fixest only works for [fixest::feols()], [fixest::feglm()], and |
| 96 |
#' [fixest::femlm()] models. It does not work with results from |
| 97 |
#' [fixest::fenegbin()], [fixest::feNmlm()], or [fixest::fepois()]. |
| 98 |
#' @export |
| 99 |
#' @family fixest tidiers |
| 100 |
#' @seealso [augment()], [fixest::feglm()], [fixest::femlm()], [fixest::feols()] |
| 101 |
augment.fixest <- function( |
| 102 |
x, |
| 103 |
data = NULL, |
| 104 |
newdata = NULL, |
| 105 |
type.predict = c("link", "response"), |
| 106 |
type.residuals = c("response", "deviance", "pearson", "working"), |
| 107 |
... |
| 108 |
) { |
| 109 |
if (!x$method %in% c("feols", "feglm", "femlm")) { |
| 110 |
cli::cli_abort(c( |
| 111 |
"augment is only supported for fixest models estimated with |
| 112 |
{.fn feols}, {.fn feglm}, or {.fn femlm}.", |
| 113 |
"i" = "Supplied model used {.fn {x$method}}." |
| 114 |
)) |
| 115 |
} |
| 116 |
type.predict <- match.arg(type.predict) |
| 117 |
type.residuals <- match.arg(type.residuals) |
| 118 |
|
| 119 |
if (is.null(newdata)) { |
| 120 |
df <- data |
| 121 |
} else { |
| 122 |
df <- newdata |
| 123 |
} |
| 124 |
if (is.null(df)) { |
| 125 |
data_error() |
| 126 |
} |
| 127 |
df <- as_augment_tibble(df) |
| 128 |
if (is.null(newdata)) { |
| 129 |
# use existing data |
| 130 |
df$.fitted <- predict(x, type = type.predict) |
| 131 |
df$.resid <- residuals(x, type = type.residuals) |
| 132 |
} else { |
| 133 |
# With new data, only provide predictions |
| 134 |
df$.fitted <- predict(x, type = type.predict, newdata = newdata) |
| 135 |
} |
| 136 |
df |
| 137 |
} |
| 138 |
|
| 139 |
#' @templateVar class fixest |
| 140 |
#' @template title_desc_glance |
| 141 |
#' |
| 142 |
#' @inherit tidy.fixest params examples |
| 143 |
#' |
| 144 |
#' @note All columns listed below will be returned, but some will be `NA`, |
| 145 |
#' depending on the type of model estimated. `sigma`, `r.squared`, |
| 146 |
#' `adj.r.squared`, and `within.r.squared` will be NA for any model other than |
| 147 |
#' `feols`. `pseudo.r.squared` will be NA for `feols`. |
| 148 |
#' @evalRd return_glance( |
| 149 |
#' "r.squared", |
| 150 |
#' "adj.r.squared", |
| 151 |
#' "within.r.squared", |
| 152 |
#' "pseudo.r.squared", |
| 153 |
#' "sigma", |
| 154 |
#' "nobs", |
| 155 |
#' "AIC", |
| 156 |
#' "BIC", |
| 157 |
#' "logLik" |
| 158 |
#' ) |
| 159 |
#' @export |
| 160 |
glance.fixest <- function(x, ...) { |
| 161 |
stopifnot(length(x$method) == 1) |
| 162 |
# results that are common to all models: |
| 163 |
res_common <- as_glance_tibble( |
| 164 |
logLik = logLik(x), |
| 165 |
AIC = AIC(x), |
| 166 |
BIC = BIC(x), |
| 167 |
nobs = nobs(x), |
| 168 |
na_types = "rrri" |
| 169 |
) |
| 170 |
|
| 171 |
if (identical(x$method, "feols")) { |
| 172 |
r2_types <- c("r2", "ar2", "wr2") |
| 173 |
r2_vals <- purrr::map_dbl(r2_types, fixest::r2, x = x) |> |
| 174 |
purrr::set_names(r2_types) |
| 175 |
r2_names <- c("r.squared", "adj.r.squared", "within.r.squared") |
| 176 |
# Pull the summary objects that are specific to OLS |
| 177 |
res_specific <- with( |
| 178 |
summary(x, ...), |
| 179 |
tibble( |
| 180 |
# catch error in models with only fixed effects and no regressors |
| 181 |
sigma = tryCatch(sqrt(sigma2), error = function(e) NA_real_), |
| 182 |
pseudo.r.squared = NA_real_, # always NA for OLS |
| 183 |
) |
| 184 |
) |
| 185 |
} else { |
| 186 |
# Note that calculating within R2 is expensive for non-OLS models: doing so |
| 187 |
# requires re-estimating the model, so don't do it. |
| 188 |
# Also, these are now pseudo R2. Could use adjusted pseudo R2, but that |
| 189 |
# isn't supported by modeltests, and seems niche. |
| 190 |
r2_vals <- fixest::r2(x, type = "pr2") |
| 191 |
r2_names <- "pseudo.r.squared" |
| 192 |
|
| 193 |
# Currently no other stats for non-OLS models (beyond pseudo R2, logLik, |
| 194 |
# AIC, BIC, and nobs), but you could add them if you wanted. |
| 195 |
# Fill in the sigma and other R2 values with NA. |
| 196 |
res_specific <- tibble( |
| 197 |
sigma = NA_real_, |
| 198 |
r.squared = NA_real_, |
| 199 |
adj.r.squared = NA_real_, |
| 200 |
within.r.squared = NA_real_, |
| 201 |
) |
| 202 |
} |
| 203 |
# Some of these will be NA, depending on the model, but for consistency we'll |
| 204 |
# always return all four R2 values. |
| 205 |
names(r2_vals) <- r2_names |
| 206 |
res_r2 <- tibble(!!!r2_vals) |
| 207 |
col_order <- c( |
| 208 |
"r.squared", |
| 209 |
"adj.r.squared", |
| 210 |
"within.r.squared", |
| 211 |
"pseudo.r.squared", |
| 212 |
"sigma", |
| 213 |
"nobs", |
| 214 |
"AIC", |
| 215 |
"BIC", |
| 216 |
"logLik" |
| 217 |
) |
| 218 |
res <- bind_cols(res_common, res_r2, res_specific) |> |
| 219 |
select(dplyr::any_of(col_order)) |
| 220 |
res |
| 221 |
} |