# Use sliding window to find variance for seasonal time series in R

I would like to estimate the variance of a time series. Say, if the time series has a period of 24, and I want to estimate the variance using

$$\sigma_t^2 = \frac{1}{2k+1} \sum^k_{-k} (y_{t+24k} - \bar{y}_t)^2$$

where $\bar{y}_t = \frac{1}{2k+1} \sum^k_{-k} y_{t+24k}$.

I found some posts suggest using roll apply, but it seems to only work when estimating variance using neighbourhood data, which does not take the period into account.

Does anyone which function to use in this case?

• Even though the question is phrased in terms of asking for an R function, I believe there is a genuine statistical question here, so I would not close this thread. @Jeannie, be aware that questions focused on software are off topic here, so it is advisable to phrase them in general statistical terms. Also, could you elaborate on it seems to only work when estimating variance using neighbourhood data, which does not take the period into account? – Richard Hardy Aug 1 '16 at 19:30