Repository navigation
DOC: Follow up #10744 on get_prediction degrees of freedom, with R reference tests - #10762
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow up to #10744 (
get_predictionintervals withdf_resid_inference). Its review left the points below. The code is unchanged; the release note, the docstrings and the tests change.NumPy's guide.
Changes
DOC:The release note named onlycov_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 ofget_predictionwithuse_t=Truealso 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 aTypeErrorfor GLM, Logit and Poisson). An example (OLS, 10 groups, mean prediction):clusterwas (-0.12712, 1.37864) with df 118 and is (-0.23429, 1.48581) with df 9,cluster-crv3was (-0.16108, 1.4126) and is (-0.27308, 1.5246). Nothing changes withdf_correction=Falseoruse_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 bringsget_predictionin line withconf_intandt_test, althoughAGENTS.mdputs changed results under 'Behavior changes'; move it if you prefer.:issue:10743and:pr:10744are kept.DOC:The docstrings ofget_prediction(regression, GLM, discrete and generic likelihood results, and the function instatsmodels/base/_prediction_inference.py) and the threeconf_intmethods of the prediction results there said nothing about the degrees of freedom. Each now says that the t distribution is used ifuse_tis True, withdf_resid_inferencedegrees of freedom if the results have it anddf_residotherwise, and the normal distribution ifuse_tis False, withaverage=Trueand forwhichother than"mean"and"linear"(checked by printing the distribution of the results for OLS, GLM with eachwhichandaverage, Logit and Poisson, withuse_tanddf_correctionon 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. Newtest_prediction_cluster_use_t_df_r_referenceuses 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 oflm(y ~ I(x - x0))withlmtest::coefci(fit, parm = "(Intercept)", vcov. = sandwich::vcovCL(fit, cluster = g, type = "HC1", cadjust = TRUE), df = 4)(anddf = Infforuse_t=False); the exact calls are in a comment and the comparison hasrtol=1e-8.test_prediction_cov_types_df_resid_inferencecoverscluster,cluster-crv3,cluster-jk,hac-panel,hac-groupsumand two-way clustering for OLS.test_prediction_cluster_df_resid_unchangedis the negative control (df_correction=Falseanduse_t=Falsekeepdf_residand 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 variablesfis defined before the branches.Mutation checks (setting the line to
df = self.df_resid, restored afterwards): inregression/_prediction.py9 tests fail (the R[t]case,hac-panelandhac-groupsumincluded); in_prediction_inference.pythe threegetattrlines make the GLM cases, and for two of them also Logit and Poisson, fail;self.df_resid - 1as the fallback fails 4 tests, among them the OLS and WLSdf_correctioncases.Not changed (noted in the review)
getattrlines,PredictionResultsDelta(always normal),sandbox/regression/predstd.py(wls_prediction_stdusesdf_residwith the robust covariance), the cosmeticdfattribute that holdsn_groups - 1even foruse_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 andstatsmodels/genmod/tests/test_glm.py -k predict: 18 passed on Python 3.13.ruff checkof the changed files andgit diff --checkpass.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.
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