Repository navigation
Conversation
|
currently this only adds the methods. |
|
my guess was wrong, it looks like all environments will be failing one failure in holt-winters edit Note that this is GLM and not discrete where unit test fails |
|
adding inherited we also need a generic consistency check that fittedvalues is allclose/equal to predict(), which is currently skipped for discrete models, and likely not included for tsa models. |
|
for the glm test that fails problem is that the test does not use the same optimizer in both cases (Pdb) res1.mle_settings (Pdb) res2.mle_settings |
|
a bit more investigation this is the direct check of the zero transformed/constrained model against the model with user dropped variables. We don't have mle_retvals attached, so I cannot check, but I guess that the starting values differ. endog and exog are the same: |
|
I am adding for changes to holtwinters see #5256 (comment) |
|
mistake: I forgot to save the module and commit the addition of |
Codecov Report
@@ Coverage Diff @@
## master #5255 +/- ##
==========================================
+ Coverage 81.71% 81.71% +<.01%
==========================================
Files 569 569
Lines 89699 89692 -7
Branches 10028 10032 +4
==========================================
- Hits 73294 73293 -1
+ Misses 14132 14125 -7
- Partials 2273 2274 +1
Continue to review full report at Codecov.
|
|
another fail in testing all CheckModelResults with added assert resid == resid_response MNLogit doesn't have a working resid_response, raises exception, same as new inherited resid. |
|
there are still more inconsistencies in discrete MNLogit MultinomialResults uses inherited fittedvalues which does not use predict() and has fittedvalues smaller shape then predict(), fittedvalues is based on params and does not include the reference category. BUG: DiscreteResults.fittedvalues does not take offset and exposure into account, i.e. explicit computation,
|
|
side issue, not a problem IIUC: Both |
This adds offset and exposure handling to fittedvalues. This fixes missing reference category in MNLogit fittedvalues. resid and resid_response are defined a (w)endog - predict()
|
The latest commit "BUG: discrete use fittedvalues=predict(linear=True)" changing the definition to |
|
streamlining the discrete unit tests looks difficult. in results_discrete.py TestGeneralizedPoisson_p2 does not subclass CheckModelResults, and if I change it, then I get several attribute errors in the unit test where reference results are not available, e.g. llnull |
|
latest commit needs amending to delete disabled tests I refactored most of the predict/fittedvalues related test methods to use inheritance, but only refactored only a few of the others. conf_int and similar need test class specific tolerance, with related switch from assert_almost_equal to assert_allclose, but that requires more work (not directly related to this PR). |
| [ 3.609675, 3.856716]] | ||
| # [ 1.28360034e+00, 1.34979803e+00]] | ||
| self.fittedvalues = [ 0.8487497 , 0.8487497 , 0.8487497 , 0.8487497, | ||
| self.linpred = [ 0.8487497 , 0.8487497 , 0.8487497 , 0.8487497, |
There was a problem hiding this comment.
Since this is getting edited anyway, can you pls document-or-delete the commented-out stuff on line 920?
There was a problem hiding this comment.
I did a global rename in the module, but didn't edit nor read it otherwise
My guess is that the commented out line 920 is the extra either alpha or lnalpha confint, but without using Stata to replicate, I don't know for sure. AFAIR, Stata reports both, but we have only one or the other.
| assert_almost_equal(self.res1.bse, self.res2.bse, DECIMAL_3) | ||
|
|
||
| def test_params(self): | ||
| def _test_params(self): |
There was a problem hiding this comment.
temporary disabling so that inherited version is used
disabling made refactoring and debugging easier because I could switch back and forth comparing both versions.
Those will be deleted, now that travis is also green
| def _test_predict(self): | ||
| pass | ||
|
|
||
| def test_predict_xb(self): |
There was a problem hiding this comment.
These should be skipped or xfailed. I’m pretty sure I have a PR that does that
There was a problem hiding this comment.
those actually have reference numbers and are now tested, at least in Negative Binomial
the inherited test method is active, assert fails if I change the numbers.
| disp=0) | ||
| res2 = RandHIE() | ||
| res2.negativebinomial_nb1_bfgs() | ||
| #res2.phat = np.exp(res2.fittedvalues) |
There was a problem hiding this comment.
one of my trials to retrofit the results, can be deleted again
It turned out it's simpler to add the exp to test_predict, as long as only count models are missing phat.
4555132 to
9c629b4
Compare
|
amended and force pushed with cleanup, travis and appveyor were both green before |
| def _dispersion_factor(self): | ||
| mu = self.predict(which='linear') | ||
| w = 1 - self.predict() / np.exp(self.predict(which='linear')) | ||
| w = 1 - self.fittedvalues / np.exp(self.predict(which='linear')) |
There was a problem hiding this comment.
+1 for this change. Confident you made this optimization in all the appropriate locations?
There was a problem hiding this comment.
I'm pretty sure that there are more outside of discrete in the generic code.
I only did a search for 'predict()' in discrete_model and count_model.
There was a problem hiding this comment.
If I track them down and point them out, mind adding them in this PR? Follow-up PRs are liable to languish.
| mu = np.exp(self.predict(which='linear')) | ||
| w = 1 - self.predict() / mu | ||
| w = 1 - self.fittedvalues / mu | ||
| return ((1 + alpha * mu**p)**2 + w * mu) |
| return np.dot(self.model.exog, self.params[:self.model.exog.shape[1]]) | ||
| @property | ||
| def resid(self): | ||
| return self.resid_response |
There was a problem hiding this comment.
What is the logic that determines when resid should be an alias for resid_response? Could go in a docstring?
There was a problem hiding this comment.
Only GLM and discrete models have resid_response, because Skipper thought it was ambiguous what resid without qualifier would be.
Now. the definition is that resid without qualifier is the same as resid_response.
There was a problem hiding this comment.
Thanks; putting that in a docstring would be helpful.
| @@ -3802,7 +3804,7 @@ def resid_pearson(self): | |||
| For now :math:`M_j` is always set to 1. | |||
| """ | |||
| # Pearson residuals | |||
| model = self.model | ||
| actual = model.endog | ||
| pred = np.array(self.predict() > threshold, dtype=float) | ||
| pred = np.array(self.fittedvalues > threshold, dtype=float) |
There was a problem hiding this comment.
possibly specify np.float64?
| p = self.fittedvalues | ||
| #Y_0 = np.where(exog == 0) | ||
| #Y_M = np.where(exog == M) | ||
| #NOTE: Common covariate patterns are not yet handled |
There was a problem hiding this comment.
Good time to pep up these comments?
| @@ -3968,7 +3970,7 @@ def resid_generalized(self): | |||
| for the Logit model. | |||
| """ | |||
| # Generalized residuals | |||
|
|
||
| @cache_readonly | ||
| def fittedvalues(self): | ||
| return self.predict() |
There was a problem hiding this comment.
In the base class this is liable to return a pandas object right?
There was a problem hiding this comment.
No, from inside the results class self is without wrapping, so it's always numpy ndarray
I made a comment about this not inline.
There was a problem hiding this comment.
I'll look again, but as I recall the Results.predict method doesn't conform to the usual patterns.
There was a problem hiding this comment.
After looking again, I'm still under the same impression I was before. Am I more confused than usual?
There was a problem hiding this comment.
I think we are talking about different things
return self.predict() is internal and returns numpy
fittedvalues itself is wrapped (AFAIR), so it will be pandas from the user side.
self in the model is without wrapper, but the user gets the wrapped instance.
There was a problem hiding this comment.
I think we are talking about different things
This seems entirely plausible; I appreciate you taking the time to help sort through my confusion.
My claim is that the unwrapped Results.predict method returns a pandas object (assuming PandasData), as a result of which the unwrapped Results.fittedvalues will be a pandas object. Not that this is a problem per se, just not readers/contributors would usually expect.
| @cache_readonly | ||
| def resid_response(self): | ||
| return self.model.endog - self.predict() | ||
| return self.model.endog - self.fittedvalues |
There was a problem hiding this comment.
docstring? I think there is one on a subclass that is generally applicable
| are also handled. | ||
| """ | ||
| return self.model.endog - self.predict() | ||
| return self.model.endog - self.fittedvalues |
There was a problem hiding this comment.
Isn't this method now unnecessary?
There was a problem hiding this comment.
i.e. just get it from the base class
There was a problem hiding this comment.
(more generally, I expect there are a bunch of subclasses where this is the case)
| .0081338625, .0910979921, .025300888, 1.059954821] | ||
| self.bse = np.reshape(bse, (6,-1), order='F') | ||
| self.yhat = np.loadtxt(os.path.join(cur_dir,'yhat_mnlogit.csv')) | ||
| self.linpred = np.loadtxt(os.path.join(cur_dir,'yhat_mnlogit.csv')) |
There was a problem hiding this comment.
as long as the git history is getting touched here anyway, mind adding a space after the comma?
| self.llr = 2092.1425097129977 # old value 1317.3888595620811 | ||
| self.llr_pvalue = 0 # old value 5.4288002863296022e-278 | ||
| self.fittedvalues = [ 0.89348994, 0.89348994, 0.89348994, | ||
| self.linpred = [ 0.89348994, 0.89348994, 0.89348994, |
There was a problem hiding this comment.
mind removing space after [
|
|
||
| hwfit = HoltWintersResults(self, self.params, fittedfcast=fitted, | ||
| fittedvalues=fitted[:-h - 1], fcastvalues=fitted[-h - 1:], | ||
| _fittedvalues=fitted[:-h - 1], fcastvalues=fitted[-h - 1:], |
There was a problem hiding this comment.
please make this an explicit keyword in __init__.
|
|
||
| @property | ||
| def resid(self): | ||
| return self.resid_response |
There was a problem hiding this comment.
like in discrete, docstring describing wen resid == resid_response
| # pvalues for NB2 and NBP2 has lower agreement | ||
| cls.rtol_pvalues = 0.04 | ||
|
|
||
| def test_jac(self): |
| def test_jac(self): | ||
| pass | ||
|
|
||
| #NOTE: The bse is much closer precitions to stata |
There was a problem hiding this comment.
precitions --> precision? predictions?
|
|
||
|
|
||
| class TestGeneralizedPoisson_p2(object): | ||
| class TestGeneralizedPoisson_p2(): |
There was a problem hiding this comment.
Any particular reason?
| @@ -61,10 +61,17 @@ def test_conf_int(self): | |||
| assert_allclose(self.res1.conf_int(), self.res2.conf_int, rtol=8e-5) | |||
|
|
|||
| def test_zstat(self): | |||
There was a problem hiding this comment.
I'm gonna call it a night, will pick up on this file in the morning.
There was a problem hiding this comment.
Still need to look at this file. Will do so after upcoming meeting.
| self.llf = -44077.91 | ||
| self.bse = [0.3653, 1.6694, 0.02178, 0.01163, 0.0217496] | ||
| self.aic = 88165.81 No newline at end of file | ||
| self.aic = 88165.81 |
There was a problem hiding this comment.
All of these tiny red arrows can be removed 5099
| assert_allclose(fitted[:len(res2_fitted)], res2_fitted, rtol=5e-4) | ||
|
|
||
| # fittedvalues in discrete are currently linear prediction | ||
| assert_allclose(self.res1.fittedvalues[:len(res2_fitted)], |
There was a problem hiding this comment.
should this be conditioning on res2_fitted is not None?
| assert_allclose(self.res1.fittedvalues[:len(res2_fitted)], | ||
| res2_fitted, atol=5e-4, rtol=5e-4) | ||
|
|
||
| if self.res1.params.ndim == 2: |
There was a problem hiding this comment.
From here down seems like it should be a separate test
There was a problem hiding this comment.
Maybe test_resid_response? Could then remove a smoke test in CheckBinaryResults
| res2_linpred = getattr(self.res2, 'linpred', None) | ||
| if res2_linpred is not None: | ||
| assert_allclose(linpred[:len(res2_linpred)], res2_linpred, | ||
| atol=5e-4, rtol=1e-4) |
There was a problem hiding this comment.
ATM TestNegativeBinomialPNB2Newton.test_predict_xb has rtol=-1e-7. In the PR^2 I'll offer an alternative to keep this tighter tolerance (... after I get back from lunch)
| resid_a, self.res1.resid_response)) | ||
| assert_allclose(resids, resid2, rtol=1e-6, atol=atol) | ||
|
|
||
| assert_allclose(self.res1.resid, self.res1.resid_response, rtol=1e-13) |
There was a problem hiding this comment.
In principal this should be a separate method (I'll update the PR^2 to do this). Besides which, the top of this test function has some comments suggesting something somewhere needs fixing; keeping narrow the code that comment refers to will make sorting that out easier.
| @@ -1206,12 +1205,6 @@ def test_conf_int(self): | |||
| def test_jac(self): | |||
There was a problem hiding this comment.
I'm getting close to a "LGTM". I'm hoping we can take care of these test_jacs and the commented-out test_cov_params while this module is fresh in your head. Any thoughts?
There was a problem hiding this comment.
I ended up updating this in the PR^2.
This is wrong,
if self is results instance, then |
|
OK, I didn't think it was a problem, but its the kind of thing that can go unnoticed.
Do we have a guarantee that these two calls are always equivalent, wrapping notwithstanding? |
Based on the code in base Results predict, there is nothing before the model.predict call if exog is None. |
see #37 especially #37 (comment)
(unrelated aside: we still have 9 open issues that predate github move)