-
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?
Changes from all commits
c2ed1c0
3693b3c
85b9674
6640a7b
5157688
6af4790
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -299,7 +299,7 @@ end | |
|
|
||
| deviance(m::AbstractGLM) = deviance(m.rr) | ||
|
|
||
| function nulldeviance(m::GeneralizedLinearModel) | ||
| function nulldeviance(m::AbstractGLM) | ||
| r = m.rr | ||
| wts = weights(r) | ||
| y = r.y | ||
|
|
@@ -359,7 +359,7 @@ function loglikelihood(m::AbstractGLM) | |
| return ll | ||
| end | ||
|
|
||
| function nullloglikelihood(m::GeneralizedLinearModel) | ||
| function nullloglikelihood(m::AbstractGLM) | ||
| r = m.rr | ||
| wts = weights(m) | ||
| sumwt = sum(wts) | ||
|
|
@@ -394,7 +394,15 @@ function nullloglikelihood(m::GeneralizedLinearModel) | |
| return ll | ||
| end | ||
|
|
||
| dof(obj::GeneralizedLinearModel) = linpred_rank(obj) + dispersion_parameter(obj.rr.d) | ||
| """ | ||
| dof(model::AbstractGLM) | ||
|
|
||
| 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)). | ||
| """ | ||
| dof(obj::AbstractGLM) = linpred_rank(obj) + dispersion_parameter(obj.rr.d) | ||
|
|
||
| function _fit!(m::AbstractGLM, maxiter::Integer, minstepfac::Real, | ||
| atol::Real, rtol::Real, start::Union{AbstractVector,Nothing}) | ||
|
|
@@ -668,14 +676,20 @@ function glm(formula::FormulaTerm, data, d::UnivariateDistribution, | |
| end | ||
|
|
||
| GLM.Link(r::GlmResp) = r.link | ||
| GLM.Link(m::GeneralizedLinearModel) = Link(m.rr) | ||
| GLM.Link(m::AbstractGLM) = Link(m.rr) | ||
|
|
||
| """ | ||
| dispersion(m::LinearModel, sqr::Bool=false) | ||
| dispersion(m::AbstractGLM, sqr::Bool=false) | ||
|
|
||
| Return the estimated dispersion (or scale) parameter for a model's distribution, | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm. For fixed
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So say "Negative Binomial (with fixed θ)"? |
||
| Binomial and Poisson families (see [`GLM.dispersion_parameter`](@ref)). | ||
| For other distributions, it is estimated as the square root of the sum of Pearson | ||
| residuals (i.e. for a linear model, the residual residual sum of squares) | ||
| divided by the residual degrees of freedom. | ||
|
|
||
| If `sqr` is `true`, the squared dispersion parameter is returned. | ||
| """ | ||
|
|
@@ -881,7 +895,7 @@ function residuals(r::GlmResp; weighted::Bool=false) | |
| return dres | ||
| end | ||
|
|
||
| function momentmatrix(m::GeneralizedLinearModel) | ||
| function momentmatrix(m::AbstractGLM) | ||
| X = modelmatrix(m; weighted=false) | ||
| r = varstruct(m) | ||
| if link(m) isa Union{Gamma,InverseGaussian} | ||
|
|
@@ -890,7 +904,7 @@ function momentmatrix(m::GeneralizedLinearModel) | |
| return Diagonal(r) * X | ||
| end | ||
|
|
||
| function varstruct(x::GeneralizedLinearModel) | ||
| function varstruct(x::AbstractGLM) | ||
| wrkwts = working_weights(x) | ||
| wts = weights(x) | ||
| wrkres = working_residuals(x) | ||
|
|
@@ -901,20 +915,20 @@ function varstruct(x::GeneralizedLinearModel) | |
| end | ||
| end | ||
|
|
||
| function invloglikhessian(m::GeneralizedLinearModel) | ||
| function invloglikhessian(m::AbstractGLM) | ||
| r = varstruct(m) | ||
| wts = weights(m) | ||
| return inverse(m.pp) * sum(wts) / nobs(m) | ||
| end | ||
|
|
||
| function StatsBase.cooksdistance(m::GeneralizedLinearModel) | ||
| function StatsBase.cooksdistance(m::AbstractGLM) | ||
| h = leverage(m) | ||
| hh = h ./ (1 .- h) .^ 2 | ||
| Rp = pearson_residuals(m) | ||
| return (Rp .^ 2) .* hh ./ (dispersion(m)^2 * dof(m)) | ||
| end | ||
|
|
||
| function pearson_residuals(m::GeneralizedLinearModel) | ||
| function pearson_residuals(m::AbstractGLM) | ||
| y = m.rr.y | ||
| μ = predict(m) | ||
| v = glmvar.(m.rr.d, μ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -208,6 +208,13 @@ function lm(f::FormulaTerm, data; | |||||
| return fit(LinearModel, f, data; wts, dropcollinear, method, contrasts) | ||||||
| end | ||||||
|
|
||||||
| """ | ||||||
| dof(model::GeneralizedLinearModel) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| For linear models, consumed degrees of freedom correspond | ||||||
| to the number of estimated coefficients, plus one for the estimated | ||||||
| dispersion parameter σ. | ||||||
| """ | ||||||
| dof(x::LinearModel) = linpred_rank(x.pp) + 1 | ||||||
|
|
||||||
| """ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,25 @@ | ||
| mutable struct NegativeBinomialModel{G<:GlmResp,L<:LinPred} <: AbstractGLM | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just wrap a GeneralizedLinearModel instead of repeating the definition?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With a |
||
| rr::G | ||
| pp::L | ||
| formula::Union{FormulaTerm,Nothing} | ||
| fit::Bool | ||
| maxiter::Int | ||
| minstepfac::Float64 | ||
| atol::Float64 | ||
| rtol::Float64 | ||
| end | ||
|
|
||
| """ | ||
| dof(model::NegativeBinomialModel) | ||
|
|
||
| For negative binomial models (fitted with [`negbin`](@ref)), consumed degrees | ||
| of freedom correspond to the number of estimated coefficients plus one for the | ||
| estimated shape parameter θ. | ||
| """ | ||
| function dof(obj::NegativeBinomialModel) | ||
| return linpred_rank(obj) + 1 | ||
| end | ||
|
|
||
| function mle_for_θ(y::AbstractVector, μ::AbstractVector, wts::AbstractWeights; | ||
| maxiter=30, tol=1.e-6) | ||
| function first_derivative(θ::Real) | ||
|
|
@@ -180,5 +202,8 @@ function _negbin(F, | |
| ll = loglikelihood(regmodel) | ||
| end | ||
| converged || throw(ConvergenceException(maxiter)) | ||
| return regmodel | ||
|
|
||
| return NegativeBinomialModel(regmodel.rr, regmodel.pp, regmodel.formula, regmodel.fit, | ||
| regmodel.maxiter, regmodel.minstepfac, regmodel.atol, | ||
| regmodel.rtol) | ||
| end | ||
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
AbstractGLMbut not aGeneralizedLinearModelthe newNegativeBinomialModelfor 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.
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.
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.