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] 54 9 4 2 23 7 29 14 21 37 18 58 61 29 16 13 27 63 35 56 9 8 52 60 12
#> [26] 14 19 62 16 61 48 34 46 59 48 31 2 5 30 25 9 3 56 53 4 31 32 6 23 49
#> [51] 55 57 32 3 64 63 14 4 30 65 61 62 14 58 48 17 11 13 1 52 21 51 61 56 52
#> [76] 21 60 17 49 37 43 42 48 63 21 43 63 1 62 11 58 64 58 49 11 10 21 56 48 63
#> [101] 53 20 4 23 6 55 30 42 6 6 39 37 28 8 33 42 39 47 55 7 43 6 43 1 60
#> [126] 43 24 56 19 48 32 16 4 38 28 29 14 44 53 39 23 20 51 13 53 3 45 60 20 24
#> [151] 35 36 36 29 41 33 10 46 51 1 40 41 17 22 8 26 52 21 27 31 1 52 64 38 64
#> [176] 63 53 16 34 8 50 57 12 10 59 64 6 10 15 43 65 25 9 48 9 32 17 15 4 43
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
#> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
#> [25] FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE
#> [37] FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE
#> [49] TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
#> [61] TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE
#> [73] FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE
#> [85] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
#> [97] TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
#> [109] TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
#> [121] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
#> [133] FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
#> [145] FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE FALSE
#> [157] FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE
#> [169] TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE
#> [181] TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
#> [193] FALSE FALSE 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: 3 × 46
#> date patient age gender ward mo PEN OXA FLC AMX
#> <date> <chr> <dbl> <chr> <chr> <mo> <rsi> <rsi> <rsi> <rsi>
#> 1 2002-07-15 C42193 84 M ICU B_STPHY_HMNS R NA R R
#> 2 2002-07-28 F54261 69 M Clinical B_STPHY_CONS R NA S NA
#> 3 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 E99D61 2015-04-07 C FALSE
#> 2 394107 2004-01-16 C FALSE
#> 3 F35553 2002-09-23 B FALSE
#> 4 F41248 2002-04-04 A FALSE
#> 5 5B78D5 2007-02-21 A FALSE
#> 6 032343 2003-06-09 B FALSE
#> 7 D43890 2008-11-28 C TRUE
#> 8 F41D7B 2005-01-12 A FALSE
#> 9 94BB11 2006-08-14 B FALSE
#> 10 D82303 2010-12-11 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 ICU 2015-04-07 E99D61 1 TRUE
#> 2 ICU 2004-01-16 394107 1 TRUE
#> 3 ICU 2002-09-23 F35553 2 TRUE
#> 4 Clinical 2002-04-04 F41248 1 TRUE
#> 5 Clinical 2007-02-21 5B78D5 1 TRUE
#> 6 Clinical 2003-06-09 032343 1 TRUE
#> 7 Outpatient 2008-11-28 D43890 1 TRUE
#> 8 ICU 2005-01-12 F41D7B 1 TRUE
#> 9 Clinical 2006-08-14 94BB11 1 TRUE
#> 10 ICU 2010-12-11 D82303 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 105 14 52 67
#> 2 ICU 64 12 38 46
#> 3 Outpatient 13 7 11 12
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] FALSE
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 [188]
#> patient mo ward flag_episode
#> <chr> <mo> <chr> <lgl>
#> 1 E99D61 B_STPHY_EPDR ICU TRUE
#> 2 394107 B_STPHY_CONS ICU TRUE
#> 3 F35553 B_STPHY_AURS ICU FALSE
#> 4 F41248 B_STPHY_AURS Clinical TRUE
#> 5 5B78D5 B_STPHY_AURS Clinical TRUE
#> 6 032343 B_STPHY_CONS Clinical TRUE
#> 7 D43890 UNKNOWN Outpatient TRUE
#> 8 F41D7B B_STRPT_PNMN ICU TRUE
#> 9 94BB11 B_ESCHR_COLI Clinical TRUE
#> 10 D82303 B_ESCHR_COLI ICU TRUE
#> # … with 190 more rows
# }