I want to make an rolling stepwise regression with dplyr, do() and rollapply(). My code for the data looks like this:
FUND_DATA <- tibble(
DATE = 1:10,
FUND1 = rnorm(10),
FUND2 = rnorm(10),
FUND3 = rnorm(10),
FUND4 = rnorm(10))
These are just sime price quates from funds for period 1-10. For the independet variables it looks the same:
FACTORS <- tibble(
DATE = 1:10,
x1 = rnorm(10),
x2 = rnorm(10),
x3 = rnorm(10),
x4 = rnorm(10))
Now I make merge the the two tibbles from above as following:
REG_DATA <- FUND_DATA %>%
pivot_longer(contains("FUND"), names_to = "FUND",
values_to = "PRICE") %>% arrange(FUND,DATE) %>% left_join(., FACTORS, by = "DATE") %>%
group_by(FUND) %>% mutate(RET = PRICE/lag(PRICE)-1) %>% drop_na()
So I have some long tibble and grouped by the FUND.
A tibble: 36 x 8
# Groups: FUND [4]
DATE FUND PRICE x1 x2 x3 x4 RET
<int> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2 FUND1 -1.19 -0.422 -0.872 -0.292 -0.176 -2.04
2 3 FUND1 -0.869 1.60 0.247 -0.610 0.170 -0.272
3 4 FUND1 -1.60 0.159 -0.757 0.730 -0.154 0.839
4 5 FUND1 -1.58 -0.688 -0.718 0.778 0.879 -0.0103
5 6 FUND1 1.14 -0.00190 -0.956 1.14 -0.953 -1.72
6 7 FUND1 -0.452 0.730 -0.344 0.925 -0.593 -1.40
7 8 FUND1 -0.809 0.895 -0.987 -0.0791 -0.0133 0.792
8 9 FUND1 1.06 -0.503 1.06 1.96 0.362 -2.31
9 10 FUND1 0.0358 0.359 -0.370 1.27 0.129 -0.966
10 2 FUND2 -0.525 -0.422 -0.872 -0.292 -0.176 -0.229
# ... with 26 more rows
On this data I want to perform a rolling stepwise regression for each fund and store the R^2 for each rolling window and fund. So for each window ths should perform a stepwise regression. I came up with the folling code:
ROLLING <- REG_DATA %>% group_by(FUND) %>% do(R2 = rollapply(., width = 2, function(x){
summary(step(lm(RET ~ x1+x2+x3+x4,
data = .), direction = "both", trace = 0))$r.squared
},by.column = FALSE,align = "right"))
The code is running without errors but the output is the problem. This code only stores the R^2 from the last rolling window (period 8-10) and overwrite the others I think, so it looks like this:
FUND1 c(0.675, 0.675, 0.675,...)
FUND2 c(0.447, 0.447, 0.447,...)
FUND3 .....
Can you guys help me so that the codes stores the R^2 for each window?