tidymodels / poissonreg

parsnip wrappers for Poisson regression
https://poissonreg.tidymodels.org
Other
22 stars 4 forks source link

Support for negative binomial models #46

Closed mattwarkentin closed 2 years ago

mattwarkentin commented 2 years ago

Hi,

Will this package (or an adjacent package) ever consider support for negative binomial, zero-inflated negative binomial, and hurdle negative binomial models?

I think it would be somewhat straightforward to implement using stats::glm(dist = MASS::negative.binomial()) / MASS::glm.nb(), pscl::zeroinfl(dist = 'negbin'), and pscl::hurdle(dist = 'negbin'). Would you be open to a PR?

hfrick commented 2 years ago

Maybe this is already possible: is this what you're looking for?

library(poissonreg)
#> Loading required package: parsnip
data("bioChemists", package = "pscl")

poisson_reg() %>% 
  set_engine("glm", family = MASS::negative.binomial(2)) %>% 
  fit(art ~ ., data = bioChemists)
#> parsnip model object
#> 
#> 
#> Call:  stats::glm(formula = art ~ ., family = ~MASS::negative.binomial(2), 
#>     data = data)
#> 
#> Coefficients:
#> (Intercept)     femWomen   marMarried         kid5          phd         ment  
#>     0.25294     -0.21589      0.15041     -0.17608      0.01560      0.02926  
#> 
#> Degrees of Freedom: 914 Total (i.e. Null);  909 Residual
#> Null Deviance:       1062 
#> Residual Deviance: 963.1     AIC: 3135

poisson_reg() %>% 
  set_engine("zeroinfl", dist = 'negbin') %>% 
  fit(art ~ ., data = bioChemists)
#> parsnip model object
#> 
#> 
#> Call:
#> pscl::zeroinfl(formula = art ~ ., data = data, dist = ~"negbin")
#> 
#> Count model coefficients (negbin with log link):
#> (Intercept)     femWomen   marMarried         kid5          phd         ment  
#>   0.4167466   -0.1955076    0.0975826   -0.1517321   -0.0006998    0.0247861  
#> Theta = 2.6548 
#> 
#> Zero-inflation model coefficients (binomial with logit link):
#> (Intercept)     femWomen   marMarried         kid5          phd         ment  
#>    -0.19161      0.63587     -1.49944      0.62841     -0.03773     -0.88227

poisson_reg() %>% 
  set_engine("hurdle", dist = 'negbin') %>% 
  fit(art ~ ., data = bioChemists)
#> parsnip model object
#> 
#> 
#> Call:
#> pscl::hurdle(formula = art ~ ., data = data, dist = ~"negbin")
#> 
#> Count model coefficients (truncated negbin with log link):
#> (Intercept)     femWomen   marMarried         kid5          phd         ment  
#>    0.355125    -0.244672     0.103417    -0.153260    -0.002933     0.023738  
#> Theta = 1.8285 
#> 
#> Zero hurdle model coefficients (binomial with logit link):
#> (Intercept)     femWomen   marMarried         kid5          phd         ment  
#>     0.23680     -0.25115      0.32623     -0.28525      0.02222      0.08012

Created on 2022-09-02 by the reprex package (v2.0.1)

mattwarkentin commented 2 years ago

Thanks for the reply, @hfrick. You're right, all of the models seem to be achievable with existing infrastructure.

I suppose I am just wondering whether it makes sense for there to be a standalone model specification like negbin_reg() which ostensibly does what you've shown in the first example above, with support for zero-inflated and hurdle variants simply by changing engines.

Happy to close the issue if there isn't interest in a standalone specification given that these models are already available. If there is interest, I am always happy to contribute a PR.

mattwarkentin commented 2 years ago

Upon further inspection it seems like the engines for poisson_reg() are spread across multiple packages (poissonreg, agua, multilevelmod).

So if negative binomial models are supported by those engines just by changing an engine-specific argument, that probably seems better than having to contribute 3 PRs. Happy to close this issue unless you think it is sensible to have a model specification specifically for negative binomial models.

github-actions[bot] commented 1 year ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.