时间序列交叉验证的实现

Implementation of time series cross-validation

我正在处理 M3 比赛月度数据的时间序列 551。

所以,我的数据是:

library(forecast)
library(Mcomp)
# Time Series
# Subset the M3 data to contain the relevant series 
ts.data<- subset(M3, 12)[[551]]
print(ts.data)

我想对样本内间隔的最后 18 个观测值实施时间序列交叉验证

有些人通常会称其为“具有滚动原点的预测评估”或类似的东西。

我怎样才能做到这一点?样本内间隔是什么意思?我必须评估哪个时间序列?

我很困惑,欢迎任何帮助来解决这个问题。

forecast 包的 tsCV 函数是一个很好的起点。

根据其文档,

tsCV(y, forecastfunction, h = 1, window = NULL, xreg = NULL, initial = 0, . ..)

Let ‘y’ contain the time series y[1:T]. Then ‘forecastfunction’ is applied successively to the time series y[1:t], for t=1,...,T-h, making predictions f[t+h]. The errors are given by e[t+h] = y[t+h]-f[t+h].

即先tsCV拟合一个模型到y[1]然后预测y[1+h],接下来拟合一个模型到y[1:2]并预测y[2+h]等等对于 T-h 步骤。

tsCV 函数returns 预测误差。

将其应用于 ts.data

的训练数据
# function to fit a model and forecast
fmodel <- function(x, h){
  forecast(Arima(x, order=c(1,1,1), seasonal = c(0, 0, 2)), h=h)
}
 
# time-series CV
cv_errs <- tsCV(ts.data$x, fmodel, h = 1)

# RMSE of the time-series CV
sqrt(mean(cv_errs^2, na.rm=TRUE))
# [1] 778.7898

在你的情况下,也许你应该

  1. 将模型拟合到 ts.data$x,然后预测 ts.data$xx[1]
  2. 拟合模式 c(ts.data$x, ts.data$xx[1]) 和 forecast(ts.data$xx[2]), 依此类推