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] 11 6 3 62 46 35 47 39 18 32 12 8 8 26 63 47 24 52 25 8 15 26 15 30 14
#> [26] 63 31 38 8 28 54 27 16 37 17 43 6 21 58 2 24 60 63 44 38 19 28 51 62 13
#> [51] 30 27 54 12 7 40 42 66 39 39 58 41 38 45 56 18 9 13 63 16 7 58 3 18 60
#> [76] 63 10 26 27 14 39 15 42 12 2 13 19 61 13 38 64 1 21 48 36 24 38 5 60 8
#> [101] 18 34 45 60 11 15 8 2 11 10 62 31 22 15 12 62 11 36 49 22 8 49 56 59 6
#> [126] 10 17 54 43 1 7 53 55 64 6 37 9 38 29 52 63 62 13 5 28 56 13 54 41 18
#> [151] 59 44 52 60 20 51 2 2 53 59 61 33 23 34 24 44 56 62 3 50 45 12 42 17 15
#> [176] 65 1 18 4 13 32 41 4 34 2 45 64 9 36 38 17 41 35 57 18 6 4 63 2 25
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
#> [13] FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE
#> [25] TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE
#> [37] FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE
#> [49] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE FALSE FALSE
#> [61] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
#> [73] FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE
#> [85] FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
#> [97] FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
#> [109] FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
#> [121] FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
#> [133] TRUE FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
#> [145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE
#> [157] FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
#> [169] TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
#> [181] TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [193] FALSE 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: 3 × 49
#> date hospita…¹ ward_…² ward_…³ ward_…⁴ age gender patie…⁵ mo
#> <date> <fct> <lgl> <lgl> <lgl> <dbl> <chr> <chr> <mo>
#> 1 2002-07-23 B TRUE FALSE FALSE 51 M F35553 B_STPHY_AURS
#> 2 2002-07-23 B TRUE FALSE FALSE 51 M F35553 B_STPHY_AURS
#> 3 2002-07-16 D FALSE FALSE TRUE 78 M 241328 B_STPHY_CONS
#> # … with 40 more variables: PEN <rsi>, OXA <rsi>, FLC <rsi>, AMX <rsi>,
#> # 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>, and …
#> # ℹ Use `colnames()` to see all variable names
# 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 = 2000,
replace = TRUE)) %>%
group_by(condition) %>%
mutate(new_episode = is_new_episode(date, 365)) %>%
select(patient_id, date, condition, new_episode)
df %>%
group_by(hospital_id, patient_id) %>%
transmute(date,
patient_id,
new_index = get_episode(date, 60),
new_logical = is_new_episode(date, 60))
df %>%
group_by(hospital_id) %>%
summarise(n_patients = n_distinct(patient_id),
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)))
# 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_id, mo) %>%
filter(is_new_episode(date, 365)) %>%
ungroup()
identical(x, y)
# 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_id, mo, hospital_id, ward_icu) %>%
mutate(flag_episode = is_new_episode(date, 365)) %>%
select(group_vars(.), flag_episode)
}
#> Error in mutate(., condition = sample(x = c("A", "B", "C"), size = 2000, replace = TRUE)): Problem while computing `condition = sample(x = c("A", "B", "C"), size =
#> 2000, replace = TRUE)`.
#> ✖ `condition` must be size 200 or 1, not 2000.
# }