Repository navigation
BUG: Use weighted residuals in the covariance of weighted GLS and 3SLS - #787
Merged
Merged
Conversation
The GLS fit of SUR and IV3SLS (method="gls", which is the default unless every equation has the same regressors) gave the covariance estimators the unweighted residuals y - x b together with the weighted regressors sqrt(w) x. The scores of the robust, kernel and clustered covariances were then sqrt(w) x e instead of w x e, so their standard errors were wrong whenever the weights were not all equal. The coefficients, the unadjusted covariance and the OLS fit (method="ols", which already used the weighted residuals) were not affected. With one equation, GLS is weighted least squares and the robust covariance is HC0. For a regression with 200 observations and chi-squared weights, the robust standard errors of SUR were 0.1299, 0.1823 and 0.1254, where statsmodels WLS(...).fit(cov_type="HC0") gives 0.1448, 0.1982 and 0.1324. For systems with several equations, 96 of the 140 weighted combinations of GLS or OLS, iterated GLS, full_cov, constraints, debiased and covariance type differed from a direct computation of the stacked sandwich estimator, by up to 28% of the largest covariance element, for SUR and for IV3SLS. After this change all of them agree to within 1e-7, and the unweighted fits are unchanged since the weights are all one. The covariance estimators now get the residuals scaled by the square root of the weights. The tests compare the weighted estimators with the unweighted estimators applied to the data scaled by the square root of the normalized weights, for every covariance type, and the robust standard errors of a weighted regression with the statsmodels WLS estimator. Seven of them fail without the change.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #787 +/- ##
==========================================
+ Coverage 99.58% 99.59% +0.01%
==========================================
Files 128 128
Lines 24188 24486 +298
Branches 1809 1827 +18
==========================================
+ Hits 24087 24387 +300
+ Misses 40 39 -1
+ Partials 61 60 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
The GLS fit of SUR and IV3SLS (method="gls", which is the default unless every equation has the same regressors) gave the covariance estimators the unweighted residuals y - x b together with the weighted regressors sqrt(w) x. The scores of the robust, kernel and clustered covariances were then sqrt(w) x e instead of w x e, so their standard errors were wrong whenever the weights were not all equal. The coefficients, the unadjusted covariance and the OLS fit (method="ols", which already used the weighted residuals) were not affected.
With one equation, GLS is weighted least squares and the robust covariance is HC0. For a regression with 200 observations and chi-squared weights, the robust standard errors of SUR were 0.1299, 0.1823 and 0.1254, where statsmodels WLS(...).fit(cov_type="HC0") gives 0.1448, 0.1982 and 0.1324. For systems with several equations, 96 of the 140 weighted combinations of GLS or OLS, iterated GLS, full_cov, constraints, debiased and covariance type differed from a direct computation of the stacked sandwich estimator, by up to 28% of the largest covariance element, for SUR and for IV3SLS. After this change all of them agree to within 1e-7, and the unweighted fits are unchanged since the weights are all one.
The covariance estimators now get the residuals scaled by the square root of the weights.
The tests compare the weighted estimators with the unweighted estimators applied to the data scaled by the square root of the normalized weights, for every covariance type, and the robust standard errors of a weighted regression with the statsmodels WLS estimator. Seven of them fail without the change.