[R-SIG-Finance] PerformanceAnalytics apply.rolling with NAs

Brian G. Peterson brian at braverock.com
Fri Sep 2 13:15:32 CEST 2011


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.

I suspect your actual use case is more complicated than the minimal
example you've supplied.  In this case, whether the function you choose
to apply is NA-safe is up to that function.  Many functions in R can
handle NA's, many others fail in ugly ways.  Since we don't know what
you're trying to apply over a rolling period, you will need to
understand the NA behavior of the function you wish to call if your data
contains NA's or other anomalies.

Regards,

  - Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list