lrberge / fixest

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

Switching between distributions for p-values #477

Closed edrubin closed 4 months ago

edrubin commented 4 months ago

It looks like feols and feols.fit use different distributions (t vs. Normal) when calculating the same p-values.

It's possible this issue relates to #408.

Probably isn't a big deal, but the p-value notation Pr(>|t|) is a bit inconsistent with feols.fit using the Normal distribution.

Example:

# Using feols:
(est_stnd = feols(
  Euros ~ dist_km |
  Year,
  cluster = ~ Destination + Origin + Year,
  data = trade
))
OLS estimation, Dep. Var.: Euros
Observations: 38,325
Fixed-effects: Year: 10
Standard-errors: Clustered (Destination & Origin & Year)
        Estimate Std. Error  t value  Pr(>|t|)
dist_km -53517.3    15435.6 -3.46713 0.0070807 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 134,922,221.0     Adj. R2: 0.074736
                      Within R2: 0.073836
# Using feols.fit:
(est_fit = feols.fit(
  y = trade$Euros,
  X = trade$dist_km,
  fixef_df = trade$Year,
  cluster = trade[, c("Destination", "Origin", "Year")]
))
OLS estimation, Dep. Var.: trade$Euros
Observations: 38,325
Fixed-effects: trade$Year: 10
Standard-errors: Clustered (Destination & Origin & Year)
              Estimate Std. Error  t value   Pr(>|t|)
trade$dist_km -53517.3    15435.6 -3.46713 0.00052604 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 134,922,221.0     Adj. R2: 0.074736
                      Within R2: 0.073836

Note the same t statistics with different standard errors.

Replicating the p-values:

2 * pt(est_stnd$coeftable[1, 't value'], df = est_stnd$cov.scaled |> attr('df.t'))
[1] 0.007080699
2 * pnorm(est_stnd$coeftable[1, 't value'])

[1] 0.0005260413

Thanks for your amazing work!

lrberge commented 4 months ago

Thanks a lot for the clear issue Ed!

I thought I had implemented a robust way to pick the t-distribution when appropriate but... it turns out I made a typo in the code.... Thanks a lot! Now fixed.

edrubin commented 4 months ago

Thanks, Laurent!