Repository navigation
BUG: use weights in OLSInfluence for WLS results - #10476
Merged
Merged
Conversation
OLSInfluence on WLS results refit the leave-one-observation-out regressions without weights and used an unweighted hat matrix, so dfbeta, dfbetas, cooks_distance, dffits, cov_ratio and the studentized residuals were wrong without any warning. For WLS the measures are those of OLS on the whitened data, so the hat matrix, the studentized residuals and the LOOO regressions now use wexog, wendog and wresid, and the closed form LOOO updates from statsmodels#10367 also cover WLS. resid_press and influence stay on the scale of endog. closes statsmodels#9067
|
|
||
| from statsmodels.graphics._regressionplots_doc import _plot_influence_doc | ||
| from statsmodels.regression.linear_model import OLS | ||
| from statsmodels.regression.linear_model import OLS, WLS |
Contributor
Author
|
@josef-pkt could you take a look at this when there’s time? It makes OLSInfluence use the weights for WLS results, following up on your comments in #9067. On the test data, Cook’s distance and the related measures were off by up to 7x before. There’s also an open question in the description: should get_influence() be added to WLS results here, or in a separate PR? I’m happy to do either. |
This was referenced Oct 24, 2026
bashtage
added a commit
that referenced
this pull request
Oct 25, 2026
Add versionchanged for the use of the weights, state that WLS weights are precision weights, how zero weights differ from R, which sum of squares ess_press is, and that only OLS and WLS are supported (GLS with sigma is silently incorrect). Follow up to gh-10476. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage
added a commit
that referenced
this pull request
Oct 25, 2026
Compare OLSInfluence of WLS results with R 4.6.1 influence measures for lm with weights, add tests for rank deficient exog, a unit hat diagonal and zero weights, and move the OLSInfluence imports to module level. Follow up to gh-10476. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage
added a commit
that referenced
this pull request
Oct 25, 2026
DOC: Follow up on #10476, document WLS influence and add R reference tests
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 #10367.
OLSInfluenceon WLS results ignored the weights:WLS(endog[i], exog[i])without weights;As a result
dfbeta,dfbetas,cooks_distance,dffits,cov_ratioand the studentized residuals were wrong, with no warning. On the test data, Cook's distance was off by up to 7x.WLS is OLS on the whitened data
sqrt(w) * endog,sqrt(w) * exog, so for WLS (and OLS as its unit-weight case) the measures are now computed there:hat_matrix_diagisdiag(W^1/2 X (X'WX)^-1 X' W^1/2);wresid;wexog,wendogandwresid.The closed form from #10367 now also covers WLS, so it remains fast. Without observation i, each LOOO regression is the WLS fit. This is what R's
influence.measuresdoes forlm(..., weights = w).resid_pressandinfluencestay on the scale of endog: with the weighted hat diagonal they are the prediction error and the fitted value change of the weighted LOOO fit, and the test checks that.Models other than OLS and WLS (e.g. GLS with sigma) keep the previous unwhitened path; leave-one-out under a non-diagonal sigma is a separate question.
Tests:
test_olsinfluence_wls_matches_weighted_looo(WLS through the closed form, plus a WLS subclass through the loop). It compares hat diagonal, params, sigma2, det cov params, dfbeta, cov_ratio, internal and external studentized residuals and PRESS residuals with explicitWLS(..., weights=w[mask])leave-one-out fits, and Cook's distance with its definitiondfbeta' X'WX dfbeta / (k s^2). It fails on main and passes here.test_olsinfluence_gls_without_sigma_matches_olscovers the unwhitened branch.Question: should I also add
get_influence()to WLS results (onlyOLSResultshas it), here or in a separate PR?AI Disclosure
Contributions must comply with the statsmodels AI Policy.
Tool(s):
Claude Code.Used for:
drafting the tests, and checking the results against brute-force weighted leave-one-out regressions.I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.