[R] rollapply - odd behaviour of NAs

herr dittmann herrdittmann at yahoo.co.uk
Mon Mar 1 22:37:42 CET 2010


Dear expeRts,

The rollapply(tseries) function puzzles me. When applying a function over a rolling window, I am getting NAs at odd places:

dow <- get.hist.quote(instrument="^DJI", start="2010-02-01", quote="Close")
dow.2 <- rollapply(dow, width=2, FUN=mean)
dow.3 <- rollapply(dow, width=3, FUN=mean)
dow.4 <- rollapply(dow, width=4, FUN=mean)

merge(dow, dow.2, dow.3, dow.4)

           Close.dow Close.dow.2 Close.dow.3 Close.dow.4
2010-02-01  10185.53   10241.190          NA          NA
2010-02-02  10296.85   10283.700   10250.977    10188.78
2010-02-03  10270.55   10136.365   10189.860    10145.45
2010-02-04  10002.18   10007.205   10094.987    10048.34
2010-02-05  10012.23    9960.310    9974.267     9995.36
2010-02-08   9908.39    9983.515    9993.087    10004.41
2010-02-09  10058.64   10048.510   10001.803    10037.40
2010-02-10  10038.38   10091.285   10080.403    10085.09
2010-02-11  10144.19   10121.665   10093.903    10137.63
2010-02-12  10099.14   10183.975   10170.713    10205.34
2010-02-16  10268.81   10289.025   10225.730    10267.52
2010-02-17  10309.24   10351.070   10323.650    10343.33
2010-02-18  10392.90   10397.625   10368.163    10371.97
2010-02-19  10402.35   10392.865   10392.877    10365.26
2010-02-22  10383.38   10332.895   10356.047    10360.58
2010-02-23  10282.41   10328.285   10346.650    10340.24
2010-02-24  10374.16   10347.595   10325.867    10325.72
2010-02-25  10321.03   10323.145   10340.150          NA
2010-02-26  10325.26          NA          NA          NA


If rollapply() is "forward looking" (lacking a better word for it), the NAs should be at the end. This seems not to be the case.

Am I missing a trivial point regarding rollapply()? Any pointers much appreciated.

Thanks,

Bernd






More information about the R-help mailing list