[R-SIG-Finance] PerformanceAnalytics seems to break rollapply

G See gsee000 at gmail.com
Fri Oct 5 21:08:09 CEST 2012


There should be a GIANT WARNING when loading PerformanceAnalytics to
let users know that their code will no longer be reproducible.

The problem comes from the zzz.R that does these nasty things:

mean.xts <- function(x,...) {
    if(is.vector(x) ||is.null(ncol(x))  || ncol(x)==1){
        x<-as.numeric(x)
        mean(x,...)
    } else apply(x,2,mean.xts,...)
}
mean.matrix <- function(x,...) {apply(x,2,mean,...)}

sd.xts <- function(x,na.rm=FALSE) {
    if(is.vector(x) || is.null(ncol(x)) || ncol(x)==1){
        x<-as.numeric(x)
        sd(x,na.rm=na.rm)
    } else apply(x,2,sd,na.rm=na.rm)
}
sd.matrix <- function(x,na.rm=FALSE) {apply(x,2,sd,na.rm=na.rm)}

rollapply.xts <- xts:::rollapply.xts


HTH,
Garrett

On Fri, Oct 5, 2012 at 2:04 PM, Bos, Roger <roger.bos at rothschild.com> wrote:
> Unless I am making a user error, it seems that rollapply stops working with some functions after the PerformanceAnalytics packages has been loaded.  I provide reproducible code below:
>
> test <- xts(1:10, order.by=Sys.time()+(1:10))
> "w" <- function(x) {
>                                                 z <- scale(x)
>        attributes(z) <- NULL
>        z
> }
> rollapply(data=test, width=3, FUN=mean)
> rollapply(data=test, width=3, FUN=scale)
> rollapply(data=test, width=3, FUN=w)
> require("PerformanceAnalytics")
> rollapply(data=test, width=3, FUN=mean)
> rollapply(data=test, width=3, FUN=scale)
> rollapply(data=test, width=3, FUN=w)
>
> The first three calls to rollapply work fine.  Then once the PerformanceAnalytics packages had been loaded "mean" still works but "scale" does not.  I suspect it is because of the attributes which scale creates, but I tried creating a function (called "w") which removes the attributes, but rollapply still does not work.
>
> Here is the error I get:
>
>> rollapply(data=test, width=3, FUN=w)
> Error in xts(xx, tt, if (by == 1) attr(data, "frequency")) :
>   NROW(x) must match length(order.by)
>>
>
>
> Thanks for any help or advice you can provide!
>
> Roger J. Bos, CFA
> Rothschild Asset Management, Inc.
> Tel   +1 (212) 403-5471
> Email roger.bos at rothschild.com
> 1251 Avenue of the Americas, NY, NY 10020
>
> ***************************************************************
> This message is for the named person's use only. It may\...{{dropped:11}}
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



More information about the R-SIG-Finance mailing list