File: dates.py

package info (click to toggle)
statsmodels 0.14.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,956 kB
  • sloc: python: 254,365; f90: 612; sh: 560; javascript: 337; asm: 156; makefile: 145; ansic: 32; xml: 9
file content (45 lines) | stat: -rw-r--r-- 1,261 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Using dates with timeseries models
"""
import statsmodels.api as sm
import pandas as pd

# Getting started
# ---------------

data = sm.datasets.sunspots.load()

# Right now an annual date series must be datetimes at the end of the year.

dates = sm.tsa.datetools.dates_from_range('1700', length=len(data.endog))

# Using Pandas
# ------------

# Make a pandas Series or DataFrame with DatetimeIndex
endog = pd.Series(data.endog, index=dates)

# and instantiate the model
ar_model = sm.tsa.AR(endog, freq='A')
pandas_ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)

# Let's do some out-of-sample prediction
pred = pandas_ar_res.predict(start='2005', end='2015')
print(pred)

# Using explicit dates
# --------------------

ar_model = sm.tsa.AR(data.endog, dates=dates, freq='A')
ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)
pred = ar_res.predict(start='2005', end='2015')
print(pred)

# This just returns a regular array, but since the model has date information
# attached, you can get the prediction dates in a roundabout way.

print(ar_res.data.predict_dates)

# 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?