Skip to content

ENH: add base Results.fittedvalues and resid - #5255

Open
josef-pkt wants to merge 10 commits into
statsmodels:mainfrom
josef-pkt:base_fittedvalues
Open

josef-pkt wants to merge 10 commits into
statsmodels:mainfrom
josef-pkt:base_fittedvalues

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Sep 24, 2018 •

Copy link
Copy Markdown
Member

see #37 especially #37 (comment)

(unrelated aside: we still have 9 open issues that predate github move)

@josef-pkt

Copy link
Copy Markdown
Member Author

currently this only adds the methods.
AFAICS it shouldn't affect anything at the moment because all models overwrite it except GMM (which should raise an NotImplementedError in a new GMMResults method that is not yet in the unit tests).
running test suite to see whether this is really the case.

@josef-pkt

josef-pkt commented Sep 24, 2018 •

Copy link
Copy Markdown
Member Author

my guess was wrong, it looks like all environments will be failing

one failure in holt-winters
one failure in generic tests that now pick up resid which was not defined in discrete, this might be a test tolerance issue, but needs checking.

self = <statsmodels.base.tests.test_generic_methods.TestGenericGLMPoissonOffset object at 0x7fb22b1225f8>
    def test_zero_collinear(self):

edit Note that this is GLM and not discrete where unit test fails

@josef-pkt

josef-pkt commented Sep 24, 2018 •

Copy link
Copy Markdown
Member Author

adding inherited resid to discrete models without fixing fittedmodels is a serious mistake, but it won't show up in unit tests.
i.e. need unit test for resid is the same as resid_response in GLM and discrete.

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.

@josef-pkt

josef-pkt commented Sep 25, 2018 •

Copy link
Copy Markdown
Member Author

for the glm test that fails
It looks like we don't get high enough convergence agreement for the current tolerance in resid test.

(Pdb) np.max(np.abs(res1.params[:4] / res2.params - 1))
7.4485002610202855e-09
(Pdb) np.max(np.abs(res1.params[:4] - res2.params))
5.4860646026000381e-09

(Pdb) np.max(np.abs(res1.fittedvalues - res2.fittedvalues))
3.1909897870718851e-06
(Pdb) np.max(np.abs(res1.fittedvalues / res2.fittedvalues - 1))
1.0434015429439114e-08

(Pdb) np.max(np.abs(res1.resid / res2.resid - 1))
4.0922609105042973e-05
(Pdb) np.max(np.abs(res1.resid - res2.resid))
3.1909897870718851e-06

(Pdb) np.max(np.abs(res1.predict(linear=True) - res2.predict(linear=True)))
1.0434015429439114e-08
(Pdb) np.max(np.abs(res1.predict(linear=True) / res2.predict(linear=True) - 1))
4.0927118600597723e-05

problem is that the test does not use the same optimizer in both cases

(Pdb) res1.mle_settings
{'wls_method': 'lstsq', 'optimizer': 'IRLS'}

(Pdb) res2.mle_settings
*** AttributeError: 'GLMResults' object has no attribute 'mle_settings'
(Pdb) res2.method
'bfgs'

@josef-pkt

Copy link
Copy Markdown
Member Author

a bit more investigation
When I adjust the unit test so it uses 'bfgs' in both cases, I get even larger differences and assert for bse fails.

this is the direct check of the zero transformed/constrained model against the model with user dropped variables.
Both converge, but 'bfgs' uses one fewer iterations in the results_constrained version than in the dropped variable version.

We don't have mle_retvals attached, so I cannot check, but I guess that the starting values differ.

(Pdb) res1.results_constrained.method
'bfgs'
(Pdb) res1.method   # this is not correctly overwritten in fit_zeros
'nm'

