I'm trying to run a rolling regression with dplyr
. I'm using rollapplyr
from package zoo
and lsfit
as I'm only interested in the beta of the regression. Here's what I've tried:
library(dplyr); library(zoo)
df1 = expand.grid(site = seq(10),
year = 2000:2004,
day = 1:50)
df1 %>%
group_by(year) %>%
mutate(beta1 = rollapplyr(data = site,
width = 5,
FUN = lsfit,
x=day))
I'm getting this error: Error: not all arguments have the same length
I think rollapplyr
accepts non-zoo objects but I may be wrong. It could also be that the piping (%>%
) does not play well with rollapplyr
as it requires a data object in the function.
Any idea?
EDIT My question is different from: rolling regression with dplyr I want to use pipes in order to use group_by