如何在Python中使用ARIMAX模型预测值?



我试图在训练样本(内生和外生变量)上拟合ARIMAX模型,然后使用外生变量(它们是可用的)进行预测。我在Python中使用statsmodels模块。

我有以下代码:

#split datasets
df_train = df.iloc[:100]
df_test= df.iloc[100:104]
# Define the model
model = ARIMA(endog= df_train['y'], exog=df_train[['x1', 'x2']], order=(2,0,2))
# Fit the model
results = model.fit()
#predict for the next 5 periods
results.predict(steps = 5,  exog = df_test[['pc1', 'pc2']])

不幸的是,它似乎使用训练数据集而不是测试数据集来预测样本内拟合,因为有100个预测点。

如果模型中有2个滞后,那么我应该从训练数据集中添加y的最后2个点还是不应该(results以某种方式保留有关y最后值的信息)?

我已经发现了类似的问题,然而,它们与r有关。

使用forecast方法:

results.forecast(steps=5,  exog=df_test[['pc1', 'pc2']])

最新更新