ts in R (again)

Prof Brian D Ripley ripley@stats.ox.ac.uk
Fri, 13 Aug 1999 00:27:13 +0100 (BST)


I have done a re-working of the time series code in base R and library
ts that will appear in Friday's snapshot. I am sure at least one
person will be dissatisfied, but I am convinced there are fewer
errors than there were before (I found more than 20, as well as several
documented features that did not exist.)

The principles in the code are:

tsp attributes can only go on vectors of length at least one, must
have a positive frequency, start and end consistent with length. This
is all S-compatible (with the implementation, if not the
documentation).

Default methods and ts methods will where possible make use of the tsp
attribute information (as in S), but ts methods will return class
"ts".  In general R code is expected to use class "ts" (and class
"mts" which inherits from it for multivariate time series), but the
scheme is intended to allow more general classes using the tsp attribute
that may or may not inherit from "ts".  To facilitate this, the
function ts() has argument `class' that governs the class of the
returned object. For strict S compatibility this should be "none".

start.default and end.default were quite unreliable: I had Jan 1995
reported as c(1994, 13), for example. I have tried to make them more
robust. For example, try
> y <- ts(1:120, end=10-1e-7, frequency=12)
> end(y)
[1] 9 13
in S3 (with a warning) and R0.64.2 (which fails to print y). 
This does not seem worth copying (or does Paul Gilbert so instruct?)

"ts" class is only added by ts() and as.ts() (makes use of a tsp
attribute, as in S and unlike current R).  There seems no reason for
diff.default() or window.default() to change the class to "ts" (only),
for example.  I think they should preserve the class, and they do in
my versions.

"ts" class is only removed by tsp(x) <- NULL and by using subsets.  I
have taken Martyn Plummer's advice (PR#217) and made subsetting except
x[, blah] remove the "ts" class. In principle I would like to have
kept it, but at least code works this way!  There were too many problems
with the existing code, for example with matrix indices. (If "[.ts"
is to be special, it has to work in all cases.)  window() can now extract
regular sub-samples. 

There is one change that occurs if library(ts) is used, which is that
simple arithmetic (+ - * /) works on time-aligned series, so that
e.g.

x - 0.8*lag(x, -3)

does what you would expect (otherwise you get 0.2*x).  It is currently
optional, as I am unsure what this might break (although I can find
nothing, and it has been silently there for a week). Should this become
standard?

Two other things I would like opinions on:

1) Given multiple series, plot.ts plots them on a single y scale, and
plot.mts uses separate plots with a common x. Each has disadvantages.
Should we try to switch between the styles depending on the inputs,
and if so how?

2) I think the current R style of

>  y <- ts(1:120, end=10-1e-7,frequency=12)
> y
   Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
0   NA   1   2   3   4   5   6   7   8   9  10  11
1   12  13  14  15  16  17  18  19  20  21  22  23
2   24  25  26  27  28  29  30  31  32  33  34  35
3   36  37  38  39  40  41  42  43  44  45  46  47
4   48  49  50  51  52  53  54  55  56  57  58  59
5   60  61  62  63  64  65  66  67  68  69  70  71
6   72  73  74  75  76  77  78  79  80  81  82  83
7   84  85  86  87  88  89  90  91  92  93  94  95
8   96  97  98  99 100 101 102 103 104 105 106 107
9  108 109 110 111 112 113 114 115 116 117 118 119
10 120  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA

is confusing, not least as it is it impossible to distinguish NAs at the
ends from NAs generated by printing. I had a variant in library ts that
used format to get blanks. Should I make that the default?


-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._