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?
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.