0

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  
lebelinoz
  • 4,890
  • 10
  • 33
  • 56
Samo
  • 2,065
  • 20
  • 41
  • http://r.789695.n4.nabble.com/PerformanceAnalytics-seems-to-break-rollapply-td4645209.html – GSee Nov 04 '12 at 16:53
  • http://stackoverflow.com/a/12019358/967840 – GSee Nov 04 '12 at 16:54
  • @GSee Thanks. I remember now this conversation from R-SIG-FINANCE... Huh, since this is quite an old problem that has not been solved yet I guess I am doomed unless I start practising some "release management" on my side making sure things do not brake apart. – Samo Nov 04 '12 at 17:01
  • I think you want to use `align='right'`, or `rollapplyr` – GSee Nov 04 '12 at 17:13
  • I am using rollaplyr in most cases and the problem persists. I am looking at xts:::rollaply.xts but I don't have a clue why it breaks. As you suggested I guess I better write a petition to package maintainers soon... :) – Samo Nov 04 '12 at 17:20
  • What is wrong with the results you get using `rollapply.zoo`? You show that the last 5 values are `NA`, but that is expected since `align='center'` is the default. Try looking at the last 50 values e.g. – GSee Nov 04 '12 at 17:24
  • Oh, I see your point. :) Sorry. Of yourse, if I use rollapplyr(as.zoo(x), ... align='right') the results will be correct. In my example I was focused to show the error. The issue I have is that I do not see how I can change huge pile of code to as.zoo(x)... I do not think it is a practical thing to do. I would much more prefer if rollaply.xts is removed from PerformaceAnalytics (I do not see why it is required, but am sure package authors know) or xts:::rollapply.xts is fixed... As always, I prefer others do the work for me. :( Will stare some more hours in this xts:::rollapply.xts. Thanks. – Samo Nov 04 '12 at 17:33
  • I'm told `rollapply.xts` can return multi-column data and `rollapply.zoo` cannot. Also, it preserves xts attributes (which could be handled with `xcoredata<-`), and it may be faster. – GSee Nov 04 '12 at 17:41

1 Answers1

1

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
GSee
  • 48,880
  • 13
  • 125
  • 145
  • I thought we fixed this on R-Forge, but we didn't. -1 beer; epic fail; F-; etc. Working on it now. – Joshua Ulrich Nov 04 '12 at 18:03
  • @GSee After destroying a few computers... :) Have not tried everything from the "huge pile of code" (since it is to huge:)), but looks promising - it works. Garret, once again, thank you very much. – Samo Nov 04 '12 at 19:17
  • I think that this will allow PerformanceAnalytics to use the version of `rollapply.xts` that it was written for, while you can use a version closer to what you're used to with `rollapply.zoo`. But, it sounds like Joshua is about to patch **xts** to make your problem disappear. – GSee Nov 04 '12 at 19:26