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):
- Define a training set using a moving window (each looping time, remove oldest observation & add most recent)
- Run optimizer thus calibrating the model
- Use the calibrated model to generate n step ahead forecast
- Store the new forecast in a vector of out-of-sample predicted values (together with the date of forecast)
- Loop through 1-4 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
do? Seems like a pythonism that doesn’t belong. $\endgroup$