Learn R Programming

surveillance (version 1.26.1)

sts_conversion: Convert an "sts" Object to "ts" or "xts"

Description

These methods convert surveillance time series objects of class "sts" to the standard "ts" and "xts" classes. Conversion to and from ts is also possible using S4-type coerce methods, e.g., as(x, "ts").

Usage

# S3 method for sts
as.ts(x, ...)

# S3 method for sts as.xts(x, order.by = epoch(x, as.Date = TRUE), ...)

Value

an object of class "ts" or "xts", respectively.

Arguments

x

an object of class "sts".

order.by

optional Dates indexing the time series. The default is only used for weekly (freq=52) and daily (freq=365) data. If not specified, other frequencies are handled via as.xts.ts.

...

further arguments are ignored by the as.ts method and passed to xts by the as.xts method.

Examples

Run this code
## convert "ts"/"mts" object to "sts"
counts <- matrix(rpois(28*3,10), 28, 3,
                 dimnames = list(NULL, paste("Region", 1:3)))
z <- ts(counts, start = c(1983, 12), frequency = 12)
head(z)
x <- as(z, "sts")
x

## direct "sts" construction gives the same:
(x2 <- sts(counts, start = start(z), frequency = frequency(z)))
stopifnot(identical(x, x2))

## conversion of "sts" objects to the "xts" class
if (require("xts")) {
    y <- as.xts(x)
    print(head(y))
    plot(y)
}

Run the code above in your browser using DataLab