Skip to content

BUG: Use the null model's scale in GLMResults.llnull - #10669

Open
DresdenGman wants to merge 3 commits into
statsmodels:mainfrom
DresdenGman:fix-9025-glm-llnull-scale
Open

DresdenGman wants to merge 3 commits into
statsmodels:mainfrom
DresdenGman:fix-9025-glm-llnull-scale

Conversation

@DresdenGman

Copy link
Copy Markdown

What this PR does

GLMResults.llnull evaluated the family log-likelihood at the fitted
values of the constant-only null model, but passed the scale estimated
for the full model. For families with an estimated scale (e.g.
Gaussian, Gamma), llnull — and with it the McFadden and Cox-Snell
pseudo R-squared — was therefore wrong. Example (Gaussian, n = 40):
llnull was -96.07, while the log-likelihood of the null model at its
own scale is -87.49.

The fix estimates the scale of the null model in exactly the same way
as the scale of the full model, i.e. through GLM.estimate_scale
evaluated at the fitted values of the null model with the residual
degrees of freedom of the null model, honoring the scale argument of
the original fit (fixed value, "x2" or "dev"). The null-model
construction (constant-only design and adjusted init keywords) is
shared between GLMResults.null and llnull via a small helper, so
the two cannot drift apart. Families with a fixed scale of 1
(Poisson, Binomial, NegativeBinomial by default) are unchanged, and
null_deviance is intentionally not touched: deviance is a
scale-free quantity in statsmodels (as in R).

Tests

  • New regression test test_glm_llnull_own_scale: Gaussian against
    the closed-form null log-likelihood, Gamma against an explicitly
    fitted constant-only model, plus fixed-scale and "dev"-scale fits.
    It fails on main and passes with this change.
  • The prsquared reference values for the Longley and Scotvote result
    classes in tests/results/results_glm.py were updated. The old
    values reproduce the pre-fix (full-model-scale) output to 3-4
    significant digits; the corrected values are 0.28273/0.99549
    (Longley) and 0.25568/0.83175 (Scotvote).
  • Full statsmodels/genmod/tests and statsmodels/gam/tests suites:
    1148 passed on main, 1149 passed with this change (the +1 is the new
    regression test), no new failures.

A release-notes entry was added to version0.15.1.rst.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Muse.
    Used for: drafting the implementation and its regression test, and verifying the change against an explicitly fitted null model and the closed-form Gaussian null log-likelihood.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

llnull evaluated the log-likelihood at the fitted values of the
constant-only null model, but used the scale estimated for the full
model. For families with an estimated scale, e.g. Gaussian and Gamma,
this made llnull and the pseudo R-squared of the full model wrong.

The scale of the null model is now estimated in the same way as the
scale of the full model, using the fitted values and the residual
degrees of freedom of the null model. Families with a fixed scale of
one, e.g. Poisson and Binomial, are unchanged.

The prsquared reference values for the Longley and Scotvote results
were computed with the full model scale and have been updated.

closes statsmodels#9025
@bashtage

Copy link
Copy Markdown
Member

Please add the PR role to the release note, after the issue role: :issue:`9025`, :pr:`10669`. The number is known now that the PR is open, so a small commit is fine. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: GLM scale is not updated when computing null model

2 participants