In [1]: import statsmodels.api as sm
In [2]: import pandas
In [3]: data = sm.datasets.sunspots.load()
Right now an annual date series must be datetimes at the end of the year.
In [4]: dates = sm.tsa.datetools.dates_from_range('1700', length=len(data.endog))
Make a pandas TimeSeries or DataFrame
In [5]: endog = pandas.TimeSeries(data.endog, index=dates)
and instantiate the model
In [6]: ar_model = sm.tsa.AR(endog, freq='A')
In [7]: pandas_ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)
Let’s do some out-of-sample prediction
In [8]: pred = pandas_ar_res.predict(start='2005', end='2015')
In [9]: print pred
2005-12-31 20.003293
2006-12-31 24.703991
2007-12-31 20.026129
2008-12-31 23.473652
2009-12-31 30.858570
2010-12-31 61.335450
2011-12-31 87.024687
2012-12-31 91.321249
2013-12-31 79.921624
2014-12-31 60.799522
2015-12-31 40.374882
Freq: A-DEC, dtype: float64
In [10]: ar_model = sm.tsa.AR(data.endog, dates=dates, freq='A')
In [11]: ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)
In [12]: pred = ar_res.predict(start='2005', end='2015')
In [13]: print pred
[ 20.0033 24.704 20.0261 23.4737 30.8586 61.3355 87.0247 91.3212
79.9216 60.7995 40.3749]
This just returns a regular array, but since the model has date information attached, you can get the prediction dates in a roundabout way.
In [14]: print ar_res.data.predict_dates
<class 'pandas.tseries.index.DatetimeIndex'>
[2005-12-31 00:00:00, ..., 2015-12-31 00:00:00]
Length: 11, Freq: A-DEC, Timezone: None
This attribute only exists if predict has been called. It holds the dates associated with the last call to predict. .. TODO: should this be attached to the results instance?