Here is my toy data.
df <- tibble::tribble(
~fund, ~dates, ~y, ~x,
"Fund_A", "03/31/2021", 0.04, 0.04,
"Fund_A", "04/30/2021", 0.04, -0.03,
"Fund_A", "05/31/2021", 0.03, 0.04,
"Fund_A", "06/30/2021", -0.01, 0.03,
"Fund_A", "07/31/2021", -0.06, -0.03,
"Fund_A", "08/31/2021", 0.04, 0.05,
"Fund_A", "09/30/2021", 0.01, -0.04,
"Fund_A", "10/31/2021", 0.02, -0.01,
"Fund_A", "11/30/2021", 0.03, -0.03,
"Fund_A", "12/31/2021", -0.02, 0.06,
"Fund_B", "03/31/2021", 0.01, 0.02,
"Fund_B", "04/30/2021", 0.01, 0.05,
"Fund_B", "05/31/2021", 0.05, -0.05,
"Fund_B", "06/30/2021", 0.01, -0.02,
"Fund_B", "07/31/2021", 0.04, 0.09,
"Fund_B", "08/31/2021", 0.02, -0.01,
"Fund_B", "09/30/2021", 0.02, 0.02,
"Fund_B", "10/31/2021", -0.01, 0.01,
"Fund_B", "11/30/2021", 0.05, 0.01,
"Fund_B", "12/31/2021", -0.03, 0.02
)
I have code that runs the rolling regression and spits out the regression output using slider package.
library(tidyverse)
library(slider)
library(broom)
df %>%
group_by(fund) %>%
mutate(model = slide(.x = cur_data(),
.f = possibly(~(lm(y ~ x, data = .x) %>%
tidy() %>%
filter(term != "(Intercept)")),
otherwise = NA),
.before = 5)) %>%
ungroup() %>%
unnest(model)
Now, I want to be able to run the above code with multiple values of funds and ".before" values and combine the results in one dataframe. In other words, I want the above code to work on say .before = seq(4, 7,1). It would be interesting to see an attempt using purrr map!