Alternative date groupings

The incidence() function wraps the date grouping functionality of the grates package, providing an easy to use interface for constructing incidence objects. Sometimes, however, you may want greater flexibility in choosing how you would like to transform your “date” inputs. For this reason we also the provide the function build_incidence(), where you can specify the function you wish to apply. We illustrate this approach using both the clock and zoo packages.

library(incidence2)
library(clock)
library(zoo)
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
# data included in incidence2 but obtained via the covidregionaldata package
data("covidregionaldataUK")
dat <- covidregionaldataUK

Month grouping based on clock functions

clock_month_inci <- 
  build_incidence(
    dat,
    date_index = date,
    groups = region,
    counts = ends_with("new"),
    FUN = function(x) calendar_narrow(as_year_month_day(x), precision = "month")
  )

clock_month_inci
#> An incidence object: 234 x 7
#> date range: [2020-01] to [2021-06]
#> cases_new: 8379330
#> deaths_new: 239477
#> recovered_new: 0
#> hosp_new: 466704
#> tested_new: 162328695
#> 
#>    date_index   region    cases_new deaths_new recovered_new hosp_new tested_new
#>    <ymd<month>> <chr>         <dbl>      <dbl>         <dbl>    <dbl>      <dbl>
#>  1 2020-01      East Mid…         0          0             0        0          0
#>  2 2020-01      East of …         0          0             0        0          0
#>  3 2020-01      England           2          0             0        0          0
#>  4 2020-01      London            0          0             0        0          0
#>  5 2020-01      North Ea…         0          0             0        0          0
#>  6 2020-01      North We…         0          0             0        0          0
#>  7 2020-01      Northern…         0          0             0        0          0
#>  8 2020-01      Scotland          0          0             0        0          0
#>  9 2020-01      South Ea…         0          0             0        0          0
#> 10 2020-01      South We…         0          0             0        0          0
#> # … with 224 more rows

Month grouping zoo function as.yearmon

zoo_month_inci <- 
  build_incidence(
    dat,
    date_index = date,
    groups = region,
    counts = ends_with("new"),
    FUN = as.yearmon
  )

zoo_month_inci
#> An incidence object: 234 x 7
#> date range: [Jan 2020] to [Jun 2021]
#> cases_new: 8379330
#> deaths_new: 239477
#> recovered_new: 0
#> hosp_new: 466704
#> tested_new: 162328695
#> 
#>    date_index region      cases_new deaths_new recovered_new hosp_new tested_new
#>    <yearmon>  <chr>           <dbl>      <dbl>         <dbl>    <dbl>      <dbl>
#>  1 Jan 2020   East Midla…         0          0             0        0          0
#>  2 Jan 2020   East of En…         0          0             0        0          0
#>  3 Jan 2020   England             2          0             0        0          0
#>  4 Jan 2020   London              0          0             0        0          0
#>  5 Jan 2020   North East          0          0             0        0          0
#>  6 Jan 2020   North West          0          0             0        0          0
#>  7 Jan 2020   Northern I…         0          0             0        0          0
#>  8 Jan 2020   Scotland            0          0             0        0          0
#>  9 Jan 2020   South East          0          0             0        0          0
#> 10 Jan 2020   South West          0          0             0        0          0
#> # … with 224 more rows

comparing the above to the grates implementation

# we can compare this to the grates implementation
grates_month_inci <- 
  incidence(
    dat,
    date_index = date,
    groups = region,
    counts = ends_with("new"),
    interval = "month"
  )

grates_month_inci
#> An incidence object: 234 x 7
#> date range: [2020-Jan] to [2021-Jun]
#> cases_new: 8379330
#> deaths_new: 239477
#> recovered_new: 0
#> hosp_new: 466704
#> tested_new: 162328695
#> interval: 1 month
#> cumulative: FALSE
#> 
#>    date_index region      cases_new deaths_new recovered_new hosp_new tested_new
#>        <mnth> <chr>           <dbl>      <dbl>         <dbl>    <dbl>      <dbl>
#>  1   2020-Jan East Midla…         0          0             0        0          0
#>  2   2020-Jan East of En…         0          0             0        0          0
#>  3   2020-Jan England             2          0             0        0          0
#>  4   2020-Jan London              0          0             0        0          0
#>  5   2020-Jan North East          0          0             0        0          0
#>  6   2020-Jan North West          0          0             0        0          0
#>  7   2020-Jan Northern I…         0          0             0        0          0
#>  8   2020-Jan Scotland            0          0             0        0          0
#>  9   2020-Jan South East          0          0             0        0          0
#> 10   2020-Jan South West          0          0             0        0          0
#> # … with 224 more rows
# and check they are all "equal" ...
clock_month_inci$date_index <- as.Date(calendar_widen(clock_month_inci$date_index, "day"))
zoo_month_inci$date_index <- as.Date(zoo_month_inci$date_index)
grates_month_inci$date_index <- as.Date(grates_month_inci$date_index)

identical(
  as.data.frame(clock_month_inci),
  as.data.frame(zoo_month_inci)
)
#> [1] TRUE
identical(
  as.data.frame(grates_month_inci),
  as.data.frame(clock_month_inci)
)
#> [1] TRUE