Skip to content

Prediction interval - #2151

Merged
josef-pkt merged 4 commits into
statsmodels:masterfrom
josef-pkt:prediction_interval
Feb 9, 2015
Merged

josef-pkt merged 4 commits into
statsmodels:masterfrom
josef-pkt:prediction_interval

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

adding get_prediction to linear_model RegressionResults

everything is written in standalone function for reuse in other models, but some things are linear model specific. (It's not a generic function.)

main issue for regression.linear model #719
also several discussions on mailing list

SUMMary issue for extension to other models #2150
some implementation discussion for wrapping #2145

Status

so far mainly the basic outline, I'm trying to get that settled first
I didn't check much of the details yet.
unit test against old sandbox function.
now also method for GLM with link handling, conf_int is endpoint-transformation, see #938

PR also contains statsmodels.genmod._prediction.params_transform_univariate, currently as a helper function for simple non-linear prediction
useful for #2235 add eform for exponentiated parameters

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) when pulling 20c09fd on josef-pkt:prediction_interval into 923aaf3 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

still need OrderedDict from compat for python 2.6

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) when pulling 7e4b9a6 on josef-pkt:prediction_interval into 923aaf3 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) when pulling 666cd19 on josef-pkt:prediction_interval into 923aaf3 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) when pulling 080ad67 on josef-pkt:prediction_interval into 923aaf3 on statsmodels:master.

@josef-pkt josef-pkt mentioned this pull request Jan 8, 2015
29 tasks
@kshedden

kshedden commented Jan 8, 2015

Copy link
Copy Markdown
Contributor

This will be extremely useful to me. Among other applications, when using b-splines in a regression fit, it is nearly always useful to plot the predicted value against the covariate to see what the fitted functional form is. This will make that much easier than it currently is.

One thing that would make it even more useful is to have the option to only partially specify the exog data at which the predictions are made. Unspecified variables would be automatically set to the mean or some other functional of the marginal distribution of that covariate in the training data. I think this is something like the behavior of "at" in Stata margins, but for the usual predictions conditioned on all covariates.

@josef-pkt

Copy link
Copy Markdown
Member Author

@kshedden Thanks for the comments. Any comment on interface, namings?

I'd like to merge this PR relatively soon, without adding many extra features now, but setting up the code in a way that it can be extended without breaking backwards compatibility (much).

I opened other issues, mainly the margin review #2154 for the predict_at features.

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 our convention is (going to be) mean_se we have effect_se somewhere

similar obs_se, obs is not really good, but I don't know an alternative

@josef-pkt

Copy link
Copy Markdown
Member Author

I'd like to get this merged for 0.7

I expect this will become popular soon as replacement for the sandbox function, and we need to get the interface stable fast.
However, this PR doesn't quite do it yet.

???

@kshedden

kshedden commented Feb 8, 2015

Copy link
Copy Markdown
Contributor

I didn't read the code carefully, but I was just thinking about the scope of what a "predict" should do. It seems like the idea is to support most forms of post-estimation inference for fitted conditional means or new observed responses (but not post-estimation inference for parameters and inference for the model structure). As such, "predict" is sort of a misnomer because it suggests exclusively the setting where the target is a (random) observation. But it's conventional due to R to call it "predict".

Thus the scope for "prediction" includes point estimation/prediction, standard errors, tests, and intervals for the estimated/predicted values (no tests for predicted values). All this seems to be covered here.

Everything should work seamlessly for either model.exog or a new exog. This seems to be handled well here.

Other things I like that aren't here yet:

  • Contrasts for fitted values. Say you have several covariates and you want to set some of them to fixed values and the others change, say by 1SD. The is basically a contrast for fitted values. I did this in predict_functional by adding a second exog2 keyword. There may be alternative ways to do this like constructing two prediction results separately and then passing them into a contrasting helper function.

  • Multiple testing: this is basically a convenience method for doing lots of tests/intervals, so simultaneity issues are relevant. Someone could just take the p-values and feed them through a multitest procedure. But since the tests/intervals all come from the same model they are likely to be quite correlated, so approaches like Scheffe (when applicable) should be less conservative.

  • Convenience methods for setting up a new exog. I like to compare fitted values at different points in the covariate space, defined say be quantiles of individual variables. Stata has convenience methods for this with 'at', which I haven't totally figured out. Setting aside the differences between marginal and conditional fitted values/predictions, it seems to me like some type of convenience method for constructing new exog's would be useful.

    Some other issues come up in specific models:

  • predict for lme4 in R has been somewhat controversial in terms of how to handle predictions and uncertainty for random effects

  • predict for Cox models involves estimation of the baseline hazard, so a lot of special handling is needed

@josef-pkt

Copy link
Copy Markdown
Member Author

I pretty much agree with the requirements and wishlist. I opened several issues for individual pieces when I was working on this PR.

Stata distinguishes predict from test. predict predict_nl calculate results for each observation, but the prediction can be for any linear or non-linear function of the parameters and exog. test test_nl are not specific to observations. margins and contrasts are in between.

Our t_test is already vectorized and can be used for linear prediction. When we have a t_test_nl, then it can be used also for generic nonlinear prediction.
My guess is that we keep predict for properties of the conditional expectation E(y | x, params_estimated) and maybe of the conditional distribution F(y | x, params_estimated), and use predict_xxx for other special cases or for a generic version.

More discussion about the general design and requirements should be in the SUMMary issue #2150 so it doesn't get lost in the comments of a closed PR.

@josef-pkt
josef-pkt force-pushed the prediction_interval branch from 080ad67 to 67f5635 Compare February 9, 2015 19:14
@josef-pkt

Copy link
Copy Markdown
Member Author

rebased and force pushed
I will merge this when travis is green, so I can go to other things

I have a notebook that shows that it's reasonably easy to work with this
http://nbviewer.ipython.org/gist/josef-pkt/4c8d1f0eadf03375d8aa

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) to 83.27% when pulling 67f5635 on josef-pkt:prediction_interval into 4e0cd0e on statsmodels:master.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants