Time-Series Objects

Usage

ts(data = NA, start = 1, end = numeric(0), frequency = 1,
   deltat = 1, ts.eps = .Options$ts.eps, class, names)
as.ts(x)
is.ts(x)

print(ts.obj, calendar, ...)
plot(ts.obj, plot.type=c("multiple", "single"), ...)
lines(ts.obj, ...)

Description

The function ts is used to create time-series objects. These are vector or matrices with class of "ts" (and additional attributes) which represent data which has been 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.

as.ts and is.ts coerce an object to a time-series and test whether an object is a time 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.

Details

The value of argument frequency is used when the series is sampled an integral number of times 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.

as.ts will use the tsp attribute of the object if it has one to set the start and end times and frequency.

See Also

tsp, frequency, start, end, time, window.

Examples

ts(1:10, frequency = 4, start = c(1959, 2)) # 2nd Quarter of 1959
print( ts(1:10, freq = 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

## Multivariate
z <- ts(matrix(rnorm(300),100,3), start=c(1961,1), frequency=12)
plot(z)
plot(z, plot.type="single", lty=1:3)

ts(1:10, frequency = 4, start = c(1959, 2)) # 2nd Quarter of 1959
print( ts(1:10, freq = 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

## Multivariate
z <- ts(matrix(rnorm(300),100,3), start=c(1961,1), frequency=12)
plot(z)
plot(z, plot.type="single", lty=1:3)

## A phase plot:
data(nhtemp)
plot(nhtemp, c(nhtemp[-1],NA), cex = .8, col="blue",
     main="Lag plot of New Haven temperatures")
## a clearer way to do this would be
library(ts)
plot(nhtemp, lag(nhtemp,1), cex = .8, col="blue",
     main="Lag plot of New Haven temperatures")

## A phase plot:
data(nhtemp)
plot(nhtemp, c(nhtemp[-1],NA), cex = .8, col="blue",
     main="Lag plot of New Haven temperatures")
## a clearer way to do this would be
library(ts)
plot(nhtemp, lag(nhtemp,1), cex = .8, col="blue",
     main="Lag plot of New Haven temperatures")


[Package Contents]