【问题标题】:Rolling forecast with xreg and re-estimation使用 xreg 和重新估计进行滚动预测
【发布时间】:2020-09-12 15:22:58
【问题描述】:

我尝试通过重新估计将 xreg 实施到我的滚动预测中。 不幸的是,我遇到了 xreg 长度的问题。

# sample data  
sample <- ts(rnorm(100, mean = 1000, sd=7), start = c(2012,1), end = c(2019,12), frequency = 12)
external <- ts(mtcars, start = c(2012,1), end = c(2019,12), frequency = 12)

#Define h --> One-step ahead (for a start, later to be increased)
h <- 1
#specify length to forecast
test  <- window(sample, start = c(2018,01), end = c(2019,12), frequency = 12)
n <- length(test) - h + 1
#provide total length of regressors available
total_xreg <- ts(external[,c(1,2,3)], start = c(2012,1), end= c(2019,12), frequency = 12)

#create empty matrix
fcmatx <- matrix(0, nrow=n, ncol=h)

# create loop
for(i in 1:n)
{  
# x is the target variable, provide training data 
  x <- window(sample, end= c(2017,12) + (i-1)/12)
# provide xregs for training data
  xregs <- window(total_xreg, end = c(2017,12) + (i-1)/12)
# provide new xregs for forecasting, assuming that xreg is available for the forecasting period
  xregs2 <- window(total_xreg, start = c(2018,1) + (i-1)/12
# limit xregs2 to show only the first line since we are only forecasting 1 step in advance
 xregs3 <- xregs2[1,]
# create auto.arima model
  refit.multirex <- auto.arima(x, xreg = xregs)
# forecast using regressors
  fcmatx[i,] <- forecast(refit.multirex, 
                         h=h, 
                         xreg = xregs3
                         )$mean
}
fcmattsx <- ts(fcmatx, start = c(2018,1), frequency = 12)

这会导致以下错误:

Error in forecast.forecast_ARIMA(refit.multirex, h = h, xreg = xregs3) : 
  Number of regressors does not match fitted model

h 的长度为 1,xregs 的长度为 3,因为我填写了 3 个变量,但它们都只在一段时间内。我尝试了各种调整,但无法正确调整。

【问题讨论】:

  • 您的代码无法运行。请编辑您的帖子以提供Minimal (!) Working (!) Example
  • 我用示例数据替换了 cody,现在它应该可以运行了@StephanKolassa,对不起!

标签: regression forecasting cross-validation arima


【解决方案1】:

下面一行

xregs3 <- xregs2[1,]

返回一个向量而不是一个矩阵。当您从矩阵中提取单个列或行时,这是 R 中的默认行为。改成

xregs3 <- xregs2[1,,drop=FALSE]

保持矩阵结构(1x3)。那么forecast()函数就不会返回错误了。

i=23 时会出现不同的错误,因为在创建 xregs2 时在 end 之后有 start

【讨论】:

  • 感谢您对矩阵结构的帮助。我改变了这一点,还用更长的 ts 替换了回归量:total_xreg &lt;- ts(external[,c(1,2,3)], start = c(2012,1), end = c(2020,5), frequency = 12)。现在运行顺利,谢谢!
  • @robjhyndman 是否有机会在每次迭代时打印出选定的 ARIMA 顺序,以显示模型选择的变化情况?
  • as.character(refit.multirex) 将打印出 ARIMA 模型。
猜你喜欢
  • 2021-09-19
  • 2021-10-18
  • 2019-03-19
  • 2018-09-08
  • 2017-06-26
  • 2013-02-09
  • 2014-04-18
  • 2016-10-28
相关资源
最近更新 更多