1

I'm stuck on a problem that I can't solve no matter how hard I try.

Imagine that you have 3 variables as described below. The y that you want to predict has 5 observations less than a or b. What I want is: run an OLS with the last 10 obs and predict one ahead. Use this new one as the original value to predict the 12th obs and so on.

I guess I'd do this easily, but I'm in love with this functional programming wave, so I keep trying...

    library(tidyverse)
    library(slider)  
    regex <- tibble(a = rnorm(10), 
                    b = rnorm(10),
                    y = rnorm(10))%>% add_row(a = rnorm(5), b = rnorm(5))
    
        regex <- regex %>%
 mutate(Pred= lag(slide(., ~stats::predict(lm(y ~  . , .x)), .before = 10, .complete = F)%>%
 map_dbl(., last)))

Also, I don't know if I'm doing anything wrong by collect the last element of the list generated by stats::predict() and changing its position with lag... this is a smaller problem, but if anyone finds a better solution I'd be pleased to know.

Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41

0 Answers0