How to get an incidence object from days to weeks?

Published

March 26, 2024

Ingredients

  • Get weekly case counts from daily collected data.
  • Use the linelist object in the ebola_sim_clean list from the {outbreaks} R package. linelist is an outbreak data set.
  • Use the date_of _onset column of linelist to calculate the case counts.

Steps in code

# Get the weekly growth rate of an outbreak

# Load required packages
library(outbreaks)
library(incidence2)
library(dplyr)

# Load the simulated Ebola outbreak data
data(ebola_sim_clean)

# Extract the first element of the list
linelist <- ebola_sim_clean$linelist

# Read data collected per day
as_tibble(linelist)
# A tibble: 5,829 × 11
   case_id generation date_of_infection date_of_onset date_of_hospitalisation
   <chr>        <int> <date>            <date>        <date>                 
 1 d1fafd           0 NA                2014-04-07    2014-04-17             
 2 53371b           1 2014-04-09        2014-04-15    2014-04-20             
 3 f5c3d8           1 2014-04-18        2014-04-21    2014-04-25             
 4 6c286a           2 NA                2014-04-27    2014-04-27             
 5 0f58c4           2 2014-04-22        2014-04-26    2014-04-29             
 6 49731d           0 2014-03-19        2014-04-25    2014-05-02             
 7 f9149b           3 NA                2014-05-03    2014-05-04             
 8 881bd4           3 2014-04-26        2014-05-01    2014-05-05             
 9 e66fa4           2 NA                2014-04-21    2014-05-06             
10 20b688           3 NA                2014-05-05    2014-05-06             
# ℹ 5,819 more rows
# ℹ 6 more variables: date_of_outcome <date>, outcome <fct>, gender <fct>,
#   hospital <fct>, lon <dbl>, lat <dbl>
# Get an incidence2 object with case counts per day
# from data collected per day
incidence2_day <- 
  incidence2::incidence(
    x = linelist, 
    date_index = "date_of_onset",
    interval = "day"
  )

# Read case counts per day
incidence2_day
# incidence:  367 x 3
# count vars: date_of_onset
   date_index count_variable count
 * <date>     <chr>          <int>
 1 2014-04-07 date_of_onset      1
 2 2014-04-15 date_of_onset      1
 3 2014-04-21 date_of_onset      2
 4 2014-04-25 date_of_onset      1
 5 2014-04-26 date_of_onset      1
 6 2014-04-27 date_of_onset      1
 7 2014-05-01 date_of_onset      2
 8 2014-05-03 date_of_onset      1
 9 2014-05-04 date_of_onset      1
10 2014-05-05 date_of_onset      1
# ℹ 357 more rows
# Plot incidence2 object with case counts per day
plot(incidence2_day)

# Get an incidence2 object with case counts per week
# from data collected per day
incidence2_week <- 
  incidence2::incidence(
    x = linelist, 
    date_index = "date_of_onset",
    interval = "week"
  )

# Read case counts per week
incidence2_week
# incidence:  56 x 3
# count vars: date_of_onset
   date_index count_variable count
 * <isowk>    <chr>          <int>
 1 2014-W15   date_of_onset      1
 2 2014-W16   date_of_onset      1
 3 2014-W17   date_of_onset      5
 4 2014-W18   date_of_onset      4
 5 2014-W19   date_of_onset     12
 6 2014-W20   date_of_onset     17
 7 2014-W21   date_of_onset     15
 8 2014-W22   date_of_onset     19
 9 2014-W23   date_of_onset     23
10 2014-W24   date_of_onset     21
# ℹ 46 more rows
# Plot incidence2 object with case counts per week
plot(incidence2_week)

Steps in detail

  • The outbreaks package is loaded to access the simulated Ebola outbreak data.
  • The ebola_sim_clean object from the package contains the simulated outbreak data.
  • The linelist object contains the first list element from ebola_sim_clean.
  • The incidence() function from the incidence2 package converts the data object linelist to an incidence2 class object.
  • The date_of_onset column is used in the date_index argument as the onset dates of the outbreak.
  • The "day" text string is used in the interval argument to count number of cases per day interval.
  • The "week" text string is used in the interval argument to count number of cases per week interval.
  • The incidence2 object provides observations arranged in descendant order with respect to the date_index.

Please note that the code assumes the necessary packages are already installed. If they are not, you can install them using first the install.packages("pak") function and then the pak::pak() function for both packages in CRAN or GitHub before loading them with library().

Additionally, make sure to adjust the serial interval distribution parameters according to the specific outbreak you are analyzing.