ts {stats} | R Documentation |
Time-Series Objects
Description
The function ts
is used to create time-series objects.
as.ts
and is.ts
coerce an object to a time-series and
test whether an object is a time series.
Usage
ts(data = NA, start = 1, end = numeric(), frequency = 1,
deltat = 1, ts.eps = getOption("ts.eps"),
class = if(nseries > 1) c("mts", "ts", "matrix", "array") else "ts",
names = )
as.ts(x, ...)
is.ts(x)
is.mts(x)
Arguments
data |
a vector or matrix of the observed time-series
values. A data frame will be coerced to a numeric matrix via
|
start |
the time of the first observation. Either a single number or a vector of two numbers (the second of which is an integer), which specify a natural time unit and a (1-based) number of samples into the time unit. See the examples for the use of the second form. |
end |
the time of the last observation, specified in the same way
as |
frequency |
the number of observations per unit of time. |
deltat |
the fraction of the sampling period between successive
observations; e.g., 1/12 for monthly data. Only one of
|
ts.eps |
time series comparison tolerance. Frequencies are
considered equal if their absolute difference is less than
|
class |
class to be given to the result, or none if |
names |
a character vector of names for the series in a multiple
series: defaults to the colnames of |
x |
an arbitrary R object. |
... |
arguments passed to methods (unused for the default method). |
Details
The function ts
is used to create time-series objects. These
are vectors or matrices which inherit from class "ts"
(and have
additional attributes) which represent data sampled at equispaced
points in time. In the matrix case, each column of the matrix
data
is assumed to contain a single (univariate) time series.
Time series must have at least one observation, and although they need
not be numeric there is very limited support for non-numeric series.
Class "ts"
has a number of methods. In particular arithmetic
will attempt to align time axes, and subsetting to extract subsets of
series can be used (e.g., EuStockMarkets[, "DAX"]
). However,
subsetting the first (or only) dimension will return a matrix or
vector, as will matrix subsetting. Subassignment can be used to
replace values but not to extend a series (see window
).
There is a method for t
that transposes the series as a
matrix (a one-column matrix if a vector) and hence returns a result
that does not inherit from class "ts"
.
Argument frequency
indicates the sampling frequency of the
time series, with the default value 1
indicating one sample in
each unit time interval. For
example, one could use a value of 7
for frequency
when
the data are sampled daily, and the natural time period is a week, or
12
when the data are sampled monthly and the natural time
period is a year. Values of 4
and 12
are assumed in
(e.g.) print
methods to imply a quarterly and monthly series
respectively. frequency
need not be a whole
number: for example, frequency = 0.2
would imply sampling
once every five time units.
as.ts
is generic. Its default method will use the
tsp
attribute of the object if it has one to set the
start and end times and frequency.
is.ts()
tests if an object is a time series, i.e., inherits from
"ts"
and is of positive length.
is.mts(x)
tests if an object x
is a multivariate time series,
i.e., fulfills is.ts(x)
, is.matrix(x)
and inherits from
class "mts"
.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
See Also
tsp
,
frequency
,
start
,
end
,
time
,
window
;
print.ts
, the print method for time series objects;
plot.ts
, the plot method for time series objects.
For other definitions of ‘time series’ (e.g., time-ordered observations) see the CRAN task view at https://CRAN.R-project.org/view=TimeSeries.
Examples
require(graphics)
ts(1:10, frequency = 4, start = c(1959, 2)) # 2nd Quarter of 1959
print( ts(1:10, frequency = 7, start = c(12, 2)), calendar = TRUE)
# print.ts(.)
## Using July 1954 as start date:
gnp <- ts(cumsum(1 + round(rnorm(100), 2)),
start = c(1954, 7), frequency = 12)
plot(gnp) # using 'plot.ts' for time-series plot
utils:: methods(class = "ts") # all functions with methods available for "ts"
## "ts" methods for head() and tail()
utils:: head(lynx, 4)
utils:: tail(lynx, -7)
## Multivariate
z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1), frequency = 12)
class(z)
is.mts(z)
head(z) # "ts" method ==> incl. times:
plot(z)
plot(z, plot.type = "single", lty = 1:3)
## A phase plot:
plot(nhtemp, lag(nhtemp, 1), cex = .8, col = "blue",
main = "Lag plot of New Haven temperatures")