lrberge / fixest

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

Standard Errors different from glm #459

Closed StarryCatte closed 6 months ago

StarryCatte commented 6 months ago

I tried to compare the estimates from fepois() and glm() in MASS package. I have found that the standard errors are slightly different. Do you know why this happened?

library(fixest)
library(MASS)

data(mtcars)

mod_fx <- fepois(hp ~ drat+wt, data=mtcars)
summary(mod_fx)

# Poisson estimation, Dep. Var.: hp
# Observations: 32 
# Standard-errors: IID 
#              Estimate Std. Error   z value  Pr(>|z|)    
# (Intercept)  4.065294   0.202043 20.120981 < 2.2e-16 ***
# drat        -0.015040   0.041544 -0.362028   0.71733    
# wt           0.290530   0.020483 14.183925 < 2.2e-16 ***
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Log-Likelihood: -380.0   Adj. Pseudo R2: 0.348892
#            BIC:  770.4     Squared Cor.: 0.38007 

mod_glm <- glm(hp ~ drat+wt, data=mtcars,'poisson')
summary(mod_glm)

# Call:
# glm(formula = hp ~ drat + wt, family = "poisson", data = mtcars)

# Deviance Residuals: 
#    Min      1Q  Median      3Q     Max  
# -7.363  -3.135  -1.374   1.050  12.421  

# Coefficients:
#             Estimate Std. Error z value Pr(>|z|)    
# (Intercept)  4.06529    0.19542  20.803   <2e-16 ***
# drat        -0.01504    0.04018  -0.374    0.708    
# wt           0.29053    0.01981  14.665   <2e-16 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

# (Dispersion parameter for poisson family taken to be 1)

#     Null deviance: 958.27  on 31  degrees of freedom
# Residual deviance: 544.90  on 29  degrees of freedom
# AIC: 765.98

# Number of Fisher Scoring iterations: 4
kylebutts commented 6 months ago

The difference between the two is a degrees of freedom correction:

library(fixest)
library(MASS)

data(mtcars)

mod_fx <- fepois(hp ~ drat+wt, data=mtcars)
summary(mod_fx)
#> Poisson estimation, Dep. Var.: hp
#> Observations: 32 
#> Standard-errors: IID 
#>              Estimate Std. Error   t value  Pr(>|t|)    
#> (Intercept)  4.065294   0.202043 20.120981 < 2.2e-16 ***
#> drat        -0.015040   0.041544 -0.362028   0.71733    
#> wt           0.290530   0.020483 14.183925 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> Log-Likelihood: -380.0   Adj. Pseudo R2: 0.348892
#>            BIC:  770.4     Squared Cor.: 0.38007
summary(mod_fx, ssc = ssc(adj= FALSE, cluster.adj = FALSE))
#> Poisson estimation, Dep. Var.: hp
#> Observations: 32 
#> Standard-errors: IID 
#>              Estimate Std. Error   t value  Pr(>|t|)    
#> (Intercept)  4.065294   0.195416 20.803241 < 2.2e-16 ***
#> drat        -0.015040   0.040181 -0.374303   0.70818    
#> wt           0.290530   0.019811 14.664872 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> Log-Likelihood: -380.0   Adj. Pseudo R2: 0.348892
#>            BIC:  770.4     Squared Cor.: 0.38007

mod_glm <- glm(hp ~ drat+wt, data=mtcars,'poisson')
summary(mod_glm)
#> 
#> Call:
#> glm(formula = hp ~ drat + wt, family = "poisson", data = mtcars)
#> 
#> Coefficients:
#>             Estimate Std. Error z value Pr(>|z|)    
#> (Intercept)  4.06529    0.19542  20.803   <2e-16 ***
#> drat        -0.01504    0.04018  -0.374    0.708    
#> wt           0.29053    0.01981  14.665   <2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> (Dispersion parameter for poisson family taken to be 1)
#> 
#>     Null deviance: 958.27  on 31  degrees of freedom
#> Residual deviance: 544.90  on 29  degrees of freedom
#> AIC: 765.98
#> 
#> Number of Fisher Scoring iterations: 4
StarryCatte commented 6 months ago

I see. Thank you so much!