PerformanceAnalytics apply.rolling with NAs
On Fri, Sep 2, 2011 at 7:15 AM, Brian G. Peterson <brian at braverock.com> wrote:
On Fri, 2011-09-02 at 13:00 +0200, Dean Marks wrote:
I'm having the following issue: require( PerformanceAnalytics ) data( managers ) #This works fine: x <- managers[ , "SP500 TR"] apply.rolling( x , width = 6 ) #This produces an error (presumably due to indexes being empty) x[] <- NA apply.rolling( x , width = 6 ) ### ?Error in xts(, order.by = time(R)) : ### ?order.by requires an appropriate time-based object Is this the expected behavior? How would I avoid the error without having to check for the special case(s)?
Well, the xts error says it all in this case. ?You don't have a time series to roll *on*. ?This is a time series function, so without a time series, it simply won't work. There is the newer function rollapply.xts, but I don't recall offhand whether it has been exported yet, or rollapply (in zoo) which is less fussy about the 'time series' part, though it does of course assume the ordered observations from which zoo takes its name.
The default method of zoo's rollapply works with ordinary R vectors and matrices too, not just zoo and ts objects.
library(zoo) x <- 1:10 rollapply(x, 3, sum)
[1] 6 9 12 15 18 21 24 27
# rollapplyr is same as rollapply(..., align = "right") rollapplyr(x, 3, sum, fill = NA)
[1] NA NA 6 9 12 15 18 21 24 27
x[] <- NA rollapplyr(x, 3, sum, fill = NA)
[1] NA NA NA NA NA NA NA NA NA NA
rollapplyr(x, 3, sum, na.rm = TRUE, fill = NA)
[1] NA NA 0 0 0 0 0 0 0 0
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com