Class "sts" – surveillance time series
sts-class.RdThis 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.
The constructor function sts can be used to setup an
"sts" object.
For areal time series, it can also capture a map
of the regions, where the counts originate from.
See Section “Slots” below for a description of all class
components, and Section “Methods” for a list of extraction,
conversion and visualization methods.
Usage
sts(observed, start = c(2000, 1), frequency = 52,
epoch = NULL, population = NULL, map = 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
as.matrix). This argument sets theobservedslot, which is the core element of the resulting"sts"object. It determines the dimensions and colnames for several other slots. The columns (“units”) typically correspond to different regions, diseases, or age groups.- start,frequency
basic characteristics of the time series data just like for simple
"ts"objects. The (historical) default values correspond to weekly data starting in the first week of 2000. TheepochandepochInYearmethods use the ISO 8601 specification when converting between week numbers and dates, seeisoWeekYear.- epoch
observation times, either as an integer sequence (default) or as a
Datevector (in which caseepochAsDateis automatically set toTRUE).- population
the population sizes (or fractions) underlying the
observedcounts, especially relevant for multivariate time series (for incidence maps or as offsets in epidemic models). If assumed constant over time,populationcan be supplied as a vector of length the number of columns (regions, groups, etc.) inobserved. Otherwise,populationneeds to be a matrix of the samedimension asobserved. Thepopulationargument is an alias for the corresponding slotpopulationFrac. The defaultNULLvalue sets equal population fractions across all units.- map
optional spatial data representing the regions, either of class
"SpatialPolygons"(or"SpatialPolygonsDataFrame") or of class"sf"(requires package sf). Itsrow.names()must contain the region IDs to be matched againstcolnames(observed).- ...
further named arguments with names corresponding to slot names (see the list below). For instance, in the public health surveillance context, the
stateslot is used to indicate outbreaks (default:FALSEfor all observations). For areal time series data, themapandneighbourhoodslots are used to store the spatial structure of the observation region.
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)^2\) describing 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 the
map).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 or modelled.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 three exceptions:
the freq slot can be extracted by frequency(),
the populationFrac slot is accessed by population(),
and the alarm slot is accessed by alarms().
- 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.- frequency
signature(x = "sts"): extract thefreqslot.- start
signature(x = "sts"): extract thestartslot.- 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. This corresponds tocycle(as.ts(x)).- [
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.
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)
}