Hi Jeremy,
On Sun, Apr 19, 2015 at 1:47 PM, Jeremy Pepper <
aac...@gmail.com> wrote:
> I'm trying to understand how to use SARIMAX. To learn, I'm using examples
> found at:
>
>
http://nbviewer.ipython.org/gist/ChadFulton/5127108f4c7025ed2648
>
> Based on what I see there, I believe I've done what I need to do, but
> graphing is giving me trouble. I think this is really due to data type
> issues.
>
> The most relevant example references 'friedman2.dta' as a data file. It
> does the sort of, multi-step-ahead predictions I need to do. The problem
> is, I can't access the 'friedman2.dta' dataset to determine what the example
> dataset looks like. That means I can't actually run the example, I can just
> sort of try to copy it...
The friedman2.dta file is from Stata's documentation, and is available
at
http://www.stata-press.com/data/r12/ts.html
>
> As of the present time, using my code to make predictions, I'm getting numpy
> array outputs that are annoying because I don't know how to use them for
> much of anything. Worse yet, I ONLY get the predicted value, not the
> confidence interval. Herein lies my biggest problem... I switched APIs
> believing I would be able to get confidence interval outputs from my code,
> but I can't seem to figure that part out. The relevant lines are:
>
> # In-sample one-step-ahead predictions and 95% confidence intervals
> (forecast without data)
>
> predict = res.predict(alpha=0.05)
>
> ax.plot(predict.index[-npredict-npre:], predict[0, -npredict-npre:], 'r--',
> label='One-step-ahead forecast');
>
>
> On the left hand on the equal sign I used to have: "predict, cov, ci, idx",
> as in the example. Now I just have: "predict."
As Josef suggested, this was due to a change before the merge with
Statsmodels to make it compatible with other models' `predict` and
`forecast` methods.
>
> When I include the "cove, ci, idx" terms, I get an error. When I don't, I
> get a lovely output of predictions, but I don't get bounds on those
> predictions. As the version of statsmodels I'm using is pre-release, there
> also don't seem to be any help lines on available functionality and proper
> implementation.
>
To get the confidence intervals, you need to retrieve the full results
object (using `full_results=True` as an argument to the `predict` or
`forecast` methods). Then the predictions are available as the
`forecasts` attribute (shape = (k_endog, nobs)), and the associated
covariance matrices are available as the `forecasts_error_cov`
attribute (shape = (k_endog, k_endog, nobs)).
The full code looks like the following:
# In-sample one-step-ahead predictions
predict_res = res.predict(full_results=True)
predict = predict_res.forecasts
cov = predict_res.forecasts_error_cov
idx = res.data.predict_dates._mpl_repr()
# 95% confidence intervals
critical_value = norm.ppf(1 - 0.05 / 2.)
std_errors = np.sqrt(cov.diagonal().T)
ci = np.c_[
(predict - critical_value*std_errors)[:, :, None],
(predict + critical_value*std_errors)[:, :, None],
]
You can then use the confidence intervals using the covariance matrics
as usual. To see a full example, go the the "ARIMA Postestimation:
Example 1 - Dynamic Forecasting" section of the notebook link that
Josef referenced.
Chad