R custom predict algorithm implementation problem

Viewed 29

Let's say I have a series c(1,2,3,4,5,6,7,8,9,10) and a model that can predict another number basing on "m" last numbers. We split the series in proportion 5:5 (training, testing). Then I need to write a function that does the predictions over time. What I have is:

predictor <- function(data, model, m){
 low <- length(data) * 1/2
 high <- length(data) - 1
 predictions <- c()
 for (i in low:high){
  a <- i - m + 1
  predictions <- append(predictions, predict(model, data[a : i]))
 }
 return(predictions)
}

Is this function actually correct? And can it be done in any better way? Unfortunately the predict(model, newdata=testing, h=1) will not work here.

0 Answers
Related