Re: [R] aggregate.ts

2007-07-26 Thread Achim Zeileis
Jeff,

I'm really not a fan of subjective mine is bigger than yours
discussions. Just three comments that I try to keep as objective as
possible.

 Bottom line: use 'tis' series from the fame package, or 'zoo` stuff from
 Gabor's zoo package.

The last time I checked
  packageDescription(zoo)$Author
had more than one entry.

 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,

That surely depends on the user and the task he has to do...

 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.

I think combination should be straightforward: zoo is general enough to
allow for time indexes of class ti. Overall, ti seems to be
well-written and only some methods might need to be added/improved to
cooperate fully with zoo. Maybe some of the functionality that is
currently available for tis but is not available for all conceivalbe
zoo+arbitrary_index objects might be special cased for zoo+ti or zooreg or
zooreg+ti etc.

Best,
Z

__
R-help@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.


Re: [R] aggregate.ts

2007-07-26 Thread Jeffrey J. Hallman
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
20004.03
2001 12.00 21.00 30.00 39.00
2002 48.00 57.00 66.00 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.5834.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

__
R-help@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.


Re: [R] aggregate.ts

2007-07-24 Thread Achim Zeileis
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. In both cases, the 24
observations are broken into 8 segments of equal length (as documented on
?aggregate.ts) and then aggregated. Therefore
  as.vector(y1)
  as.vector(y2)
are identical (and not matched by quarter...as you would probably like).

 And don't tell me that the aggregating a monthly series into quarters
 makes no sense!! (see response to Bug 9798).

1. Your tone is not appropriate.
2. You're not quoting the reply correctly. It said: You cannot aggregate
   a time series that does not run over quarters into quarters. The
   speculation is plain wrong.
   The reply means that aggregate.ts() does not do what you think
   it does. As I tried to illustrate with the example above.

One can probably argue about whether it makes sense to aggregate a monthly
time series into quarter when I don't have complete observations in each
quarter. But maybe it might be worth considering a change in
aggregate.ts() so that the old and new frequency are matched even with
incomplete observations?

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))
where z1 is identical to y1, and z2 is what you probably want.

hth,
Z

__
R-help@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.


[R] aggregate.ts

2007-07-24 Thread laimonis
Consider the following scrap of code:

  x- ts(1:50,start=c(1,11),freq=12)
  y - aggregate(x,nfreq=4)
  c(y)
 [1]   6  15  24  33  42  51  60  69  78  87  96 105 114 123 132 141
  y
Error in rep.int(, start.pad) : invalid number of copies in rep.int()
  tsp(y)
[1] 1.83 5.58 4.00

So we can aggregate into quarters, but we cannot print it using 
print.ts  Even if print.ts cannot line the series into columns as it 
normally does for quarterly data, we would expect it to behave as it 
does when we  aggregate into thirds.

  y3 - aggregate(x,nfreq=3)
  y3
Time Series:
Start = 1.83
End = 5.5
Frequency = 3
 [1]  10  26  42  58  74  90 106 122 138 154 170 186

And don't tell me that the aggregating a monthly series into quarters 
makes no sense!! (see response to Bug 9798).

Laimonis Kavalieris

__
R-help@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.