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] 11 2 13 51 18 14 11 15 4 31 11 6 45 56 10 63 32 62 24 42 40 60 25 28 46
#> [26] 46 33 27 2 64 10 30 28 18 52 57 39 12 61 18 58 5 55 46 53 4 29 60 23 55
#> [51] 51 10 22 25 9 14 1 54 62 27 22 43 50 29 48 1 3 40 54 11 45 1 2 57 20
#> [76] 56 45 21 59 63 33 62 19 47 20 31 50 19 23 61 28 27 22 46 30 49 7 26 31 57
#> [101] 25 56 62 20 8 17 58 61 50 13 33 61 12 49 34 43 41 1 27 59 55 59 63 54 21
#> [126] 13 37 35 52 27 25 36 59 63 3 34 20 60 28 55 16 1 57 25 13 42 7 54 15 29
#> [151] 34 1 23 38 56 21 46 48 60 10 47 17 47 10 59 35 54 59 43 46 57 7 20 62 38
#> [176] 21 59 43 58 50 8 27 53 3 3 45 44 4 30 60 38 58 39 27 21 13 13 1 62 13
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
#> [13] TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE TRUE FALSE FALSE FALSE
#> [25] FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
#> [37] FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE
#> [49] TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
#> [61] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
#> [73] FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE
#> [85] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE
#> [97] FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
#> [109] TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
#> [121] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE
#> [133] FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE
#> [145] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [157] FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE
#> [169] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [181] TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE FALSE
#> [193] TRUE TRUE FALSE FALSE FALSE FALSE 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: 4 × 46
#> date patient age gender ward mo PEN OXA FLC AMX
#> <date> <chr> <dbl> <chr> <chr> <mo> <rsi> <rsi> <rsi> <rsi>
#> 1 2002-07-21 955940 82 F Clinical B_PSDMN_AERG R NA NA R
#> 2 2002-07-24 F35553 51 M ICU B_STPHY_AURS R NA S R
#> 3 2002-06-22 FD8039 75 F ICU B_ESCHR_COLI R NA NA NA
#> 4 2002-07-23 F35553 51 M ICU B_STPHY_AURS R NA S R
#> # … with 36 more variables: 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>
# 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 = 200,
replace = TRUE
)) %>%
group_by(condition) %>%
mutate(new_episode = is_new_episode(date, 365)) %>%
select(patient, date, condition, new_episode)
}
#> # A tibble: 200 × 4
#> # Groups: condition [3]
#> patient date condition new_episode
#> <chr> <date> <chr> <lgl>
#> 1 318447 2004-05-28 A FALSE
#> 2 C70694 2002-05-15 B FALSE
#> 3 E48302 2004-11-29 B FALSE
#> 4 D60054 2014-10-16 A FALSE
#> 5 5DF436 2006-01-04 B TRUE
#> 6 4F6B71 2005-01-24 B FALSE
#> 7 4B6270 2004-07-01 A FALSE
#> 8 A92398 2005-05-14 A FALSE
#> 9 F35553 2002-10-20 C FALSE
#> 10 A54805 2008-11-19 B FALSE
#> # … with 190 more rows
if (require("dplyr")) {
df %>%
group_by(ward, patient) %>%
transmute(date,
patient,
new_index = get_episode(date, 60),
new_logical = is_new_episode(date, 60)
)
}
#> # A tibble: 200 × 5
#> # Groups: ward, patient [182]
#> ward date patient new_index new_logical
#> <chr> <date> <chr> <dbl> <lgl>
#> 1 Outpatient 2004-05-28 318447 1 TRUE
#> 2 Clinical 2002-05-15 C70694 1 TRUE
#> 3 ICU 2004-11-29 E48302 1 TRUE
#> 4 Clinical 2014-10-16 D60054 1 TRUE
#> 5 ICU 2006-01-04 5DF436 1 TRUE
#> 6 Clinical 2005-01-24 4F6B71 1 TRUE
#> 7 Clinical 2004-07-01 4B6270 1 TRUE
#> 8 Clinical 2005-05-14 A92398 1 TRUE
#> 9 ICU 2002-10-20 F35553 3 TRUE
#> 10 Clinical 2008-11-19 A54805 1 TRUE
#> # … with 190 more rows
if (require("dplyr")) {
df %>%
group_by(ward) %>%
summarise(
n_patients = n_distinct(patient),
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))
)
}
#> # A tibble: 3 × 5
#> ward n_patients n_episodes_365 n_episodes_60 n_episodes_30
#> <chr> <int> <int> <int> <int>
#> 1 Clinical 116 15 56 78
#> 2 ICU 56 13 33 42
#> 3 Outpatient 10 5 8 8
if (require("dplyr")) {
# 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, mo) %>%
filter(is_new_episode(date, 365)) %>%
ungroup()
identical(x, y)
}
#> Including isolates from ICU.
#> [1] TRUE
if (require("dplyr")) {
# 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, mo, ward) %>%
mutate(flag_episode = is_new_episode(date, 365)) %>%
select(group_vars(.), flag_episode)
}
#> # A tibble: 200 × 4
#> # Groups: patient, mo, ward [189]
#> patient mo ward flag_episode
#> <chr> <mo> <chr> <lgl>
#> 1 318447 B_STPHY_CONS Outpatient TRUE
#> 2 C70694 B_STPHY_AURS Clinical TRUE
#> 3 E48302 B_STRPT_PNMN ICU TRUE
#> 4 D60054 B_STRPT_SLVR Clinical TRUE
#> 5 5DF436 B_ENTRC ICU TRUE
#> 6 4F6B71 B_STRPT_GRPB Clinical TRUE
#> 7 4B6270 B_PSDMN_AERG Clinical TRUE
#> 8 A92398 B_ESCHR_COLI Clinical TRUE
#> 9 F35553 B_STPHY_AURS ICU FALSE
#> 10 A54805 B_STRPT_PNMN Clinical TRUE
#> # … with 190 more rows
# }