我正在使用该forecast包并auto.arima使用xreg. 在这里,我只想预测 1 年,但我无法h在函数中使用参数forecast。以下是其原因:
手册中给出了定义(F1检查):
h = "预测期数,但如果使用 xreg,则忽略 'h',预测期将为行数"
请建议我另一种h用于特定时期预测的方法。
我正在使用该forecast包并auto.arima使用xreg. 在这里,我只想预测 1 年,但我无法h在函数中使用参数forecast。以下是其原因:
手册中给出了定义(F1检查):
h = "预测期数,但如果使用 xreg,则忽略 'h',预测期将为行数"
请建议我另一种h用于特定时期预测的方法。
使用xreg表明您有外部(外生)变量。在此,回归模型适合具有 ARIMA 误差的外部变量。
预测时,您需要提供这些外部变量的未来值。在实践中,这些通常是预测或可能是已知的。例如,如果您尝试预测销售额并且使用广告支出作为外部变量,您可能知道来年的广告支出。
auto.arima然后产生对长度的预测xreg,因此不予考虑h。
根据您在下面的评论,我提供了一个示例脚本,基于上面的销售示例演示了这一点。
library(forecast)
# Generate sample data
sales <- sample(100:170, 4*10, replace = TRUE)
advertising <- sample(50:70, 4*10, replace = TRUE)
# Create time series objects.
sales_ts <- ts(sales, frequency = 4, end = c(2017, 4))
fit <- auto.arima(sales_ts, xreg = advertising)
# If we pass external_regressor into the forecast, h will be disregarded and we will
# get a forecast for length(external_regressor)
wrong_forecast = forecast(fit, h = 4, xreg = advertising)
length(wrong_forecast) # Will be 40
# To forecast four quarters in advance, we must provide forecasted external regressor data
# for the upcoming four quarters, so that length(new_regressor) == 4.
# In reality, this data is either forecasted from another forecast, or is known. We'll randomly generate it.
upcoming_advertising <- sample(50:70, 4, replace = TRUE)
correct_forecast <- forecast(fit, xreg = upcoming_advertising)
length(correct_forecast$mean) # Will be 4
需要注意的关键事项是:
如果我们使用与生成预测时相同的回归量进行预测,h则将被忽略,并且将针对xreg您的情况生成 10 年的预测。
因此,我们必须提供xreg我们希望预测的时间长度的新数据——在您的情况下,为 4 个季度。