Type: | Package |
Title: | Compute, Handle and Plot Incidence of Dated Events |
Version: | 2.6.3 |
Description: | Provides functions and classes to compute, handle and visualise incidence from dated events for a defined time interval. Dates can be provided in various standard formats. The class 'incidence2' is used to store computed incidence and can be easily manipulated, subsetted, and plotted. |
Encoding: | UTF-8 |
License: | MIT + file LICENSE |
URL: | https://www.reconverse.org/incidence2/, https://github.com/reconverse/incidence2 |
BugReports: | https://github.com/reconverse/incidence2/issues |
Depends: | grates (≥ 1.3.0), R (≥ 4.1.0) |
Imports: | grDevices, data.table (≥ 1.15.0), pillar, utils, stats, tibble, tidyr, dplyr (≥ 1.1.0), tidyselect, rlang, vctrs, ympes (≥ 1.3.0) |
RoxygenNote: | 7.3.2 |
Suggests: | outbreaks, ggplot2, scales, litedown, testthat (≥ 3.0.0), ciTools, withr |
VignetteBuilder: | litedown |
Config/testthat/load-all: | list(export_all = FALSE, helpers = FALSE) |
Config/testthat/edition: | 3 |
Config/testthat/parallel: | true |
Config/testthat/start-first: | plot, incidence, bootstrap |
LazyData: | true |
NeedsCompilation: | no |
Packaged: | 2025-06-04 13:25:13 UTC; tim |
Author: | Tim Taylor |
Maintainer: | Tim Taylor <tim.taylor@hiddenelephants.co.uk> |
Repository: | CRAN |
Date/Publication: | 2025-06-04 14:00:02 UTC |
incidence2: Compute, Handle and Plot Incidence of Dated Events
Description
Provides functions and classes to compute, handle and visualise incidence from dated events for a defined time interval. Dates can be provided in various standard formats. The class 'incidence2' is used to store computed incidence and can be easily manipulated, subsetted, and plotted.
Author(s)
Maintainer: Tim Taylor tim.taylor@hiddenelephants.co.uk (ORCID)
Other contributors:
Thibaut Jombart thibautjombart@gmail.com [contributor]
See Also
Useful links:
Report bugs at https://github.com/reconverse/incidence2/issues
Access various elements of an incidence object
Description
Access various elements of an incidence object
Usage
get_date_index_name(x, ...)
## Default S3 method:
get_date_index_name(x, ...)
## S3 method for class 'incidence2'
get_date_index_name(x, ...)
get_dates_name(x, ...)
get_count_variable_name(x, ...)
## Default S3 method:
get_count_variable_name(x, ...)
## S3 method for class 'incidence2'
get_count_variable_name(x, ...)
get_count_value_name(x, ...)
## Default S3 method:
get_count_value_name(x, ...)
## S3 method for class 'incidence2'
get_count_value_name(x, ...)
get_group_names(x, ...)
## Default S3 method:
get_group_names(x, ...)
## S3 method for class 'incidence2'
get_group_names(x, ...)
get_date_index(x, ...)
## Default S3 method:
get_date_index(x, ...)
## S3 method for class 'incidence2'
get_date_index(x, ...)
get_dates(x, ...)
get_count_variable(x, ...)
## Default S3 method:
get_count_variable(x, ...)
## S3 method for class 'incidence2'
get_count_variable(x, ...)
get_count_value(x, ...)
## Default S3 method:
get_count_value(x, ...)
## S3 method for class 'incidence2'
get_count_value(x, ...)
get_groups(x, ...)
## Default S3 method:
get_groups(x, ...)
## S3 method for class 'incidence2'
get_groups(x, ...)
Arguments
x |
An R object. |
... |
Not currently used. |
Value
-
get_date_index_name()
: The name of the date_index variable of x. -
get_dates_name()
: Alias forget_date_index_name()
. -
get_count_variable_name()
: The name of the count variable of x. -
get_count_value_name()
: The name of the count value of x. -
get_group_names()
: The name(s) of the group variable(s) of x. -
get_date_index()
: The date_index variable of x. -
get_dates()
: Alias forget_date_index()
. -
get_count_variable()
: The count variable of x. -
get_count_value()
: The count value of x. -
get_groups()
: List of the group variable(s) of x.
Examples
if (requireNamespace("outbreaks", quietly = TRUE)) {
data(ebola_sim_clean, package = "outbreaks")
dat <- ebola_sim_clean$linelist
i <- incidence(
dat,
date_index = "date_of_onset",
groups = c("gender", "hospital")
)
get_count_variable_name(i)
get_group_names(i)
get_dates_name(i)
}
Coerce to and from an incidence object
Description
Generic for coercion to an <incidence2>
object.
Usage
as_incidence(x, ...)
## Default S3 method:
as_incidence(x, ...)
## S3 method for class 'incidence2'
as_incidence(x, ...)
## S3 method for class 'incidence2'
as.data.frame(x, row.names, optional, ...)
## S3 method for class 'incidence2'
as.data.table(x, keep.rownames, ...)
## S3 method for class 'incidence2'
as_tibble(x, ..., .rows, .name_repair, rownames)
Arguments
x |
An R object. |
... |
Additional arguments to be passed to or from other methods. |
row.names |
Not used. |
optional |
Not used. |
keep.rownames |
Not used. |
.rows |
The number of rows, useful to create a 0-column tibble or just as an additional check. |
.name_repair |
Treatment of problematic column names:
This argument is passed on as |
rownames |
How to treat existing row names of a data frame or matrix:
Read more in rownames. |
Value
An object of the desired type with additional attributes dropped.
Examples
if (requireNamespace("outbreaks", quietly = TRUE)) {
data(ebola_sim_clean, package = "outbreaks")
dat <- ebola_sim_clean$linelist
x <- incidence(dat, "date_of_onset")
as.data.frame(dat)
as.data.table(x)
as_tibble(x)
}
Bootstrap incidence time series
Description
This function can be used to bootstrap incidence2 objects. Bootstrapping is done by sampling with replacement the original input dates.
Usage
bootstrap_incidence(x, randomise_groups = FALSE)
Arguments
x |
An incidence2 object. |
randomise_groups |
Should groups be randomised as well in the resampling procedure; respective group sizes will be preserved, but this can be used to remove any group-specific temporal dynamics. If |
Details
As original data are not stored in incidence2 objects, the bootstrapping is achieved by multinomial sampling of date bins weighted by their relative incidence.
Value
An incidence2 object.
Author(s)
Thibaut Jombart, Tim Taylor
Examples
if (requireNamespace("outbreaks", quietly = TRUE)) {
data(fluH7N9_china_2013, package = "outbreaks")
i <- incidence(
fluH7N9_china_2013,
date_index = "date_of_onset",
groups = "gender"
)
bootstrap_incidence(i)
}
Complete dates for all group combinations
Description
This function ensures that an incidence object has the same range of dates
for each grouping. By default missing counts will be filled with 0L
.
Usage
complete_dates(x, expand = TRUE, fill = 0L, by = 1L, allow_POSIXct = FALSE)
Arguments
x |
|
expand |
Should a range of dates from the minimum to maximum value of the date index also be created. If |
fill |
The value to replace missing counts by. Defaults to |
by |
Ignored. |
allow_POSIXct |
Should this function work with POSIXct dates? Defaults to |
Value
An incidence2 object.
Examples
x <- data.frame(
dates = Sys.Date() + c(1,3,4),
groups = c("grp1","grp2", "grp1"),
counts = 1:3
)
i <- incidence(x, date_index = "dates", groups = "groups", counts = "counts")
complete_dates(i)
Regional data for COVID-19 cases in the UK
Description
A dataset containing the daily time-series of cases, tests, hospitalisations, and deaths for UK.
Usage
covidregionaldataUK
Format
A data frame with 6370 rows and 26 variables:
- date
the date that the counts were reported (YYYY-MM-DD)
- region
the region name
- region_code
the region code
- cases_new
new reported cases for that day
- cases_total
total reported cases up to and including that day
- deaths_new
new reported deaths for that day
- deaths_total
total reported deaths up to and including that day
- recovered_new
new reported recoveries for that day
- recovered_total
total reported coveries up to and including that day
- hosp_new
new reported hospitalisations for that day
- hosp_total
total reported hospitalisations up to and including that day (note this is cumulative total of new reported, not total currently in hospital).
- tested_new
tests for that day
- tested_total
total tests completed up to and including that day
Details
Extracted using the covidregionaldata package on 2021-06-03.
Source
https://CRAN.R-project.org/package=covidregionaldata
Compute cumulative 'incidence'
Description
cumulate()
computes the cumulative incidence over time for an
incidence2 object.
Usage
cumulate(x)
Arguments
x |
incidence2 object. |
Examples
dat <- data.frame(
dates = as.integer(c(0,1,2,2,3,5,7)),
groups = factor(c(1, 2, 3, 3, 3, 3, 1))
)
i <- incidence(dat, date_index = "dates", groups = "groups")
cumulate(i)
dplyr and tidyr verbs
Description
dplyr and tidyr methods that implicitly account for the inherent grouping structure of incidence2 objects.
Usage
## S3 method for class 'incidence2'
mutate(
.data,
...,
.by,
.keep = c("all", "used", "unused", "none"),
.before = NULL,
.after = NULL
)
## S3 method for class 'incidence2'
nest(.data, ..., .by, .key, .names_sep)
## S3 method for class 'incidence2'
summarise(.data, ..., .by, .groups)
Arguments
.data |
An incidence2 object. |
... |
Only used by |
.by |
Not used as grouping structure implicit. |
.keep |
Control which columns from
|
.before , .after |
< |
.key |
The name of the resulting nested column. Only applicable when
If |
.names_sep |
Not used. |
.groups |
Not used. |
Value
For
mutate()
a modified incidence2 object if the necessary invariants are preserved, otherwise a tibble.For
nest()
a nested tibble with rows corresponding to the count variable and (optionally) group columns of the input object.For summarise a tibble with rows corresponding to the underlying groupings. The columns are a combination of the grouping keys and the summary expressions provided.
See Also
dplyr::mutate, tidyr::nest and dplyr::summarise for the underlying generics.
Examples
if (requireNamespace("outbreaks", quietly = TRUE) && requireNamespace("ggplot2", quietly = TRUE)) {
data(ebola_sim_clean, package = "outbreaks")
x <- subset(ebola_sim_clean$linelist, !is.na(hospital))
dat <- incidence_(x, date_of_onset, hospital, interval = "isoweek")
mutate(dat, ave = data.table::frollmean(count, n = 3L, align = "right")) |>
plot(border_colour = "white", angle = 45) +
ggplot2::geom_line(ggplot2::aes(x = date_index, y = ave))
nest(dat)
summarise(dat, model = list(glm(count ~ date_index, family = "poisson")))
}
Estimate the peak date of an incidence curve
Description
This function can be used to estimate the peak of an epidemic curve using bootstrapped samples of the available data.
Usage
estimate_peak(x, n = 100L, alpha = 0.05, first_only = TRUE, progress = TRUE)
Arguments
x |
An incidence2 object. |
n |
The number of bootstrap datasets to be generated; defaults to 100.
|
alpha |
The type 1 error chosen for the confidence interval; defaults to 0.05. |
first_only |
Should only the first peak (by date) be kept. Defaults to |
progress |
Should a progress bar be displayed (default = TRUE) |
Details
Input dates are resampled with replacement to form bootstrapped datasets; the peak is reported for each, resulting in a distribution of peak times. When there are ties for peak incidence, only the first date is reported.
Note that the bootstrapping approach used for estimating the peak time makes the following assumptions:
the total number of event is known (no uncertainty on total incidence)
dates with no events (zero incidence) will never be in bootstrapped datasets
the reporting is assumed to be constant over time, i.e. every case is equally likely to be reported
Value
A data frame with the the following columns:
-
observed_date
: the date of peak incidence of the original dataset. -
observed_count
: the peak incidence of the original dataset. -
estimated
: the median peak time of the bootstrap datasets. -
lower_ci/upper_ci
: the confidence interval based on bootstrap datasets. -
bootstrap_peaks
: a nested tibble containing the the peak times of the bootstrapped datasets.
Author(s)
Thibaut Jombart and Tim Taylor, with inputs on caveats from Michael Höhle.
See Also
bootstrap_incidence()
for the bootstrapping underlying this approach and
keep_peaks()
to get the peaks in a single
incidence2 object.
Examples
if (requireNamespace("outbreaks", quietly = TRUE)) {
# load data and create incidence
data(fluH7N9_china_2013, package = "outbreaks")
i <- incidence(fluH7N9_china_2013, date_index = "date_of_onset")
# find 95% CI for peak time using bootstrap
estimate_peak(i)
}
Compute the incidence of events
Description
incidence()
calculates the incidence of different events across specified
time periods and groupings. incidence_()
does the same but with support for
tidy-select semantics in some of its arguments.
Usage
incidence(
x,
date_index,
groups = NULL,
counts = NULL,
count_names_to = "count_variable",
count_values_to = "count",
date_names_to = "date_index",
rm_na_dates = TRUE,
interval = NULL,
offset = NULL,
complete_dates = FALSE,
fill = 0L,
...
)
incidence_(
x,
date_index,
groups = NULL,
counts = NULL,
count_names_to = "count_variable",
count_values_to = "count",
date_names_to = "date_index",
rm_na_dates = TRUE,
interval = NULL,
offset = NULL,
complete_dates = FALSE,
...
)
Arguments
x |
A data frame object representing a linelist or pre-aggregated dataset. |
date_index |
The time index(es) of the given data. This should be the name(s) corresponding to the desired date column(s) in x. A named vector can be used for convenient relabelling of the resultant output. Multiple indices only make sense when |
groups |
An optional vector giving the names of the groups of observations for which incidence should be grouped. A named vector can be used for convenient relabelling of the resultant output. |
counts |
The count variables of the given data. If NULL (default) the data is taken to be a linelist of individual observations. A named vector can be used for convenient relabelling of the resultant output. |
count_names_to |
The column to create which will store the |
count_values_to |
The name of the column to store the resultant count values in. |
date_names_to |
The name of the column to store the date variables in. |
rm_na_dates |
Should |
interval |
An optional scalar integer or string indicating the (fixed) size of the desired time interval you wish to use for for computing the incidence. Defaults to NULL in which case the date_index columns are left unchanged. Numeric values are coerced to integer and treated as a number of days to group. Text strings can be one of: * day or daily * week(s) or weekly * epiweek(s) * isoweek(s) * month(s) or monthly * yearmonth(s) * quarter(s) or quarterly * yearquarter(s) * year(s) or yearly More details can be found in the "Interval specification" section. |
offset |
Only applicable when An optional scalar integer or date indicating the value you wish to start counting periods from relative to the Unix Epoch:
|
complete_dates |
Should the resulting object have the same range of dates for each grouping. Missing counts will be filled with Will attempt to use More flexible completion is possible by using the |
fill |
Only applicable when The value to replace missing counts caused by completing dates. If unset then will default to |
... |
Not currently used. |
Details
incidence2
objects are a sub class of data frame with some
additional invariants. That is, an incidence2
object must:
have one column representing the date index (this does not need to be a
date
object but must have an inherent ordering over time);have one column representing the count variable (i.e. what is being counted) and one variable representing the associated count;
have zero or more columns representing groups;
not have duplicated rows with regards to the date and group variables.
Value
A tibble
with subclass incidence2
.
Interval specification
Where interval
is specified, incidence()
, predominantly uses the
grates
package to generate
appropriate date groupings. The grouping used depends on the value of
interval
. This can be specified as either an integer value or a string
corresponding to one of the classes:
integer values:
<grates_period>
object, grouped by the specified number of days.day, daily:
<Date>
objects.week(s), weekly, isoweek:
<grates_isoweek>
objects.epiweek(s):
<grates_epiweek>
objects.month(s), monthly, yearmonth:
<grates_yearmonth>
objects.quarter(s), quarterly, yearquarter:
<grates_yearquarter>
objects.year(s) and yearly:
<grates_year>
objects.
For "day" or "daily" interval, we provide a thin wrapper around as.Date()
that ensures the underlying data are whole numbers and that time zones are
respected. Note that additional arguments are not forwarded to as.Date()
so for greater flexibility users are advised to modifying your input prior to
calling incidence()
.
See Also
browseVignettes("grates")
for more details on the grate object classes.
Examples
if (requireNamespace("outbreaks", quietly = TRUE)) {
data(ebola_sim_clean, package = "outbreaks")
dat <- ebola_sim_clean$linelist
incidence(dat, "date_of_onset")
incidence_(dat, date_of_onset)
incidence(dat, "date_of_onset", groups = c("gender", "hospital"))
incidence_(dat, date_of_onset, groups = c(gender, hospital))
}
Functions now defunct in package incidence2
Description
These functions are now defunct.
Usage
new_incidence(
x,
date,
groups = NULL,
counts,
measurements = NULL,
validate = TRUE
)
validate_incidence(x)
build_incidence(
x,
date_index,
groups = NULL,
counts = NULL,
na_as_group = TRUE,
FUN = identity,
args = list()
)
get_n(x)
get_interval(x, ...)
get_timespan(x, ...)
facet_plot(x, ...)
Keep first, last and peak occurences
Description
keep_first()
and keep_last()
keep the first and last n
rows to occur
for each grouping when in ascending date order. keep_peaks()
keeps the rows
with the maximum count value for each group. first_peak()
is a convenience
wrapper around keep_peaks()
with the first_only
argument set to TRUE
.
Usage
keep_first(x, n, complete_dates = TRUE, ...)
keep_last(x, n, complete_dates = TRUE, ...)
keep_peaks(x, complete_dates = TRUE, first_only = FALSE, ...)
first_peak(x, complete_dates = TRUE, ...)
Arguments
x |
incidence2 object. |
n |
Number of entries to keep.
|
complete_dates |
Should Defaults to TRUE. |
... |
Other arguments passed to |
first_only |
Should only the first peak (by date) be kept. Defaults to |
Value
incidence2 object with the chosen entries.
Examples
if (requireNamespace("outbreaks", quietly = TRUE)) {
data(ebola_sim_clean, package = "outbreaks")
dat <- ebola_sim_clean$linelist
inci <- incidence(dat, "date_of_onset")
keep_first(inci, 3)
keep_last(inci, 3)
}
Plot an incidence object
Description
plot()
can be used to provide a bar plot of an incidence object. Due
to the complexities with automating plotting it is some what experimental in
nature and it may be better to use ggplot2 directly.
Usage
## S3 method for class 'incidence2'
plot(
x,
y,
width = 1,
colour_palette = vibrant,
border_colour = NA,
na_colour = "grey",
alpha = 0.7,
fill = NULL,
legend = c("right", "left", "bottom", "top", "none"),
title = NULL,
angle = 0,
size = NULL,
nrow = NULL,
n_breaks = 6L,
show_cases = FALSE,
...
)
Arguments
x |
incidence2 object. |
y |
Not used. Required for compatibility with the |
width |
Value between 0 and 1 indicating the relative size of the bars to the interval. Default 1. |
colour_palette |
The color palette to be used for the different count variables. Defaults to |
border_colour |
The color to be used for the borders of the bars. Use |
na_colour |
The colour to plot Defaults to |
alpha |
The alpha level for color transparency, with 1 being fully opaque and 0 fully transparent Defaults to 0.7. |
fill |
Which variable to colour plots by. Must be a If NULL no distinction if made for plot colours. |
legend |
Position of legend in plot. Only applied if One of "right" (default), "left", "bottom", "top" or "none". |
title |
Optional title for the graph. |
angle |
Rotation angle for text. |
size |
text size in pts. |
nrow |
Number of rows used for facetting if there are group variables present and just one count in the incidence object. Numeric values are coerced to integer via |
n_breaks |
Approximate number of breaks calculated using Numeric values are coerced to integer via Default 6L. |
show_cases |
if Normally only used for outbreaks with a small number of cases. Defaults to |
... |
Not currently used. |
Details
Faceting will occur automatically if either grouping variables or multiple counts are present.
If there are multiple count variables, each count will occupy a different row of the resulting plot.
Utilises ggplot2 so this must be installed to use.
Value
A
ggplot2::ggplot()
object.
Examples
if (requireNamespace("outbreaks", quietly = TRUE) && requireNamespace("ggplot2", quietly = TRUE)) {
data(ebola_sim_clean, package = "outbreaks")
dat <- ebola_sim_clean$linelist
inci <- incidence(dat, date_index = "date_of_onset", groups = "hospital")
plot(inci, angle = 45)
inci2 <- regroup(inci)
plot(inci2)
}
Objects exported from other packages
Description
These objects are imported from other packages. Follow the links below to see their documentation.
Regroup 'incidence' objects
Description
This function regroups an incidence2 object across
the specified groups. The resulting incidence2
object will contains counts aggregated over the specified groups. The only
difference between regroup()
and regroup_()
is that the latter is built
on top of tidy-select semantics for the group
input.
Usage
regroup(x, groups = NULL)
regroup_(x, groups = NULL)
Arguments
x |
|
groups |
The groups to sum over. If |
Examples
if (requireNamespace("outbreaks", quietly = TRUE)) {
data(ebola_sim_clean, package = "outbreaks")
dat <- ebola_sim_clean$linelist
i <- incidence(
dat,
date_index = "date_of_onset",
groups = c("gender", "hospital")
)
regroup(i)
regroup_(i)
regroup(i, "hospital")
regroup_(i, hospital)
}
Divide an incidence2 object in to it's implicit groupings
Description
Split divides and incidence2 object in to it's underlying groupings (count variable and optionally groups).
Usage
## S3 method for class 'incidence2'
split(x, f, drop, ...)
Arguments
x |
An incidence2 object. |
f |
Not used. Present only for generic compatibility. |
drop |
Not used. Present only for generic compatibility. |
... |
Not used. Present only for generic compatibility. |
Value
A list of tibbles contained the split data. This list also has a "key" attribute which is a tibble with rows corresponding to the grouping of each split.
See Also
vctrs::vec_split()
on which split.incidence2()
is built.
Examples
if (requireNamespace("outbreaks", quietly = TRUE)) {
data(ebola_sim_clean, package = "outbreaks")
ebola_sim_clean$linelist |>
subset(!is.na(hospital)) |>
incidence_(date_of_onset, hospital, interval = "isoweek") |>
split()
}
Summary of an incidence object
Description
Summary of an incidence object
Usage
## S3 method for class 'incidence2'
summary(object, ...)
Arguments
object |
An incidence2 object. |
... |
Not used. |
Value
object (invisibly).
Examples
data(ebola_sim_clean, package = "outbreaks")
dat <- ebola_sim_clean$linelist
inci <- incidence(dat, "date_of_onset", groups = c("gender", "hospital"))
summary(inci)
Color palettes used in incidence
Description
These functions are color palettes used in incidence. The palettes come from
https://personal.sron.nl/~pault/#sec:qualitative and exclude grey
, which
is reserved for missing data.
Usage
vibrant(n)
muted(n)
Arguments
n |
Number of colours.
|
Examples
vibrant(5)
muted(10)