Solved – ARIMA with seasonality in Statsmodels

arimaarmapythonstatsmodelstime series

I'd like to have a seasonal ARIMA model implemented with Statsmodels ARIMA. Specifically, I'd like to log before the weekly seasonality and then be able to make forecasts.

Perhaps an example with ARIMA's from_formula method could accomplish this. I'd also love to be able to do this with patsy.

Here's my sample code for logging before the weekly seasonality, and then transforming back to compare to the original time series (I've also skipped checking the validity of the model through testing stationarity and the residuals):

import pandas as pd
import numpy as np
from statsmodels.tsa.arima_model import ARIMA

# ts is a time series
logged_ts = np.log(ts)
# Differencing by the week forces us to drop the first 7 values.
diffed_logged_ts = (logged_ts - logged_ts.shift(7))[7:]

p = 0
d = 1
q = 1

arima = ARIMA(diffed_logged_ts.values, [p, d, q], exog=None, dates=diffed_logged_ts.index, freq='D', missing='none')
diffed_logged_results = arima.fit(trend='c', disp=False)
predicted_diffed_logged = diffed_logged_results.predict(exog=None, dynamic=False)
predicted_diffed_logged_ts = pd.Series(predicted_diffed_logged, index=diffed_logged_ts.index[d:])
predicted_diffed_logged_ts = np.exp(logged_ts.shift(7) + diffed_logged_ts.shift(d) + predicted_diffed_logged_ts)

concatenated = pd.concat([ts, predicted_diffed_logged_ts], axis=1, keys=['original', 'predicted'])
print concatenated[-7:]

What do you think of this approach? I hope there's a less error-prone way coming in a future version of Statsmodels. Could someone tag this question with "statsmodels"? Thanks!

Best Answer

You are correct that there's currently no good way to do seasonal ARIMA in statsmodels. Currently, I only have a half-baked solution for doing non-consecutive lags, but it's not public anywhere. It's a bit heavy, computations-wise. Unfortunately, I doubt I'll be able to work on this anytime soon (unless someone would be willing to fund the enhancement...). Contributions in this area would be very welcome.

https://github.com/statsmodels/statsmodels/issues/247 https://github.com/statsmodels/statsmodels/issues/232

Related Question