Timeseries Crossvalidation in R: using tsCV() with tslm()-Models

99封情书 提交于 2019-12-08 04:41:27

For time series cross-validation, you should be fitting a separate model to every training set, not passing an existing model. With predictor variables, the function needs to be able to grab the relevant elements when fitting each model, and other elements when producing forecasts.

The following will work.

fc <- function(y, h, xreg)
{
  if(NROW(xreg) < length(y) + h)
    stop("Not enough xreg data for forecasting")
  X <- xreg[seq_along(y),]
  fit <- tslm(y ~ X)
  X <- xreg[length(y)+seq(h),]
  forecast(fit, newdata=X)
}

# Predictors of the same length as the data
# and with the same time series characteristics.    
pred <- ts(rnorm(length(AirPassengers)), start=start(AirPassengers),
           frequency=frequency(AirPassengers))

# Now pass the whole time series and the corresponding predictors 
tsCV(AirPassengers, fc, xreg=pred)

If you have more than one predictor variable, then xreg should be a matrix.

I ended up using a function to forecast a trend. I'm not sure if this is correctly specified but the rmse looks about right.

flm <- function(y, h) { forecast(tslm(y ~ trend, lambda=0), h=h) }

e <- tsCV(tsDF, flm, h=6)
sqrt(mean(e^2, na.rm=TRUE))

@robhyndman

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!