包的tsCV
功能forecast
是一个很好的起点。
从其文档中,
tsCV(y,预测函数,h = 1,窗口 = NULL,xreg = NULL,初始 = 0,...)
让 'y' 包含时间序列 y[1:T]。然后将“预测函数”连续应用于时间序列 y[1:t],对于 t=1,...,Th,做出预测 f[t+h]。误差由 e[t+h] = y[t+h]-f[t+h] 给出。
即首先 tsCV 将模型拟合到 y[1] 然后预测 y[1 + h],然后将模型拟合到 y[1:2] 并预测 y[2 + h] 等等,以此类推。
tsCV 函数返回预测误差。
将其应用于训练数据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
就您而言,也许您应该
- 将模型拟合到 ts.data$x,然后预测 ts.data$xx[1]
- 拟合模式 c(ts.data$x, ts.data$xx[1]) 和预测(ts.data$xx[2]) 等等。