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] 28 28 18 40 67 67 29 37 63 60 40 26 15 63 46 7 50 54 7 37 7 43 19 30 20
#> [26] 59 42 12 27 46 2 47 22 64 41 18 15 14 31 55 49 63 47 67 43 47 6 30 7 42
#> [51] 13 2 60 30 10 17 34 1 57 24 10 22 11 22 61 44 15 2 11 26 64 31 10 12 33
#> [76] 23 49 8 12 50 6 57 29 51 28 59 48 6 35 60 12 25 17 15 40 62 61 64 2 7
#> [101] 56 10 24 66 30 65 47 54 31 64 7 13 41 60 23 35 11 37 24 12 53 58 39 31 26
#> [126] 34 60 56 62 31 58 36 17 13 54 67 53 37 23 60 15 30 61 43 61 45 5 20 34 33
#> [151] 47 10 20 29 15 39 38 34 65 7 40 57 23 59 3 9 60 62 54 66 40 26 42 63 16
#> [176] 23 16 15 67 48 65 63 27 33 52 6 60 44 41 54 64 32 4 16 3 5 21 19 49 4
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [13] FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE
#> [25] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
#> [37] FALSE TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE
#> [49] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
#> [61] FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE
#> [73] FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE
#> [85] TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE
#> [97] FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE
#> [109] FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
#> [121] TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE
#> [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
#> [145] FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
#> [157] TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
#> [169] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
#> [181] FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
#> [193] TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
# filter on results from the third 60-day episode only, using base R
df[which(get_episode(df$date, 60) == 3), ]
#> # A tibble: 2 × 49
#> date hospita…¹ ward_…² ward_…³ ward_…⁴ age gender patie…⁵ mo
#> <date> <fct> <lgl> <lgl> <lgl> <dbl> <chr> <chr> <mo>
#> 1 2002-11-12 B TRUE FALSE FALSE 29 M CF9318 B_STPHY_CONS
#> 2 2002-09-24 B TRUE FALSE FALSE 29 M CF9318 B_CMPYL_JEJN
#> # … 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 …
# 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.
# }