tsCV computes the forecast errors obtained by applying forecastfunction to subsets of the time series y using a rolling forecast origin.

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



Univariate time series


Function to return an object of class forecast. Its first argument must be a univariate time series, and it must have an argument h for the forecast horizon.


Forecast horizon


Length of the rolling window, if NULL, a rolling window will not be used.


Other arguments are passed to forecastfunction.


Numerical time series object containing the forecast errors as a vector (if h=1) and a matrix otherwise.


Let y contain the time series \(y_1,\dots,y_T\). Then forecastfunction is applied successively to the time series \(y_1,\dots,y_t\), for \(t=1,\dots,T-h\), making predictions \(\hat{y}_{t+h|t}\). The errors are given by \(e_{t+h} = y_{t+h}-\hat{y}_{t+h|t}\). If h=1, these are returned as a vector, \(e_1,\dots,e_T\). For h>1, they are returned as a matrix with the hth column containing errors for forecast horizon h. The first few errors may be missing as it may not be possible to apply forecastfunction to very short time series.

See also

CV, CVar, residuals.Arima, https://robjhyndman.com/hyndsight/tscv/.


#Fit an AR(2) model to each rolling origin subset far2 <- function(x, h){forecast(Arima(x, order=c(2,0,0)), h=h)} e <- tsCV(lynx, far2, h=1) #Fit the same model with a rolling window of length 30 e <- tsCV(lynx, far2, h=1, window=30)