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] 37 22 35 49 56 34 5 24 8 19 29 58 1 25 23 2 63 43 7 18 52 35 30 50 39
#> [26] 51 60 35 37 36 7 26 55 12 28 31 29 21 12 26 39 11 5 43 15 45 32 11 51 2
#> [51] 12 1 59 39 10 26 46 1 57 46 44 61 17 59 17 53 39 4 27 48 51 10 16 52 8
#> [76] 45 16 56 39 34 34 36 30 15 28 53 3 12 29 32 7 10 20 38 12 61 16 1 59 29
#> [101] 34 2 39 35 14 49 41 28 48 58 54 6 7 38 46 50 54 25 34 49 54 3 18 5 36
#> [126] 42 63 24 13 42 26 19 8 36 54 56 18 11 13 14 58 43 13 9 54 32 52 50 49 47
#> [151] 22 4 40 49 27 22 55 43 38 1 33 47 9 30 5 11 60 37 62 24 18 50 9 5 29
#> [176] 63 50 32 51 5 3 44 4 43 63 62 33 54 9 7 4 54 48 40 19 37 53 12 49 24
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [13] TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
#> [25] FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE
#> [37] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
#> [49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
#> [61] TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
#> [73] FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
#> [85] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE
#> [97] FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
#> [109] FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE TRUE FALSE TRUE
#> [121] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE
#> [133] FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
#> [145] FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
#> [157] FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
#> [169] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [181] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [193] TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE
# 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-07-30 A TRUE TRUE FALSE 76 F 218912 B_ESCHR_COLI
#> 2 2002-07-30 A TRUE TRUE FALSE 76 F 218912 B_ESCHR_COLI
#> 3 2002-07-16 D FALSE FALSE TRUE 78 M 241328 B_STPHY_CONS
#> # … 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.
# }