user6083088
user6083088

Reputation: 1037

Refit Python Statsmodel ARIMA model parameters to new data and predict

I've stored the coefficients of intercept, AR, MA off ARIMA model of statsmodel package

x = df_sku
x_train = x['Weekly_Volume_Sales']

x_train_log = np.log(x_train)
x_train_log[x_train_log == -np.inf] = 0
x_train_mat = x_train_log.as_matrix()

model = ARIMA(x_train_mat, order=(1,1,1))
model_fit = model.fit(disp=0)
res = model_fit.predict(start=1, end=137, exog=None, dynamic=False)
print(res)
params = model_fit.params

But I'm unable to find any documentation on statsmodel that lets me refit the model parameters onto a set of new data and predict N steps.

Has anyone been able to accomplishing refitting the model and predicting out of time samples ?

I'm trying to accomplish something similar to R:

# Refit the old model with testData
new_model <- Arima(as.ts(testData.zoo), model = old_model)

Upvotes: 2

Views: 2753

Answers (2)

Armin Okić
Armin Okić

Reputation: 315

Here is a code you can use:

def ARIMAForecasting(data, best_pdq, start_params, step):
    model = ARIMA(data, order=best_pdq)
    model_fit = model.fit(start_params = start_params)
    prediction = model_fit.forecast(steps=step)[0]
    #This returns only last step
    return prediction[-1], model_fit.params

#Get the starting parameters on train data
best_pdq = (3,1,3) #It is fixed, but you can search for the best parameters

model = ARIMA(train_data, best_pdq)
model_fit = model.fit()
start_params = model_fit.params
    
data = train_data
predictions = list()
for t in range(len(test_data)):
    real_value = data[t]
    prediction = ARIMAForecasting(data, best_pdq, start_params, step)
    predictions.append(prediction)
    data.append(real_value)
#After you can compare test_data with predictions

Details you can check here: https://www.statsmodels.org/dev/generated/statsmodels.tsa.arima_model.ARIMA.fit.html#statsmodels.tsa.arima_model.ARIMA.fit

Upvotes: 3

Curious Watcher
Curious Watcher

Reputation: 689

Great question. I have found such example: https://alkaline-ml.com/pmdarima/develop/auto_examples/arima/example_add_new_samples.html

briefly:

import pmdarima as pmd

...


### split data as train/test:
train, test = ...


### fit initial model on `train` data:
arima = pmd.auto_arima(train)

...

### update initial fit with `test` data:
arima.update(test)

...

### create forecast using updated fit for N steps:
new_preds = arima.predict(n_periods=10)

Upvotes: 0

Related Questions