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

Brian G. Peterson brian at braverock.com
Fri Sep 2 13:42:23 CEST 2011


On Fri, 2011-09-02 at 13:24 +0200, Dean Marks wrote:
> If you append to my minimal example:
> 
> x
> class(x)
> 
> You get something that looks like, and says it is a time series
> object. Am I mistaken here?

Yes.

> x<-NA
> class(x)
[1] "logical"

    ^^^^^^^^  that is not a time series.

> y<-xts()
> y
Data:
numeric(0)

Index:
integer(0)
> class(y)
[1] "xts" "zoo"

This is an (empty) time series.  

All of the rolling functions will still fail, of course, since there is
no data to apply *to*

> apply.rolling(y, width=6,FUN=sum)
Error in xts(, order.by = time(R)) : 
  order.by requires an appropriate time-based object

So far, no bug anywhere I can see.

If, however, we construct an actual time series:

####

z <- xts(1:20,order.by=seq.Date(from=Sys.Date()-19,to=Sys.Date(),by=1))

z

#            [,1]
# 2011-08-14    1
# 2011-08-15    2
# 2011-08-16    3
# 2011-08-17    4
# 2011-08-18    5
# 2011-08-19    6
# 2011-08-20    7
# 2011-08-21    8
# 2011-08-22    9
# 2011-08-23   10
# 2011-08-24   11
# 2011-08-25   12
# 2011-08-26   13
# 2011-08-27   14
# 2011-08-28   15
# 2011-08-29   16
# 2011-08-30   17
# 2011-08-31   18
# 2011-09-01   19
# 2011-09-02   20

# things will now work as documented and expected:

apply.rolling(z, width=6,FUN=sum)

#            calcs
# 2011-08-14    NA
# 2011-08-15    NA
# 2011-08-16    NA
# 2011-08-17    NA
# 2011-08-18    NA
# 2011-08-19    21
# 2011-08-20    27
# 2011-08-21    33
# 2011-08-22    39
# 2011-08-23    45
# 2011-08-24    51
# 2011-08-25    57
# 2011-08-26    63
# 2011-08-27    69
# 2011-08-28    75
# 2011-08-29    81
# 2011-08-30    87
# 2011-08-31    93
# 2011-09-01    99
# 2011-09-02   105
 
####

I continue to assume that your actual use case is more complicated, so
perhaps you can supply an example that's a little more on-point to what
you're trying to do if you're still in need of assistance.

Regards,

  - Brian


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



More information about the R-SIG-Finance mailing list