Conversions to and from the incidence class

  collapse = TRUE,
  comment = "#>", 

This vignette documents to types of conversion which can be made using the incidence class:

Exporting results

To export results, we first compute semi-weekly incidence (with weeks starting on Sunday, the beginning of the CDC epiweek) by gender from the simulated Ebola data used in the overview vignette:

dat <- ebola_sim$linelist$date_of_onset
i_14 <- incidence(dat, interval = "2 epiweeks", groups = ebola_sim$linelist$gender)
plot(i_14, border = "white")

To export the data to a data.frame, one simply needs:

The first column contains the dates marking the (inclusive) left side of the time intervals used for computing incidence, and the other columns give counts for the different groups. This function also has an option for exporting data as a 'long' format, i.e. with a column for 'groups' and a column for counts. This format can be useful especially when working with ggplot2, which expect data in this shape:

df <-, long = TRUE)

## example of custom plot using steps:
ggplot(df, aes(x = dates, y = counts)) + geom_step(aes(color = groups))

Finally, note that when ISO weeks are used, these are also reported in the output:

i_7 <- incidence(dat, interval = "week")
plot(i_7, border = "white")

Importing pre-computed incidence

The function as.incidence facilitates the conversion of pre-computed incidences to an incidence object. Typically, the input will be imported into R from a .csv file or other spreadsheet formats.

as.incidence is a generic with methods for several types of objects (see ?as.incidence). The main method is matrix, as other types are coerced to matrix first and then passed to as.incidence.matrix:


The only mandatory argument x is a table of counts, with time intervals in rows and groups in columns; if there are no groups, then the column doesn't need a name; but if there are several groups, then columns should be named to indicate group labels. Optionally, dates can be provided to indicate the (inclusive) lower bounds of the time intervals, corresponding to the rows of x; most sensible date formats will do; if indicated as a character string, make sure the format is YYYY-mm-dd, e.g. 2017-04-01 for the 1st April 2017.

Let us illustrate the conversion using a simple vector of incidence:

vec <- c(1,2,3,0,3,2,4,1,2,1)

i <- as.incidence(vec)

plot(vec, type = "s")
plot(i, border = "white")

Assuming the above incidences are computed weekly, we would then use:

i <- as.incidence(vec, interval = 7)
plot(i, border = "white")

Note that in this case, incidences have been treated as per week, and corresponding dates in days have been computed during the conversion (the first day is always '1'), so that the first days of weeks 1, 2, 3... are:


In practice, it is best to provide the actual dates marking the lower bounds of the time intervals. We can illustrate this by a round trip using the example of the previous section:

## convertion: incidence --> data.frame:
df <-

## conversion: data.frame --> incidence
new_i <- as.incidence(df[group_names(i_14)], df$dates, interval = "2 epiweeks")

## check round trip
identical(new_i, i_14)

Try the incidence package in your browser

Any scripts or data that you put into this service are public.

incidence documentation built on Nov. 8, 2020, 4:30 p.m.