PageRenderTime 49ms CodeModel.GetById 45ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/tsa/ex_dates.py

http://github.com/statsmodels/statsmodels
Python | 47 lines | 16 code | 13 blank | 18 comment | 0 complexity | aecaed14e53fab57646d3cf98e60cf2f MD5 | raw file
 1"""
 2Using dates with timeseries models
 3"""
 4import statsmodels.api as sm
 5import numpy as np
 6import pandas
 7
 8# Getting started
 9# ---------------
10
11data = sm.datasets.sunspots.load()
12
13# Right now an annual date series must be datetimes at the end of the year.
14
15from datetime import datetime
16dates = sm.tsa.datetools.dates_from_range('1700', length=len(data.endog))
17
18# Using Pandas
19# ------------
20
21# Make a pandas TimeSeries or DataFrame
22endog = pandas.TimeSeries(data.endog, index=dates)
23
24# and instantiate the model
25ar_model = sm.tsa.AR(endog, freq='A')
26pandas_ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)
27
28# Let's do some out-of-sample prediction
29pred = pandas_ar_res.predict(start='2005', end='2015')
30print pred
31
32# Using explicit dates
33# --------------------
34
35ar_model = sm.tsa.AR(data.endog, dates=dates, freq='A')
36ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)
37pred = ar_res.predict(start='2005', end='2015')
38print pred
39
40# This just returns a regular array, but since the model has date information
41# attached, you can get the prediction dates in a roundabout way.
42
43print ar_res._data.predict_dates
44
45# This attribute only exists if predict has been called. It holds the dates
46# associated with the last call to predict.
47#..TODO: should this be attached to the results instance?