Title: | 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. This package is part of the RECON (<https://www.repidemicsconsortium.org/>) toolkit for outbreak analysis (<https://www.reconverse.org>). |
Authors: | Tim Taylor [aut, cre] , Thibaut Jombart [ctb] |
Maintainer: | Tim Taylor <[email protected]> |
License: | MIT + file LICENSE |
Version: | 2.6.0 |
Built: | 2024-11-22 11:22:58 UTC |
Source: | https://github.com/reconverse/incidence2 |
Access various elements of an incidence object
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, ...)
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, ...)
x |
An R object. |
... |
Not currently used. |
get_date_index_name()
: The name of the date_index variable of x.
get_dates_name()
: Alias for get_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 for get_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.
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) }
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) }
Generic for coercion to an <incidence2>
object.
as_incidence(x, ...) ## Default S3 method: as_incidence(x, ...) ## S3 method for class 'incidence2' as_incidence(x, ...)
as_incidence(x, ...) ## Default S3 method: as_incidence(x, ...) ## S3 method for class 'incidence2' as_incidence(x, ...)
x |
An R object. |
... |
Additional arguments to be passed to or from other methods. |
An <incidence2>
object.
Coerce to a tibble
## S3 method for class 'incidence2' as_tibble(x, ..., .rows, .name_repair, rownames)
## S3 method for class 'incidence2' as_tibble(x, ..., .rows, .name_repair, rownames)
x |
An incidence2 object. |
... |
Unused, for extensibility. |
.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. |
A tibble of the original input but with no additional attributes.
if (requireNamespace("outbreaks", quietly = TRUE)) { data(ebola_sim_clean, package = "outbreaks") dat <- ebola_sim_clean$linelist x <- incidence(dat, "date_of_onset") as_tibble(x) }
if (requireNamespace("outbreaks", quietly = TRUE)) { data(ebola_sim_clean, package = "outbreaks") dat <- ebola_sim_clean$linelist x <- incidence(dat, "date_of_onset") as_tibble(x) }
Convert incident object to a data frame
## S3 method for class 'incidence2' as.data.frame(x, row.names, optional, ...)
## S3 method for class 'incidence2' as.data.frame(x, row.names, optional, ...)
x |
incidence2 object. |
row.names |
Not used. |
optional |
Not used. |
... |
Not used. |
as.data.frame for the underlying generic.
dat <- data.frame( dates = Sys.Date() + 1:100, names = rep(c("Jo", "John"), 5) ) dat <- incidence(dat, date_index = "dates", groups = "names") as.data.frame(dat)
dat <- data.frame( dates = Sys.Date() + 1:100, names = rep(c("Jo", "John"), 5) ) dat <- incidence(dat, date_index = "dates", groups = "names") as.data.frame(dat)
Coerce to a data.table
## S3 method for class 'incidence2' as.data.table(x, keep.rownames, ...)
## S3 method for class 'incidence2' as.data.table(x, keep.rownames, ...)
x |
An incidence2 object. |
keep.rownames |
Not used. |
... |
Passed to other methods. |
A data.table of the original input but with no additional attributes.
data.table::as.data.table for the underlying generic.
if (requireNamespace("outbreaks", quietly = TRUE)) { data(ebola_sim_clean, package = "outbreaks") dat <- ebola_sim_clean$linelist x <- incidence(dat, "date_of_onset") as.data.table(x) }
if (requireNamespace("outbreaks", quietly = TRUE)) { data(ebola_sim_clean, package = "outbreaks") dat <- ebola_sim_clean$linelist x <- incidence(dat, "date_of_onset") as.data.table(x) }
This function can be used to bootstrap incidence2 objects. Bootstrapping is done by sampling with replacement the original input dates.
bootstrap_incidence(x, randomise_groups = FALSE)
bootstrap_incidence(x, randomise_groups = FALSE)
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 |
As original data are not stored in incidence2 objects, the bootstrapping is achieved by multinomial sampling of date bins weighted by their relative incidence.
An incidence2 object.
Thibaut Jombart, Tim Taylor
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) }
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) }
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
.
complete_dates(x, expand = TRUE, fill = 0L, by = 1L, allow_POSIXct = FALSE)
complete_dates(x, expand = TRUE, fill = 0L, by = 1L, allow_POSIXct = FALSE)
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 |
An incidence2 object.
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)
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)
A dataset containing the daily time-series of cases, tests, hospitalisations, and deaths for UK.
covidregionaldataUK
covidregionaldataUK
A data frame with 6370 rows and 26 variables:
the date that the counts were reported (YYYY-MM-DD)
the region name
the region code
new reported cases for that day
total reported cases up to and including that day
new reported deaths for that day
total reported deaths up to and including that day
new reported recoveries for that day
total reported coveries up to and including that day
new reported hospitalisations for that day
total reported hospitalisations up to and including that day (note this is cumulative total of new reported, not total currently in hospital).
tests for that day
total tests completed up to and including that day
Extracted using the covidregionaldata package on 2021-06-03.
https://CRAN.R-project.org/package=covidregionaldata
cumulate()
computes the cumulative incidence over time for an
incidence2 object.
cumulate(x)
cumulate(x)
x |
incidence2 object. |
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)
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)
This function can be used to estimate the peak of an epidemic curve using bootstrapped samples of the available data.
estimate_peak(x, n = 100L, alpha = 0.05, first_only = TRUE, progress = TRUE)
estimate_peak(x, n = 100L, alpha = 0.05, first_only = TRUE, progress = TRUE)
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) |
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
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.
Thibaut Jombart and Tim Taylor, with inputs on caveats from Michael Höhle.
bootstrap_incidence()
for the bootstrapping underlying this approach and
keep_peaks()
to get the peaks in a single
incidence2 object.
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) }
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) }
incidence()
calculates the incidence of different events across specified
time periods and groupings.
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, fill = 0L, ... )
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. |
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.
A tibble
with subclass incidence2
.
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()
.
browseVignettes("grates")
for more details on the grate object classes.
incidence_()
for a version supporting
tidy-select semantics in some arguments.
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", groups = c("gender", "hospital")) }
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", groups = c("gender", "hospital")) }
incidence_()
calculates the incidence of different events across
specified time periods and groupings. It differs from incidence()
only in
its support for tidy-select
semantics in some of its arguments.
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, ... )
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, ... )
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 |
... |
Not currently used. |
<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.
A tibble
with subclass incidence2
.
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_()
.
browseVignettes("grates")
for more details on the grate object classes.
incidence()
for a the underlying function without support for tidyselect
semantics. This may be preferable for programatic usage.
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, groups = c(gender, hospital)) }
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, groups = c(gender, hospital)) }
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
.
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, ...)
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, ...)
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 |
incidence2 object with the chosen entries.
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) }
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) }
Method for dplyr::mutate that implicitly accounts for the inherent grouping structure of incidence2 objects.
## S3 method for class 'incidence2' mutate( .data, ..., .by, .keep = c("all", "used", "unused", "none"), .before = NULL, .after = NULL )
## S3 method for class 'incidence2' mutate( .data, ..., .by, .keep = c("all", "used", "unused", "none"), .before = NULL, .after = NULL )
.data |
An incidence2 object. |
... |
< The value can be:
|
.by |
Not used as grouping structure implicit. |
.keep |
Control which columns from
|
.before , .after
|
< |
A modified incidence2 object if the necessary invariants are preserved, otherwise a tibble.
dplyr::mutate for the underlying generic.
if (requireNamespace("outbreaks", quietly = TRUE) && requireNamespace("ggplot2", quietly = TRUE)) { data(ebola_sim_clean, package = "outbreaks") ebola_sim_clean$linelist |> subset(!is.na(hospital)) |> incidence_(date_of_onset, hospital, interval = "isoweek") |> mutate(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)) }
if (requireNamespace("outbreaks", quietly = TRUE) && requireNamespace("ggplot2", quietly = TRUE)) { data(ebola_sim_clean, package = "outbreaks") ebola_sim_clean$linelist |> subset(!is.na(hospital)) |> incidence_(date_of_onset, hospital, interval = "isoweek") |> mutate(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)) }
Method for tidyr::nest that implicitly accounts for the inherent grouping structure of incidence2 objects.
## S3 method for class 'incidence2' nest(.data, ..., .by, .key, .names_sep)
## S3 method for class 'incidence2' nest(.data, ..., .by, .key, .names_sep)
.data |
An incidence2 object. |
... |
Not used. |
.by |
Not used. |
.key |
The name of the resulting nested column. Only applicable when
If |
.names_sep |
Not used. |
A nested tibble with rows corresponding to the count variable and (optionally) group columns of the input object.
tidyr::nest for the underlying generic.
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") |> nest() }
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") |> nest() }
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.
## 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, ... )
## 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, ... )
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. |
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.
A ggplot2::ggplot()
object.
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) }
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) }
This function regroups an incidence2 object across the specified groups. The resulting incidence2 object will contains counts aggregated over the specified groups.
regroup(x, groups = NULL)
regroup(x, groups = NULL)
x |
|
groups |
The groups to sum over. If |
regroup_()
for a version supporting
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, "hospital") }
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, "hospital") }
This function regroups an <incidence2>
object across the specified groups.
The resulting <incidence2>
object will contains counts summed over the
groups present in the input. It differs from regroup()
only in
support for <tidy-select>
semantics in the groups
argument.
regroup_(x, groups = NULL)
regroup_(x, groups = NULL)
x |
|
groups |
The groups to sum over. If |
regroup()
for a version without tidyselect semantics. This may be
preferable for programatic usage.
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, hospital) }
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, hospital) }
Split divides and incidence2 object in to it's underlying groupings (count variable and optionally groups).
## S3 method for class 'incidence2' split(x, f, drop, ...)
## S3 method for class 'incidence2' split(x, f, drop, ...)
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. |
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.
vctrs::vec_split()
on which split.incidence2()
is built.
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() }
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() }
Method for dplyr::summarise that implicitly accounts for the inherent grouping structure of incidence2 objects.
## S3 method for class 'incidence2' summarise(.data, ..., .by, .groups)
## S3 method for class 'incidence2' summarise(.data, ..., .by, .groups)
.data |
An incidence2 object. |
... |
< The value can be:
|
.by |
Not used as grouping structure implicit. |
.groups |
Not used. |
A tibble.
dplyr::summarise for the underlying grouping.
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") |> summarise(model = list(glm(count ~ date_index, family = "poisson"))) }
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") |> summarise(model = list(glm(count ~ date_index, family = "poisson"))) }
Summary of an incidence object
## S3 method for class 'incidence2' summary(object, ...)
## S3 method for class 'incidence2' summary(object, ...)
object |
An incidence2 object. |
... |
Not used. |
object (invisibly).
data(ebola_sim_clean, package = "outbreaks") dat <- ebola_sim_clean$linelist inci <- incidence(dat, "date_of_onset", groups = c("gender", "hospital")) summary(inci)
data(ebola_sim_clean, package = "outbreaks") dat <- ebola_sim_clean$linelist inci <- incidence(dat, "date_of_onset", groups = c("gender", "hospital")) summary(inci)
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.
vibrant(n) muted(n)
vibrant(n) muted(n)
n |
Number of colours.
|
vibrant(5) muted(10)
vibrant(5) muted(10)