Predict based on lag(prediction)?

A poor example that nevertheless illustrates the question / need:

example_df <- data.frame(
  x = 1:5
) %>% 
  mutate(cumulative = x + lag(x), 
         somepredictor = rnorm(5))

# very bad example, I would not do this in real life
mod <- lm(cumulative ~ lag(cumulative), data = example_df)

With this model, how could I predict on new data? The catch is that I need the prediction from the previous row as an input to the current prediction, so regular old predict(newdf) won't do.

Is there a way that I can predict based on a lag in this way?

I am sure there is something far more elegant than this, but...

nPredictions <- 27
predictions <- rep(NA,nPredictions)
predictions[1] <- mod$coefficients[1] + mod$coefficients[2]*last(example_df$cumulative)

for (i in 2:nPredictions){
  predictions[i] <- mod$coefficients[1] + mod$coefficients[2]*predictions[i-1]

Thanks, this does indeed answer the question as I had posted it but when I came to implementing I realized it won't work in my particular case and I'm going to make a fresh post about that, essentially the issue is that I'm working on a grouped df. Thank you for this though.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.