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] 44 41 9 51 35 64 60 49 41 26 55 1 37 2 46 54 2 62 58 25 57 38 1 50 21
#> [26] 48 11 9 24 55 40 57 60 52 33 3 11 63 51 47 36 59 57 14 57 32 60 13 54 26
#> [51] 20 32 7 39 48 22 1 52 27 30 6 15 2 64 33 8 21 2 47 41 19 31 43 64 43
#> [76] 6 42 14 42 36 8 42 34 43 5 38 57 18 46 37 27 18 60 2 33 23 14 18 42 8
#> [101] 56 29 12 16 55 60 27 3 15 46 57 63 27 6 18 64 3 59 8 45 49 9 52 13 37
#> [126] 6 21 16 64 12 62 59 38 61 27 11 63 2 9 49 47 52 22 62 53 20 5 1 9 48
#> [151] 10 20 20 18 20 11 5 38 43 18 22 50 44 59 14 31 62 6 10 60 14 43 59 13 42
#> [176] 15 28 60 13 4 45 58 45 61 12 1 55 37 31 46 17 63 21 50 41 1 38 32 7 51
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [13] FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE
#> [25] FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE FALSE
#> [37] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [49] FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
#> [61] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
#> [73] TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
#> [85] FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE
#> [97] TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
#> [109] TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE
#> [121] TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE
#> [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE
#> [145] TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE
#> [157] TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE
#> [169] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
#> [181] FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
#> [193] TRUE TRUE TRUE FALSE TRUE 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 × 46
#> date patient age gender ward mo PEN OXA FLC AMX
#> <date> <chr> <dbl> <chr> <chr> <mo> <rsi> <rsi> <rsi> <rsi>
#> 1 2002-08-14 785317 51 F ICU B_ESCHR_COLI R NA NA NA
#> 2 2002-07-30 218912 76 F ICU B_ESCHR_COLI R NA NA NA
#> 3 2002-07-24 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 C25552 2012-11-22 A FALSE
#> 2 76F141 2012-05-04 A TRUE
#> 3 1B144C 2004-05-10 B TRUE
#> 4 0C0688 2014-09-05 C TRUE
#> 5 725779 2010-09-04 B FALSE
#> 6 527306 2017-12-12 B FALSE
#> 7 E68281 2017-03-02 B FALSE
#> 8 A96395 2014-03-11 C FALSE
#> 9 686445 2012-05-24 C FALSE
#> 10 195736 2008-08-29 A 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 2012-11-22 C25552 1 TRUE
#> 2 Clinical 2012-05-04 76F141 1 TRUE
#> 3 Outpatient 2004-05-10 1B144C 1 TRUE
#> 4 Clinical 2014-09-05 0C0688 1 TRUE
#> 5 Outpatient 2010-09-04 725779 1 TRUE
#> 6 ICU 2017-12-12 527306 1 TRUE
#> 7 Clinical 2017-03-02 E68281 1 TRUE
#> 8 Clinical 2014-03-11 A96395 1 TRUE
#> 9 Clinical 2012-05-24 686445 1 TRUE
#> 10 Clinical 2008-08-29 195736 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 111 14 51 73
#> 2 ICU 61 13 36 42
#> 3 Outpatient 10 6 9 9
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 [193]
#> patient mo ward flag_episode
#> <chr> <mo> <chr> <lgl>
#> 1 C25552 B_STPHY_CONS Outpatient TRUE
#> 2 76F141 B_ESCHR_COLI Clinical TRUE
#> 3 1B144C B_STRPT_PNMN Outpatient TRUE
#> 4 0C0688 B_ESCHR_COLI Clinical TRUE
#> 5 725779 B_STRPT_PNMN Outpatient TRUE
#> 6 527306 B_STRPT_SLVR ICU TRUE
#> 7 E68281 B_STPHY_AURS Clinical TRUE
#> 8 A96395 B_ENTRC_FCLS Clinical TRUE
#> 9 686445 B_PROTS_MRBL Clinical TRUE
#> 10 195736 B_STPHY_AURS Clinical TRUE
#> # … with 190 more rows
# }