PerformanceAnalytics apply.rolling with NAs
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: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
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