[Rd] [R] aggregate.ts

Paul Gilbert pgilbert at bank-banque-canada.ca
Wed Jul 25 16:31:06 CEST 2007

(moved from r-help)

Achim Zeileis wrote:

>On Wed, 25 Jul 2007, laimonis wrote:
>>Consider the following scrap of code:
>...slightly modified to
>  x1 <- ts(1:24, start = c(2000, 10), freq = 12)
>  x2 <- ts(1:24, start = c(2000, 11), freq = 12)
>and then
>  y1 <- aggregate(x1, nfreq = 4)
>gives the desired result while
>  y2 <- aggregate(x2, nfreq = 4)
>probably does not what you would like it to do. 

I've been caught by this before, and complained before.  It does not do 
what most people that work with economic time series would expect.  (One 
might argue that not all time series are economic, but other time series 
don't usually fit with ts very well.)  At the very least aggregate 
should issue a warning.  Quarterly observations are for quarters of the 
year, so just arbitrarily grouping in 3 beginning with the first 
observation is *extremely* misleading, even if it is documented.

[ BTW, there is a bug in the print method here (R-2.5.1 on Linux) :
 >   y2 <- aggregate(x2, nfreq = 4) 
 > y2
Error in rep.int("", start.pad) : invalid number of copies in rep.int()
 > traceback()
5: rep.int("", start.pad)
4: as.vector(data)
3: matrix(c(rep.int("", start.pad), format(x, ...), rep.int("",
       end.pad)), nc = fr.x, byrow = TRUE, dimnames = list(dn1,
2: print.ts(c(6L, 15L, 24L, 33L, 42L, 51L, 60L, 69L))
1: print(c(6L, 15L, 24L, 33L, 42L, 51L, 60L, 69L))


>Currently, the "zoo" implementation allows this: Coercing back and forth
>  library("zoo")
>  z1 <- as.ts(aggregate(as.zoo(x1), as.yearqtr, sum))
>  z2 <- as.ts(aggregate(as.zoo(x2), as.yearqtr, sum))
This is better, but still potentially misleading. I would prefer a 
default  NA when only some of the observations are available for a 
quarter  (and the syntax is a bit cumbersome for something one needs to 
do fairly often).


>where z1 is identical to y1, and z2 is what you probably want.
>R-help at stat.math.ethz.ch mailing list
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

La version française suit le texte anglais.


This email may contain privileged and/or confidential inform...{{dropped}}

More information about the R-devel mailing list