Question

I am trying to calculate some function on a rolling basis on some xts object. There seems to be a problem with doing so in many cases with xts after I load PerformanceAnalytics package... Please see below for example. What am I doing wrong? This is quite important to me since it brakes many of the calculations I am performing (good that it is still weekend). I have rebuilt all major packages a few hours ago. Please see my sessionInfo below. EDIT: Looking at this PerformanceAnalytics thing, I guess more things are broken right now. For example:

table.DownsideRisk(ret)

Error: could not find function "sd.xts"

Here is my original example:

require(quantmod)

# Get SP500 Index
getSymbols("^GSPC")

# This does not work
tail(rollapply(Cl(GSPC), 20, FUN=sd, fill=NA), 5)

# Hm, something with xts maybe?
z <- zoo(1:1500, as.Date(1:1500))
tail(rollapply(z, 20, mean, fill=NA), 5)
tail(rollapply(z, 20, sd, fill=NA), 5)

z <- as.xts(z)
tail(rollapply(z, 20, mean, fill=NA), 5)
tail(rollapply(z, 20, sd, fill=NA), 5)

require(PerformanceAnalytics)
# This does not work anymore
tail(rollapply(z, 20, sd, fill=NA), 5)
# This does not work anymore
tail(rollapply(Cl(GSPC), 20, FUN=sd, fill=NA), 5)
# Well, it looks like xts or PerformanceAnalytics problem... When I load PerformanceAnalytics, problems...

> require(quantmod)
Loading required package: quantmod
Loading required package: Defaults
Loading required package: xts
Loading required package: zoo

Attaching package: ‘zoo’

The following object(s) are masked from ‘package:base’:

    as.Date, as.Date.numeric

Loading required package: TTR
> 
> # Get SP500 Index
> getSymbols("^GSPC")
[1] "GSPC"
> 
> # This does not work
> tail(rollapply(Cl(GSPC), 20, FUN=sd, fill=NA), 5)
           GSPC.Close
2012-10-25         NA
2012-10-26         NA
2012-10-31         NA
2012-11-01         NA
2012-11-02         NA
> 
> # Hm, something with xts maybe?
> z <- zoo(1:1500, as.Date(1:1500))
> tail(rollapply(z, 20, mean, fill=NA), 5)
1974-02-05 1974-02-06 1974-02-07 1974-02-08 1974-02-09 
        NA         NA         NA         NA         NA 
> tail(rollapply(z, 20, sd, fill=NA), 5)
1974-02-05 1974-02-06 1974-02-07 1974-02-08 1974-02-09 
        NA         NA         NA         NA         NA 
> 
> z <- as.xts(z)
> tail(rollapply(z, 20, mean, fill=NA), 5)
           [,1]
1974-02-05   NA
1974-02-06   NA
1974-02-07   NA
1974-02-08   NA
1974-02-09   NA
> tail(rollapply(z, 20, sd, fill=NA), 5)

1974-02-05 NA
1974-02-06 NA
1974-02-07 NA
1974-02-08 NA
1974-02-09 NA
> 
> require(PerformanceAnalytics)
Loading required package: PerformanceAnalytics

Package PerformanceAnalytics (1.0.5.1) loaded.
Econometric tools for performance and risk analysis.
(c) 2004-2012 Peter Carl, Brian G. Peterson. License: GPL
http://r-forge.r-project.org/projects/returnanalytics/


Attaching package: ‘PerformanceAnalytics’

The following object(s) are masked from ‘package:graphics’:

    legend

> # This does not work anymore
> tail(rollapply(z, 20, sd, fill=NA), 5)
Error in FUN(.subset_xts(data, (i - width + 1):i), ...) : 
  unused argument(s) (fill = NA)
> # This does not work anymore
> tail(rollapply(Cl(GSPC), 20, FUN=sd, fill=NA), 5)
Error in FUN(.subset_xts(data, (i - width + 1):i), ...) : 
  unused argument(s) (fill = NA)
> # Well, it looks like xts or PerformanceAnalytics problem... When I load PerformanceAnalytics, problems...
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-pc-linux-gnu (64-bit)

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

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

other attached packages:
[1] PerformanceAnalytics_1.0.5.1 quantmod_0.3-21             
[3] TTR_0.21-1                   xts_0.8-8                   
[5] zoo_1.7-9                    Defaults_1.1-1              
[7] rj_1.1.0-4                  

loaded via a namespace (and not attached):
[1] grid_2.15.2    lattice_0.20-0 tools_2.15.2  
Was it helpful?

Solution

PerformanceAnalytics registers a rollapply.xts method that apparently has some issues.

You can use as.zoo on your objects to use rollapply.zoo instead.

Try tail(rollapply(as.zoo(z), 20, sd, fill=NA, align='right'), 5)

I don't know what other problems you'll run into regarding sd.xts (which is not a method) and mean.xts (which is now in xts) while using PerformanceAnalytics -- at best, it depends on which version you're using.


This may make your computer explode, but try sourcing this before running your "huge pile of code"

rollapply.xts <- function(data, width, FUN, ...) {
  xdata <- xcoredata(data)
  out <- zoo:::rollapply.zoo(data=data, width=width, FUN=FUN, ...)
  xcoredata(out) <- xdata
  out
}

R> suppressPackageStartupMessages(library(PerformanceAnalytics))
R> tail(rollapply(Cl(GSPC), 20, FUN=sd, fill=NA), 20)
           GSPC.Close
2012-10-04   10.80165
2012-10-05   10.80888
2012-10-08   10.82280
2012-10-09   12.94614
2012-10-10   14.96816
2012-10-11   16.27298
2012-10-12   17.48353
2012-10-15   18.42794
2012-10-16   18.44652
2012-10-17   18.76495
2012-10-18         NA
2012-10-19         NA
2012-10-22         NA
2012-10-23         NA
2012-10-24         NA
2012-10-25         NA
2012-10-26         NA
2012-10-31         NA
2012-11-01         NA
2012-11-02         NA
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top