The dof() method for GeneralizedLinearModel incorrectly counts the dispersion parameter θ for NegativeBinomial(θ) and Geometric() distributions, even though θ is fixed (not estimated). This causes dof() to be off by 1 (too high) and the AIC/BIC to be inflated.
For distributions where dispersion IS estimated (Normal, Gamma, InverseGaussian), Julia's dof() correctly matches R, and AIC agrees. The bug only affects NegativeBinomial and Geometric.
Here a comparison between GLM and R (using MASS package)
| Distribution |
dof (Jl) |
AIC (Jl) |
loglik_df (R) |
AIC (R) |
| Geometric |
8 |
1112.74 |
7 |
1110.74 |
| NegBinomial(2) |
8 |
1122.52 |
7 |
1120.52 |
The cause is in src/glmfit.jl
function dof(x::GeneralizedLinearModel)
modelrank = linpred_rank(x.pp)
dispersion_parameter(x.rr.d) ? modelrank + 1 : modelrank
end
The issue is that dispersion_parameter() returns true for NegativeBinomial and Geometric, but their θ parameter is fixed by the user, not estimated from the data. The +1 should only apply when dispersion is actually being estimated.
Since #485 is almost ready, I propose tackling this issue after it is merged. In #485, dispersion_parameter() is used in the calculation of the dispersion parameter for weighted models, and the best option is a special handling in dof() to not count fixed θ for these distributions.
The
dof()method for GeneralizedLinearModel incorrectly counts the dispersion parameter θ for NegativeBinomial(θ) and Geometric() distributions, even though θ is fixed (not estimated). This causesdof()to be off by 1 (too high) and the AIC/BIC to be inflated.For distributions where dispersion IS estimated (Normal, Gamma, InverseGaussian), Julia's dof() correctly matches
R, and AIC agrees. The bug only affectsNegativeBinomialandGeometric.Here a comparison between
GLMandR(usingMASSpackage)The cause is in
src/glmfit.jlThe issue is that
dispersion_parameter()returnstrueforNegativeBinomialandGeometric, but their θ parameter is fixed by the user, not estimated from the data. The +1 should only apply when dispersion is actually being estimated.Since #485 is almost ready, I propose tackling this issue after it is merged. In #485,
dispersion_parameter()is used in the calculation of the dispersion parameter for weighted models, and the best option is a special handling in dof() to not count fixed θ for these distributions.