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] 46 12 62 58 1 50 44 18 56 48 8 63 1 19 54 62 29 63 13 49 17 50 58 32 53
#> [26] 65 47 11 60 65 33 59 19 41 35 7 5 60 61 38 15 15 39 62 65 9 7 59 3 67
#> [51] 42 55 3 11 52 46 43 28 23 63 32 36 57 21 24 58 22 23 38 3 59 66 41 3 45
#> [76] 18 39 29 44 65 67 20 46 47 24 8 55 16 51 25 51 26 16 57 48 58 56 54 44 26
#> [101] 59 64 3 9 18 10 19 1 29 23 31 28 63 49 37 43 52 5 38 17 46 29 13 31 19
#> [126] 51 10 49 54 10 66 32 35 30 4 32 21 14 21 41 33 6 26 13 11 63 17 13 54 47
#> [151] 34 21 6 4 30 4 58 53 48 19 9 20 64 40 13 61 10 64 22 7 47 28 45 54 4
#> [176] 40 64 42 32 49 51 4 4 2 12 12 4 18 45 67 19 65 8 14 24 18 20 60 42 27
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
#> [13] TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
#> [25] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [37] TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE
#> [49] TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE FALSE
#> [61] TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE
#> [73] FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE
#> [85] TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE
#> [97] TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
#> [109] TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE
#> [121] FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
#> [133] TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE TRUE TRUE FALSE FALSE
#> [145] TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE
#> [157] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [169] FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
#> [181] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [193] FALSE FALSE FALSE FALSE FALSE FALSE 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: 5 × 46
#> date patient age gender ward mo PEN OXA FLC AMX
#> <date> <chr> <dbl> <chr> <chr> <mo> <rsi> <rsi> <rsi> <rsi>
#> 1 2002-06-18 012595 30 M ICU B_CRYNB I NA NA NA
#> 2 2002-07-23 F35553 51 M ICU B_STPHY_AURS R NA S R
#> 3 2002-07-21 955940 82 F Clinical B_PSDMN_AERG R NA NA R
#> 4 2002-07-15 426426 67 F ICU B_ESCHR_COLI R NA NA NA
#> 5 2002-08-14 785317 51 F ICU B_ESCHR_COLI R NA NA NA
#> # … 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 C541BC 2012-09-03 A FALSE
#> 2 F35553 2004-09-24 A FALSE
#> 3 C76410 2016-09-09 B FALSE
#> 4 A76045 2015-10-06 B TRUE
#> 5 067927 2002-01-13 A FALSE
#> 6 835073 2013-09-27 B FALSE
#> 7 76F141 2012-05-04 C FALSE
#> 8 A2C4D5 2006-01-10 B TRUE
#> 9 E99D61 2015-04-07 A FALSE
#> 10 B43936 2013-05-16 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 [178]
#> ward date patient new_index new_logical
#> <chr> <date> <chr> <dbl> <lgl>
#> 1 Clinical 2012-09-03 C541BC 1 TRUE
#> 2 ICU 2004-09-24 F35553 5 TRUE
#> 3 Clinical 2016-09-09 C76410 1 TRUE
#> 4 ICU 2015-10-06 A76045 1 TRUE
#> 5 ICU 2002-01-13 067927 1 FALSE
#> 6 Clinical 2013-09-27 835073 1 TRUE
#> 7 Clinical 2012-05-04 76F141 1 TRUE
#> 8 Clinical 2006-01-10 A2C4D5 1 TRUE
#> 9 ICU 2015-04-07 E99D61 1 TRUE
#> 10 ICU 2013-05-16 B43936 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 114 15 57 76
#> 2 ICU 56 12 38 45
#> 3 Outpatient 8 4 7 7
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 [186]
#> patient mo ward flag_episode
#> <chr> <mo> <chr> <lgl>
#> 1 C541BC B_ESCHR_COLI Clinical TRUE
#> 2 F35553 B_STPHY_AURS ICU FALSE
#> 3 C76410 B_STPHY_EPDR Clinical TRUE
#> 4 A76045 B_STPHY_HMNS ICU TRUE
#> 5 067927 B_STPHY_EPDR ICU TRUE
#> 6 835073 B_STPHY_HMNS Clinical TRUE
#> 7 76F141 B_ESCHR_COLI Clinical TRUE
#> 8 A2C4D5 B_STRPT_GRPG Clinical TRUE
#> 9 E99D61 B_STPHY_EPDR ICU TRUE
#> 10 B43936 B_BCTRD_FRGL ICU TRUE
#> # … with 190 more rows
# }