Skip to content

BUG: use weights in OLSInfluence for WLS results - #10476

Merged
bashtage merged 1 commit into
statsmodels:mainfrom
bhaskargurram-ai:fix/wls-influence
Oct 24, 2026
Merged

bashtage merged 1 commit into
statsmodels:mainfrom
bhaskargurram-ai:fix/wls-influence

Conversation

@bhaskargurram-ai

Copy link
Copy Markdown
Contributor

Follow-up to #10367. OLSInfluence on WLS results ignored the weights:

  • the leave-one-observation-out regressions were refit as WLS(endog[i], exog[i]) without weights;
  • the hat matrix was not the weighted one.

As a result dfbeta, dfbetas, cooks_distance, dffits, cov_ratio and 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_diag is diag(W^1/2 X (X'WX)^-1 X' W^1/2);
  • the studentized residuals use wresid;
  • the LOOO results use wexog, wendog and wresid.

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.measures does for lm(..., weights = w).

resid_press and influence stay 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 explicit WLS(..., weights=w[mask]) leave-one-out fits, and Cook's distance with its definition dfbeta' X'WX dfbeta / (k s^2). It fails on main and passes here.
  • test_olsinfluence_gls_without_sigma_matches_ols covers the unwhitened branch.

Question: should I also add get_influence() to WLS results (only OLSResults has it), here or in a separate PR?

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    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.

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
@bhaskargurram-ai

Copy link
Copy Markdown
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.

@bashtage
bashtage merged commit 33bf724 into statsmodels:main Oct 24, 2026
13 checks passed
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
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.

ENH: WLS outlier influence

3 participants