-
Notifications
You must be signed in to change notification settings - Fork 117
Stop considering that Negative Binomial has a dispersion parameter #624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #624 +/- ##
=======================================
Coverage 96.94% 96.95%
=======================================
Files 8 8
Lines 1213 1216 +3
=======================================
+ Hits 1176 1179 +3
Misses 37 37 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
The two cases here are different, and I think it is best to handle them in separate PRs. |
This is just incorrect. Fix tests to reflect correct degrees of freedom and therefore AIC, BIC and standard errors. Also avoid defining `dispersion_parameter` fallback for any distribution: it's clearer for users to get a `MethodError` so that they have to define the appropriate method themselves, than silently returning a wrong value. `NegativeBinomial` will be fixed separately (#624).
This is just incorrect. Fix tests to reflect correct degrees of freedom and therefore AIC, BIC and standard errors. Also avoid defining `dispersion_parameter` fallback for any distribution: it's clearer for users to get a `MethodError` so that they have to define the appropriate method themselves, than silently returning a wrong value. `NegativeBinomial` will be fixed separately as it requires a deeper refactoring of `negbin` (#624).
|
It's hard to fix the two cases completely separately as we have tests checking that geometric and negative binomial give the same results, but we can leave some tests as broken temporarily. Let's start by fixing |
This is incorrect when the shape parameter has been fixed manually and the model estimated via `glm`. Fix tests to reflect correct degrees of freedom and therefore AIC, BIC and standard errors. This requires adapting how `negbin` works as we need to count one additional degree of freedom due to the shape parameter (distinct from the dispersion, which is 1) being estimated from the data. Do this by returning a `NegativeBinomialModel` instead of a standard GLM.
2bc8e3a to
3693b3c
Compare
|
I've updated the PR so that it sits on top of #633 and introduced a (CI doesn't run for some reason, but maybe that will work once I base PR on master.) |
This is just incorrect. Fix tests to reflect correct degrees of freedom and therefore AIC, BIC and standard errors. Also avoid defining `dispersion_parameter` fallback for any distribution: it's clearer for users to get a `MethodError` so that they have to define the appropriate method themselves, than silently returning a wrong value. `NegativeBinomial` will be fixed separately as it requires a deeper refactoring of `negbin` (#624).
| For generalized linear models, consumed degrees of freedom correspond | ||
| to the number of estimated coefficients, plus one for the estimated | ||
| dispersion parameter if the distribution is `Gamma`, `InverseGaussian` or `Normal` | ||
| (see [GLM.dispersion_parameter](@ref)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should just define this as the number of estimated parameters in the model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure it is useful to loosen the signature from GeneralizedLinearModel to AbstractGLM? Isn't the only type that is an AbstractGLM but not a GeneralizedLinearModel the new NegativeBinomialModel for which there is a separate model?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should just define this as the number of estimated parameters in the model.
That's actually the definition in the generic docstring in StatsModels: "Return the number of degrees of freedom consumed in the model, including when applicable the intercept and the distribution's dispersion parameter." Here I was trying to be a bit more specific about what this means for different distributions.
Are you sure it is useful to loosen the signature from GeneralizedLinearModel to AbstractGLM? Isn't the only type that is an
AbstractGLMbut not aGeneralizedLinearModelthe newNegativeBinomialModelfor which there is a separate model?
It's not really required for this method, but I figured it would be better as it's consistent with what we do for all other methods. I don't have a strong opinion.
| generally written σ for linear models and ϕ for generalized linear models. | ||
| It is, by definition, equal to 1 for the Bernoulli, Binomial, and Poisson families. | ||
|
|
||
| It is, by definition, equal to 1 for the Bernoulli, Binomial, Geometric, Negative |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is correct to say that it is one for the negative binomial. As I understand McCullagh and Nelder, the proper dispersion parameter is only defined in the case when the variance can be written
in which case
so
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. For fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So say "Negative Binomial (with fixed θ)"?
| @@ -1,3 +1,25 @@ | |||
| mutable struct NegativeBinomialModel{G<:GlmResp,L<:LinPred} <: AbstractGLM | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just wrap a GeneralizedLinearModel instead of repeating the definition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With a getproperty override to delegate field accesses? Doesn't really seem simpler in the end?
| end | ||
|
|
||
| """ | ||
| dof(model::GeneralizedLinearModel) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| dof(model::GeneralizedLinearModel) | |
| dof(model::LinearModel) |
This is just incorrect. Fix tests to reflect correct degrees of freedom and therefore AIC, BIC and standard errors.
This requires adapting how
negbinworks as we need to count one additional degree of freedom due to the shape parameter (distinct from the dispersion, which is 1) being estimated from the data. Do this by wrapping the shape parameter in anEstimatedtype that we use to detect that degrees of freedom need adjusting.Also avoid defining
dispersion_parameterfallback for any distribution: it's clearer for users to get aMethodErrorso that they have to define the appropriate method themselves, than silently returning a wrong value.Fixes #564, #617.
I have tentatively implemented the first of three possible approaches I see to the
negbinissue to illustrate how it looks:NegativeBinomialin anEstimatedtype: I find this is somewhat elegant. The disadvantage is that users can't really work with that parameter if they extract it. We could define more methods on this type but that would be weird anyway. We'd better provide an accessor which would unwrap it, which is needed anyway.GeneralizedLinearModel(or one of its component) indicating that one parameter was estimated. This has the advantage of being super simple. The drawback is that its a bit ad-hoc but in practice it probably doesn't matter.negbinreturn aNegativeBinomialModel <: GLMLikeModel <: LinPredModel. This has the advantage of clarity (in R alsoglm.nbreturns an object of a different class) since these are not really GLMs. In terms of implementation I'm not sure whether it would be easy (just replaceAbstractGLMwithGLMLikeModelfor most methods) or not.Opinions on the best approach?
Note that R's glm seems broken with
negative.binomialas it estimates a dispersion parameter even if it's 1 by theory. You have to dosummary(m, dispersion=1)to get the correct standard errors. Yet?negative.binomialclaims it's meant do be used withglmwith a fixed shape.