Faster alternative to function 'rollapply'

霸气de小男生 提交于 2021-02-05 04:50:18

问题


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?


回答1:


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 


来源:https://stackoverflow.com/questions/25470659/faster-alternative-to-function-rollapply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!