Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rollapply applied to xts object

Tags:

r

zoo

xts

require(quantmod)
require(PerformanceAnalytics)
getSymbols('INTC')
x<- monthlyReturn(INTC)
rollapply(1+x,12,cumprod)

Given the code above, I got this error

Error in array(r, dim = d, dimnames = if (!(is.null(n1 <- names(x[[1L]])) &  : 
  length of 'dimnames' [1] not equal to array extent

I can verify that the class of x is indeed xts.

class(x) [1] "xts" "zoo"

I suppose rollapply works on xts/zoo object. I am not sure how to resolve the issue here.

Thanks for the help.

Update

SessionInfo Output:

R version 2.15.3 (2013-03-01)
Platform: i686-pc-linux-gnu (32-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] PerformanceAnalytics_1.1.0 quantmod_0.4-0             TTR_0.22-0                
[4] xts_0.9-3                  zoo_1.7-9                  Defaults_1.1-1            

loaded via a namespace (and not attached):
[1] grid_2.15.3     lattice_0.20-13 tools_2.15.3   
like image 679
zsljulius Avatar asked Feb 13 '26 00:02

zsljulius


2 Answers

rollapply.xts assumes the function returns a single value for each rolling window. So you either need to use prod with rollapply.xts or cumprod with a split-apply-combine strategy, depending on what you actually want to do.

rollapply(1+x,12,prod)
do.call(rbind, lapply(split(1+x,"years"), cumprod))
like image 53
Joshua Ulrich Avatar answered Feb 15 '26 14:02

Joshua Ulrich


You have found a bug. Here is a workaround:

z <- as.zoo(x)
dim(z) <- NULL
rollapply(1+z, 12, cumprod)
like image 24
G. Grothendieck Avatar answered Feb 15 '26 13:02

G. Grothendieck