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] 67 46 16 36 38 5 49 55 15 40 15 27 43 31 1 8 65 17 32 23 21 42 55 33 12
#> [26] 40 37 19 63 16 16 13 18 40 58 28 58 60 22 59 41 1 60 46 9 31 1 67 16 12
#> [51] 66 56 3 5 53 48 6 25 64 28 13 34 48 11 25 15 54 23 58 10 49 2 29 65 16
#> [76] 42 19 34 51 38 52 2 42 9 45 62 59 1 60 14 36 59 39 15 1 63 66 11 57 27
#> [101] 24 38 42 44 9 14 45 31 7 1 64 20 24 24 43 64 33 43 20 57 63 30 28 38 13
#> [126] 20 10 46 14 7 47 22 28 51 9 6 46 11 53 40 44 56 44 13 61 30 67 35 13 33
#> [151] 7 58 63 62 37 49 60 5 12 10 38 11 33 33 62 17 13 53 62 60 15 8 21 39 28
#> [176] 4 52 65 21 19 40 23 8 25 50 21 13 35 57 23 66 13 65 65 26 12 62 2 27 59
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
#> [13] FALSE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
#> [25] FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
#> [37] FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [49] TRUE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
#> [61] FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE
#> [73] TRUE FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
#> [85] FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE
#> [97] FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
#> [109] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
#> [121] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE
#> [133] TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE
#> [145] TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE
#> [157] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE
#> [169] FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE
#> [181] FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [193] FALSE FALSE TRUE TRUE 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> <sir> <sir> <sir> <sir>
#> 1 2002-05-16 E52286 47 M Clinical B_STPHY_AURS R NA S R
#> # … with 36 more variables: AMC <sir>, AMP <sir>, TZP <sir>, CZO <sir>,
#> # FEP <sir>, CXM <sir>, FOX <sir>, CTX <sir>, CAZ <sir>, CRO <sir>,
#> # GEN <sir>, TOB <sir>, AMK <sir>, KAN <sir>, TMP <sir>, SXT <sir>,
#> # NIT <sir>, FOS <sir>, LNZ <sir>, CIP <sir>, MFX <sir>, VAN <sir>,
#> # TEC <sir>, TCY <sir>, TGC <sir>, DOX <sir>, ERY <sir>, CLI <sir>,
#> # AZM <sir>, IPM <sir>, MEM <sir>, MTR <sir>, CHL <sir>, COL <sir>,
#> # MUP <sir>, RIF <sir>
# 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 904640 2017-11-08 C FALSE
#> 2 685398 2013-04-16 B FALSE
#> 3 B82692 2005-09-18 B FALSE
#> 4 D19627 2010-08-15 C TRUE
#> 5 414930 2011-01-10 B FALSE
#> 6 384360 2003-01-09 B FALSE
#> 7 479516 2013-09-30 A FALSE
#> 8 375220 2014-12-22 C TRUE
#> 9 C82046 2005-08-08 A FALSE
#> 10 035268 2011-05-31 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 [180]
#> ward date patient new_index new_logical
#> <chr> <date> <chr> <dbl> <lgl>
#> 1 ICU 2017-11-08 904640 1 TRUE
#> 2 Clinical 2013-04-16 685398 1 TRUE
#> 3 Clinical 2005-09-18 B82692 1 TRUE
#> 4 Clinical 2010-08-15 D19627 1 TRUE
#> 5 Clinical 2011-01-10 414930 1 TRUE
#> 6 Clinical 2003-01-09 384360 1 TRUE
#> 7 Clinical 2013-09-30 479516 1 TRUE
#> 8 Clinical 2014-12-22 375220 1 TRUE
#> 9 ICU 2005-08-08 C82046 1 TRUE
#> 10 ICU 2011-05-31 035268 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 115 15 58 77
#> 2 ICU 56 13 32 42
#> 3 Outpatient 9 6 8 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] TRUE
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 [191]
#> patient mo ward flag_episode
#> <chr> <mo> <chr> <lgl>
#> 1 904640 B_CRYNB_STRT ICU TRUE
#> 2 685398 B_STRPT_ORLS Clinical TRUE
#> 3 B82692 B_STPHY_CONS Clinical TRUE
#> 4 D19627 B_ESCHR_COLI Clinical TRUE
#> 5 414930 B_STPHY_CONS Clinical TRUE
#> 6 384360 B_STPHY_CONS Clinical TRUE
#> 7 479516 B_STPHY_HMNS Clinical TRUE
#> 8 375220 B_ENTRC_FCLS Clinical TRUE
#> 9 C82046 B_ESCHR_COLI ICU TRUE
#> 10 035268 B_STPHY_CONS ICU TRUE
#> # … with 190 more rows
# }