On Tue, 2010-07-13 at 17:41 +0530, sayan dasgupta wrote: > > > On Tue, Jul 13, 2010 at 5:27 PM, Gavin Simpson > <gavin.simp...@ucl.ac.uk> wrote: > On Tue, 2010-07-13 at 17:13 +0530, sayan dasgupta wrote: > > Hi folks, > > > > I am confused whether the following is a bug or it is fine > > > > Here is the explanation > > > > a <- zoo(c(NA,1:9),1:10) > > > > Now If I do > > > > rollapply(a,FUN=mean,width=3,align="right") > > > mean() has argument na.rm which defaults to FALSE. As such, if > NA are in > the computation the mean is undefined and the answer will be > NA. If you > pass na.rm = TRUE to rollapply, mean ignores the NA and works > on the > remaining values: > > > rollapply(a,FUN=mean,width=3,align="right", na.rm = TRUE) > 3 4 5 6 7 8 9 10 > > 1.5 2.0 3.0 4.0 5.0 6.0 7.0 8.0 > > This is fine but the problem is logically when you are doing rollapply > only the first 2 values should be NA > but suppose for index 10 as I have mentioned the rollapply should be a > mean of b9, 8 ,7 and there is no NA here. > So it should not return NA
Indeed, there seems to be something odd happening here: consider, > rollapply(a,FUN=mean,width=3) 2 3 4 5 6 7 8 9 NA NA NA NA NA NA NA NA > rollapply(a,FUN=mean,width=3, na.rm = FALSE) 2 3 4 5 6 7 8 9 NA 2 3 4 5 6 7 8 > rollapply(a,FUN=mean,width=3, na.rm = TRUE) 2 3 4 5 6 7 8 9 1.5 2.0 3.0 4.0 5.0 6.0 7.0 8.0 and if you debug zoo:rollapply.zoo, the top one gets passed off to rollmean early on in the code, whilst the second (na.rm = FALSE) is handled by rollapply itself. And I see why this is happening. If ... contains anything, is anything then the code will not enter the switch statement which passes off control to functions like rollmean() (in this case). This explains the difference between the first and second calls with na.rm = FALSE. And of course, this is mentioned on ?rollapply. Must read the help!!! So, as rollmean doesn't accept an na.rm argument or pass it on, you need to do rollapply(a,FUN=mean,width=3, na.rm = FALSE) This is not a bug as ?rollapply tells you what it does, passes you to ?rollmean which states that it doesn't work for inputs with NAs. To get behaviour you want though, you have to do the somewhat odd workaround and force computation via rollapply by providing an extra argument, even a gibberish one, e.g.: rollapply(a,FUN=mean,width=3, foo = 1) will work. HTH G > > > > > > > HTH > > G > > > > > > I get > > > rollapply(a,FUN=mean,width=3,align="right") > > 3 4 5 6 7 8 9 10 > > NA NA NA NA NA NA NA NA > > > > But I shouldn't be getting NA right ? i.e for index 10 I > should get > > (1/3)*(9+8+7) > > > > Similarly > > > > > rollapply(a,FUN=mean,width=3) > > 2 3 4 5 6 7 8 9 > > NA NA NA NA NA NA NA NA > > > > > > Zoo version : > > > > > installed.packages()["zoo","Version"] > > [1] "1.6-3" > > > > > > > > > My machine details > > > > > sessionInfo() > > R version 2.10.1 (2009-12-14) > > i386-pc-intel32 > > > > locale: > > [1] LC_COLLATE=English_India.1252 > LC_CTYPE=English_India.1252 > > LC_MONETARY=English_India.1252 LC_NUMERIC=C > > [5] LC_TIME=English_India.1252 > > > > attached base packages: > > [1] stats graphics grDevices datasets utils > methods base > > > > other attached packages: > > [1] zoo_1.6-3 rcom_2.2-1 rscproxy_1.3-1 > Revobase_3.2.0 > > > > loaded via a namespace (and not attached): > > [1] grid_2.10.1 lattice_0.18-3 tools_2.10.1 > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible > code. > > -- > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~ > %~%~%~% > Dr. Gavin Simpson [t] +44 (0)20 7679 0522 > ECRC, UCL Geography, [f] +44 (0)20 7679 0565 > Pearson Building, [e] > gavin.simpsonATNOSPAMucl.ac.uk > Gower Street, London [w] > http://www.ucl.ac.uk/~ucfagls/ > UK. WC1E 6BT. [w] > http://www.freshwaters.org.uk > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~ > %~%~%~% > > -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.