Skip to content

BUG: Use weights in IV specification tests - #737

Open
raashish1601 wants to merge 1 commit into
bashtage:mainfrom
raashish1601:fix/iv-weighted-diagnostics
Open

raashish1601 wants to merge 1 commit into
bashtage:mainfrom
raashish1601:fix/iv-weighted-diagnostics

Conversation

@raashish1601

Copy link
Copy Markdown
Contributor

When an IV model is fit with weights, the specification tests in IVResults and IVGMMResults were computed from the unweighted data. Some of them also mixed in the residuals of the weighted fit. On the simulated test data, durbin() and wu_hausman() came out negative (about -2.6) for a weighted IV2SLS.

Affected: sargan (and so basmann), durbin, wu_hausman, wooldridge_score, wooldridge_regression, wooldridge_overid and c_stat. They now use the weighted residuals and the root-weight scaled regressors and instruments (_wy, _wx, _wz), the same data the estimators use. The refits inside durbin/wu_hausman and c_stat now pass the model weights. Unweighted results are unchanged, since the weights are then all 1.

Comparisons:

  • R's AER::ivreg(y_robust ~ x3 + x4 + x5 + x1 | x3 + x4 + x5 + z1 + z2, weights = weights) on simulated-data.dta, summary(m, diagnostics = TRUE): Sargan 1.3091639 and Wu-Hausman 0.0126420. On main linearmodels gives 2.8064 and -2.5984. With this change it gives 1.3091639 and 0.0126420.
  • Each test on a weighted model now gives the same value as the unweighted model on data scaled by the root of the weights. The parameters and standard errors already agreed this way.

Tests: added test_weighted_sargan_wu_hausman (the R values above) and test_weighted_diagnostics_match_rescaled_data (all of the tests above, for IV2SLS with one and two endogenous variables, and c_stat for IVGMM) to test_postestimation.py. Both fail on main and pass with this change. linearmodels/tests/iv and linearmodels/tests/system pass locally, and black, isort, ruff and flake8 are clean on the changed files.

Sargan, Basmann, Durbin, Wu-Hausman, the Wooldridge tests and the
C-statistic were computed from unweighted data when the model was
estimated with weights. Use the weighted residuals and the scaled
regressors and instruments, and pass the weights to the refits.
@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.53%. Comparing base (40e672d) to head (1d35e5b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #737   +/-   ##
=======================================
  Coverage   99.53%   99.53%           
=======================================
  Files         101      101           
  Lines       17501    17531   +30     
  Branches     1436     1440    +4     
=======================================
+ Hits        17419    17449   +30     
  Misses         31       31           
  Partials       51       51           
Flag Coverage Δ
adder 99.51% <100.00%> (+<0.01%) ⬆️
subtractor 99.51% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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