【问题标题】:Refit Python Statsmodel ARIMA model parameters to new data and predict将 Python Statsmodel ARIMA 模型参数重新拟合为新数据并进行预测
【发布时间】:2018-02-19 09:37:44
【问题描述】:

我已经存储了 statsmodel 包的 ARIMA 模型的截距、AR、MA 系数

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

但是我找不到任何关于 statsmodel 的文档,可以让我将模型参数重新拟合到一组新数据并预测 N 个步骤。

有没有人能够完成模型的重新拟合和超时样本的预测?

我正在尝试完成类似于 R 的事情:

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

【问题讨论】:

  • 首先我认为您的意思是model.fit(),而不是model_fit(),对吗?其次,您是否存储(腌制或类似)fitted = model.fit()?这样更容易预测:fitted.predict().
  • 它是关于使用旧模型的系数(如 AR、MA)预测新值,截取新数据。

标签: python python-3.x statsmodels arima


【解决方案1】:

这是一个你可以使用的代码:

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)
    predictions.append(prediction)
    data.append(real_value)
#After you can compare test_data with predictions

您可以在此处查看详细信息: https://www.statsmodels.org/dev/generated/statsmodels.tsa.arima_model.ARIMA.fit.html#statsmodels.tsa.arima_model.ARIMA.fit

【讨论】:

  • 首先感谢您发布此功能。由于某种原因,我无法编辑;因此作为评论:您要么需要从 ARIMAForecasting 函数中删除 step 参数,要么将其添加到调用中。
【解决方案2】:

很好的问题。我找到了这样的例子:https://alkaline-ml.com/pmdarima/develop/auto_examples/arima/example_add_new_samples.html

简单地说:

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)

【讨论】:

    猜你喜欢
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    相关资源
    最近更新 更多