使用 tscv auto.arima 预测 R 中的预测值

forecasting with tscv auto.arima predicted values in R

我想使用 auto.arima 函数进行样本外预测实验。此外,应应用具有固定滚动 window 大小的时间序列交叉验证。目标是获得提前 1,3 和 6 步的一步预测。

library(forecast)
library(tseries)

#the time series
y1 = 2+ 0.15*(1:20) + rnorm(20,2)
y2 = y1[20]+ 0.3*(1:30) + rnorm(30,2)
y =  as.ts(c(y1,y2))

#10obs in test set, 40obs in training set
ntest <- 10
ntrain <- length(y)-ntest

#auto.arima with some prefered specifications
farima <- function(x,h){forecast(auto.arima(x,ic="aic",test=c("adf"),seasonal=FALSE, 
                                        stepwise=FALSE, approximation = FALSE,
                                        method=c("ML")),h=h)}

# executing the following function, gives the forecast errors in a matrix for each one-step forecast
e <- tsCV(y,farima,h = 6,window=40)

预测值由真实值减去误差得到:

#predicted values
fc1 <- c(NA,y[2:50]-e[1:49,1])
fc1 <- fc1[41:50]

fc3 <- c(NA,y[2:50]-e[1:49,3])
fc3 <- fc3[41:50]

fc6 <- c(NA,y[2:50]-e[1:49,6])
fc6 <- fc6[41:50]

不过我很好奇前面 3 步的预测值是否编码正确。既然第一个3步超前预测就是第43次观测的预测? 我也不明白为什么前 3 步错误 [第 3 列] 的矩阵 e 具有观察 40 的值。因为我认为前 3 步预测是针对观察 43 获得的,因此不应该有观察错误 40.

经常阅读帮助文件:

Value

Numerical time series object containing the forecast errors as a vector (if h=1) and a matrix otherwise. The time index corresponds to the last period of the training data. The columns correspond to the forecast horizons.

所以 tsCV() returns 矩阵中的错误,其中第 (i,j) 个条目包含预测原点 i 和预测范围 h 的错误。因此,第 40 行和第 3 列中的值是在时间段 43 的时间 40 发生的三步错误。

感谢您的帮助!

因此对于前面的 h=1,2,3 步,预测值如下:

#predicted values
#h=1
fc1 <- c(NA,y[41:50]-e[40:49,1])
fc1 <- fc1[2:11]

#h=2
fc2 <- c(NA,y[42:50]-e[40:49,2])
fc2 <- fc2[2:10]

#h=3
fc3 <- c(NA,y[43:50]-e[40:49,3])
fc3 <- fc3[2:9]

对吗?