lrberge / fixest

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

Tweedie Distribution from glm package #423

Closed alfaromartino closed 7 months ago

alfaromartino commented 1 year ago

Is there any plan to add the family of Tweedie distribution? For instance, this R package does it for glm (but you can't use high dimensional fixed effects)

https://search.r-project.org/CRAN/refmans/statmod/html/tweedie.html

lrberge commented 7 months ago

Hi, it works out of the box... Anything glm can do actually:

library(statmod)
y <- rgamma(20,shape=5)
x <- 1:20

# Fit a poisson generalized linear model with identity link
summary(glm(y ~ x, family = tweedie(var.power = 1, link.power = 1)))
#> Call:
#> glm(formula = y ~ x, family = tweedie(var.power = 1, link.power = 1))
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)   
#> (Intercept)   3.9798     1.1394   3.493   0.0026 **
#> x             0.1368     0.1015   1.349   0.1942
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> (Dispersion parameter for Tweedie family taken to be 1.285689)
#> 
#>     Null deviance: 22.832  on 19  degrees of freedom
#> Residual deviance: 20.855  on 18  degrees of freedom
#> AIC: NA
#> 
#> Number of Fisher Scoring iterations: 6

base = data.frame(x, y)
feglm(y ~ x, base, vcov = iid ~ ssc(adj = FALSE),
      family = tweedie(var.power = 1, link.power = 1))
#> GLM estimation, family = tweedie(var.power = 1, link.power = 1)
#> Dep. Var.: y
#> Observations: 20
#> Standard-errors: IID
#>             Estimate Std. Error t value  Pr(>|t|)
#> (Intercept) 3.979770   1.139380 3.49293 0.0025972 **
#> x           0.136822   0.101452 1.34864 0.1941785
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#>   Squared Cor.: 0.072159

#
# With FEs
#

base$fe = 1:2
feglm(y ~ x | fe, base, vcov = iid ~ ssc(adj = FALSE),
      family = tweedie(var.power = 1, link.power = 1))
#> GLM estimation, family = tweedie(var.power = 1, link.power = 1)
#> Dep. Var.: y
#> Observations: 20
#> Fixed-effects: fe: 2
#> Standard-errors: IID
#>   Estimate Std. Error t value Pr(>|t|)
#> x 0.140294   0.097659 1.43657  0.16899
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#>   Squared Cor.: 0.162488