Back to Statsmodels

Dates in timeseries models

examples/notebooks/tsa_dates.ipynb

0.15.0.dev0978 B
Original Source

Dates in timeseries models

python
import matplotlib.pyplot as plt
import pandas as pd
import statsmodels.api as sm
from statsmodels.tsa.ar_model import AutoReg, ar_select_order

plt.rc("figure", figsize=(16, 8))
plt.rc("font", size=14)

Getting started

python
data = sm.datasets.sunspots.load()

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

python
from datetime import datetime

dates = pd.date_range("1700-1-1", periods=len(data.endog), freq="YE-DEC")

Using Pandas

Make a pandas TimeSeries or DataFrame

python
data.endog.index = dates
endog = data.endog
endog

Instantiate the model

python
selection_res = ar_select_order(endog, 9, old_names=False, seasonal=True, period=11)
pandas_ar_res = selection_res.model.fit()

Out-of-sample prediction

python
pred = pandas_ar_res.predict(start="2005", end="2027")
print(pred)
python
fig = pandas_ar_res.plot_predict(start="2005", end="2027")