(Pdb) res1.results_constrained.mle_retvals
{'fopt': 1.6588950776356561, 'gopt': array([ -8.82991908e-07,   7.02731930e-06,
 -5.74055974e-06,
        -2.35102165e-06]), 'Hinv': array([[  4.34743060e-01,  -9.19000998e-02,
-9.57986457e-02,
         -1.33122550e-01],
       [ -9.19000998e-02,   9.79532273e-02,  -1.72551866e-02,
          2.19138302e-04],
       [ -9.57986457e-02,  -1.72551866e-02,   1.00655363e-01,
          9.72514169e-04],
       [ -1.33122550e-01,   2.19138302e-04,   9.72514169e-04,
          1.35324378e-01]]), 'fcalls': 10, 'warnflag': 0, 'gcalls': 10, 'converg
ed': True}

(Pdb) res2.mle_retvals
{'fopt': 1.6588950776306406, 'gopt': array([ -5.19194994e-08,  -5.24933229e-08,
 -6.87380167e-08,
        -4.27573734e-08]), 'Hinv': array([[  4.32772228e-01,  -9.11632378e-02,
-9.56646047e-02,
         -1.32160335e-01],
       [ -9.11632378e-02,   9.60384608e-02,  -1.59279563e-02,
          2.96095293e-04],
       [ -9.56646047e-02,  -1.59279563e-02,   9.91078561e-02,
          8.35560140e-04],
       [ -1.32160335e-01,   2.96095293e-04,   8.35560140e-04,
          1.34659104e-01]]), 'fcalls': 11, 'warnflag': 0, 'gcalls': 11, 'converg
ed': True}
(Pdb) res2.method
'bfgs'

(Pdb) res2.mle_settings
*** AttributeError: 'GLMResults' object has no attribute 'mle_settings'

endog and exog are the same:

(Pdb) np.max(np.abs(res1.results_constrained.model.exog - res2.model.exog))
0.0
(Pdb) np.max(np.abs(res1.results_constrained.model.endog - res2.model.endog))
0

@josef-pkt

Copy link
Copy Markdown
Member Author

I am adding mle_settings to GLM with fit_gradient, and add start_params to mle_settings in fit_irls
see #5158 (comment)
for unit test adjustments to get resid to work, unit tests now use start_params also in GLM case

for changes to holtwinters see #5256 (comment)
essentially some minimal refactoring so fittedvalues and resid are correctly, resp., overwritten or inherited.

@josef-pkt

Copy link
Copy Markdown
Member Author

mistake: I forgot to save the module and commit the addition of resid_biased to holtwinters
requires interactive rebase if I want to edit the commit.

@coveralls

coveralls commented Sep 25, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.003%) to 84.21% when pulling b03adf6 on josef-pkt:base_fittedvalues into e1d1156 on statsmodels:master.

@codecov

codecov Bot commented Sep 25, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #5255 into master will increase coverage by <.01%.
The diff coverage is 97.1%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
statsmodels/tsa/tests/test_holtwinters.py 100% <100%> (ø) ⬆️
statsmodels/discrete/count_model.py 93.2% <100%> (ø) ⬆️
statsmodels/base/tests/test_generic_methods.py 96.4% <100%> (-0.59%) ⬇️
statsmodels/discrete/discrete_model.py 87.99% <100%> (+0.01%) ⬆️
statsmodels/genmod/generalized_linear_model.py 90.77% <100%> (+0.06%) ⬆️
statsmodels/genmod/tests/test_glm.py 99.01% <100%> (ø) ⬆️
statsmodels/base/model.py 87.68% <100%> (+0.07%) ⬆️
statsmodels/tsa/holtwinters.py 90.25% <100%> (+0.06%) ⬆️
statsmodels/discrete/tests/test_discrete.py 99.17% <93.33%> (-0.09%) ⬇️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e1d1156...b03adf6. Read the comment docs.

@josef-pkt

Copy link
Copy Markdown
Member Author

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.

(Pdb) self
<statsmodels.discrete.discrete_model.MultinomialResults object at 0x000000000DB6
4E80>
(Pdb) self.resid_response.shape
*** ValueError: operands could not be broadcast together with shapes (944,) (944
,7)

(Pdb) self.resid.shape     # new inherited
*** ValueError: operands could not be broadcast together with shapes (944,) (944
,7)

(Pdb) self.resid_misclassified[:5]
array([ 0.,  0.,  0.,  0.,  0.])

@josef-pkt

Copy link
Copy Markdown
Member Author

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, exog dot params instead of using the more complete implementation in predict.
(I will open new issue for that)

predict(linear=True): this is the original signature, however the new countmodels like NB-P don't have the linear keyword and have which with more options instead.
i.e. replacing fittedvalues = predict(linear=True) does not work across all discrete unit tests that subclass CheckModelResults.

@josef-pkt

Copy link
Copy Markdown
Member Author

side issue, not a problem IIUC:

Both predict() and fittedvalues generically return pandas Series or DataFrame in the multivariate case, converted to by the wrapper.
However, when calling predict() internally, then we get the underlying numpy based results instance (results._results from the outside), so we still need the wrapper for fittedvalues and it does not double convert to pandas.

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()
@josef-pkt

Copy link
Copy Markdown
Member Author

The latest commit "BUG: discrete use fittedvalues=predict(linear=True)"
fixed the missing offset/exposure and missing reference category
but fittedvalues is still the linear predictor

changing the definition to fittedvalues=predict() will require larger changes to unit tests.
AFAICS, many test methods are copied across models in test_discrete and it would be better to streamline those first. which needs test class specific atol and rtol.

@josef-pkt

josef-pkt commented Sep 26, 2018 •

Copy link
Copy Markdown
Member Author

streamlining the discrete unit tests looks difficult.
Too many differences across test classes, models and reference results attributes, and reference results saved at different precision. (3 generations/authors for the main unit tests, plus mine and L1 tests separately)

in results_discrete.py
phat is predicted mean, not separately saved in count models
yhat, fittedvalues is linear prediction NB results only saves the first 10 predictions

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

@josef-pkt

Copy link
Copy Markdown
Member Author

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is getting edited anyway, can you pls document-or-delete the commented-out stuff on line 920?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be skipped or xfailed. I’m pretty sure I have a PR that does that

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@josef-pkt

Copy link
Copy Markdown
Member Author

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'))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this change. Confident you made this optimization in all the appropriate locations?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extraneous parens

return np.dot(self.model.exog, self.params[:self.model.exog.shape[1]])
@property
def resid(self):
return self.resid_response

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the logic that determines when resid should be an alias for resid_response? Could go in a docstring?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment is extraneous

model = self.model
actual = model.endog
pred = np.array(self.predict() > threshold, dtype=float)
pred = np.array(self.fittedvalues > threshold, dtype=float)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good time to pep up these comments?

@@ -3968,7 +3970,7 @@ def resid_generalized(self):
for the Logit model.
"""
# Generalized residuals

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extraneous comment

Comment thread statsmodels/base/model.py

@cache_readonly
def fittedvalues(self):
return self.predict()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the base class this is liable to return a pandas object right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, from inside the results class self is without wrapping, so it's always numpy ndarray
I made a comment about this not inline.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look again, but as I recall the Results.predict method doesn't conform to the usual patterns.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking again, I'm still under the same impression I was before. Am I more confused than usual?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this method now unnecessary?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. just get it from the base class

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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'))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make this an explicit keyword in __init__.


@property
def resid(self):
return self.resid_response

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed?

def test_jac(self):
pass

#NOTE: The bse is much closer precitions to stata

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precitions --> precision? predictions?



class TestGeneralizedPoisson_p2(object):
class TestGeneralizedPoisson_p2():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna call it a night, will pick up on this file in the morning.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From here down seems like it should be a separate test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up updating this in the PR^2.

@josef-pkt

Copy link
Copy Markdown
Member Author

return self.predict() is internal and returns numpy

This is wrong, predict() converts to pandas in the main method and doesn't use the wrapper class.
ie. fittedvalues = self.predict() converts to pandas twice.
(The reason is that there is no fixed index for predict(exog))

fiittedvalues and resid are included in _attrs in base.model.LikelihoodResultsWrapper

if self is results instance, then self.predict() has more overhead compared to self.model.predict(self.params), but not much most cases in base.models.Results.predict only apply if exog is not None.

@jbrockmendel

Copy link
Copy Markdown
Contributor

OK, I didn't think it was a problem, but its the kind of thing that can go unnoticed.

self.predict() has more overhead compared to self.model.predict(self.params)

Do we have a guarantee that these two calls are always equivalent, wrapping notwithstanding?

@josef-pkt

Copy link
Copy Markdown
Member Author

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.
So I'm pretty sure they are equivalent, but there is no guarantee, i.e. no unit tests to verify for all models.
GLM and similar have the kwargs handling in model.predict, but most of the time I don't check tsa for these cases.
We also don't have generic unit tests (I didn't write any) that include survival and MixedLM, AFAIK.
e.g. MixedLMResults overrides fittedvalues and resid but not predict, and I never looked at those (and have no guess on any "guarantees")
AFAICS based on a quick look, MixedLMResults.fittedvalues includes predicted random effects, predict does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants