[Rd] [R] aggregate.ts

Jeffrey J. Hallman jhallman at frb.gov
Thu Jul 26 16:14:24 CEST 2007


aggregate.tis() in the fame package does what I think is the right thing: 

> x2 <- tis(1:24, start = c(2000, 11), freq = 12)
> y2 <- aggregate(x2, nfreq = 4)
> x2
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2000                                           1   2
2001   3   4   5   6   7   8   9  10  11  12  13  14
2002  15  16  17  18  19  20  21  22  23  24        
class: tis
> y2
     Qtr1 Qtr2 Qtr3 Qtr4
2001   12   21   30   39
2002   48   57   66     
class: tis

If you really want y2 to have an observation for 2000Q4, you can use

> convert(x2, tif = "quarterly", observed = "summed", ignore = T)
          Qtr1      Qtr2      Qtr3      Qtr4
2000                                4.033333
2001 12.000000 21.000000 30.000000 39.000000
2002 48.000000 57.000000 66.000000 71.225806
class: tis



Paul Gilbert <pgilbert at bank-banque-canada.ca> writes:

> 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,
>        dn2))
> 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
> >gives:
> >  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).
> 
> Paul
> 
> >where z1 is identical to y1, and z2 is what you probably want.
> >
> >hth,
> >Z
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch 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.
> >  
> >
> ====================================================================================
> 
> La version française suit le texte anglais.
> 
> ------------------------------------------------------------------------------------
> 
> This email may contain privileged and/or confidential inform...{{dropped}}
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 

-- 
Jeff



More information about the R-devel mailing list