knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width=7, fig.height=5 )
This vignette documents to types of conversion which can be made using the incidence class:
'exports': conversion from an incidence object to another type of object; this can be useful for processing incidence data in another software, or for reporting results.
'imports'conversion from already computed incidence into an incidence object; this can be useful for using features of the incidence package for data handling and plotting with incidence data computed elsewhere.
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:
library(outbreaks) library(incidence) dat <- ebola_sim$linelist$date_of_onset i_14 <- incidence(dat, interval = "2 epiweeks", groups = ebola_sim$linelist$gender) i_14 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 <- as.data.frame(i_14, long = TRUE) head(df) tail(df) ## example of custom plot using steps: library(ggplot2) 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") i_7 plot(i_7, border = "white") head(as.data.frame(i_7)) tail(as.data.frame(i_7))
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
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
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) i 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) i 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: i_14 df <- as.data.frame(i_14) head(df) tail(df) ## conversion: data.frame --> incidence new_i <- as.incidence(df[group_names(i_14)], df$dates, interval = "2 epiweeks") new_i ## check round trip identical(new_i, i_14)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.