[R] aggregate.ts

Jeffrey J. Hallman jhallman at frb.gov
Thu Jul 26 15:43:51 CEST 2007


Your troubles with 'aggregate' for a ts are one of the reasons I created the
'tis' and 'ti' classes in the fame package.  If you do this:

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

Everything pretty much works as you would expect.  One thing to notice is
that, even using a 'tis' rather than a 'ts', aggregate will only sum up the
monthly observations for a quarter if all three of the months are there.
That's why y2 starts with 2001Q1, rather than 2000Q4.  If you really want the
2000Q4 observation to be the sum of the first two x2 months, the convert()
function in fame can handle that.

> 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

Now back to ts.  If you look deeper into what's happening here:

> y3 <- aggregate(as.ts(x2), nf = 4)
> y3
Error in rep.int("", start.pad) : invalid number of copies in rep.int()

Enter a frame number, or 0 to exit   

1: print(c(6, 15, 24, 33, 42, 51, 60, 69))
2: print.ts(c(6, 15, 24, 33, 42, 51, 60, 69))
3: matrix(c(rep.int("", start.pad), format(x, ...), rep.int("", end.pad)), nc 
4: as.vector(data)
5: rep.int("", start.pad)

Selection: 0
> unclass(y3)
[1]  6 15 24 33 42 51 60 69
attr(,"tsp")
[1] 2000.833 2002.583    4.000

what you see is that aggregate() did indeed create a quarterly series, but the
quarters cover (Nov-Jan, Feb-Apr, May-Jul, Aug-Oct), not the usual (Jan-Mar,
Apr-Jun, Jul-Sep, Oct-Dec).  The author of the print.ts code evidently never
even thought of this possibility.  Not that I blame him.  I work with monthly
and quarterly data all the time, and the behavior of aggregate.ts() is so
counter-intuitive that I wouldn't have imagined it either.

Bottom line: use 'tis' series from the fame package, or 'zoo` stuff from
Gabor's zoo package.  As the author of the fame package, I hope you'll excuse
me for asserting that the 'tis' class is easier to understand and use than the
zoo stuff, which takes a more general approach.  Some day Gabor or I or some
other enterprising soul should try combining the best ideas from zoo and fame
into a package that is better than either one.

Jeff




-- 
Jeff



More information about the R-help mailing list