| sts-class {surveillance} | R Documentation |
Class "sts" – surveillance time series
Description
This is a lightweight S4 class to implement (multivariate) time
series of counts, typically from public health surveillance.
The "sts" class supersedes the informal "disProg" class
used in early versions of package surveillance. Converters are
available, see disProg2sts.
For areal time series, the class can also capture the spatial layout
of the regions, where the data originate from.
The constructor function sts can be used to setup an
"sts" object. Conversion of simple time-series objects
(of class "ts") is also possible.
The slots of the "sts" class and available methods are
described below.
Usage
sts(observed, start = c(2000, 1), frequency = 52,
epoch = NULL, population = NULL, ...)
Arguments
observed |
a vector (for a single time series) or matrix (one
time series per column) of counts. A purely numeric data frame will
also do (transformed via |
start, frequency |
basic characteristics of the time series data
just like for simple |
epoch |
observation times, either as an integer sequence (default)
or as a |
population |
a vector of length the number of columns in
|
... |
further named arguments with names corresponding to slot
names (see the list below). For instance, in the public health surveillance context,
the |
Slots
epoch:a numeric vector specifying the time of observation, typically a week index. Depending on the
freqslot, it could also index days or months. Furthermore, ifepochAsDate=TRUEthenepochis the integer representation ofDates giving the exact date of the observation.freq:number of observations per year, e.g., 52 for weekly data, 12 for monthly data.
start:vector of length two denoting the year and the sample number (week, month, etc.) of the first observation.
observed:matrix of size
length(epoch)times the number of regions containing the weekly/monthly number of counts in each region. The colnames of the matrix should match the ID values of the shapes in themapslot.state:matrix with the same dimensions as
observedcontaining Booleans whether at the specific time point there was an outbreak in the region.alarm:matrix with the same dimensions as
observedspecifying whether an outbreak detection algorithm declared a specific time point in the region as having an alarm.upperbound:matrix with upper-bound values.
neighbourhood:symmetric matrix of size
(number of regions)^2describing the neighbourhood structure. It may either be a binary adjacency matrix or contain neighbourhood orders (see the Examples for how to infer the latter from themap).populationFrac:matrixof population fractions or absolute numbers (seemultinomialTSbelow) with dimensionsdim(observed).map:object of class
"SpatialPolygons"(or"SpatialPolygonsDataFrame") providing a shape of the areas which are monitored.control:listof settings; this is a rather free data type to be returned by the surveillance algorithms.epochAsDate:a Boolean indicating if the
epochslot corresponds toDates.multinomialTS:a Boolean stating whether to interpret the object as
observedout ofpopulation, i.e. a multinomial interpretation instead of a count interpretation.
Methods
Extraction of slots
There is an extraction (and replacement) method for almost every slot.
The name of the method corresponds to the slot name, with two exceptions:
the populationFrac slot is addressed by a population method,
and the alarm slot is addressed by an alarms method.
- epoch
signature(x = "sts"): extract theepochslot. If thestsobject is indexed by dates (epochAsDate= TRUE), the returned vector is of classDate, otherwise numeric (usually the integer sequence1:nrow(x)).
By explicitly requestingepoch(x, as.Date = TRUE), dates can also be extracted if thestsobject is not internally indexed by dates but has a standard frequency of 12 (monthly) or 52 (weekly). The transformation is based onstartandfreqand will return the first day of each month (freq=12) and the Monday of each week (freq=52), respectively.- observed
signature(x = "sts"): extract theobservedslot.- alarms
signature(x = "sts"): extract thealarmslot.- upperbound
signature(x = "sts"): extract theupperboundslot.- neighbourhood
signature(x = "sts"): extract theneighbourhoodslot.- population
signature(x = "sts"): extract thepopulationFracslot.- control
signature(x = "sts"): extract thecontrolslot.- multinomialTS
signature(x = "sts"): extract themultinomialTSslot.
Other extraction methods
- dim
signature(x = "sts"): extract matrix dimensions ofobserved. This method also enablesnrow(x)andncol(x).- dimnames
signature(x = "sts"): extract thedimnamesof theobservedmatrix. This method also enablesrownames(x)andcolnames(x).- year
signature(x = "sts"): extract the corresponding year of each observation.- epochInYear
signature(x = "sts"): extract the epoch number within the year.- [
signature(x = "sts"): subset rows (time points) and/or columns (units), seehelp("[,sts-method").
Transformation methods
- aggregate
signature(x = "sts"): seeaggregate.sts.- as.data.frame
signature(x = "sts"): the defaultas.data.framecall will collect the following slots into a data frame:observed,epoch,state,alarm,upperbound, andpopulationFrac. Additional columns will be created forfreq(potentially varying by year for weekly or daily data ifx@epochAsDateisTRUE) andepochInPeriod(the epoch fraction within the current year).
Calling theas.data.framemethod with the argumenttidy = TRUEwill returntidy.sts(x), which reshapes multivariatestsobjects to the “long” format (one row per epoch and observational unit). The tidy format is particularly useful for standard regression models and customized plotting.- coerce
signature(from="sts", to="ts")andsignature(from="ts", to="sts"), to be called viaas(stsObj, "ts")(oras.ts(stsObj)) andas(tsObj, "sts"), respectively.- as.xts
convert to the xts package format.
Visualization methods
- plot
signature(x = "sts", y = "missing"): entry point to a collection of plot variants. Thetypeof plot is specified using a formula, seeplot.stsfor details.- autoplot
a ggplot2 variant of the standard time-series-type plot, see
autoplot.sts.- animate
see
animate.sts.- toLatex
see
toLatex.sts.
Author(s)
Michael Höhle and Sebastian Meyer
Examples
showClass("sts")
## create an sts object from time-series data
salmonellaDF <- read.table(system.file("extdata/salmonella.agona.txt",
package = "surveillance"), header = TRUE)
str(salmonellaDF)
salmonella <- with(salmonellaDF,
sts(observed = observed, state = state,
start = c(1990, 1), frequency = 52))
salmonella
plot(salmonella)
## these data are also available as a legacy "disProg" object in the package
data(salmonella.agona)
stopifnot(all.equal(salmonella, disProg2sts(salmonella.agona)))
## A typical dataset with weekly counts of measles from several districts
data("measlesWeserEms")
measlesWeserEms
## reconstruct data("measlesWeserEms") from its components
counts <- observed(measlesWeserEms)
map <- measlesWeserEms@map
populationFrac <- population(measlesWeserEms)
weserems_nbOrder <- neighbourhood(measlesWeserEms)
## orders of adjacency can also be determined from the map
if (requireNamespace("spdep")) {
stopifnot(identical(weserems_nbOrder,
nbOrder(poly2adjmat(map))))
}
mymeasles <- sts(counts, start = c(2001, 1), frequency = 52,
population = populationFrac,
neighbourhood = weserems_nbOrder, map = map)
stopifnot(identical(mymeasles, measlesWeserEms))
## convert ts/mts object to sts
z <- ts(matrix(rpois(300,10), 100, 3), start = c(1961, 1), frequency = 12)
z.sts <- as(z, "sts")
plot(z.sts)
## conversion of "sts" objects to the quasi-standard "xts" class
if (requireNamespace("xts")) {
z.xts <- as.xts.sts(z.sts)
plot(z.xts)
}