incidence/ 0000755 0001762 0000144 00000000000 13750553417 012205 5 ustar ligges users incidence/NAMESPACE 0000644 0001762 0000144 00000003422 13750310652 013415 0 ustar ligges users # Generated by roxygen2: do not edit by hand
S3method("[",incidence)
S3method("group_names<-",default)
S3method("group_names<-",incidence)
S3method(as.data.frame,incidence)
S3method(as.incidence,data.frame)
S3method(as.incidence,matrix)
S3method(as.incidence,numeric)
S3method(cumulate,default)
S3method(cumulate,incidence)
S3method(dim,incidence)
S3method(get_counts,incidence)
S3method(get_dates,default)
S3method(get_dates,incidence)
S3method(get_fit,incidence_fit)
S3method(get_fit,incidence_fit_list)
S3method(get_info,incidence_fit)
S3method(get_info,incidence_fit_list)
S3method(get_interval,default)
S3method(get_interval,incidence)
S3method(get_n,default)
S3method(get_n,incidence)
S3method(get_timespan,default)
S3method(get_timespan,incidence)
S3method(group_names,default)
S3method(group_names,incidence)
S3method(incidence,Date)
S3method(incidence,POSIXt)
S3method(incidence,character)
S3method(incidence,default)
S3method(incidence,integer)
S3method(incidence,numeric)
S3method(plot,incidence)
S3method(plot,incidence_fit)
S3method(plot,incidence_fit_list)
S3method(print,incidence)
S3method(print,incidence_fit)
S3method(print,incidence_fit_list)
S3method(subset,incidence)
export("group_names<-")
export(add_incidence_fit)
export(as.incidence)
export(bootstrap)
export(cumulate)
export(estimate_peak)
export(find_peak)
export(fit)
export(fit_optim_split)
export(get_counts)
export(get_dates)
export(get_fit)
export(get_info)
export(get_interval)
export(get_n)
export(get_timespan)
export(group_names)
export(incidence)
export(incidence_pal1)
export(incidence_pal1_dark)
export(incidence_pal1_light)
export(make_breaks)
export(pool)
export(scale_x_incidence)
importFrom(grDevices,colorRampPalette)
importFrom(graphics,plot)
importFrom(stats,as.ts)
importFrom(utils,head)
importFrom(utils,tail)
incidence/demo/ 0000755 0001762 0000144 00000000000 13750256226 013127 5 ustar ligges users incidence/demo/00Index 0000644 0001762 0000144 00000000070 13750256226 014256 0 ustar ligges users incidence-demo Demonstration of the incidence package
incidence/demo/incidence-demo.R 0000644 0001762 0000144 00000006027 13750256226 016122 0 ustar ligges users #' Example 1 ------------------------------------------------------------------
#'
#' **computing and manipulating stratified weekly incidence**
#'
#' 1) import data
#'
library('outbreaks')
dat1 <- ebola_sim_clean$linelist
str(dat1, strict.width = "cut", width = 76)
#' 2) build an incidence object
#'
#+ incidence-curve, fig.width=9, fig.height=5
library('incidence')
library('ggplot2')
# compute weekly stratified incidence
i.7.group <- incidence(dat1$date_of_onset, interval = 7, groups = dat1$hospital)
# print incidence object
i.7.group
# plot incidence object
my_theme <- theme_bw(base_size = 12) +
theme(panel.grid.minor = element_blank()) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5, color = "black"))
plot(i.7.group, border = "white") +
my_theme +
theme(legend.position = c(0.8, 0.75))
#' 3) Manipulate incidence object
#'
#+ incidence-early-curve, fig.width=6, fig.height=6
# plot the first 18 weeks, defined hospitals, and use different colors
i.7.sub <- i.7.group[1:18, c(1:2, 4:5)]
hosp_colors <- c("#899DA4", "#C93312", "#FAEFD1", "#DC863B")
plot(i.7.sub, show_cases = TRUE, border = "black", color = hosp_colors) +
my_theme +
theme(legend.position = c(0.35, 0.8))
# exclude NA group by disabling treating NA as a separate group
i.7.group0 <- incidence(dat1$date_of_onset,
interval = 7,
groups = dat1$hospital,
na_as_group = FALSE)
# exclude NA using [ operator
i.7.group1 <- subset(i.7.group, groups = -ncol(i.7.group))
# exclude NA group using [ operator
i.7.group2 <- i.7.group[, -ncol(i.7.group)]
# check the resulting incidence objects are identical
identical(i.7.group0$counts, i.7.group1$counts)
identical(i.7.group1, i.7.group2)
# check groups
colnames(i.7.group1$counts)
#' Example 2 ------------------------------------------------------------------
#'
#' **importing pre-computed daily incidence and fitting log-linear model**
#'
#' 1) Import pre-computed daily incidence
#'
#+ incidence-curve2, fig.width=9, fig.height=6
# preview datasets
head(zika_girardot_2015, 3)
head(zika_sanandres_2015, 3)
# combine two datasets into one
dat2 <- merge(zika_girardot_2015, zika_sanandres_2015, by = "date", all = TRUE)
# rename variables
names(dat2)[2:3] <- c("Girardot", "San Andres")
# replace NA with 0
dat2[is.na(dat2)] <- 0
# convert pre-computed incidence in data.frame into incidence object
# grouped by locations
i.group <- as.incidence(x = dat2[, 2:3], dates = dat2$date)
# pool incidence across two locations
i.pooled <- pool(i.group)
cowplot::plot_grid(
plot(i.group, border = "white") + my_theme + theme(legend.position = c(0.9, 0.7)),
plot(i.pooled, border = "white") + my_theme,
ncol = 1,
labels = c("(A)", "(B)"),
label_size = 16,
label_x = 0.06,
label_y = 0.94
)
#' 2) Fit log-linear regression model
#'
#+ incidence-fit, fig.width=9, fig.height=4
library('magrittr')
fos <- fit_optim_split(i.pooled)
fos$split
fos$fit
plot(i.pooled, border = "white") %>%
add_incidence_fit(fos$fit) +
my_theme
incidence/LICENSE 0000644 0001762 0000144 00000000054 13750256226 013207 0 ustar ligges users YEAR: 2016
COPYRIGHT HOLDER: Thibaut Jombart incidence/man/ 0000755 0001762 0000144 00000000000 13750256226 012756 5 ustar ligges users incidence/man/palettes.Rd 0000644 0001762 0000144 00000001603 13750256226 015066 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/palettes.R
\name{incidence_pal1}
\alias{incidence_pal1}
\alias{palettes}
\alias{incidence_pal1_light}
\alias{incidence_pal1_dark}
\title{Color palettes used in incidence}
\usage{
incidence_pal1(n)
incidence_pal1_light(n)
incidence_pal1_dark(n)
}
\arguments{
\item{n}{a number of colors}
}
\description{
These functions are color palettes used in incidence.
}
\examples{
plot(1:4, cex=8, pch=20, col = incidence_pal1(4),
main = "palette: incidence_pal1")
plot(1:100, cex=8, pch=20, col = incidence_pal1(100),
main ="palette: incidence_pal1")
plot(1:100, cex=8, pch=20, col = incidence_pal1_light(100),
main="palette: incidence_pal1_light")
plot(1:100, cex=8, pch=20, col = incidence_pal1_dark(100),
main="palette: incidence_pal1_dark")
}
\author{
Thibaut Jombart \email{thibautjombart@gmail.com}
}
incidence/man/find_peak.Rd 0000644 0001762 0000144 00000002226 13750256226 015167 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/find_peak.R
\name{find_peak}
\alias{find_peak}
\title{Find the peak date of an incidence curve}
\usage{
find_peak(x, pool = TRUE)
}
\arguments{
\item{x}{An \code{incidence} object.}
\item{pool}{If \code{TRUE} (default), any groups will be pooled before finding
a peak. If \code{FALSE}, separate peaks will be found for each group.}
}
\value{
The date of the (first) highest incidence in the data.
}
\description{
This function can be used to find the peak of an epidemic curve stored as an
\code{incidence} object.
}
\examples{
if (require(outbreaks) && require(ggplot2)) { withAutoprint({
i <- incidence(fluH7N9_china_2013$date_of_onset)
i
plot(i)
## one simple bootstrap
x <- bootstrap(i)
x
plot(x)
## find 95\% CI for peak time using bootstrap
find_peak(i)
## show confidence interval
plot(i) + geom_vline(xintercept = find_peak(i), col = "red", lty = 2)
})}
}
\seealso{
\code{\link[=estimate_peak]{estimate_peak()}} for bootstrap estimates of the peak time
}
\author{
Thibaut Jombart \email{thibautjombart@gmail.com}, Zhian N. Kamvar
\email{zkamvar@gmail.com}
}
incidence/man/cumulate.Rd 0000644 0001762 0000144 00000002202 13750256226 015060 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cumulate.R
\name{cumulate}
\alias{cumulate}
\alias{cumulate.default}
\alias{cumulate.incidence}
\title{Compute cumulative 'incidence'}
\usage{
cumulate(x)
\method{cumulate}{default}(x)
\method{cumulate}{incidence}(x)
}
\arguments{
\item{x}{An incidence object.}
}
\description{
\code{cumulate} is an S3 generic to compute cumulative numbers, with methods
for different types of objects:
}
\details{
\itemize{
\item default method is a wrapper for \code{cumsum}
\item \code{incidence} objects: computes cumulative incidence over time
\item \code{projections} objects: same, for \code{projections} objects,
implemented in the similarly named package; see \code{?cumulate.projections}
for more information, after loading the package
}
}
\examples{
dat <- as.integer(c(0,1,2,2,3,5,7))
group <- factor(c(1, 2, 3, 3, 3, 3, 1))
i <- incidence(dat, groups = group)
i
plot(i)
i_cum <- cumulate(i)
i_cum
plot(i_cum)
}
\seealso{
The \code{\link[=incidence]{incidence()}} function to generate the 'incidence'
objects.
}
\author{
Thibaut Jombart \email{thibautjombart@gmail.com}
}
incidence/man/fit.Rd 0000644 0001762 0000144 00000013053 13750256226 014031 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fit.R, R/fit_optim_split.R, R/print.R
\name{fit}
\alias{fit}
\alias{fit_optim_split}
\alias{print.incidence_fit}
\alias{print.incidence_fit_list}
\title{Fit exponential models to incidence data}
\usage{
fit(x, split = NULL, level = 0.95, quiet = FALSE)
fit_optim_split(
x,
window = x$timespan/4,
plot = TRUE,
quiet = TRUE,
separate_split = TRUE
)
\method{print}{incidence_fit}(x, ...)
\method{print}{incidence_fit_list}(x, ...)
}
\arguments{
\item{x}{An incidence object, generated by the function
\code{\link[=incidence]{incidence()}}. For the plotting function, an \code{incidence_fit}
object.}
\item{split}{An optional time point identifying the separation between the
two models. If NULL, a single model is fitted. If provided, two models would
be fitted on the time periods on either side of the split.}
\item{level}{The confidence interval to be used for predictions; defaults to
95\%.}
\item{quiet}{A logical indicating if warnings from \code{fit} should be
hidden; FALSE by default. Warnings typically indicate some zero incidence,
which are removed before performing the log-linear regression.}
\item{window}{The size, in days, of the time window either side of the
split.}
\item{plot}{A logical indicating whether a plot should be added to the
output (\code{TRUE}, default), showing the mean R2 for various splits.}
\item{separate_split}{If groups are present, should separate split dates be
determined for each group? Defaults to \code{TRUE}, in which separate split dates
and thus, separate models will be constructed for each group. When \code{FALSE},
the split date will be determined from the pooled data and modelled with the
groups as main effects and interactions with date.}
\item{...}{currently unused.}
}
\value{
For \code{fit()}, a list with the class \code{incidence_fit} (for a
single model), or a list containing two \code{incidence_fit} objects (when
fitting two models). \code{incidence_fit} objects contain:
\itemize{
\item \verb{$model}: the fitted linear model
\item \verb{$info}: a list containing various information extracted from the model
(detailed further)
\item \verb{$origin}: the date corresponding to day '0'
}
The \verb{$info} item is a list containing:
\itemize{
\item \code{r}: the growth rate
\item \code{r.conf}: the confidence interval of 'r'
\item \code{pred}: a \code{data.frame} containing predictions of the model,
including the true dates (\code{dates}), their numeric version used in the
model (\code{dates.x}), the predicted value (\code{fit}), and the lower
(\code{lwr}) and upper (\code{upr}) bounds of the associated confidence
interval.
\item \code{doubling}: the predicted doubling time in days; exists only if 'r' is
positive
\item \code{doubling.conf}: the confidence interval of the doubling time
\item \code{halving}: the predicted halving time in days; exists only if 'r' is
negative
\item \code{halving.conf}: the confidence interval of the halving time
}
For \code{fit_optim_split}, a list containing:
\itemize{
\item \code{df}: a \code{data.frame} of dates that were used in the optimization
procedure, and the corresponding average R2 of the resulting models.
\item \code{split}: the optimal splitting date
\item \code{fit}: an \code{incidence_fit_list} object containing the fit for each split.
If the \code{separate_split = TRUE}, then the \code{incidence_fit_list} object will
contain these splits nested within each group. All of the \code{incidence_fit}
objects can be retrieved with \code{\link[=get_fit]{get_fit()}}.
\item \code{plot}: a plot showing the content of \code{df} (ggplot2 object)
}
}
\description{
The function \code{fit} fits two exponential models to incidence data, of the
form: \eqn{log(y) = r * t + b} \cr where 'y' is the incidence, 't' is time
(in days), 'r' is the growth rate, and 'b' is the origin. The function \code{fit}
will fit one model by default, but will fit two models on either side of a
splitting date (typically the peak of the epidemic) if the argument \code{split}
is provided. When groups are present, these are included in the model as main
effects and interactions with dates. The function \code{fit_optim_split()} can be
used to find the optimal 'splitting' date, defined as the one for which the
best average R2 of the two models is obtained. Plotting can be done using
\code{plot}, or added to an existing incidence plot by the piping-friendly
function \code{add_incidence_fit()}.
}
\examples{
if (require(outbreaks)) { withAutoprint({
dat <- ebola_sim$linelist$date_of_onset
## EXAMPLE WITH A SINGLE MODEL
## compute weekly incidence
i.7 <- incidence(dat, interval=7)
plot(i.7)
plot(i.7[1:20])
## fit a model on the first 20 weeks
f <- fit(i.7[1:20])
f
names(f)
head(get_info(f, "pred"))
## plot model alone (not recommended)
plot(f)
## plot data and model (recommended)
plot(i.7, fit = f)
plot(i.7[1:25], fit = f)
## piping versions
if (require(magrittr)) { withAutoprint({
plot(i.7) \%>\% add_incidence_fit(f)
## EXAMPLE WITH 2 PHASES
## specifying the peak manually
f2 <- fit(i.7, split = as.Date("2014-10-15"))
f2
plot(i.7) \%>\% add_incidence_fit(f2)
## finding the best 'peak' date
f3 <- fit_optim_split(i.7)
f3
plot(i.7) \%>\% add_incidence_fit(f3$fit)
})}
})}
}
\seealso{
the \code{\link[=incidence]{incidence()}} function to generate the 'incidence'
objects. The \code{\link[=get_fit]{get_fit()}} function to flatten \code{incidence_fit_list} objects to
a list of \code{incidence_fit} objects.
}
\author{
Thibaut Jombart \email{thibautjombart@gmail.com}, Zhian N. Kamvar
\email{zkamvar@gmail.com}.
}
incidence/man/subset.Rd 0000644 0001762 0000144 00000003222 13750256226 014551 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/subset.R
\name{subset.incidence}
\alias{subset.incidence}
\alias{"subset.incidence"}
\alias{"[.incidence"}
\alias{[.incidence}
\title{Subsetting 'incidence' objects}
\usage{
\method{subset}{incidence}(x, ..., from = min(x$dates), to = max(x$dates), groups = TRUE)
\method{[}{incidence}(x, i, j)
}
\arguments{
\item{x}{An incidence object, generated by the function
\code{\link[=incidence]{incidence()}}.}
\item{...}{Further arguments passed to other methods (not used).}
\item{from}{The starting date; data strictly before this date are discarded.}
\item{to}{The ending date; data strictly after this date are discarded.}
\item{groups}{(optional) The groups to retained, indicated as subsets of the
columns of x$counts.}
\item{i}{a subset of dates to retain}
\item{j}{a subset of groups to retain}
}
\description{
Two functions can be used to subset incidence objects. The function
\code{subset} permits to retain dates within a specified range and,
optionally, specific groups. The operator "[" can be used as for matrices,
using the syntax \code{x[i,j]} where 'i' is a subset of dates, and 'j' is a
subset of groups.
}
\examples{
## example using simulated dataset
if(require(outbreaks)) { withAutoprint({
onset <- ebola_sim$linelist$date_of_onset
## weekly incidence
inc <- incidence(onset, interval = 7)
inc
inc[1:10] # first 10 weeks
plot(inc[1:10])
inc[-c(11:15)] # remove weeks 11-15
plot(inc[-c(11:15)])
})}
}
\seealso{
The \code{\link[=incidence]{incidence()}} function to generate the 'incidence'
objects.
}
\author{
Thibaut Jombart \email{thibautjombart@gmail.com}
}
incidence/man/get_fit.Rd 0000644 0001762 0000144 00000004200 13750256226 014662 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_fit.R, R/get_info.R
\name{get_fit}
\alias{get_fit}
\alias{get_fit.incidence_fit}
\alias{get_fit.incidence_fit_list}
\alias{get_info}
\alias{get_info.incidence_fit}
\alias{get_info.incidence_fit_list}
\title{Accessors for \code{incidence_fit} objects}
\usage{
get_fit(x)
\method{get_fit}{incidence_fit}(x)
\method{get_fit}{incidence_fit_list}(x)
get_info(x, what = "r", ...)
\method{get_info}{incidence_fit}(x, what = "r", ...)
\method{get_info}{incidence_fit_list}(x, what = "r", groups = NULL, na.rm = TRUE, ...)
}
\arguments{
\item{x}{an \code{incidence_fit} or \code{incidence_fit_list}
object.}
\item{what}{the name of the item in the "info" element of the \code{incidence_fit}
object.}
\item{...}{currently unused.}
\item{groups}{if \code{what = "pred"} and \code{x} is an \code{incidence_fit_list} object,
then this indicates what part of the nesting hierarchy becomes the column
named "groups". Defaults to \code{NULL}, indicating that no groups column will
be added/modified.}
\item{na.rm}{when \code{TRUE} (default), missing values will be excluded from the
results.}
}
\value{
a list of \code{incidence_fit} objects.
}
\description{
Accessors for \code{incidence_fit} objects
}
\examples{
if (require(outbreaks)) { withAutoprint({
dat <- ebola_sim$linelist$date_of_onset
## EXAMPLE WITH A SINGLE MODEL
## compute weekly incidence
sex <- ebola_sim$linelist$gender
i.sex <- incidence(dat, interval = 7, group = sex)
## Compute the optimal split for each group separately
fits <- fit_optim_split(i.sex, separate_split = TRUE)
## `fits` contains an `incidence_fit_list` object
fits$fit
## Grab the list of `incidence_fit` objects
get_fit(fits$fit)
## Get the predictions for all groups
get_info(fits$fit, "pred", groups = 1)
## Get the predictions, but set `groups` to "before" and "after"
get_info(fits$fit, "pred", groups = 2)
## Get the reproduction number
get_info(fits$fit, "r")
## Get the doubling confidence interval
get_info(fits$fit, "doubling.conf")
## Get the halving confidence interval
get_info(fits$fit, "halving.conf")
})}
}
incidence/man/get_dates.Rd 0000644 0001762 0000144 00000002401 13750256226 015201 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_dates.R
\name{get_dates}
\alias{get_dates}
\alias{get_dates.default}
\alias{get_dates.incidence}
\title{Retrieve dates from an incidence object}
\usage{
get_dates(x, ...)
\method{get_dates}{default}(x, ...)
\method{get_dates}{incidence}(x, position = "left", count_days = FALSE, ...)
}
\arguments{
\item{x}{an \link{incidence} object}
\item{...}{Unused}
\item{position}{One of "left", "center", "middle", or "right" specifying what
side of the bin the date should be drawn from.}
\item{count_days}{If \code{TRUE}, the result will be represented as the number of
days from the first date.}
}
\value{
a vector of dates or numerics
}
\description{
Retrieve dates from an incidence object
}
\examples{
set.seed(999)
dat <- as.Date(Sys.Date()) + sample(-3:50, 100, replace = TRUE)
x <- incidence(dat, interval = "month")
get_dates(x)
get_dates(x, position = "middle")
set.seed(999)
dat <- as.Date(Sys.Date()) + sample(-3:50, 100, replace = TRUE)
x <- incidence(dat, interval = "month")
get_dates(x)
get_dates(x, "center")
get_dates(x, "right")
# Return dates by number of days from the first date
get_dates(x, count_days = TRUE)
get_dates(incidence(-5:5), count_days = TRUE)
}
\keyword{accessors}
incidence/man/plot.incidence.Rd 0000644 0001762 0000144 00000013021 13750256226 016140 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot.R, R/scale_x_incidence.R
\name{plot.incidence}
\alias{plot.incidence}
\alias{add_incidence_fit}
\alias{plot.incidence_fit}
\alias{plot.incidence_fit_list}
\alias{scale_x_incidence}
\alias{make_breaks}
\title{Plot function for incidence objects}
\usage{
\method{plot}{incidence}(
x,
...,
fit = NULL,
stack = is.null(fit),
color = "black",
border = NA,
col_pal = incidence_pal1,
alpha = 0.7,
xlab = "",
ylab = NULL,
labels_week = !is.null(x$weeks),
labels_iso = !is.null(x$isoweeks),
show_cases = FALSE,
n_breaks = 6
)
add_incidence_fit(p, x, col_pal = incidence_pal1)
\method{plot}{incidence_fit}(x, ...)
\method{plot}{incidence_fit_list}(x, ...)
scale_x_incidence(x, n_breaks = 6, labels_week = TRUE, ...)
make_breaks(x, n_breaks = 6L, labels_week = TRUE)
}
\arguments{
\item{x}{An incidence object, generated by the function
\code{\link[=incidence]{incidence()}}.}
\item{...}{arguments passed to \code{\link[ggplot2:scale_date]{ggplot2::scale_x_date()}},
\code{\link[ggplot2:scale_date]{ggplot2::scale_x_datetime()}}, or \code{\link[ggplot2:scale_continuous]{ggplot2::scale_x_continuous()}}, depending
on how the \verb{$date} element is stored in the incidence object.}
\item{fit}{An 'incidence_fit' object as returned by \code{\link[=fit]{fit()}}.}
\item{stack}{A logical indicating if bars of multiple groups should be
stacked, or displayed side-by-side.}
\item{color}{The color to be used for the filling of the bars; NA for
invisible bars; defaults to "black".}
\item{border}{The color to be used for the borders of the bars; NA for
invisible borders; defaults to NA.}
\item{col_pal}{The color palette to be used for the groups; defaults to
\code{incidence_pal1}. See \code{\link[=incidence_pal1]{incidence_pal1()}} for other palettes implemented in
incidence.}
\item{alpha}{The alpha level for color transparency, with 1 being fully
opaque and 0 fully transparent; defaults to 0.7.}
\item{xlab}{The label to be used for the x-axis; empty by default.}
\item{ylab}{The label to be used for the y-axis; by default, a label will be
generated automatically according to the time interval used in incidence
computation.}
\item{labels_week}{a logical value indicating whether labels x axis tick
marks are in week format YYYY-Www when plotting weekly incidence; defaults to
TRUE.}
\item{labels_iso}{(deprecated) This has been superceded by \code{labels_iso}.
Previously:a logical value indicating whether labels x axis tick marks are
in ISO 8601 week format yyyy-Www when plotting ISO week-based weekly
incidence; defaults to be TRUE.}
\item{show_cases}{if \code{TRUE} (default: \code{FALSE}), then each observation will be
colored by a border. The border defaults to a white border unless specified
otherwise. This is normally used outbreaks with a small number of cases.
Note: this can only be used if \code{stack = TRUE}}
\item{n_breaks}{the ideal number of breaks to be used for the x-axis
labeling}
\item{p}{An existing incidence plot.}
}
\value{
\itemize{
\item \code{plot()} a \code{\link[ggplot2:ggplot]{ggplot2::ggplot()}} object.
\item \code{make_breaks()} a two-element list. The "breaks" element will contain the
evenly-spaced breaks as either dates or numbers and the "labels" element
will contain either a vector of weeks OR a \code{\link[ggplot2:waiver]{ggplot2::waiver()}} object.
\item \code{scale_x_incidence()} a \pkg{ggplot2} "ScaleContinuous" object.
}
}
\description{
This function is used to visualise the output of the \code{\link[=incidence]{incidence()}}
function using the package \code{ggplot2}. #'
}
\details{
\itemize{
\item \code{plot()} will visualise an incidence object using \code{ggplot2}
\item \code{make_breaks()} calculates breaks from an incidence object that always
align with the bins and start on the first observed incidence.
\item \code{scale_x_incidence()} produces and appropriate \code{ggplot2} scale based on
an incidence object.
}
}
\examples{
if(require(outbreaks) && require(ggplot2)) { withAutoprint({
onset <- outbreaks::ebola_sim$linelist$date_of_onset
## daily incidence
inc <- incidence(onset)
inc
plot(inc)
## weekly incidence
inc.week <- incidence(onset, interval = 7)
inc.week
plot(inc.week) # default to label x axis tick marks with isoweeks
plot(inc.week, labels_week = FALSE) # label x axis tick marks with dates
plot(inc.week, border = "white") # with visible border
## use group information
sex <- outbreaks::ebola_sim$linelist$gender
inc.week.gender <- incidence(onset, interval = "1 epiweek", groups = sex)
plot(inc.week.gender)
plot(inc.week.gender, labels_week = FALSE)
## show individual cases at the beginning of the epidemic
inc.week.8 <- subset(inc.week.gender, to = "2014-06-01")
p <- plot(inc.week.8, show_cases = TRUE, border = "black")
p
## update the range of the scale
lim <- c(min(get_dates(inc.week.8)) - 7*5,
aweek::week2date("2014-W50", "Sunday"))
lim
p + scale_x_incidence(inc.week.gender, limits = lim)
## customize plot with ggplot2
plot(inc.week.8, show_cases = TRUE, border = "black") +
theme_classic(base_size = 16) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
## adding fit
fit <- fit_optim_split(inc.week.gender)$fit
plot(inc.week.gender, fit = fit)
plot(inc.week.gender, fit = fit, labels_week = FALSE)
})}
}
\seealso{
The \code{\link[=incidence]{incidence()}} function to generate the 'incidence'
objects.
}
\author{
Thibaut Jombart \email{thibautjombart@gmail.com}
Zhian N. Kamvar \email{zkamvar@gmail.com}
}
incidence/man/bootstrap.Rd 0000644 0001762 0000144 00000002502 13750256226 015261 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/bootstrap.R
\name{bootstrap}
\alias{bootstrap}
\title{Bootstrap incidence time series}
\usage{
bootstrap(x, randomise_groups = FALSE)
}
\arguments{
\item{x}{An \code{incidence} object.}
\item{randomise_groups}{A \code{logical} indicating whether groups should 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 \code{FALSE} (default), data are resampled within groups.}
}
\value{
An \code{incidence} object.
}
\description{
This function can be used to bootstrap \code{incidence} objects. Bootstrapping is
done by sampling with replacement the original input dates. See \code{details} for
more information on how this is implemented.
}
\details{
As original data are not stored in \code{incidence} objects, the
bootstrapping is achieved by multinomial sampling of date bins weighted by
their relative incidence.
}
\examples{
if (require(outbreaks) && require(ggplot2)) { withAutoprint({
i <- incidence(fluH7N9_china_2013$date_of_onset)
i
plot(i)
## one simple bootstrap
x <- bootstrap(i)
x
plot(x)
})}
}
\seealso{
\link{find_peak} to use estimate peak date using bootstrap
}
\author{
Thibaut Jombart \email{thibautjombart@gmail.com}
}
incidence/man/accessors.Rd 0000644 0001762 0000144 00000004325 13750256226 015236 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/dim.R, R/get_interval.R, R/get_n.R,
% R/get_timespan.R
\name{dim.incidence}
\alias{dim.incidence}
\alias{get_interval}
\alias{get_interval.default}
\alias{get_interval.incidence}
\alias{get_n}
\alias{get_n.default}
\alias{get_n.incidence}
\alias{get_timespan}
\alias{get_timespan.default}
\alias{get_timespan.incidence}
\title{Access various elements of an incidence object}
\usage{
\method{dim}{incidence}(x)
get_interval(x, ...)
\method{get_interval}{default}(x, ...)
\method{get_interval}{incidence}(x, integer = TRUE, ...)
get_n(x)
\method{get_n}{default}(x)
\method{get_n}{incidence}(x)
get_timespan(x)
\method{get_timespan}{default}(x)
\method{get_timespan}{incidence}(x)
}
\arguments{
\item{x}{an \link{incidence} object.}
\item{...}{Unused}
\item{integer}{When \code{TRUE} (default), the interval will be converted to an
integer vector if it is stored as a character in the incidence object.}
}
\value{
\itemize{
\item \code{dim()} the dimensions in the number of bins and number of groups
}
\itemize{
\item \code{get_interval()} if \code{integer = TRUE}: an integer vector, otherwise: the
value stored in \code{x$interval}
}
\itemize{
\item \code{get_n()} The total number of cases stored in the object
}
\itemize{
\item \code{get_timespan()}: an \code{integer} denoting the timespan represented by the
incidence object.
}
}
\description{
Access various elements of an incidence object
}
\examples{
set.seed(999)
dat <- as.Date(Sys.Date()) + sample(-3:50, 100, replace = TRUE)
x <- incidence(dat, interval = "month")
# the value stored in the interval element
get_interval(x)
# the numeric value of the interval in days
get_interval(x, integer = FALSE)
# the number of observations in the object
get_n(x)
# the length of time represented
get_timespan(x)
# the number of groups
ncol(x)
# the number of bins (intervals)
nrow(x)
}
\seealso{
\itemize{
\item \code{\link[=get_counts]{get_counts()}} to access the matrix of counts
\item \code{\link[=get_dates]{get_dates()}} to access the dates on the right, left, and center of the
interval.
\item \code{\link[=group_names]{group_names()}} to access and possibly re-name the groups
}
}
\keyword{accessors}
incidence/man/pool.Rd 0000644 0001762 0000144 00000001514 13750256226 014217 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/pool.R
\name{pool}
\alias{pool}
\title{Pool 'incidence' across groups}
\usage{
pool(x)
}
\arguments{
\item{x}{An 'incidence' object.}
}
\description{
This function pools incidence across all groups of an \code{incidence}
object. The resulting \code{\link[=incidence]{incidence()}} object will contains counts
summed over all groups present in the input.
}
\examples{
dat <- as.integer(c(0,1,2,2,3,5,7))
group <- factor(c(1, 2, 3, 3, 3, 3, 1))
i <- incidence(dat, groups = group)
i
i$counts
## pool all groups
pool(i)
pool(i)$counts
## pool only groups 1 and 3
pool(i[,c(1,3)])
pool(i[,c(1,3)])$counts
}
\seealso{
The \code{\link[=incidence]{incidence()}} function to generate the 'incidence'
objects.
}
\author{
Thibaut Jombart \email{thibautjombart@gmail.com}
}
incidence/man/group_names.Rd 0000644 0001762 0000144 00000003520 13750256226 015564 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/group_names.R
\name{group_names}
\alias{group_names}
\alias{group_names<-}
\alias{group_names.default}
\alias{group_names<-.default}
\alias{`group_names<-`.default}
\alias{group_names.incidence}
\alias{group_names<-.incidence}
\title{extract and set group names}
\usage{
group_names(x, value)
group_names(x) <- value
\method{group_names}{default}(x, value)
\method{group_names}{default}(x) <- value
\method{group_names}{incidence}(x, value = NULL)
\method{group_names}{incidence}(x) <- value
}
\arguments{
\item{x}{an \code{\link[=incidence]{incidence()}} object.}
\item{value}{character vector used to rename groups}
}
\value{
an integer indicating the number of groups present in the incidence
object.
}
\description{
extract and set group names
}
\details{
This accessor will return a
}
\examples{
i <- incidence(dates = sample(10, 100, replace = TRUE),
interval = 1L,
groups = sample(letters[1:3], 100, replace = TRUE))
i
group_names(i)
# change the names of the groups
group_names(i) <- c("Group 1", "Group 2", "Group 3")
i
# example if there are mistakes in the original data, e.g.
# something is misspelled
set.seed(50)
grps <- sample(c("child", "adult", "adlut"), 100, replace = TRUE, prob = c(0.45, 0.45, 0.05))
i <- incidence(dates = sample(10, 100, replace = TRUE),
interval = 1L,
groups = grps)
colSums(get_counts(i))
# If you change the name of the mis-spelled group, it will be merged with the
# correctly-spelled group
gname <- group_names(i)
gname[gname == "adlut"] <- "adult"
# without side-effects
print(ii <- group_names(i, gname))
colSums(get_counts(i)) # original still has three groups
colSums(get_counts(ii))
# with side-effects
group_names(i) <- gname
colSums(get_counts(i))
}
\keyword{accessors}
incidence/man/get_counts.Rd 0000644 0001762 0000144 00000002274 13750256226 015424 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_counts.R
\name{get_counts}
\alias{get_counts}
\alias{get_counts.incidence}
\title{Get counts from an incidence object}
\usage{
get_counts(x, groups = NULL)
\method{get_counts}{incidence}(x, groups = NULL)
}
\arguments{
\item{x}{an \code{incidence} object.}
\item{groups}{if there are groups, use this to specify a group or groups to
subset. Defaults to \code{NULL} indicating that all groups are returned.}
}
\value{
a matrix of counts where each row represents a date bin
}
\description{
Get counts from an incidence object
}
\examples{
if (require(outbreaks)) { withAutoprint({
dat <- ebola_sim$linelist$date_of_onset
gend <- ebola_sim$linelist$gender
i <- incidence(dat, interval = "week", groups = gend)
## Use with an object and no arguments gives the counts matrix
head(get_counts(i))
## Specifying a position or group name will return a matrix subset to that
## group
head(get_counts(i, 1L))
head(get_counts(i, "f"))
## Specifying multiple groups allows you to rearrange columns
head(get_counts(i, c("m", "f")))
## If you want a vector, you can use drop
drop(get_counts(i, "f"))
})}
}
incidence/man/incidence.Rd 0000644 0001762 0000144 00000024567 13750256226 015204 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/incidence.R, R/print.R
\name{incidence}
\alias{incidence}
\alias{incidence.default}
\alias{incidence.Date}
\alias{incidence.character}
\alias{incidence.integer}
\alias{incidence.numeric}
\alias{incidence.POSIXt}
\alias{print.incidence}
\title{Compute incidence of events from a vector of dates.}
\usage{
incidence(dates, interval = 1L, ...)
\method{incidence}{default}(dates, interval = 1L, ...)
\method{incidence}{Date}(
dates,
interval = 1L,
standard = TRUE,
groups = NULL,
na_as_group = TRUE,
first_date = NULL,
last_date = NULL,
...
)
\method{incidence}{character}(
dates,
interval = 1L,
standard = TRUE,
groups = NULL,
na_as_group = TRUE,
first_date = NULL,
last_date = NULL,
...
)
\method{incidence}{integer}(
dates,
interval = 1L,
groups = NULL,
na_as_group = TRUE,
first_date = NULL,
last_date = NULL,
...
)
\method{incidence}{numeric}(
dates,
interval = 1L,
groups = NULL,
na_as_group = TRUE,
first_date = NULL,
last_date = NULL,
...
)
\method{incidence}{POSIXt}(
dates,
interval = 1L,
standard = TRUE,
groups = NULL,
na_as_group = TRUE,
first_date = NULL,
last_date = NULL,
...
)
\method{print}{incidence}(x, ...)
}
\arguments{
\item{dates}{A vector of dates, which can be provided as objects of the
class: integer, numeric, Date, POSIXct, POSIXlt, and character. (See Note
about \code{numeric} and \code{character} formats)}
\item{interval}{An integer or character indicating the (fixed) size of the
time interval used for computing the incidence; defaults to 1 day. This can
also be a text string that corresponds to a valid date interval: day, week,
month, quarter, or year. (See Note).}
\item{...}{Additional arguments passed to other methods (none are used).}
\item{standard}{(Only applicable to Date objects) When \code{TRUE} (default) and the
\code{interval} one of "week", "month", "quarter", or "year", then this will
cause the bins for the counts to start at the beginning of the interval
(See Note).}
\item{groups}{An optional factor defining groups of observations for which
incidence should be computed separately.}
\item{na_as_group}{A logical value indicating if missing group (NA) should be
treated as a separate group.}
\item{first_date, last_date}{optional first/last dates to be used in the
epicurve. When these are \code{NULL} (default), the dates from the first/last
dates are taken from the observations. If these dates are provided, the
observations will be trimmed to the range of [first_date, last_date].}
\item{x}{An 'incidence' object.}
}
\value{
An list with the class \code{incidence}, which contains the
following items:
\itemize{
\item \strong{dates}: The dates marking the left side of the bins used for counting
events. When \code{standard = TRUE} and the interval represents weeks, months,
quarters, or years, the first date will represent the first standard date
(See Interval specification, below).
\item \strong{counts}: A matrix of incidence counts, which one column per group (and
a single column if no groups were used).
\item \strong{timespan}: The length of the period for which incidence is computed, in
days.
\item \strong{interval}: The bin size. If it's an integer, it represents the number
of days between each bin. It can also be a character, e.g. "2 weeks" or
"6 months".
\item \strong{n}: The total number of cases.
\item \strong{weeks}: Dates in week format (YYYY-Www), where YYYY corresponds to the
year of the given week and ww represents the numeric week of the year.
This will be a produced from the function \code{\link[aweek:date2week]{aweek::date2week()}}. Note that
these will have a special \code{"week_start"} attribute indicating which day of
the ISO week the week starts on (see Weeks, below).
\item \strong{isoweeks}: ISO 8601 week format YYYY-Www, which is returned only when
ISO week-based weekly incidence is computed.
}
}
\description{
This function computes incidence based on dates of events provided in
various formats. A fixed interval, provided as numbers of days, is used to
define time intervals. Counts within an interval always include the first
date, after which they are labeled, and exclude the second. For instance,
intervals labeled as 0, 3, 6, ... mean that the first bin includes days 0, 1
and 2, the second interval includes 3, 4 and 5 etc.
}
\details{
For details about the \verb{incidence class}, see the dedicated
vignette:\cr \code{vignette("incidence_class", package = "incidence")}
}
\note{
\subsection{Input data (\code{dates})}{
\itemize{
\item \strong{Decimal (numeric) dates}: will be truncated with a warning
\item \strong{Character dates} should be in the unambiguous \code{yyyy-mm-dd} (ISO 8601)
format. Any other format will trigger an error.
}
}
\subsection{Interval specification (\code{interval})}{
If \code{interval} is a valid character (e.g. "week" or "1 month"), then
the bin will start at the beginning of the interval just before the first
observation by default. For example, if the first case was recorded on
Wednesday, 2018-05-09:
\itemize{
\item "week" : first day of the week (i.e. Monday, 2018-05-07) (defaults to ISO weeks, see "Week intervals", below)
\item "month" : first day of the month (i.e. 2018-05-01)
\item "quarter" : first day of the quarter (i.e. 2018-04-01)
\item "year" : first day of the calendar year (i.e. 2018-01-01)
}
These default intervals can be overridden with \code{standard = FALSE}, which
sets the interval to begin at the first observed case.
}
\subsection{Week intervals}{
As of \emph{incidence} version 1.7.0, it is possible to construct standardized
incidence objects standardized to any day of the week thanks to the
\code{\link[aweek:date2week]{aweek::date2week()}} function from the \pkg{aweek} package. The default
state is to use ISO 8601 definition of weeks, which start on Monday. You can
specify the day of the week an incidence object should be standardised to by
using the pattern "{n} {W} weeks" where "{W}" represents the weekday in an
English or current locale and "{n}" represents the duration, but this can be
ommitted. Below are examples of specifying weeks starting on different days
assuming we had data that started on 2016-09-05, which is ISO week 36 of
2016:
\itemize{
\item interval = "2 monday weeks" (Monday 2016-09-05)
\item interval = "1 tue week" (Tuesday 2016-08-30)
\item interval = "1 Wed week" (Wednesday 2016-08-31)
\item interval = "1 Thursday week" (Thursday 2016-09-01)
\item interval = "1 F week" (Friday 2016-09-02)
\item interval = "1 Saturday week" (Saturday 2016-09-03)
\item interval = "Sunday week" (Sunday 2016-09-04)
}
It's also possible to use something like "3 weeks: Saturday"; In addition,
there are keywords reserved for specific days of the week:
\itemize{
\item interval = "week", standard = TRUE (Default, Monday)
\item interval = "ISOweek" (Monday)
\item interval = "EPIweek" (Sunday)
\item interval = "MMWRweek" (Sunday)
}
The "EPIweek" specification is not strictly reserved for CDC epiweeks, but
can be prefixed (or posfixed) by a day of the week: "1 epiweek: Saturday".
}
\subsection{The \code{first_date} argument}{
Previous versions of \emph{incidence} had the \code{first_date} argument override
\code{standard = TRUE}. It has been changed as of \emph{incidence} version 1.6.0 to
be more consistent with the behavior when \code{first_date = NULL}. This, however
may be a change in behaviour, so a warning is now issued once and only once
if \code{first_date} is specified, but \code{standard} is not. To never see this
warning, use \code{options(incidence.warn.first_date = FALSE)}.
}
The intervals for "month", "quarter", and "year" will necessarily vary in the
number of days they encompass and warnings will be generated when the first
date falls outside of a calendar date that is easily represented across the
interval.
}
\examples{
## toy example
incidence(c(1, 5, 8, 3, 7, 2, 4, 6, 9, 2))
incidence(c(1, 5, 8, 3, 7, 2, 4, 6, 9, 2), 2)
## example using simulated dataset
if(require(outbreaks)) { withAutoprint({
onset <- outbreaks::ebola_sim$linelist$date_of_onset
## daily incidence
inc <- incidence(onset)
inc
plot(inc)
## weekly incidence
inc.week <- incidence(onset, interval = 7, standard = FALSE)
inc.week
plot(inc.week)
plot(inc.week, border = "white") # with visible border
# Starting on Monday
inc.isoweek <- incidence(onset, interval = "isoweek")
inc.isoweek
# Starting on Sunday
inc.epiweek <- incidence(onset, interval = "epiweek")
inc.epiweek
# Starting on Saturday
inc.epiweek <- incidence(onset, interval = "saturday epiweek")
inc.epiweek
## use group information
sex <- outbreaks::ebola_sim$linelist$gender
inc.week.gender <- incidence(onset, interval = 7,
groups = sex, standard = FALSE)
inc.week.gender
head(inc.week.gender$counts)
plot(inc.week.gender, border = "grey90")
inc.satweek.gender <- incidence(onset, interval = "2 epiweeks: saturday",
groups = sex)
inc.satweek.gender
plot(inc.satweek.gender, border = "grey90")
})}
# Use of first_date
d <- Sys.Date() + sample(-3:10, 10, replace = TRUE)
# `standard` specified, no warning
di <- incidence(d, interval = "week", first_date = Sys.Date() - 10, standard = TRUE)
# warning issued if `standard` not specified
di <- incidence(d, interval = "week", first_date = Sys.Date() - 10)
# second instance: no warning issued
di <- incidence(d, interval = "week", first_date = Sys.Date() - 10)
}
\seealso{
The main other functions of the package include:
\itemize{
\item \code{\link[=plot.incidence]{plot.incidence()}}: Plot epicurves from an incidence object.
\item \code{\link[=fit]{fit()}}: Fit log-linear model to computed incidence.
\item \code{\link[=fit_optim_split]{fit_optim_split()}}: Find the optimal peak of the epidemic
and fits log-linear models on either side of the peak.
\item \code{\link[=subset]{subset()}}: Handling of \code{incidence}
objects.
\item \code{\link[=pool]{pool()}}: Sum incidence over groups.
\item \code{\link[=as.data.frame.incidence]{as.data.frame.incidence()}}: Convert an \code{incidence} object to a
\code{data.frame}.
}
The following vignettes are also available:
\itemize{
\item \code{overview}: Provides an overview of the package's features.
\item \code{customize_plot}: Provides some tips on finer plot customization.
\item \code{incidence_class}: Details the content of the \code{incidence}
class.
}
}
\author{
Thibaut Jombart, Rich Fitzjohn, Zhian Kamvar
}
incidence/man/estimate_peak.Rd 0000644 0001762 0000144 00000004763 13750256226 016072 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/estimate_peak.R
\name{estimate_peak}
\alias{estimate_peak}
\title{Estimate the peak date of an incidence curve using bootstrap}
\usage{
estimate_peak(x, n = 100, alpha = 0.05)
}
\arguments{
\item{x}{An \code{incidence} object.}
\item{n}{The number of bootstrap datasets to be generated; defaults to 100.}
\item{alpha}{The type 1 error chosen for the confidence interval; defaults to
0.05.}
}
\value{
A list containing the following items:
\itemize{
\item \code{observed}: the peak incidence of the original dataset
\item \code{estimated}: the mean peak time of the bootstrap datasets
\item \code{ci}: the confidence interval based on bootstrap datasets
\item \code{peaks}: the peak times of the bootstrap datasets
}
}
\description{
This function can be used to estimate the peak of an epidemic curve stored as
\code{incidence}, using bootstrap. See \link{bootstrap} for more information
on the resampling.
}
\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:
\itemize{
\item the total number of event is known (no uncertainty on total incidence)
\item dates with no events (zero incidence) will never be in bootstrapped datasets
\item the reporting is assumed to be constant over time, i.e. every case is
equally likely to be reported
}
}
\examples{
if (require(outbreaks) && require(ggplot2)) { withAutoprint({
i <- incidence(fluH7N9_china_2013$date_of_onset)
i
plot(i)
## one simple bootstrap
x <- bootstrap(i)
x
plot(x)
## find 95\% CI for peak time using bootstrap
peak_data <- estimate_peak(i)
peak_data
summary(peak_data$peaks)
## show confidence interval
plot(i) + geom_vline(xintercept = peak_data$ci, col = "red", lty = 2)
## show the distribution of bootstrapped peaks
df <- data.frame(peak = peak_data$peaks)
plot(i) + geom_density(data = df,
aes(x = peak, y = 10 * ..scaled..),
alpha = .2, fill = "red", color = "red")
})}
}
\seealso{
\link{bootstrap} for the bootstrapping underlying this
approach and \link{find_peak} to find the peak in a single
\code{incidence} object.
}
\author{
Thibaut Jombart \email{thibautjombart@gmail.com}, with inputs on
caveats from Michael Höhle.
}
incidence/man/conversions.Rd 0000644 0001762 0000144 00000005214 13750256226 015617 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/conversion.R
\name{as.data.frame.incidence}
\alias{as.data.frame.incidence}
\alias{as.incidence}
\alias{as.incidence.matrix}
\alias{as.incidence.data.frame}
\alias{as.incidence.numeric}
\title{Conversion of incidence objects}
\usage{
\method{as.data.frame}{incidence}(x, ..., long = FALSE)
as.incidence(x, ...)
\method{as.incidence}{matrix}(
x,
dates = NULL,
interval = NULL,
standard = TRUE,
isoweeks = standard,
...
)
\method{as.incidence}{data.frame}(x, dates = NULL, interval = NULL, isoweeks = TRUE, ...)
\method{as.incidence}{numeric}(x, dates = NULL, interval = NULL, isoweeks = TRUE, ...)
}
\arguments{
\item{x}{An \code{incidence} object, or an object to be converted as
\code{incidence} (see details).}
\item{...}{Further arguments passed to other functions (no used).}
\item{long}{A logical indicating if the output data.frame should be 'long', i.e. where a single
column containing 'groups' is added in case of data computed on several groups.}
\item{dates}{A vector of dates, each corresponding to the (inclusive) lower
limit of the bins.}
\item{interval}{An integer indicating the time interval used in the
computation of the incidence. If NULL, it will be determined from the first
time interval between provided dates. If only one date is provided, it will
trigger an error.}
\item{standard}{A logical indicating whether standardised dates should be
used. Defaults to \code{TRUE}.}
\item{isoweeks}{Deprecated. Use standard.}
}
\description{
These functions convert \code{incidence} objects into other classes.
}
\details{
Conversion to \code{incidence} objects should only be done when the
original dates are not available. In such case, the argument \code{x}
should be a matrix corresponding to the \verb{$counts} element of an
\code{incidence} object, i.e. giving counts with time intervals in rows and
named groups in columns. In the absence of groups, a single unnamed columns
should be given. \code{data.frame} and vectors will be coerced to a matrix.
}
\examples{
## create fake data
data <- c(0,1,1,2,1,3,4,5,5,5,5,4,4,26,6,7,9)
sex <- sample(c("m","f"), length(data), replace=TRUE)
## get incidence per group (sex)
i <- incidence(data, groups = sex)
i
plot(i)
## convert to data.frame
as.data.frame(i)
## same, 'long format'
as.data.frame(i, long = TRUE)
## conversion from a matrix of counts to an incidence object
i$counts
new_i <- as.incidence(i$counts, i$dates)
new_i
all.equal(i, new_i)
}
\seealso{
the \code{\link[=incidence]{incidence()}} function to generate the 'incidence' objects.
}
\author{
Thibaut Jombart \email{thibautjombart@gmail.com}, Rich Fitzjohn
}
incidence/DESCRIPTION 0000644 0001762 0000144 00000004437 13750553417 013723 0 ustar ligges users Package: incidence
Type: Package
Title: Compute, Handle, Plot and Model Incidence of Dated Events
Version: 1.7.3
Authors@R: c(
person("Thibaut", "Jombart", role = c("aut"), email = "thibautjombart@gmail.com"),
person("Zhian N.", "Kamvar", role = "aut", email = "zkamvar@gmail.com",
comment = c(ORCID = "0000-0003-1458-7108")),
person("Rich", "FitzJohn", role = "aut", email = "rich.fitzjohn@gmail.com"),
person("Tim", "Taylor", role = "cre", email = "tim.taylor@hiddenelephants.co.uk",
comment = c(ORCID = "0000-0002-8587-7113")),
person("Jun", "Cai", role = "ctb", email = "cai-j12@mails.tsinghua.edu.cn",
comment = c(ORCID = "0000-0001-9495-1226")),
person("Sangeeta", "Bhatia", role = "ctb", email = "sangeetabhatia03@gmail.com"),
person("Jakob", "Schumacher", role = "ctb"),
person("Juliet R.C.", "Pulliam", role = "ctb", email = "pulliam@sun.ac.za",
comment = c(ORCID = "0000-0003-3314-8223"))
)
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 'incidence' is used to store computed
incidence and can be easily manipulated, subsetted, and plotted. In addition,
log-linear models can be fitted to 'incidence' objects using 'fit'. This
package is part of the RECON () toolkit
for outbreak analysis.
Encoding: UTF-8
License: MIT + file LICENSE
URL: https://www.repidemicsconsortium.org/incidence/
BugReports: https://github.com/reconhub/incidence/issues
RoxygenNote: 7.1.1
Imports: ggplot2 (>= 3.3.2), aweek (>= 0.2.0)
Suggests: magrittr, outbreaks, testthat, vdiffr, covr, knitr,
rmarkdown, scales, cowplot
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2020-11-03 17:29:35 UTC; tim
Author: Thibaut Jombart [aut],
Zhian N. Kamvar [aut] (),
Rich FitzJohn [aut],
Tim Taylor [cre] (),
Jun Cai [ctb] (),
Sangeeta Bhatia [ctb],
Jakob Schumacher [ctb],
Juliet R.C. Pulliam [ctb] ()
Maintainer: Tim Taylor
Repository: CRAN
Date/Publication: 2020-11-04 16:30:07 UTC
incidence/build/ 0000755 0001762 0000144 00000000000 13750311577 013303 5 ustar ligges users incidence/build/vignette.rds 0000644 0001762 0000144 00000000501 13750311577 015636 0 ustar ligges users SN0MM}?eBʝk-uDoZ(lsZι'8#.q\\3#SC8O{%\ik.H'lD(1NY&ݵԖ#RОAQa]ֆ-HQc034Fu{SJ^uӯO]
T`KDc_uA'{IJ6A0s`h!Ri[̆]/Wz90ϖ>G!r̻B\?,Q;2G*
9)B incidence/tests/ 0000755 0001762 0000144 00000000000 13750311577 013346 5 ustar ligges users incidence/tests/testthat/ 0000755 0001762 0000144 00000000000 13750553417 015207 5 ustar ligges users incidence/tests/testthat/test-non-exported.R 0000644 0001762 0000144 00000004003 13750256226 020724 0 ustar ligges users context("Non-exported functions")
test_that("check_dates works", {
msg <- "NA detected in the dates"
expect_error(check_dates(c(1,2,NA), TRUE), msg)
msg <- paste0("Flooring from non-integer date caused approximations:\n",
"Mean relative difference: 0.1")
expect_warning(check_dates(1.1), msg)
msg <- paste0("Input could not be converted to date. Accepted formats are:\n",
"Date, POSIXct, integer, numeric")
expect_error(check_dates(factor("2001-01-01")), msg)
x <- list(1L,
as.POSIXct("2001-01-01"),
as.Date("2001-01-01") + 1:10,
1.0,
100:1)
for (e in x) {
expect_equal(e, check_dates(e))
}
})
test_that("check_interval", {
skip_on_cran()
expect_error(check_interval(),
"Interval is missing or NULL")
expect_error(check_interval(NULL),
"Interval is missing or NULL")
expect_error(check_interval(1:2),
"Exactly one value should be provided as interval \\(2 provided\\)")
expect_error(check_interval(integer(0)),
"Exactly one value should be provided as interval \\(0 provided\\)")
expect_error(check_interval(NA),
"Interval is not finite")
expect_error(check_interval(-Inf),
"Interval is not finite")
expect_error(check_interval(.1),
"Interval must be at least 1 \\(input: 0.100; after rounding: 0\\)")
expect_equal(check_interval(1), 1)
expect_equal(check_interval(2.4), 2)
expect_equal(check_interval(2.7), 3)
})
test_that("check_groups", {
skip_on_cran()
expect_is(check_groups(1, 1, FALSE), "factor")
expect_error(check_groups(1, 1:2, FALSE),
"'x' does not have the same length as dates \\(1 vs 2\\)")
expect_equal(check_groups(NULL, NULL, FALSE), NULL)
expect_equal(check_groups(c(1, 1, 2, NA, 2), 1:5, na_as_group = FALSE),
factor(c(1, 1, 2, NA, 2)))
expect_equal(check_groups(c(1,1,2,NA,2), 1:5, na_as_group = TRUE),
factor(c(1, 1, 2, "NA", 2)))
})
incidence/tests/testthat/test-cumulate.R 0000644 0001762 0000144 00000000631 13750256226 020124 0 ustar ligges users context("Test cumulate")
test_that("Results as expected", {
skip_on_cran()
dat <- as.Date("2016-01-02") + as.integer(c(7,7,7,0,1,2,2,2,3,3,5,7))
i <- incidence(dat)
out <- cumulate(i)
expect_identical(cumsum(i$counts), as.vector(out$counts))
expect_true(out$cumulative)
expect_identical(cumulate(-3:10), cumsum(-3:10))
expect_error(cumulate(out), "x is already a cumulative incidence")
})
incidence/tests/testthat/test-bootstrap.R 0000644 0001762 0000144 00000004315 13750256226 020325 0 ustar ligges users context("Bootstrapping incidence")
set.seed(1)
dates <- as.integer(sample(-3:100, 50, replace = TRUE))
DATES <- as.Date("2016-09-20") + dates
groups <- sample(c("toto", "tata"), 50, replace = TRUE)
test_that("Bootstrap needs an incidence object", {
expect_error(bootstrap(DATES), "x is not an incidence object")
})
test_that("estimate_peak needs an incidence object", {
expect_error(estimate_peak(DATES), "x is not an incidence object")
})
test_that("Bootstrap incidence with groups", {
skip_on_cran()
x <- incidence(DATES, 3, groups = groups)
y <- bootstrap(x)
z <- bootstrap(x, TRUE)
expect_identical(colSums(x$counts), colSums(y$counts))
expect_identical(colSums(x$counts), colSums(z$counts))
expect_identical(colnames(x$counts), colnames(y$counts))
expect_identical(colnames(x$counts), colnames(z$counts))
expect_identical(x$interval, y$interval)
expect_identical(x$interval, z$interval)
})
context("Mountain Climbing")
test_that("find_peak can find the peak", {
skip_on_cran()
x <- incidence(DATES, 3, groups = groups)
expect_error(find_peak(1:10), "x is not an incidence object")
expect_message(p1 <- find_peak(x), "'x' is stratified by groups\npooling groups before finding peaks")
expect_length(p1, 1L)
expect_named(find_peak(x, pool = FALSE), c("tata", "toto"))
})
test_that("estimate_peak can roughly estimate it", {
x <- incidence(DATES, 3, groups = groups)
y <- incidence(dates, 3)
expect_message(e1 <- estimate_peak(x), "'x' is stratified by groups\npooling groups before finding peaks")
e2 <- estimate_peak(y)
expect_named(e1, c("observed", "estimated", "ci", "peaks"))
expect_named(e2, c("observed", "estimated", "ci", "peaks"))
## The observed is identical to find_peak
expect_identical(e1$observed, find_peak(pool(x)))
expect_identical(e2$observed, find_peak(pool(y)))
## The number of peaks defaults to 100
expect_length(e1$peaks, 100)
expect_length(e2$peaks, 100)
## The observed falls within the confidence interval
expect_gt(as.integer(e1$observed), as.integer(e1$ci[1]))
expect_lt(as.integer(e1$observed), as.integer(e1$ci[2]))
expect_gt(as.integer(e2$observed), as.integer(e2$ci[1]))
expect_lt(as.integer(e2$observed), as.integer(e2$ci[2]))
})
incidence/tests/testthat/rds/ 0000755 0001762 0000144 00000000000 13750256226 015775 5 ustar ligges users incidence/tests/testthat/rds/fit.i.rds 0000644 0001762 0000144 00000005377 13750256226 017534 0 ustar ligges users Z\eYdAL¦>#A>R3]wgarwf^1Ŭ(/R,*6(^5{Vfj>zj{gwvū,9g|}9bF`292N< 㺺{.,g}"8uܪPvNN_TƏ:(w$K
A58
Nb2=D܅}=ޅ짚/O?`Cj$g9u-Z`t{hZ?uK'b!KjVdl:FW6iT ٦Ț%;o~7bKu*S#Z$$"! HHAB*Ґ݂DAJdAJTAJtA0 CoJ`>'=\LmKEbQd#H!2Q~.D; eϘH%e%⑯o&O>tiuɣc[keϞ4EZYS6:eirU!vC_.|&supJ>4kv_R1!
`Hi/!&u}oXh@N:
wOy5Gĸ@̈*R\?z!' s XBxAQ1&Bkt:J,I6o!\R
w{g`f)mCϑ>:KIW5gح#ɷy:Kv3m`רgT֩"ek6-uu۔OHgTz.|!={XTg)'b?4'ݱӧv?j~L OZh .y*!.Q=Dؾs<<'1BW)RH=UGM.zw:vKk^-ouw%*N6HJ?^.❭dw}:A08*P&m)nEiSoL
B?x9D;QS7;)4 g1hҠT!%)Ƞ7yYna3<j/E7۪ +VI&jRbnGK*RAǺ|^3,O;6.G-j8VĞ)E
.6./YA5&Z+۲ nMM'&
ZѨ ~^^
ò&PSӶ
Oq~}PoL+/';H;S4hAȖ9%}34+s+@"W]֍_NMlkrח U]gk$h4QG()5.gf %!{{/4nAW*r@ȇO˦(A?xܴ29k&$^Vz)5@Z
fH9ynLaCf4[JI3Ǜ@ujhRm M\[Vt1cgd^]L&n*O5="j m;Ψ@/g|7W.ȸL# "Rd|ە$@ψ?E@q4P;;~5duIm]#O4jB629fښ ~iG&