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] 60 52 8 61 6 52 60 61 13 32 21 42 36 27 16 14 58 22 9 35 57 60 21 8 26
#> [26] 65 22 14 37 28 64 32 58 2 12 64 63 59 27 49 43 53 7 19 50 31 1 11 36 16
#> [51] 46 10 45 25 33 56 63 16 37 59 8 16 34 14 47 21 43 2 22 8 26 65 9 57 22
#> [76] 1 18 22 40 41 11 48 57 44 34 4 1 57 40 48 15 23 62 53 45 54 19 21 6 44
#> [101] 2 42 36 61 45 8 11 25 23 54 58 3 7 20 16 57 2 48 54 28 44 42 63 58 39
#> [126] 4 13 10 40 63 9 13 20 42 47 8 10 52 62 65 59 42 37 27 9 25 33 60 57 63
#> [151] 58 46 15 53 56 13 27 1 59 29 60 62 30 40 64 36 61 8 42 49 31 14 29 2 22
#> [176] 5 19 24 25 33 55 42 1 19 15 57 33 38 51 44 13 23 31 17 44 26 56 9 9 46
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE
#> [13] TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE
#> [25] FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE
#> [37] TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
#> [49] FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE
#> [61] FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE FALSE
#> [73] FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE
#> [85] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [97] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
#> [109] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [121] FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE
#> [133] TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
#> [145] FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE TRUE
#> [157] FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE
#> [169] FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE
#> [181] TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE
#> [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: 1 × 46
#> date patient age gender ward mo PEN OXA FLC AMX
#> <date> <chr> <dbl> <chr> <chr> <mo> <rsi> <rsi> <rsi> <rsi>
#> 1 2002-07-16 241328 78 M Outpatie… B_STPHY_CONS 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 305134 2016-10-28 C TRUE
#> 2 789292 2014-08-05 A FALSE
#> 3 82C90B 2003-09-19 A FALSE
#> 4 F5F794 2017-02-17 A FALSE
#> 5 065187 2003-05-26 C TRUE
#> 6 F25686 2014-07-23 C TRUE
#> 7 219664 2016-11-19 B FALSE
#> 8 D31017 2016-12-22 A TRUE
#> 9 B95551 2004-12-17 B FALSE
#> 10 451000 2009-08-29 A TRUE
#> # … 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 [173]
#> ward date patient new_index new_logical
#> <chr> <date> <chr> <dbl> <lgl>
#> 1 ICU 2016-10-28 305134 1 TRUE
#> 2 Clinical 2014-08-05 789292 1 TRUE
#> 3 Clinical 2003-09-19 82C90B 1 TRUE
#> 4 Clinical 2017-02-17 F5F794 1 TRUE
#> 5 ICU 2003-05-26 065187 1 TRUE
#> 6 Clinical 2014-07-23 F25686 1 TRUE
#> 7 Clinical 2016-11-19 219664 1 TRUE
#> 8 ICU 2016-12-22 D31017 1 TRUE
#> 9 Outpatient 2004-12-17 B95551 1 TRUE
#> 10 Clinical 2009-08-29 451000 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 53 71
#> 2 ICU 60 12 36 44
#> 3 Outpatient 8 5 7 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] 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 [185]
#> patient mo ward flag_episode
#> <chr> <mo> <chr> <lgl>
#> 1 305134 B_PROTS_VLGR ICU TRUE
#> 2 789292 B_STRPT_SLVR Clinical TRUE
#> 3 82C90B B_STPHY_EPDR Clinical TRUE
#> 4 F5F794 B_STPHY_AURS Clinical TRUE
#> 5 065187 B_STPHY_AURS ICU TRUE
#> 6 F25686 B_PROTS_MRBL Clinical TRUE
#> 7 219664 B_STPHY_HMNS Clinical TRUE
#> 8 D31017 F_CANDD_TRPC ICU TRUE
#> 9 B95551 B_STPHY_CONS Outpatient TRUE
#> 10 451000 B_STPHY_CONS Clinical TRUE
#> # … with 190 more rows
# }