I simulate the following dataset :
library(tidyverse)
date = seq(as.Date("2022/1/1"), by = "day", length.out = 252)
close = rnorm(252)
fr = tibble(date,close);fr
the length of the data are
T = dim(fr)[1];T
I want to split the data into in sample period and out of sample period.
outofsample = 50
insample = T - outofsample
insample
In the in sample period dataset to calculate the mean of the data and then in the out of sample period to calculate the mean with rolling window 50 adding one observation until the end:
fr2 = fr%>%
dplyr::mutate(rtn = as.numeric((close - dplyr::lag(close, 2)) / close))%>%
tidyr::drop_na()%>%
dplyr::mutate(back = zoo::rollapplyr(rtn,outofsample,mean),fill = NA)
but R gives me a error that says:
Error: Problem with `mutate()` column `back`.
ℹ `back = zoo::rollapplyr(rtn, outofsample, mean)`.
ℹ `back` must be size 250 or 1, not 201.
what modifications I have to do in order to calculate this out of sample rolling mean? Any help ?