I am trying to calculate the rolling mean (last 20 Periods) of stock returns by a certain ID (called PERMNO). Firstly, I just tried to create the rolling mean. I worked on it with this code:
CRSP_dt[, rolltest1 := rollmeanr(Return, 20, na.rm = TRUE, fill = NA)]
which did not work. However, when I try it with this code:
CRSP_dt[, rolltest2 := rollapplyr(Return, 20, mean, na.rm = TRUE, fill = NA)]
it actually works, so I am not entirely sure, why?
Secondly, I tried to calculate the rolling mean by PERMNO, so I added the "by" addition:
CRSP_dt[, rolltest2 := rollapplyr(Return, 20, mean, na.rm = TRUE, fill = NA),
by = PERMNO]
Now, it returns an error "Error in [.data.table(CRSP_dt, , :=(rolltest2, rollapplyr(Return, : Type of RHS ('logical') must match LHS ('double'). To check and coerce would impact performance too much for the fastest cases. Either change the type of the target column, or coerce the RHS of := yourself (e.g. by using 1L instead of 1)". Since then I have been trying to convert the target column into a "Double" too, by simply using an as.numeric function, resulting in:
CRSP_dt[, rolltest2:= as.numeric(rolltest2)]
[, rolltest2 := rollapplyr(Return, 20, mean, na.rm = TRUE, fill = NA),
by = PERMNO]
Unfortunately, it still gives out the same error code, so I am not sure, how to proceed from here.