I have a matrix (V) with daily stock returns between a certain time period on which I try to get the variance of the preceding 1-month (=21 days) and 6-months (=126 days).
This is how I would do it based on future 1-month and 6-months data.
Variance.x <- rollapply(data=V, width=21, var, by.column=F, align="right")
Variance.x <- rollapply(data=V, width=126, var, by.column=F, align="right")
Any suggestions?