Skip to content

DOC: Follow up #10744 on get_prediction degrees of freedom, with R reference tests - #10762

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-prediction-df-followup
Nov 16, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-prediction-df-followup

Conversation

@bashtage

Copy link
Copy Markdown
Member

Follow up to #10744 (get_prediction intervals with df_resid_inference). Its review left the points below. The code is unchanged; the release note, the docstrings and the tests change.

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

Changes

  • DOC: The release note named only cov_type="cluster". Run on the code before BUG: Use df_resid_inference in get_prediction intervals #10744 and now (OLS with 10 groups, plus GLM, Logit and Poisson fits), the intervals of get_prediction with use_t=True also change for two-way clustering (the smaller number of groups minus one: df 3 with 4 and 10 groups), "hac-panel" and "hac-groupsum", and, for linear models, "cluster-crv3" and "cluster-jk" (these two raise a TypeError for GLM, Logit and Poisson). An example (OLS, 10 groups, mean prediction): cluster was (-0.12712, 1.37864) with df 118 and is (-0.23429, 1.48581) with df 9, cluster-crv3 was (-0.16108, 1.4126) and is (-0.27308, 1.5246). Nothing changes with df_correction=False or use_t=False. The bullet now names these cases and says so. It stays under 'Bug Fixes' next to BUG: include finite exact-diffuse innovations in concentrated loglike (closes #10741) #10742, BUG: Fix NegativeBinomialP hessian for p other than 1 and 2 #10737 and BUG: Pass p to the submodels in the truncated count score #10736, because it brings get_prediction in line with conf_int and t_test, although AGENTS.md puts changed results under 'Behavior changes'; move it if you prefer. :issue:10743 and :pr:10744 are kept.
  • DOC: The docstrings of get_prediction (regression, GLM, discrete and generic likelihood results, and the function in statsmodels/base/_prediction_inference.py) and the three conf_int methods of the prediction results there said nothing about the degrees of freedom. Each now says that the t distribution is used if use_t is True, with df_resid_inference degrees of freedom if the results have it and df_resid otherwise, and the normal distribution if use_t is False, with average=True and for which other than "mean" and "linear" (checked by printing the distribution of the results for OLS, GLM with each which and average, Logit and Poisson, with use_t and df_correction on and off), and has a .. versionchanged:: 0.15.1.
  • TST: The only test of BUG: Use df_resid_inference in get_prediction intervals #10744 compared the library with itself. New test_prediction_cluster_use_t_df_r_reference uses a literal data set (5 clusters of 4 observations) and R 4.6.1 with sandwich 3.1.3 and lmtest 0.9.40: the interval of the mean prediction is the interval of the intercept of lm(y ~ I(x - x0)) with lmtest::coefci(fit, parm = "(Intercept)", vcov. = sandwich::vcovCL(fit, cluster = g, type = "HC1", cadjust = TRUE), df = 4) (and df = Inf for use_t=False); the exact calls are in a comment and the comparison has rtol=1e-8. test_prediction_cov_types_df_resid_inference covers cluster, cluster-crv3, cluster-jk, hac-panel, hac-groupsum and two-way clustering for OLS. test_prediction_cluster_df_resid_unchanged is the negative control (df_correction=False and use_t=False keep df_resid and the normal distribution, 10 cases). The imports of the existing test are at the top of the module, its data come from a helper and the variable sf is defined before the branches.

Mutation checks (setting the line to df = self.df_resid, restored afterwards): in regression/_prediction.py 9 tests fail (the R [t] case, hac-panel and hac-groupsum included); in _prediction_inference.py the three getattr lines make the GLM cases, and for two of them also Logit and Poisson, fail; self.df_resid - 1 as the fallback fails 4 tests, among them the OLS and WLS df_correction cases.

Not changed (noted in the review)

  • A helper for the four getattr lines, PredictionResultsDelta (always normal), sandbox/regression/predstd.py (wls_prediction_std uses df_resid with the robust covariance), the cosmetic df attribute that holds n_groups - 1 even for use_t=False, and the PR body of BUG: Use df_resid_inference in get_prediction intervals #10744. Two-way clustering is covered for OLS only.

Tests

  • statsmodels/regression/tests/test_predict.py: 31 passed on Python 3.13 and on Python 3.10 (NumPy 1.23.5, SciPy 1.8.1); statsmodels/base/tests/test_predict.py: 22, statsmodels/discrete/tests/test_predict.py: 30 and statsmodels/genmod/tests/test_glm.py -k predict: 18 passed on Python 3.13.
  • ruff check of the changed files and git diff --check pass.

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): Claude Code (Claude Sonnet 5.5).
    Used for: reviewing BUG: Use df_resid_inference in get_prediction intervals #10744, and writing and checking the follow-up changes.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

bashtage and others added 3 commits November 16, 2026 20:37
The release note of statsmodels#10744 named only cov_type="cluster". Every covariance
type that sets df_resid_inference changes: "cluster" (also two-way
clustering), "hac-panel" and "hac-groupsum" for all models and "cluster-crv3"
and "cluster-jk" for linear models, unless df_correction=False. With
use_t=False nothing changes. Checked with seeded OLS, GLM, Logit and Poisson
fits with 10 clusters on main before and after statsmodels#10744.

The entry stays in Bug Fixes with the neighbouring statsmodels#10742, statsmodels#10737 and statsmodels#10736.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
statsmodels#10744 made the intervals of get_prediction use df_resid_inference
(n_groups - 1 with cluster-robust covariances) when the results have it, but
only PredictionResults.conf_int of the regression results said so. Add one
paragraph and a versionchanged directive to the get_prediction docstrings of
the regression, GLM, discrete and generic likelihood results and to
conf_int of the prediction result classes in base/_prediction_inference.py.
The normal distribution is used with use_t=False, with average=True and for
which other than "mean" and "linear"; checked by running each option.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The test added in statsmodels#10744 compared get_prediction with intervals computed
from the same covariance, so it only checked consistency. Add a reference
from R 4.6.1 (sandwich 3.1.3, lmtest 0.9.40: coefci with vcovCL HC1 and
df = G - 1 or Inf) for the mean prediction of an OLS fit with literal data,
a parametrized test of cluster-crv3, cluster-jk, hac-panel, hac-groupsum
and two-way clustering, and a negative control: with df_correction=False
the intervals use t(df_resid) and with use_t=False they are normal.

Move the imports of scipy, GLM, Logit and the families to the module top,
build the models in a helper and define the summary frame before the model
branches. Reverting any one of the four getattr lines of statsmodels#10744 fails tests.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@bashtage
bashtage marked this pull request as ready for review November 16, 2026 20:56
@bashtage
bashtage merged commit 7be1c05 into statsmodels:main Nov 16, 2026
11 of 12 checks passed
@bashtage
bashtage deleted the wip-prediction-df-followup branch November 16, 2026 22:19
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.

1 participant