Skip to content

BUG: Use df_resid_inference in get_prediction intervals - #10744

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
bhaskargurram-ai:fix/prediction-df-resid-inference
Nov 16, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
bhaskargurram-ai:fix/prediction-df-resid-inference

Conversation

@bhaskargurram-ai

Copy link
Copy Markdown
Contributor

Fixes #10743

With cov_type="cluster" and use_t=True, conf_int, t_test and pvalues use the t distribution with n_groups - 1 degrees of freedom (df_resid_inference), but get_prediction used df_resid. The confidence and prediction intervals of the predicted mean were too narrow, for example [1.012, 3.281] instead of [0.845, 3.448] for an OLS model with 10 clusters, where t_test of the same linear combination gives the latter. In a simulation with 10 clusters the 95% interval covered 89.0% before and 93.4% after the change.

The fix uses getattr(self, "df_resid_inference", self.df_resid), the same as LikelihoodModelResults.conf_int, in the four places that build prediction results: regression/_prediction.get_prediction and get_prediction_glm, get_prediction_linear, get_prediction_monotonic in base/_prediction_inference.py. Without df_resid_inference (all other cov types) nothing changes.

Test: test_prediction_cluster_use_t_df in regression/tests/test_predict.py, for OLS, WLS, Gaussian GLM, Poisson GLM and Logit. It compares the mean and observation intervals of summary_frame, and conf_int for which="linear" and which="mean", with x0 @ params ± t(0.975, G - 1) * se and with t_test(exog0).conf_int(). All 5 cases fail on main and pass with the fix. Release note added under regression bug fixes.

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

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.
    Used for: drafting the tests and this PR description.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

With cov_type="cluster" and use_t=True, conf_int, t_test and pvalues use
the t distribution with n_groups - 1 degrees of freedom, but
get_prediction used df_resid, so the confidence and prediction intervals
of the predicted mean were too narrow. This affected regression, GLM and
discrete results.
@bashtage

Copy link
Copy Markdown
Member

The release note the PR body and the new code comment say the confidence and prediction intervals "were too narrow". This is only true for the confidence interval of the mean.

Please do one of the following:

  • keep the mean interval on t(G - 1) and leave obs_ci on a large-df t, or
  • keep the code as is, drop "prediction" from the sentence, and say in the note and the docstring that obs_ci is now conservative.

Once you have taken a decision, rewrite the release note so it only claims what is true, and add the roles :issue:10743 and :pr:10744.

Reword the release note, add the issue and PR roles, and document in
PredictionResults.conf_int that the prediction interval for a new
observation uses df_resid_inference too, which is conservative.
@bashtage

Copy link
Copy Markdown
Member

Thanks. LGTM.

@bashtage
bashtage merged commit 59b3768 into statsmodels:main Nov 16, 2026
13 checks passed
bashtage added a commit that referenced this pull request Nov 16, 2026
The release note of #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 #10744.

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

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage added a commit that referenced this pull request Nov 16, 2026
#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]>
bashtage added a commit that referenced this pull request Nov 16, 2026
The test added in #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 #10744 fails tests.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage added a commit that referenced this pull request Nov 16, 2026
DOC: Follow up #10744 on get_prediction degrees of freedom, with R reference tests
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.

get_prediction intervals ignore df_resid_inference with cluster-robust use_t=True

2 participants