linearmodels icon indicating copy to clipboard operation
linearmodels copied to clipboard

ENH: Add predict()

Open ybdesire opened this issue 8 years ago • 19 comments

https://stackoverflow.com/questions/47645280/how-to-do-predict-for-linearmodels

ybdesire avatar Dec 05 '17 02:12 ybdesire

There is probably some room for improvement here. So far I have avoided predict since it can be ambiguous in some cases. For example, what should the prediction be in a first difference OLS? Another challenge with predict in fixed effects regressions is that the fixed effects are not actually estimated but are purged from the data using a possibly iterative method. It is conceptually easy to recover the combined fixed effects, It is harder to recover the separate fixed effects (although not impossible).

bashtage avatar Dec 05 '17 08:12 bashtage

Thanks @bashtage for the answer. Do you have any suggestion for this question?

ybdesire avatar Dec 05 '17 14:12 ybdesire

I'm biased towards linearmodels. All of the quantities you require can be computed from the outputs provided, although it requires some knowledge of the underlying model and mathematics. For example, if you want entity and time effects, and time is short, then you could include time dummies in your model which would allow you to get the time effects. Then you have the relationship

effects = dependent - exog @ params - resids 

which will be the entity effects.

bashtage avatar Dec 05 '17 23:12 bashtage

It seems this predict feature has been merged into latest 4.2 release?

If so, how can I use predict() for below code ?

from linearmodels import PanelOLS
mod = PanelOLS(dat.gdp, dat[['para1','para2','para3']],weights=None)
res = mod.fit(cov_type='clustered', cluster_entity=True)

I tried 4.2, but neither mod.predict() nor res.predict() works.

ybdesire avatar Dec 07 '17 13:12 ybdesire

It is not releases. You can use predict on res as in

res.predict(fitted=True,effects=True,idiosyncratic=True)

to get in sample fitted values. Out of sample prediction has not been implemented.

bashtage avatar Dec 07 '17 14:12 bashtage

Thanks @bashtage for the help. Looking forward to this new feature.

ybdesire avatar Dec 07 '17 15:12 ybdesire

This is all fixed and will be out in PyPi in the next couple of days. Look for version 4.5.

bashtage avatar Dec 11 '17 18:12 bashtage

is it ready to use already?

cludovique avatar Mar 05 '19 13:03 cludovique

In-sample is there, out-of-sample has not been implemented.

bashtage avatar Mar 14 '19 12:03 bashtage

I've been using AbsorbingLS() class to fit a few Linear regression with high-dimensional effects models. Works great.

Been wondering if it is possible to get fitted values of dependent variable, as in PanelOLS().fit().Predict() shown above. I have been reading about AborsorbingLSResults().fitted_values , but not sure if it is what I am lookig for.

colagiovannifranco avatar Sep 28 '20 20:09 colagiovannifranco

fitted values are in-sample. There is no easy way to do out-of-sample prediction in AbsorbingLS since the absorbed coefficients are not saved. Is that your question?

bashtage avatar Sep 28 '20 21:09 bashtage

I think that is what I mean. Buy I may have not been clear enough. Sorry, not a native english speaker. I will try to explain.

I am running an AborsbingLS() regression and getting results: mod = AbsorbingLS(dependent = dep, exog = exog , absorb = fe) print(mod.fit())

I want to save each observation's fitted value from that regression, so I can be able to use those values as exog variable to run another Regression. You could think of it as 2SLS.

colagiovannifranco avatar Sep 28 '20 21:09 colagiovannifranco

Yes, then fitted_values is what you want.

bashtage avatar Sep 28 '20 21:09 bashtage

Thanks for the help @bashtage !

colagiovannifranco avatar Sep 28 '20 22:09 colagiovannifranco

Hi @bashtage, is out of sample working now? I estimated a model int his package but i also need to predict the next year now and when i use predict i dont get it ofcourse. Any help is appreciated!

Ahad-boop avatar Mar 31 '21 14:03 Ahad-boop

Still not implemented. I'm going to reopen this as an enhancement.

bashtage avatar Mar 31 '21 21:03 bashtage

Hello @bashtage, regarding this issue of out-of-sample prediction, I noticed that right now the function allows for the input 'exog', which I think suggests it should work for additional data, is that correct? But I haven't managed to make it work, so I just wanted to confirm if the enhancement was already implemented or not, thanks!

lsparanhos avatar Aug 26 '21 09:08 lsparanhos

Hi @bashtage, same question as above. The exog seems to indicate this should work with out-of-sample data but I have had no luck implementing this. Is this feature implemented?

ariboyarsky avatar Oct 08 '21 04:10 ariboyarsky

@bashtage , any updates on predict for test data?

OleksiyAnokhin avatar Mar 11 '22 11:03 OleksiyAnokhin