Search code examples
pythonstatsmodelsarima

AttributeError: 'SARIMAXResults' object has no attribute 'append'


I have trained an ARMA model and now I am trying to make a prediction on the validation data without retraining the model. Each time I make a prediction, I want to append the previous target to the model in order to make the next prediction. I am trying to use the method append, but I get an AttributeError, though the method is listed in the documentation.

import numpy
import pandas
from statsmodels.tsa.statespace.sarimax import SARIMAX

N_TRAIN = 100

train_data = pandas.Series(0.05 + numpy.random.randn(N_TRAIN)*0.01)
valid_data = pandas.Series(0.05 + numpy.random.randn(N_TRAIN)*0.01)

model = SARIMAX(
    endog=train_data,
    order=(1, 0, 1),              # (p, d, q)
    seasonal_order=(0, 0, 0, 0),  # (P, D, Q, S)
    trend='ct',                   # 'n', 'c', 't', 'ct'
    enforce_stationarity=False,
    enforce_invertibility=False
)

fitted_model = model.fit(disp=False, maxiter=100, method='powell')

predictions = []
for target in valid_data:
    prediction = fitted_model.forecast()
    predictions.append(prediction)
    fitted_model.append(endog=target, refit=False)
Out:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-79-9c27f92bfa88> in <module>()
     23     prediction = fitted_model.forecast()
     24     predictions.append(prediction)
---> 25     fitted_model.append(endog=target, refit=False)

C:\projects\utilities\anaconda35\lib\site-packages\statsmodels\base\wrapper.py in __getattribute__(self, attr)
     33             pass
     34 
---> 35         obj = getattr(results, attr)
     36         data = results.model.data
     37         how = self._wrap_attrs.get(attr)

AttributeError: 'SARIMAXResults' object has no attribute 'append'

Solution

  • The linked documentation is for the development version of Statsmodels. The most recent release, v0.10, does not include this feature. To use it, you can install the development version from Github. Otherwise, this feature will be in the v0.11 release (no date scheduled yet).