Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

tsCV

Time series cross-validation


Description

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

Usage

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

Arguments

y

Univariate time series

forecastfunction

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. If exogenous predictors are used, then it must also have xreg and newxreg arguments corresponding to the training and test periods.

h

Forecast horizon

window

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

xreg

Exogeneous predictor variables passed to the forecast function if required.

initial

Initial period of the time series where no cross-validation is performed.

...

Other arguments are passed to forecastfunction.

Details

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]. If h=1, these are returned as a vector, e[1: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.

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.

Author(s)

Rob J Hyndman

See Also

Examples

#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)

#Example with exogenous predictors
far2_xreg <- function(x, h, xreg, newxreg) {
  forecast(Arima(x, order=c(2,0,0), xreg=xreg), xreg=newxreg)
}

y <- ts(rnorm(50))
xreg <- matrix(rnorm(100),ncol=2)
e <- tsCV(y, far2_xreg, h=3, xreg=xreg)

forecast

Forecasting Functions for Time Series and Linear Models

v8.14
GPL-3
Authors
Rob Hyndman [aut, cre, cph] (<https://orcid.org/0000-0002-2140-5352>), George Athanasopoulos [aut], Christoph Bergmeir [aut] (<https://orcid.org/0000-0002-3665-9021>), Gabriel Caceres [aut], Leanne Chhay [aut], Mitchell O'Hara-Wild [aut] (<https://orcid.org/0000-0001-6729-7695>), Fotios Petropoulos [aut] (<https://orcid.org/0000-0003-3039-4955>), Slava Razbash [aut], Earo Wang [aut], Farah Yasmeen [aut] (<https://orcid.org/0000-0002-1479-5401>), R Core Team [ctb, cph], Ross Ihaka [ctb, cph], Daniel Reid [ctb], David Shaub [ctb], Yuan Tang [ctb] (<https://orcid.org/0000-0001-5243-233X>), Zhenyu Zhou [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.