Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rollapply in R time frame indexing

Let’s say I have the following data frame of series.

library(zoo)
n=10
date = as.Date(1:n);d
y = rnorm(10);y
dat = data.frame(date,y)
dat

         date           y
1  1970-01-02 -0.02052313
2  1970-01-03  0.28255304
3  1970-01-04 -0.10718621
4  1970-01-05 -1.19299366
5  1970-01-06  1.17072468
6  1970-01-07  0.55849119
7  1970-01-08  0.30474050
8  1970-01-09 -0.30777180
9  1970-01-10 -0.01874367
10 1970-01-11 -0.74233556

Calculating the rolling standard deviation with width (rolling window) of 3 days,i do:

a = zoo::rollapply(da$y,3,sd)

With results:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

       2         3         4         5         6 
1.7924102 0.4189522 0.4599979 0.4164408 0.3786601 
        7         8         9 
0.9481849 0.7048868 0.2494578 

And finding the maximum standard deviation

max(a)
1.79241

Now I want to find out in which time 3-day interval this maximum refers to.How can I do that? Imagine that my series is 20 years, so I want to find the maximum standard deviation of this 20 years and extract the specific 3 day time interval.

>Solution :

In the case of a centered window, you can do:

step = (3-1)/2
wmax = which.max(a)
int = (wmax - step):(wmax + step)
dat[int[int > 0],] #Avoids error when wmax < step.

#         date          y
# 1 1970-01-02 -0.6264538
# 2 1970-01-03  0.1836433
# 3 1970-01-04 -0.8356286
#Data
library(zoo)
set.seed(1)
n=10
date = as.Date(1:n)
y = rnorm(10)
dat = data.frame(date,y)
dat

a = rollapply(dat$y,3,sd)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading