From b2f92c51c0f41778b153e6edc49d87e6d3cfffbd Mon Sep 17 00:00:00 2001 From: "Richard T. Guy" Date: Thu, 31 Oct 2013 22:20:59 -0700 Subject: [PATCH 1/2] ENH: Add test on norm_resid. This means switching it to a cached property. --- statsmodels/regression/linear_model.py | 17 +++++------ .../tests/results/results_regression.py | 29 +++++++++++++++++++ .../regression/tests/test_regression.py | 5 ++++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/statsmodels/regression/linear_model.py b/statsmodels/regression/linear_model.py index b4e06b1d335..8827e5db970 100644 --- a/statsmodels/regression/linear_model.py +++ b/statsmodels/regression/linear_model.py @@ -114,6 +114,9 @@ def df_resid(self): def df_resid(self, value): self._df_resid = value + def whiten(self, X): + raise NotImplementedError("Subclasses should implement.") + def fit(self, method="pinv", **kwargs): """ Full fit of the model. @@ -1122,7 +1125,7 @@ def HC3_se(self): self._HC3_se = np.sqrt(np.diag(self.cov_HC3)) return self._HC3_se - #TODO: this needs a test + @cache_readonly def norm_resid(self): """ Residuals, normalized to have unit length and unit variance. @@ -1130,10 +1133,6 @@ def norm_resid(self): Returns ------- An array wresid/sqrt(scale) - - Notes - ----- - This method is untested """ if not hasattr(self, 'resid'): raise ValueError('need normalized residuals to estimate standard ' @@ -1691,19 +1690,19 @@ class RegressionResultsWrapper(wrap.ResultsWrapper): 'HC0_se' : 'columns', 'HC1_se' : 'columns', 'HC2_se' : 'columns', - 'HC3_se' : 'columns' + 'HC3_se' : 'columns', + 'norm_resid' : 'rows', } _wrap_attrs = wrap.union_dicts(base.LikelihoodResultsWrapper._attrs, _attrs) - _methods = { - 'norm_resid' : 'rows', - } + _methods = {} _wrap_methods = wrap.union_dicts( base.LikelihoodResultsWrapper._wrap_methods, _methods) + wrap.populate_wrapper(RegressionResultsWrapper, RegressionResults) diff --git a/statsmodels/regression/tests/results/results_regression.py b/statsmodels/regression/tests/results/results_regression.py index 427a928c8a5..17c950b2a3e 100644 --- a/statsmodels/regression/tests/results/results_regression.py +++ b/statsmodels/regression/tests/results/results_regression.py @@ -43,6 +43,14 @@ def __init__(self): -17.26893, -39.05504, -155.54997, -85.67131, 341.93151, -206.75783)) + # Obtained from R using m$residuals / sqrt(sum(m$residuals * m$residuals) / m$df.residual) + self.norm_resid = np.array((0.87694426, -0.30838998, + 0.15183385, -1.34528175, 1.01594375, + -0.81780510, -0.53812289, -0.04323497, + 0.04692334, 1.49381010, -0.05664654, + -0.12811061, -0.51024404, -0.28102399, + 1.12162357, -0.67821900)) + def conf_int(self): # a method to be consistent with sm return [(-177.0291,207.1524), (-.111581,.0399428),(-3.125065, -.9153928),(-1.517948,-.5485049),(-.5625173,.4603083), @@ -130,6 +138,20 @@ def __init__(self): -100.632293701, 34.9342498779, -96.6685943604, -367.32925415, -40.1300048828, -72.8692245483, -60.8728256226, -35.9937324524, -222.944747925] + self.norm_resid = \ + [-0.90569581, -0.75496938, -1.28663890, -0.11309411, -0.24746253, -0.47181831, + -1.02062293, -0.31403683, 1.62862142, -0.84973225, -0.42919669, -0.78007426, + -0.63913772, 0.29787637, -0.38364568, -0.21381846, -0.85577361, 0.54156452, + -0.48496031, -1.15374603, 0.41145856, 0.23996158, 2.70305838, -0.53171027, + 0.79057028, 1.82433320, -0.04150362, 0.97048328, 0.13667658, 0.26750667, + -0.12690810, 0.11703354, -0.72689772, 0.34160874, 0.71332338, -0.75079661, + 1.73137185, -0.39477348, 0.04107215, -0.11332274, 0.22952063, -0.88580496, + -0.67239515, -0.17656300, 4.48867723, 2.61499898, -0.16988320, -0.63136893, + -0.68135396, 0.06351572, -0.64467367, -0.37800911, -0.64304809, 1.88607184, + 0.57624742, 0.60875207, -0.78636761, -0.17897383, -0.21716827, -0.07885570, + -0.57566752, 0.25202879, -0.29176531, -0.54378274, 0.30203654, -0.57460072, + -0.72378394, -0.23853382, -0.17325464, -0.24121979, -0.10269489, -0.57826451] + def conf_int(self): # a method to be consistent with sm return [( -10.2936, 4.90523), ( 5.949595, 310.9044), @@ -165,6 +187,13 @@ def __init__(self): 201.321121216, 215.908889771, 73.0936813354, 913.216918945, 424.824859619, -8.56475830078, -361.329742432, 27.3456058502, 151.28956604, -492.499359131] + # Obtained from R using m$residuals / sqrt(sum(m$residuals * m$residuals) / m$df.residual) + self.norm_resid = [0.58906369, -0.27427213, 0.19094881, + -0.84457419, -0.92697740, -1.14390695, + 0.42368630, 0.45438671, 0.15382784, + 1.92189233, 0.89405658, -0.01802479, + -0.76042924, 0.05754964, 0.31839340, + -1.03647964] def conf_int(self): return [(-341.6373, 235.6502), ( .0038583, .1382881), diff --git a/statsmodels/regression/tests/test_regression.py b/statsmodels/regression/tests/test_regression.py index d4a1e2a9fba..852fb36ecbc 100644 --- a/statsmodels/regression/tests/test_regression.py +++ b/statsmodels/regression/tests/test_regression.py @@ -135,6 +135,11 @@ def test_resids(self): assert_almost_equal(self.res1.resid, self.res2.resid, self.decimal_resids) + decimal_norm_resids = DECIMAL_4 + def test_norm_resids(self): + assert_almost_equal(self.res1.norm_resid, self.res2.norm_resid, + self.decimal_norm_resids) + #TODO: test fittedvalues and what else? class TestOLS(CheckRegressionResults): From c6d82f8b2242649a3eb3e2d90feeebe4fc81b163 Mon Sep 17 00:00:00 2001 From: "Richard T. Guy" Date: Fri, 1 Nov 2013 19:54:05 -0700 Subject: [PATCH 2/2] DOC: noted breaking change in v0.6 changelog --- docs/source/release/version0.6.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/release/version0.6.rst b/docs/source/release/version0.6.rst index dfb8e76a05a..1075fef9e87 100644 --- a/docs/source/release/version0.6.rst +++ b/docs/source/release/version0.6.rst @@ -20,7 +20,7 @@ Other important new features ---------------------------- * Other new changes can go -* In a +* In a * Bullet list Major Bugs fixed @@ -33,14 +33,14 @@ Major Bugs fixed Backwards incompatible changes and deprecations ----------------------------------------------- -* Note any backwards incompatible changes here +* RegressionResults.norm_resid is now a readonly property, rather than a function. Development summary and credits ------------------------------- A blurb about the number of changes and the contributors list. -.. note:: +.. note:: Obtained by running ``git log v0.5.0..HEAD --format='* %aN <%aE>' | sed 's/@/\-at\-/' | sed 's/<>//' | sort -u``.