ll2incidence: Computing Incidence From Line Listing

Description Usage Arguments Details Value Author(s) Examples

View source: R/ll2incidence.R

Description

ll2incidence takes individual dates of infections and returns population incidences by a specified time step.

Usage

1
ll2incidence(x, unit = c("day", "week", "month", "quarter", "year"))

Arguments

x

either a vector or a 1-column data frame of dates of "Date" or "POSIXct" class.

unit

a character string (one of "day", "week", "month", "quarter" or "year") specifying the temporal aggregation wished for the incidence calculation. Value set to "day" by default.

Details

The date variable of the output is defined with a regular time step as defined by the unit argument. In absence of cases between two steps, a zero incidence is explicitly displayed.

Value

ll2incidence returns a data frame of 4 variables: start, middle, end and incidence. The first 3 variables are respectively the dates of the beginning, the middle and the end of the time period over which the incidence is defined and are of class POSIXct with time zone UTC. The HH:MM:SS of the start and end variables are 00:00:00.

Author(s)

Marc Choisy

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Four different data sets of infections dates:
infection_date_v <- infections_dates
infection_posixct_v <- as.POSIXct(infection_date_v)
infection_date_df <- as.data.frame(infection_date_v)
infection_posixct_df <- as.data.frame(infection_posixct_v)
data_sets <- list(infection_date_v,      # vector of Date class
                  infection_date_df,     # data frame of Date class
                  infection_posixct_v,   # vector of POSIXct class
                  infection_posixct_df)  # data frame of POSIXct class
lapply(data_sets, head)

# Five time resolutions we want to consider for incidence calculations:
steps <- c("day", "week", "month", "quarter", "year")

# Calculating of the 4 x 5 = 20 incidence data sets:
incidences <- lapply(steps,
                     function(y) lapply(data_sets, function(x)
                      ll2incidence(x, y)))

# Comparing the results:
n <- length(incidences[[1]])
any(!unlist(lapply(1:n,
                   function(x) sapply(1:(n - 1),
                     function(y) sapply((y + 1):n, function(z)
                      identical(incidences[[c(x, y)]],
                       incidences[[c(x, z)]]))))))

# Showing the results:
for(i in 1:5) print(head(incidences[[c(i, 1)]]))

# Checking that the number of cases is the same whatever the data type:
any(!as.vector(sapply(1:4,
        function(x) sapply(1:5,
          function(y) sum(incidences[[c(y, x)]]$incidence)))) ==
           length(infections_dates))

choisy/poseid documentation built on Aug. 22, 2019, 4:45 a.m.