Skip to content

BUG: GLM score_test uses wrong df_resid for scale estimate #7840

Closed

Description

@josef-pkt

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 fit and effective degrees of freedom changes.
  • using nobs instead of df_resid in scale estimatewould remove the issue but that would be inconsistent with unconstrained/unpenalized model behavior.

Activity

  1. added
    type-bug-wrong
    on Nov 3, 2021
  2. added this to the 0.14 milestone on Nov 3, 2021
  3. josef-pkt commented on Nov 4, 2021

    @josef-pkt
    MemberAuthor

    digging in

    If we have the full model and exog_extra is None, then score and hessian are computed in
    _parameter_inference.score_test

            score = model.score(params_constrained)
            score_obs = model.score_obs(params_constrained)
            hessian = model.hessian(params_constrained, **hess_kwd)
    

    However, in GLM scale is recomputed if scale is None (default) in method call.
    That means we use the correct params_constrained but df in chisquare "x2" scale estimate is wrong, not for the restricted model.
    We need to specify the scale argument, so that the scale of the results instance is used

    So, 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 fit keyword but it's a model attribute that might be "stale" for a specific results instance.

  4. josef-pkt commented on Nov 4, 2021

    @josef-pkt
    MemberAuthor

    3 options to fix score_test for scale or parameters not included in params

    • isinstance check for GLM
    • add scale=None to _parameter_inference.score_test signature
    • add something more general to _parameter_inference.score_test signature, 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.

  5. josef-pkt commented on Nov 4, 2021

    @josef-pkt
    MemberAuthor

    Unfortunately that doesn't work properly

    • When the results.instance is a fit_constrained result, then using results.scale fixes the problem
    • However, we also have the case when the results instance is for the full model, and the restricted model is provided only by params_constrained and k_constraint. In that case results.scale is not the one for the restricted model. We also need the restricted scale as additional param or compute it with the correct restricted df_resid

    So, a scale solution to this requires options to compute scale without model attribute, #4693

    A quickfix is to temporarily change model.df_resid in GLMResults.score_test.

  6. changed the title [-]BUG: GLM `fit_constrained` uses wrong df_resid for scale estimate[/-] [+]BUG: GLM `score_test` uses wrong df_resid for scale estimate[/+] on Nov 4, 2021
  7. josef-pkt commented on Oct 19, 2022

    @josef-pkt
    MemberAuthor

    seems to be fixed by #7843

    closing for now
    but might need more consistent solution than quickfix

  8. modified the milestones: 0.14, 0.13.2, 0.13.3 on Oct 30, 2022
  9. modified the milestones: 0.13.3, 0.14 on Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-genmodprio-elevtype-bugtype-bug-wrong

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions