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. The time index corresponds to the last period of the training data. The columns correspond to the forecast horizons.
Let y
contain the time series forecastfunction
is applied successively to the time series
forecastfunction
to very short time
series.
CV, CVar, residuals.Arima, https://robjhyndman.com/hyndsight/tscv/.
# NOT RUN {
#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)
# }
Run the code above in your browser using DataLab