| 1 |
#' @templateVar class felm |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `felm` object returned from [lfe::felm()]. |
| 5 |
#' @template param_confint |
| 6 |
#' @param fe Logical indicating whether or not to include estimates of |
| 7 |
#' fixed effects. Defaults to `FALSE`. |
| 8 |
#' @param se.type Character indicating the type of standard errors. Defaults to |
| 9 |
#' using those of the underlying felm() model object, e.g. clustered errors |
| 10 |
#' for models that were provided a cluster specification. Users can override |
| 11 |
#' these defaults by specifying an appropriate alternative: "iid" (for |
| 12 |
#' homoskedastic errors), "robust" (for Eicker-Huber-White robust errors), or |
| 13 |
#' "cluster" (for clustered standard errors; if the model object supports it). |
| 14 |
#' @template param_unused_dots |
| 15 |
#' |
| 16 |
#' @evalRd return_tidy(regression = TRUE) |
| 17 |
#' |
| 18 |
#' @examplesIf rlang::is_installed("lfe") |
| 19 |
#' |
| 20 |
#' # load libraries for models and data |
| 21 |
#' library(lfe) |
| 22 |
#' |
| 23 |
#' # use built-in `airquality` dataset |
| 24 |
#' head(airquality) |
| 25 |
#' |
| 26 |
#' # no FEs; same as lm() |
| 27 |
#' est0 <- felm(Ozone ~ Temp + Wind + Solar.R, airquality) |
| 28 |
#' |
| 29 |
#' # summarize model fit with tidiers |
| 30 |
#' tidy(est0) |
| 31 |
#' augment(est0) |
| 32 |
#' |
| 33 |
#' # add month fixed effects |
| 34 |
#' est1 <- felm(Ozone ~ Temp + Wind + Solar.R | Month, airquality) |
| 35 |
#' |
| 36 |
#' # summarize model fit with tidiers |
| 37 |
#' tidy(est1) |
| 38 |
#' tidy(est1, fe = TRUE) |
| 39 |
#' augment(est1) |
| 40 |
#' glance(est1) |
| 41 |
#' |
| 42 |
#' # the "se.type" argument can be used to switch out different standard errors |
| 43 |
#' # types on the fly. In turn, this can be useful exploring the effect of |
| 44 |
#' # different error structures on model inference. |
| 45 |
#' tidy(est1, se.type = "iid") |
| 46 |
#' tidy(est1, se.type = "robust") |
| 47 |
#' |
| 48 |
#' # add clustered SEs (also by month) |
| 49 |
#' est2 <- felm(Ozone ~ Temp + Wind + Solar.R | Month | 0 | Month, airquality) |
| 50 |
#' |
| 51 |
#' # summarize model fit with tidiers |
| 52 |
#' tidy(est2, conf.int = TRUE) |
| 53 |
#' tidy(est2, conf.int = TRUE, se.type = "cluster") |
| 54 |
#' tidy(est2, conf.int = TRUE, se.type = "robust") |
| 55 |
#' tidy(est2, conf.int = TRUE, se.type = "iid") |
| 56 |
#' |
| 57 |
#' @export |
| 58 |
#' @aliases felm_tidiers lfe_tidiers |
| 59 |
#' @family felm tidiers |
| 60 |
#' @seealso [tidy()], [lfe::felm()] |
| 61 |
tidy.felm <- function( |
| 62 |
x, |
| 63 |
conf.int = FALSE, |
| 64 |
conf.level = .95, |
| 65 |
fe = FALSE, |
| 66 |
se.type = c("default", "iid", "robust", "cluster"), |
| 67 |
... |
| 68 |
) { |
| 69 |
check_ellipses("exponentiate", "tidy", "felm", ...) |
| 70 |
|
| 71 |
has_multi_response <- length(x$lhs) > 1 |
| 72 |
|
| 73 |
# warn users about deprecated "robust" argument |
| 74 |
dots <- list(...) |
| 75 |
if (!is.null(dots$robust)) { |
| 76 |
cli::cli_warn(c( |
| 77 |
"The {.arg robust} argument has been deprecated in {.fn tidy.felm} and |
| 78 |
will be ignored.", |
| 79 |
"i" = "Please use the {.arg se.type} argument instead." |
| 80 |
)) |
| 81 |
} |
| 82 |
|
| 83 |
# match SE args |
| 84 |
se.type <- match.arg(se.type) |
| 85 |
if (se.type == "default") { |
| 86 |
se.type <- NULL |
| 87 |
} |
| 88 |
|
| 89 |
# get "robust" logical to pass on to summary.lfe |
| 90 |
if (is.null(se.type)) { |
| 91 |
robust <- !is.null(x$clustervar) |
| 92 |
} else if (se.type == "iid") { |
| 93 |
robust <- FALSE |
| 94 |
} else { |
| 95 |
# catch potential user error, asking for clusters where none exist |
| 96 |
if (se.type == "cluster" && is.null(x$clustervar)) { |
| 97 |
cli::cli_warn(c( |
| 98 |
"Clustered SEs requested, but weren't calculated in underlying model object.", |
| 99 |
"i" = "Reverting to default SEs." |
| 100 |
)) |
| 101 |
se.type <- NULL |
| 102 |
} |
| 103 |
|
| 104 |
robust <- TRUE |
| 105 |
} |
| 106 |
|
| 107 |
nn <- c("estimate", "std.error", "statistic", "p.value") |
| 108 |
if (has_multi_response) { |
| 109 |
ret <- map_df(x$lhs, function(y) { |
| 110 |
stats::coef(summary(x, lhs = y, robust = robust)) |> |
| 111 |
as_tidy_tibble(new_names = nn) |> |
| 112 |
mutate(response = y) |
| 113 |
}) |> |
| 114 |
select(response, dplyr::everything()) |
| 115 |
} else { |
| 116 |
ret <- as_tidy_tibble( |
| 117 |
stats::coef(summary(x, robust = robust)), |
| 118 |
new_names = nn |
| 119 |
) |
| 120 |
} |
| 121 |
|
| 122 |
# Catch edge case where users specify "robust" SEs on felm() object that |
| 123 |
# contains clusters. Reason: Somewhat confusingly, summary.felm(robust = TRUE) |
| 124 |
# reports clustered SEs even though robust SEs are available. In contrast, |
| 125 |
# confint.felm distinguishes between robust and clustered SEs regardless |
| 126 |
# of the underlying model. See also: https://github.com/sgaure/lfe/pull/17/files |
| 127 |
if (!is.null(se.type)) { |
| 128 |
if (se.type == "robust" && !is.null(x$clustervar)) { |
| 129 |
ret$std.error <- x$rse |
| 130 |
ret$statistic <- x$rtval |
| 131 |
ret$p.value <- x$rpval |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if (conf.int) { |
| 136 |
if (has_multi_response) { |
| 137 |
ci <- map_df(x$lhs, function(y) { |
| 138 |
broom_confint_terms(x, level = conf.level, type = NULL, lhs = y) |> |
| 139 |
mutate(response = y) |
| 140 |
}) |
| 141 |
ret <- dplyr::left_join(ret, ci, by = c("response", "term")) |
| 142 |
} else { |
| 143 |
ci <- broom_confint_terms(x, level = conf.level, type = se.type) |
| 144 |
ret <- dplyr::left_join(ret, ci, by = "term") |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (fe) { |
| 149 |
ret <- mutate(ret, N = NA, comp = NA) |
| 150 |
|
| 151 |
nn <- c("estimate", "std.error", "N", "comp") |
| 152 |
ret_fe_prep <- lfe::getfe(x, se = TRUE, bN = 100) |> |
| 153 |
tibble::rownames_to_column(var = "term") |> |
| 154 |
# effect and se are multiple if multiple y |
| 155 |
select(term, contains("effect"), contains("se"), obs, comp) |> |
| 156 |
rename(N = obs) |
| 157 |
|
| 158 |
if (has_multi_response) { |
| 159 |
ret_fe_prep <- ret_fe_prep |> |
| 160 |
tidyr::pivot_longer( |
| 161 |
cols = c( |
| 162 |
starts_with("effect."), |
| 163 |
starts_with("se.") |
| 164 |
), |
| 165 |
names_to = "stat_resp", |
| 166 |
values_to = "value" |
| 167 |
) |> |
| 168 |
tidyr::separate( |
| 169 |
col = "stat_resp", |
| 170 |
c("stat", "response"), |
| 171 |
sep = "\\." |
| 172 |
) |> |
| 173 |
tidyr::pivot_wider( |
| 174 |
id_cols = c(term, N, comp, response), |
| 175 |
names_from = stat, |
| 176 |
values_from = value |
| 177 |
) |> |
| 178 |
dplyr::arrange(term) |> |
| 179 |
as.data.frame() |
| 180 |
} |
| 181 |
ret_fe <- ret_fe_prep |> |
| 182 |
rename(estimate = effect, std.error = se) |> |
| 183 |
select(contains("response"), dplyr::everything()) |> |
| 184 |
mutate(statistic = estimate / std.error) |> |
| 185 |
mutate(p.value = 2 * (1 - stats::pt(statistic, df = N))) |
| 186 |
|
| 187 |
if (conf.int) { |
| 188 |
crit_val_low <- stats::qnorm(1 - (1 - conf.level) / 2) |
| 189 |
crit_val_high <- stats::qnorm(1 - (1 - conf.level) / 2) |
| 190 |
|
| 191 |
ret_fe <- ret_fe |> |
| 192 |
mutate( |
| 193 |
conf.low = estimate - crit_val_low * std.error, |
| 194 |
conf.high = estimate + crit_val_high * std.error |
| 195 |
) |
| 196 |
} |
| 197 |
ret <- rbind(ret, ret_fe) |
| 198 |
} |
| 199 |
as_tibble(ret) |
| 200 |
} |
| 201 |
|
| 202 |
#' @templateVar class felm |
| 203 |
#' @template title_desc_augment |
| 204 |
#' |
| 205 |
#' @inherit tidy.felm params examples |
| 206 |
#' @template param_data |
| 207 |
#' |
| 208 |
#' @evalRd return_augment() |
| 209 |
#' |
| 210 |
#' @export |
| 211 |
#' @family felm tidiers |
| 212 |
#' @seealso [augment()], [lfe::felm()] |
| 213 |
augment.felm <- function(x, data = model.frame(x), ...) { |
| 214 |
check_ellipses("newdata", "augment", "felm", ...) |
| 215 |
|
| 216 |
has_multi_response <- length(x$lhs) > 1 |
| 217 |
|
| 218 |
if (has_multi_response) { |
| 219 |
cli::cli_abort( |
| 220 |
"{.fn felm} models with multiple responses are not supported." |
| 221 |
) |
| 222 |
} |
| 223 |
df <- as_augment_tibble(data) |
| 224 |
mutate( |
| 225 |
df, |
| 226 |
.fitted = as.vector(x$fitted.values), |
| 227 |
.resid = as.vector(x$residuals) |
| 228 |
) |
| 229 |
} |
| 230 |
|
| 231 |
#' @templateVar class felm |
| 232 |
#' @template title_desc_glance |
| 233 |
#' |
| 234 |
#' @inherit tidy.felm params examples |
| 235 |
#' |
| 236 |
#' @evalRd return_glance( |
| 237 |
#' "r.squared", |
| 238 |
#' "adj.r.squared", |
| 239 |
#' "sigma", |
| 240 |
#' "statistic", |
| 241 |
#' "p.value", |
| 242 |
#' "df", |
| 243 |
#' "df.residual", |
| 244 |
#' "nobs" |
| 245 |
#' ) |
| 246 |
#' |
| 247 |
#' @export |
| 248 |
glance.felm <- function(x, ...) { |
| 249 |
has_multi_response <- length(x$lhs) > 1 |
| 250 |
|
| 251 |
if (has_multi_response) { |
| 252 |
cli::cli_abort( |
| 253 |
"{.fn felm} models with multiple responses are not supported." |
| 254 |
) |
| 255 |
} |
| 256 |
|
| 257 |
s <- summary(x) |
| 258 |
|
| 259 |
as_glance_tibble( |
| 260 |
r.squared = s$r2, |
| 261 |
adj.r.squared = s$r2adj, |
| 262 |
sigma = s$rse, |
| 263 |
statistic = s$fstat, |
| 264 |
p.value = unname(s$pval), |
| 265 |
df = s$df[1], |
| 266 |
df.residual = s$rdf, |
| 267 |
nobs = stats::nobs(x), |
| 268 |
na_types = "rrrrriii" |
| 269 |
) |
| 270 |
} |