Repository navigation
BUG: GLM-Gaussian fit_constrained, wrong bse #4195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
josef-pkt
merged 4 commits into
statsmodels:master
from
josef-pkt:bug_fit_constrained_glm
Jan 8, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f1c8c2f
TST: add unit tests GLM-Gaussian fit_constrained issue #4193
josef-pkt 5a7582a
ENH fix scaling factor in cov_type closes #4198
josef-pkt f94acdf
TST: GLM Binomial fit_constrained with cov_type='HC0' against Stata
josef-pkt a2e65a2
REF/BUG/ENH: fit_constrained set cov_params_default, closes #4193
josef-pkt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Unit tests for fit_constrained | ||
| Tests for Poisson and Binomial are in discrete | ||
|
|
||
|
|
||
| Created on Sun Jan 7 09:21:39 2018 | ||
|
|
||
| Author: Josef Perktold | ||
| """ | ||
|
|
||
|
|
||
| import numpy as np | ||
| from numpy.testing import assert_allclose, assert_equal | ||
| from statsmodels.tools.tools import add_constant | ||
| from statsmodels.regression.linear_model import OLS | ||
| from statsmodels.genmod.generalized_linear_model import GLM | ||
|
|
||
|
|
||
| class ConstrainedCompareMixin(object): | ||
|
|
||
| @classmethod | ||
| def setup_class(cls): | ||
| nobs, k_exog = 100, 5 | ||
| np.random.seed(987125) | ||
| x = np.random.randn(nobs, k_exog - 1) | ||
| x = add_constant(x) | ||
|
|
||
| y_true = x.sum(1) / 2 | ||
| y = y_true + 2 * np.random.randn(nobs) | ||
| cls.endog = y | ||
| cls.exog = x | ||
| cls.idx_uc = [0, 2, 3, 4] | ||
| cls.idx_p_uc = np.array(cls.idx_uc) | ||
| cls.idx_c = [1] | ||
| cls.exogc = xc = x[:, cls.idx_uc] | ||
| mod_ols_c = OLS(y - 0.5 * x[:, 1], xc) | ||
| mod_ols_c.exog_names[:] = ['const', 'x2', 'x3', 'x4'] | ||
| cls.mod2 = mod_ols_c | ||
| cls.init() | ||
|
|
||
| def test_params(self): | ||
| res1 = self.res1 | ||
| res2 = self.res2 | ||
| assert_allclose(res1.params[self.idx_p_uc], res2.params, rtol=1e-10) | ||
|
|
||
| def test_se(self): | ||
| res1 = self.res1 | ||
| res2 = self.res2 | ||
|
|
||
| assert_equal(res1.df_resid, res2.df_resid) | ||
| # why does scale differ | ||
| assert_allclose(res1.scale, res2.scale, rtol=0.05) | ||
| assert_allclose(res1.bse[self.idx_p_uc], res2.bse, rtol=1e-10) | ||
| assert_allclose(res1.cov_params()[self.idx_p_uc[:,None], self.idx_p_uc], | ||
| res2.cov_params(), rtol=1e-10) | ||
|
|
||
| def test_resid(self): | ||
| res1 = self.res1 | ||
| res2 = self.res2 | ||
| assert_allclose(res1.resid_response, res2.resid, rtol=1e-10) | ||
|
|
||
|
|
||
| class TestGLMGaussianOffset(ConstrainedCompareMixin): | ||
|
|
||
| @classmethod | ||
| def init(cls): | ||
| cls.res2 = cls.mod2.fit() | ||
| mod = GLM(cls.endog, cls.exogc, | ||
| offset=0.5 * cls.exog[:, cls.idx_c].squeeze()) | ||
| mod.exog_names[:] = ['const', 'x2', 'x3', 'x4'] | ||
| cls.res1 = mod.fit() | ||
| cls.idx_p_uc = np.arange(cls.exogc.shape[1]) | ||
|
|
||
|
|
||
| class TestGLMGaussianConstrained(ConstrainedCompareMixin): | ||
|
|
||
| @classmethod | ||
| def init(cls): | ||
| cls.res2 = cls.mod2.fit() | ||
| mod = GLM(cls.endog, cls.exog) | ||
| mod.exog_names[:] = ['const', 'x1', 'x2', 'x3', 'x4'] | ||
| cls.res1 = mod.fit_constrained('x1=0.5') | ||
|
|
||
|
|
||
| class TestGLMGaussianOffsetHC(ConstrainedCompareMixin): | ||
|
|
||
| @classmethod | ||
| def init(cls): | ||
| cov_type = 'HC0' | ||
| cls.res2 = cls.mod2.fit(cov_type=cov_type) | ||
| mod = GLM(cls.endog, cls.exogc, | ||
| offset=0.5 * cls.exog[:, cls.idx_c].squeeze()) | ||
| mod.exog_names[:] = ['const', 'x2', 'x3', 'x4'] | ||
| cls.res1 = mod.fit(cov_type=cov_type) | ||
| cls.idx_p_uc = np.arange(cls.exogc.shape[1]) | ||
|
|
||
|
|
||
| class TestGLMGaussianConstrainedHC(ConstrainedCompareMixin): | ||
|
|
||
| @classmethod | ||
| def init(cls): | ||
| cov_type = 'HC0' | ||
| cls.res2 = cls.mod2.fit(cov_type=cov_type) | ||
| mod = GLM(cls.endog, cls.exog) | ||
| mod.exog_names[:] = ['const', 'x1', 'x2', 'x3', 'x4'] | ||
| cls.res1 = mod.fit_constrained('x1=0.5', cov_type=cov_type) |
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.
Uh oh!
There was an error while loading. Please reload this page.