Search code examples
rback-testing

How to build a loop function for out-of-sample backtesting?


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

  1. Define a training set using a moving window (each looping time, remove oldest observation & add most recent)
  2. Run optimizer thus calibrating the model
  3. Use the calibrated model to generate n step ahead forecast
  4. Store the new forecast in a vector of out-of-sample predicted values (together with the date of forecast)
  5. 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?


Solution

  • Range in R returns only the first and last result from the loop