Rollapply in R error in size column of out of sample

I simulate the following dataset :

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

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))%>%
  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 ?

Hi @homerjaysimpson,
One bracket in the wrong spot?

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)
  dplyr::mutate(back = zoo::rollapplyr(rtn, outofsample, mean, fill = NA))

Yes I am so frustrated sorry

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.