Many statistical libraries in R offer the possibility to fit a model and then use the results of optimization to predict values some periods ahead. However, many do not have the possibility to backtest the results out-of-sample.
Therefore, I want to build an R function that allows me to (walk forward approach):
I tried the following (x is the length of the out-of-sample set, n the fixed length of the training set):
for (j in range (0:x)){
append <- vector()
forecast <- vector()
set <- train [j+1:n+j,]
fit <- fit(data = set, model)
forecast <- predict(fit, ahead = 1)
append <- cbind(lubridate::as_date(ts_date[n+j+1]), forecast)
forc <- rbind(forc, append)
}
However, the matrix forc contains only the first and the last result of the loop.
Can anyone spot a mistake here?
Range in R returns only the first and last result from the loop