Faster alternative to function 'rollapply'
Asked Answered
C

1

7

I need to run rolling window function on a xts data which contains about 7,000 rows and 11,000 columns. I did the following:

require(PerformanceAnalytics)
ssd60<-rollapply(wddxts,width=60,FUN=function(x) SemiDeviation(x),by.column=TRUE)

I waited till 12 hours but the computation did not finish. However, when I tried with small dataset as follows:

sample<-wddxts[,1:5]
ssd60<-rollapply(sample,width=60,FUN=function(x) SemiDeviation(x),by.column=TRUE)

the computation was done within 60 seconds. I ran them on computer with Intel i5-2450M CPU, Windows 7 OS and 12 GB RAM.

Can anyone please suggest me if there is any faster way to perform the above computation on a large xts data-set?

Casserole answered 24/8, 2014 at 10:20 Comment(3)
Try the RcppRoll packageWoodworker
Thanks David, I will have a look.Casserole
try data.table@adapt branchMethodology
T
6

If you can, convert them to zoo objects. rollapply.zoo is more efficient than rollapply.xts (in this case. I'm not sure which is more efficient in general):

R> require(PerformanceAnalytics)
R> set.seed(21)
R> x <- .xts(rnorm(7000,0,0.01), 1:7000)
R> system.time({
+   r <- rollapply(x, 60, SemiDeviation, by.column=TRUE, fill=NA)
+ })
   user  system elapsed 
  9.936   0.111  10.075 
R> system.time({
+   z <- rollapplyr(as.zoo(x), 60, SemiDeviation, by.column=TRUE, fill=NA)
+ })
   user  system elapsed 
  1.950   0.010   1.964 
Turmel answered 24/8, 2014 at 16:38 Comment(1)
Thanks Joshua, it took about 4.5 hours.Casserole

© 2022 - 2024 — McMap. All rights reserved.