Skip to content

method signature of "predict" for regression predictions (mean, confidence interval, prediction interval) #719

Closed

Description

@kurtforrester

I am a new user of the statsmodels module and use it for a very limited case performing OLS regression on mostly continuous data.

I have however found an area that I feel could be improved, at least in terms of my current workflow.

Below is my workflow and how I would like to see the predict method work.

  1. do some basic regression
  2. print the results
model = ols('LREO_recovery ~ HREO_recovery', df)
results = model.fit()
print results.summary()

As for communicating the results I produce some scatter plots and line plots of the regressed mean. Ideally, I would like to include, without much additional code, the confidence interval of the mean and a prediction interval for new observations.

I have been reading on the R-project website and based on the call signature for their OLS predict I have come up with the following example (written in pseudo-python) as an enhanced predict method.

results.predict(values=values, interval=None, level=0.95)

# values : array-like or DataFrame
# interval : single value in [None,"confidence","prediction","both"]
# level :  float, list or tuple where [level_ci, level_pi]

I am aware there may be other use cases for the other regression techniques of which I not currently familiar with as I only use OLS, but this is a starting point (or end).

If if the predict method is called with the following signature:

results.predict()

or,

results.predict(values=array)
results.predict(values=DataFrame)

then the predict method would behave as it currently does.

Alternatively, if the method is called with any of the following signatures,

results.predict(values=DataFrame, interval="confidence", level=0.95)
# above call would return a DataFrame which includes the columns 'mean',
# 'uci', 'lci' for each of the observation points in the supplied DataFrame 
# at a 95 % confidence interval.

results.predict(values=DataFrame, interval="prediction", level=0.90)
# above call would return a DataFrame which includes the columns 'mean',
# 'upi', 'lpi' for each of the observation points in the supplied DataFrame 
# at a 90 % prediction interval.

results.predict(values=DataFrame, interval="both", level=(0.95, 0.90))
# above call would return a DataFrame which includes the columns 'mean',
#  'uci', 'lci', 'upi', 'lpi' for each of the observation points in the 
# supplied DataFrame at a 95 % confidence interval for the mean 
# and a 90 % prediction interval for a new observation.

results.predict(values=DataFrame, interval="both", level=0.95)
# above call would return the same as the example previous but 
# the PI and CI levels would both be 0.95.

the behaviour I would like to see would happen as noted in the comments. The cases presented above would also hold for when an array is supplied and the returned object could/would be an array or record array.

Any thoughts or comments? I would classify myself as a software user but would offer my time to assist in the development if this has any traction and direction.

Activity

  1. josef-pkt commented on Mar 21, 2013

    @josef-pkt
    Member

    Thanks for the very explicit proposal.

    We need something like this. The only undecided point we had last time, was whether we are adding this to the predict method itself, or create an extra method for it.

    and, except for OLS, it doesn't have unit tests yet.

    I will have a look today, how this would play with the other models. results.predict is currently inherited from the top-level Results class, AFAICS.

  2. jseabold commented on Mar 21, 2013

    @jseabold
    Member

    On Thu, Mar 21, 2013 at 8:35 AM, Josef Perktold [email protected]:

    I will have a look today, how this would play with the other models.
    results.predict is currently inherited from the top-level Results class,
    AFAICS.

    Yes, and it calls the Model.predict, I believe, as we discussed before.
    Perhaps Model.predict stays the same, but Results.predict gets the
    option to compute confidence intervals, etc. if available using the results
    instance predict_std before the call to Model.predict. We could even
    make these options dynamically generated from available methods that being
    predict_ (down the road). First, we could add them explicitly to the
    regression models. Cf. ARMAResults.forecast.

  3. josef-pkt commented on Mar 21, 2013

    @josef-pkt
    Member

    @kurtforrester forgot to ask

    Is there a use case for different levels
    level=(0.95, 0.90)

    BTW: usually I (we) define alpha=0.05 as the alpha = 1 - level

  4. josef-pkt commented on Mar 21, 2013

    @josef-pkt
    Member

    for example LikelihoodModelResults.confint

  5. kurtforrester commented on Mar 21, 2013

    @kurtforrester
    Author

    @josef-pkt I do not have a specific use for it, at least not now. Alternatively it may serve as a means to overload the method call to produce multiple levels (0.01, 0.025, 0.05) without computing the predicted mean each time (which is different to the case usage that I provided)

    As for level versus alpha I was just borrowing more from the R example than using the statsmodels usage.

  6. josef-pkt commented on Mar 21, 2013

    @josef-pkt
    Member

    allowing for multiple, vectorized alpha would be a good idea, since it comes almost for free with broadcasting. Each confidence interval bound would be an array.

    separate alpha_mean alpha_prediction would be possible independent of this, but cannot think of case where I would want this.

  7. ltrabuco commented on Feb 19, 2014

    @ltrabuco

    +1

  8. added this to the 0.6 milestone on Feb 19, 2014
  9. jseabold commented on Sep 26, 2014

    @jseabold
    Member

    Recent ARIMA changes may provide a good template for this.

    Unfortunately, I'm bumping this to 0.7 since no one has a PR.

  10. modified the milestones: 0.7, 0.6 on Sep 26, 2014
  11. josef-pkt commented on Dec 18, 2014

    @josef-pkt
    Member

    t_test, ContrastResults is now a template for presenting the results
    also DiscreteMargins have similar pattern to what's required

    naming convention: ContrastResults uses sd instead of se or similar.

  12. josef-pkt commented on Sep 26, 2020

    @josef-pkt
    Member

    I'm closing this as outdated

    the pattern is now to have get_prediction methods to include standard errors and confidence/prediction intervals
    predict stays unchanged with just predicting expected values or similar statistics.

  13. modified the milestones: 0.7, 0.12 on Jan 27, 2021
  14. modified the milestones: 0.12, 0.13 on Sep 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions