Repository navigation
Description
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.
- do some basic regression
- 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
josef-pkt commentedon Mar 21, 2013
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.predictis currently inherited from the top-level Results class, AFAICS.jseabold commentedon Mar 21, 2013
On Thu, Mar 21, 2013 at 8:35 AM, Josef Perktold [email protected]:
Yes, and it calls the Model.predict, I believe, as we discussed before.
Perhaps
Model.predictstays the same, butResults.predictgets theoption to compute confidence intervals, etc. if available using the results
instance
predict_stdbefore the call toModel.predict. We could evenmake these options dynamically generated from available methods that being
predict_(down the road). First, we could add them explicitly to theregression models. Cf.
ARMAResults.forecast.josef-pkt commentedon Mar 21, 2013
@kurtforrester forgot to ask
Is there a use case for different levels
level=(0.95, 0.90)BTW: usually I (we) define
alpha=0.05as thealpha = 1 - leveljosef-pkt commentedon Mar 21, 2013
for example
LikelihoodModelResults.confintkurtforrester commentedon Mar 21, 2013
@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
levelversusalphaI was just borrowing more from the R example than using thestatsmodelsusage.josef-pkt commentedon Mar 21, 2013
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.
josef-pkt commentedon Jul 12, 2013
http://stackoverflow.com/questions/17559408/confidence-and-prediction-intervals-with-statsmodels/17560456
ltrabuco commentedon Feb 19, 2014
+1
jseabold commentedon Sep 26, 2014
Recent ARIMA changes may provide a good template for this.
Unfortunately, I'm bumping this to 0.7 since no one has a PR.
josef-pkt commentedon Dec 18, 2014
t_test, ContrastResults is now a template for presenting the results
also DiscreteMargins have similar pattern to what's required
naming convention: ContrastResults uses
sdinstead ofseor similar.josef-pkt commentedon Sep 26, 2020
I'm closing this as outdated
the pattern is now to have
get_predictionmethods to include standard errors and confidence/prediction intervalspredictstays unchanged with just predicting expected values or similar statistics.