incidence() works with POSIXt columns when interval = "week" but interval = "day"
Closed this issue · 2 comments
Bisaloo commented
Please place an "x" in all the boxes that apply
- I have the most recent version of incidence2 and R
- I have found a bug
- I have a reproducible example
If a column is stored as POSIXt
/ datetime rather than a plain Date
, incidence()
will have an inconsistent behaviour and work when interval = "week" but interval = "day".
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(incidence2)
# Create a dataset with Dates stored as datetimes / POSIXt
data("covid19_england_nhscalls_2020", package = "outbreaks")
d <- covid19_england_nhscalls_2020 %>%
mutate(across(where(lubridate::is.Date), as.POSIXct, tz = "UTC"))
# Convert this dataset to incidence2
# WORKS
d %>%
incidence("date",
interval = "week",
counts = "count"
)
#> An incidence object: 27 x 2
#> date range: [2020-W12] to [2020-W38]
#> cases: 4101446
#> interval: 1 (Monday) week
#> cumulative: FALSE
#>
#> date_index count
#> <yrwk> <int>
#> 1 2020-W12 677547
#> 2 2020-W13 866757
#> 3 2020-W14 522298
#> 4 2020-W15 331617
#> 5 2020-W16 212969
#> 6 2020-W17 156984
#> 7 2020-W18 154765
#> 8 2020-W19 117314
#> 9 2020-W20 107629
#> 10 2020-W21 88949
#> # … with 17 more rows
# DOESN'T WORK
d %>%
incidence("date",
interval = "day",
counts = "count"
)
#> Error in `create_interval_string()`:
#> ! Not implemented for class POSIXct, POSIXt
#> Backtrace:
#> ▆
#> 1. ├─d %>% incidence("date", interval = "day", counts = "count")
#> 2. └─incidence2::incidence(., "date", interval = "day", counts = "count")
#> 3. ├─incidence2:::create_interval_string(dat$date_index)
#> 4. └─incidence2:::create_interval_string.default(dat$date_index)
#> 5. └─rlang::abort(...)
Created on 2022-11-21 with reprex v2.0.2.9000
TimTaylor commented
Cheers - confirmed. Currently unsure if there will be a patch release with the current API though. Working on a new major release which will explicitly push date categorisation on to the user/developer in which case this approach will be defunct.
TimTaylor commented
Closed by 1ef0420
library(dplyr, warn.conflicts = FALSE)
library(incidence2)
#> Loading required package: grates
data("covid19_england_nhscalls_2020", package = "outbreaks")
covid19_england_nhscalls_2020 |>
mutate(across(where(lubridate::is.Date), as.POSIXct, tz = "UTC")) |>
incidence(date_index = "date", interval = "week", counts = "count")
#> # incidence: 27 x 3
#> date_index count_variable count
#> * <isowk> <fct> <int>
#> 1 2020-W12 count 677547
#> 2 2020-W13 count 866757
#> 3 2020-W14 count 522298
#> 4 2020-W15 count 331617
#> 5 2020-W16 count 212969
#> 6 2020-W17 count 156984
#> 7 2020-W18 count 154765
#> 8 2020-W19 count 117314
#> 9 2020-W20 count 107629
#> 10 2020-W21 count 88949
#> # … with 17 more rows
Created on 2023-01-24 with reprex v2.0.2