Repository navigation
Description
I started to write this as a bug report for score_test (followup #7815). However the bug is in existing fit_constrained part.
chisquare scale uses df_resid from model which does not have df corrected by k_constraints
update see below, it's a problem in score_test or other code that recomputes loglike, score or hessian with "stale" scale or df_resid attributes in GLM model instance.
The size of the bug is small in large samples df_resid_full / (df_resid_full + k_constraints)
"bug-wrong" but small error, and I'm not sure where and when this get's triggered, besides the new score_test.
TODO: check unit test coverage for scale with fit_constrained.
related, similar issues
#4693 refactor scale estimate, more options
#4694 this sounds very similar to this issue here
closed issue for scale used for bse/cov_params in fit_constrained #4193, PR #4195
maybe this bug is code comment in unit test https://github.com/statsmodels/statsmodels/pull/4195/files#diff-e4965f6ed9b1c4d13936b9d5523cbc92cfb461a5ea1ee3275d503245f37322e9R52
long version:
It looks like there is a df bug in sccore_test: 3 ways of doing score_tests don't agree.
Difference looks like it comes from using df_resid of full model in scale estimate. In that case it will not show up in models/families with fixed scale like poisson.
2-sample case with gaussian, res is constant-only model
mod_full = GLM(counts, ex_diff, family=families.Gaussian())
res_f = mod_full.fit()
res_constr = mod_full.fit_constrained("x1")
st = res_constr.score_test()
st
(array([4.95]), array([0.02609067]), 1)
st = res.score_test(exog_extra=groups)
st
(array([5.025]), array([0.02498393]), 1)
st = res_f.score_test(params_constrained=res_constr.params, k_constraints=1)
st
(array([4.95]), array([0.02609067]), 1)
However, df_resid looks correct:
res_constr.df_resid, res.df_resid, res_f.df_resid
(67, 67, 66)
Bug: the model.df_resid in constraint model does not take number of constraints into account.
The model.df_resid is used in computing the chisquare scale _estimate_x2_scale
res_constr.model.df_resid
66
Fixing this is not trivial because method to compute scale is in model and not results class.
The model can be reused for several different calls of fit and fit_constrained. Only the results class is supposed to have and use the constraints information.
Possibly a similar cases:
- llf, loglike based on concentrated likelihood with implicit scale computation.
- check GAM and penalized models when pen_weight is set in
fitand effective degrees of freedom changes. - using nobs instead of df_resid in
scaleestimatewould remove the issue but that would be inconsistent with unconstrained/unpenalized model behavior.
Activity
josef-pkt commentedon Nov 4, 2021
digging in
If we have the full model and exog_extra is None, then score and hessian are computed in
_parameter_inference.score_testHowever, in GLM scale is recomputed if scale is None (default) in method call.
That means we use the correct
params_constrainedbut df in chisquare "x2" scale estimate is wrong, not for the restricted model.We need to specify the
scaleargument, so that the scale of the results instance is usedSo, now it looks like it's the problem in score_test and not directly in fit_constrained.
"scale" for all practical purposes is a param in GLM and we need the appropriate scale when we call model methods from the reults or from the outside.
The main problem comes for scale_type being a
fitkeyword but it's a model attribute that might be "stale" for a specific results instance.josef-pkt commentedon Nov 4, 2021
3 options to fix
score_testfor scale or parameters not included inparamsscale=Noneto_parameter_inference.score_testsignature_parameter_inference.score_testsignature, e.g. kwds for score, hessian,It's a private internal function, so backwards compatibility will not be a large constraint.
Same will be in linear_model if score, hessian include scale.
(I never checked Kerby's models for those details)
The specific problem is that model.df_resid is not the right one. but I guess using the results scale is the more robust solution, and we cannot change model.df_resid.
option 2 it is: add scale=None, and leave generalization to later if or when they will be needed.
josef-pkt commentedon Nov 4, 2021
Unfortunately that doesn't work properly
So, a
scalesolution to this requires options to compute scale without model attribute, #4693A quickfix is to temporarily change model.df_resid in GLMResults.score_test.
[-]BUG: GLM `fit_constrained` uses wrong df_resid for scale estimate[/-][+]BUG: GLM `score_test` uses wrong df_resid for scale estimate[/+]josef-pkt commentedon Oct 19, 2022
seems to be fixed by #7843
closing for now
but might need more consistent solution than quickfix