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] 38 54 24 6 7 33 20 44 53 63 25 55 56 49 61 62 9 8 45 9 36 17 37 2 50
#> [26] 35 20 13 11 23 63 59 61 43 59 41 25 22 12 46 18 62 57 17 27 8 36 29 24 41
#> [51] 58 35 61 48 12 18 3 31 20 41 56 9 56 51 64 16 1 36 40 50 53 19 25 36 12
#> [76] 20 9 18 36 62 29 51 45 12 10 23 40 3 38 5 15 7 17 21 3 63 38 40 7 14
#> [101] 63 7 17 15 51 17 62 2 61 12 52 32 46 32 27 4 13 61 19 37 41 38 51 22 50
#> [126] 41 14 12 29 64 58 52 57 46 57 33 56 16 34 26 5 34 17 42 63 61 22 58 20 44
#> [151] 25 38 25 17 12 22 56 28 13 34 3 47 25 63 16 1 5 1 22 39 31 14 31 51 30
#> [176] 1 61 46 6 27 19 1 56 40 30 20 10 57 60 18 24 60 30 22 47 23 62 26 62 5
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE
#> [13] FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
#> [25] FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE FALSE
#> [37] TRUE FALSE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
#> [49] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
#> [61] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE
#> [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
#> [85] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
#> [97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [109] FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE TRUE TRUE TRUE
#> [121] FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE
#> [133] FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE
#> [145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [157] FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [169] FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
#> [181] FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
#> [193] FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE
# 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-08-19 A49852 70 M Clinical B_ESCHR_COLI R NA NA NA
#> 2 2002-07-16 241328 78 M Outpatie… B_STPHY_CONS R NA S R
#> 3 2002-06-23 798871 82 M Clinical B_ENTRC_FCLS NA NA NA NA
#> 4 2002-06-22 FD8039 75 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 687590 2011-10-29 C FALSE
#> 2 C54153 2015-08-09 A FALSE
#> 3 1710B8 2008-01-22 B FALSE
#> 4 914520 2003-07-11 B FALSE
#> 5 999565 2003-10-09 B FALSE
#> 6 953526 2010-04-23 A FALSE
#> 7 418311 2006-11-10 C FALSE
#> 8 298662 2013-03-12 B TRUE
#> 9 2C8499 2015-05-22 B FALSE
#> 10 F67215 2017-08-15 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 [189]
#> ward date patient new_index new_logical
#> <chr> <date> <chr> <dbl> <lgl>
#> 1 Clinical 2011-10-29 687590 1 TRUE
#> 2 Clinical 2015-08-09 C54153 1 TRUE
#> 3 Clinical 2008-01-22 1710B8 1 TRUE
#> 4 ICU 2003-07-11 914520 1 TRUE
#> 5 Clinical 2003-10-09 999565 1 TRUE
#> 6 ICU 2010-04-23 953526 1 TRUE
#> 7 ICU 2006-11-10 418311 1 TRUE
#> 8 Clinical 2013-03-12 298662 1 TRUE
#> 9 ICU 2015-05-22 2C8499 1 TRUE
#> 10 Clinical 2017-08-15 F67215 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 119 14 48 72
#> 2 ICU 59 14 37 42
#> 3 Outpatient 11 8 9 11
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 687590 B_STPHY_CONS Clinical TRUE
#> 2 C54153 B_STPHY_EPDR Clinical TRUE
#> 3 1710B8 B_PROTS_MRBL Clinical TRUE
#> 4 914520 B_STPHY_CONS ICU TRUE
#> 5 999565 B_STPHY_CONS Clinical TRUE
#> 6 953526 B_STPHY_CONS ICU TRUE
#> 7 418311 B_STPHY_CONS ICU TRUE
#> 8 298662 B_ESCHR_COLI Clinical TRUE
#> 9 2C8499 B_ESCHR_COLI ICU TRUE
#> 10 F67215 B_STPHY_EPDR Clinical TRUE
#> # … with 190 more rows
# }