Skip to content

PerformanceAnalytics apply.rolling with NAs

5 messages · Brian G. Peterson, Dean Marks, Peter Carl

#
---------- Forwarded message ----------
From: Dean Marks <dean at aeonim.co.za>
Date: 2 September 2011 13:23
Subject: Re: [R-SIG-Finance] PerformanceAnalytics apply.rolling with NAs
To: "Brian G. Peterson" <brian at braverock.com>


Brian, thanks for the prompt response.

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?

In any event, I'm willing to take your advise. However, are you sure
this is not a bug?

Many thanks,
On 2 September 2011 13:15, Brian G. Peterson <brian at braverock.com> wrote:
--
Dean Marks
Investment Analyst
Aeon Investment Management
Tel: +27 (0)21 670 5295
Fax: +27 (0)86 508 0685
Mobile: +27 (0)72 417 4720
Email: dean at aeonim.co.za
www.aeonim.co.za
4th Floor, MontClare Place
Cnr Campground & Main Roads
Claremont, 7708
PO Box 24020, Claremont, 7735
An authorised financial services provider FSP No. 27126
#
On Fri, 2011-09-02 at 13:24 +0200, Dean Marks wrote:
Yes.
[1] "logical"

    ^^^^^^^^  that is not a time series.
Data:
numeric(0)

Index:
integer(0)
[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*
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
#
Apologies, but I believe we're at cross purposes here.

My use case requires that I determine rolling returns for a collection
of stocks where in some cases data is missing (repressented by NAs),
for example if a stock did not exist for a period of time. I don't
mean to frustrate you but if I go into the use case further it might
unnecessarily complicate things. My work around, in any case, is
simply to test for these cases prior to calling apply.rolling.

If you paste my original code _in full_ and then append:

x
index(x)
class(x)

you will see what I mean.

I don't mean to be stubborn here, but I'd first like to get onto the same page.

Thanks
On 2 September 2011 13:42, Brian G. Peterson <brian at braverock.com> wrote:

  
    
#
On Fri, 2011-09-02 at 13:55 +0200, Dean Marks wrote:
OK, now we're getting somewhere.

You could apply apply.rolling by column, but this won't  give you
comparable results for the rolling return case.

You could aggregate your returns to some lower-frequency where all the
indices will agree and you only have leading NA's to deal with.

Or, you could see 

?zerofill

since an NA return is equivalent to a zero return (unless your manager
is not reporting to hide losses, but that's a different problem
altogether)

Regards,

  - Brian
#
?try