lrberge / fixest

Fixed-effects estimations
https://lrberge.github.io/fixest/
362 stars 59 forks source link

Use t-distribution for `coefplot`/`iplot` CIs #408

Closed grantmcdermott closed 10 months ago

grantmcdermott commented 1 year ago

Almost all of the dedicated fixest methods that yield confidence intervals do so using a t-distribution. (See also https://github.com/lrberge/fixest/issues/362#issuecomment-1275200731.) A notable exception can be found in the coefplot/iplot functions, which currently use a normal distribution:

https://github.com/lrberge/fixest/blob/d8fb28abb9cca39da28697ec705a1a10169423d7/R/coefplot.R#L1660-L1661

The importance of this difference obviously diminishes with the size of the dataset (since DoF -> Inf). But it does create an awkward situation where the figures from coefplot/iplot don't necessarily correspond to the results from, say, etable.

This PR brings the CIs from coefplot and iplot into alignment with the other fixest methods. I've tried to include some catches so that, if it can't extract the right DoF from the model object for some reason, it will default to Inf and thus effectively revert back to a normal distribution.

Here is the current behaviour (fixest 0.11.1):

library(fixest)

base = setNames(iris, c("y", "x1", "x2", "x3", "species"))
est = feols(y ~ x1 + x2 | species, base)

confint(est)
#>         2.5 %   97.5 %
#> x1 -0.2618362 1.126271
#> x2  0.2311479 1.320111
coefplot(est, only.params = TRUE)$prms[, 2:3]
#>       ci_low   ci_high
#> x1 0.1160589 0.7483756
#> x2 0.5276048 1.0236542

Created on 2023-04-02 with reprex v2.0.2

And the same code following this PR:

devtools::load_all("~/Documents/Projects/fixest")
#> ℹ Loading fixest

base = setNames(iris, c("y", "x1", "x2", "x3", "species"))
est = feols(y ~ x1 + x2 | species, base)

confint(est)
#>         2.5 %   97.5 %
#> x1 -0.2618362 1.126271
#> x2  0.2311479 1.320111
coefplot(est, only.params = TRUE)$prms[, 2:3]
#>        ci_low  ci_high
#> x1 -0.2618362 1.126271
#> x2  0.2311479 1.320111

Created on 2023-04-02 with reprex v2.0.2