These functions determine which items in a vector can be considered (the start of) a new episode, based on the argument episode_days
. This can be used to determine clinical episodes for any epidemiological analysis. The get_episode()
function returns the index number of the episode per group, while the is_new_episode()
function returns values TRUE
/FALSE
to indicate whether an item in a vector is the start of a new episode.
Arguments
- x
vector of dates (class
Date
orPOSIXt
), will be sorted internally to determine episodes- episode_days
required episode length in days, can also be less than a day or
Inf
, see Details- ...
ignored, only in place to allow future extensions
Details
Dates are first sorted from old to new. The oldest date will mark the start of the first episode. After this date, the next date will be marked that is at least episode_days
days later than the start of the first episode. From that second marked date on, the next date will be marked that is at least episode_days
days later than the start of the second episode which will be the start of the third episode, and so on. Before the vector is being returned, the original order will be restored.
The first_isolate()
function is a wrapper around the is_new_episode()
function, but is more efficient for data sets containing microorganism codes or names and allows for different isolate selection methods.
The dplyr
package is not required for these functions to work, but these functions do support variable grouping and work conveniently inside dplyr
verbs such as filter()
, mutate()
and summarise()
.
Examples
# `example_isolates` is a data set available in the AMR package.
# See ?example_isolates
df <- example_isolates[sample(seq_len(2000), size = 200), ]
get_episode(df$date, episode_days = 60) # indices
#> [1] 63 42 61 46 22 21 21 10 46 12 31 47 53 64 9 27 12 7 30 25 15 61 52 20 18
#> [26] 46 53 39 38 54 61 58 12 58 39 21 27 35 66 46 59 57 13 43 30 29 60 51 25 35
#> [51] 22 4 56 20 5 18 18 22 9 21 50 9 58 38 59 44 46 11 41 55 55 5 14 56 26
#> [76] 53 63 33 20 5 31 32 10 1 51 46 2 28 24 40 10 13 26 55 12 49 54 16 43 26
#> [101] 51 37 60 27 49 23 42 63 5 60 7 23 4 37 47 22 52 45 65 56 11 38 17 36 12
#> [126] 9 47 25 14 1 56 38 66 42 53 19 51 10 34 16 11 15 62 31 40 22 63 49 32 62
#> [151] 45 6 48 5 58 57 7 56 46 13 58 65 15 21 7 42 8 5 6 6 28 29 4 26 19
#> [176] 3 56 3 60 61 63 63 56 65 16 21 40 57 23 16 47 58 61 21 32 9 60 3 17 62
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
#> [13] FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE
#> [25] FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE
#> [37] FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE TRUE
#> [49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
#> [61] TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE
#> [73] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
#> [85] FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
#> [97] FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
#> [109] TRUE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
#> [121] FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
#> [133] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE
#> [145] TRUE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE
#> [157] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
#> [169] FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE
#> [181] TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE
#> [193] FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
# filter on results from the third 60-day episode only, using base R
df[which(get_episode(df$date, 60) == 3), ]
#> # A tibble: 3 × 49
#> date hospita…¹ ward_…² ward_…³ ward_…⁴ age gender patie…⁵ mo
#> <date> <fct> <lgl> <lgl> <lgl> <dbl> <chr> <chr> <mo>
#> 1 2002-06-06 D FALSE TRUE FALSE 20 F 24D393 B_ESCHR_COLI
#> 2 2002-06-18 B TRUE FALSE FALSE 30 M 012595 B_CRYNB
#> 3 2002-07-23 B TRUE FALSE FALSE 51 M F35553 B_STPHY_AURS
#> # … with 40 more variables: PEN <rsi>, OXA <rsi>, FLC <rsi>, AMX <rsi>,
#> # AMC <rsi>, AMP <rsi>, TZP <rsi>, CZO <rsi>, FEP <rsi>, CXM <rsi>,
#> # FOX <rsi>, CTX <rsi>, CAZ <rsi>, CRO <rsi>, GEN <rsi>, TOB <rsi>,
#> # AMK <rsi>, KAN <rsi>, TMP <rsi>, SXT <rsi>, NIT <rsi>, FOS <rsi>,
#> # LNZ <rsi>, CIP <rsi>, MFX <rsi>, VAN <rsi>, TEC <rsi>, TCY <rsi>,
#> # TGC <rsi>, DOX <rsi>, ERY <rsi>, CLI <rsi>, AZM <rsi>, IPM <rsi>,
#> # MEM <rsi>, MTR <rsi>, CHL <rsi>, COL <rsi>, MUP <rsi>, RIF <rsi>, and …
#> # ℹ Use `colnames()` to see all variable names
# the functions also work for less than a day, e.g. to include one per hour:
get_episode(c(Sys.time(),
Sys.time() + 60 * 60),
episode_days = 1/24)
#> [1] 1 2
# \donttest{
if (require("dplyr")) {
# is_new_episode() can also be used in dplyr verbs to determine patient
# episodes based on any (combination of) grouping variables:
df %>%
mutate(condition = sample(x = c("A", "B", "C"),
size = 2000,
replace = TRUE)) %>%
group_by(condition) %>%
mutate(new_episode = is_new_episode(date, 365)) %>%
select(patient_id, date, condition, new_episode)
df %>%
group_by(hospital_id, patient_id) %>%
transmute(date,
patient_id,
new_index = get_episode(date, 60),
new_logical = is_new_episode(date, 60))
df %>%
group_by(hospital_id) %>%
summarise(n_patients = n_distinct(patient_id),
n_episodes_365 = sum(is_new_episode(date, episode_days = 365)),
n_episodes_60 = sum(is_new_episode(date, episode_days = 60)),
n_episodes_30 = sum(is_new_episode(date, episode_days = 30)))
# grouping on patients and microorganisms leads to the same
# results as first_isolate() when using 'episode-based':
x <- df %>%
filter_first_isolate(include_unknown = TRUE,
method = "episode-based")
y <- df %>%
group_by(patient_id, mo) %>%
filter(is_new_episode(date, 365)) %>%
ungroup()
identical(x, y)
# but is_new_episode() has a lot more flexibility than first_isolate(),
# since you can now group on anything that seems relevant:
df %>%
group_by(patient_id, mo, hospital_id, ward_icu) %>%
mutate(flag_episode = is_new_episode(date, 365)) %>%
select(group_vars(.), flag_episode)
}
#> Error in mutate(., condition = sample(x = c("A", "B", "C"), size = 2000, replace = TRUE)): Problem while computing `condition = sample(x = c("A", "B", "C"), size =
#> 2000, replace = TRUE)`.
#> ✖ `condition` must be size 200 or 1, not 2000.
# }