[R] Zoo - bug ???
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Nov 30 22:19:01 CET 2010
On Tue, Nov 30, 2010 at 1:01 PM, clangkamp <christian.langkamp at gmxpro.de> wrote:
>
> Dear Joshua
> thank you for the pointer to the width element. Actually still with
> na.pad=TRUE it doesn't seem to work but attaching an extra row of NAs did.
>
> A1a<-zoo(c( NA, NA, 1,2,3,4,5,6, NA, NA))
> A1b<-rollapply(A1a,4,mean, na.rm=TRUE, na.pad=FALSE, align="right")
> A1c<-rollapply(A1a,4,mean, na.rm=TRUE, na.pad=TRUE, align="right")
> cbind(A1a,A1b, A1c)
> A1a A1b A1c
> 1 NA NA NA
> 2 NA NA NA
> 3 1 NA NA
> 4 2 1.5 1.5
>
> vs. the same outcome with another NA
>> cbind(A1a,A1b, A1c)
> A1a A1b A1c
> 1 NA NA NA
> 2 NA NA NA
> 3 NA NA NA
> 4 1 1.0 1.0
> 5 2 1.5 1.5
>
> But for now the issue is solved with the workaround.
The way rollapply works is that the input at each iteration must be k
long. For the first k-1 entries of the input the corresponding
output for align= "right" is either NA if na.pad = TRUE or the entry
is just dropped if na.pad = FALSE. That is correct and intended
behavior and also ensures that the variance of each output component
is the same in simple cases because each such output component is
derived from the same number of input components.
I believe what you are asking for is a new feature where optionally,
for k = 4 and align = "right" the first output component would be
FUN(x[1], ...), the second would be FUN(x[1:2], ...), the third would
be FUN(x[1:3], ...) and the remaining ones would be the same as they
are now.
The devel version of zoo actually has such a feature in it if you
specify the argument partial = TRUE. Try this:
> library(zoo)
> source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/rollapply.R?revision=802&root=zoo")
> A1a <- zoo(c( NA, NA, 1,2,3,4,5,6, NA, NA))
> rollapply(A1a, 4, mean, na.rm = TRUE, align = "right", partial = TRUE)
1 2 3 4 5 6 7 8 9 10
NaN NaN 1.0 1.5 2.0 2.5 3.5 4.5 5.0 5.5
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list