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] 9 46 28 50 35 57 44 17 9 56 15 7 12 51 27 22 32 14 29 44 55 54 12 3 15
#> [26] 46 6 5 47 12 40 5 51 16 40 46 26 37 1 50 3 23 13 11 19 25 54 36 49 15
#> [51] 63 19 55 54 5 62 11 55 6 50 60 44 52 45 57 40 10 46 19 2 6 2 12 19 2
#> [76] 37 7 16 47 59 49 24 57 19 25 48 43 28 59 56 59 41 4 23 25 58 56 32 43 34
#> [101] 33 42 33 17 7 48 12 50 48 15 49 8 34 59 25 49 29 35 61 58 23 63 59 25 40
#> [126] 57 46 20 21 26 16 12 16 7 27 8 21 38 48 8 26 52 51 41 24 42 60 11 52 31
#> [151] 36 26 24 54 20 53 23 61 52 57 25 55 43 63 30 38 47 60 5 62 3 61 5 27 14
#> [176] 37 12 25 63 54 30 39 1 15 55 5 55 34 38 63 20 18 24 31 12 32 20 2 7 32
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
#> [13] FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
#> [25] FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE
#> [37] FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE
#> [49] TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE
#> [61] TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE
#> [73] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [85] FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE
#> [97] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
#> [109] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
#> [121] TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
#> [133] FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE
#> [145] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
#> [157] FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
#> [169] FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
#> [181] FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
#> [193] FALSE TRUE FALSE TRUE FALSE TRUE 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 × 46
#> date patient age gender ward mo PEN OXA FLC AMX
#> <date> <chr> <dbl> <chr> <chr> <mo> <sir> <sir> <sir> <sir>
#> 1 2002-11-16 762305 87 F Clinical B_BCTRD_FRGL R NA NA R
#> 2 2002-11-16 762305 87 F Clinical B_PROTS_MRBL R NA NA NA
#> 3 2002-09-23 F35553 51 M ICU B_STPHY_AURS S NA S NA
#> # … 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 F24801 2004-02-10 A TRUE
#> 2 263940 2013-03-05 C FALSE
#> 3 E95747 2008-06-30 A FALSE
#> 4 414858 2014-08-29 A FALSE
#> 5 823896 2010-03-04 B FALSE
#> 6 976813 2016-05-09 A FALSE
#> 7 107DD1 2012-09-03 B TRUE
#> 8 89F819 2005-11-27 C FALSE
#> 9 89F578 2004-02-28 A FALSE
#> 10 329C35 2016-04-03 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 [182]
#> ward date patient new_index new_logical
#> <chr> <date> <chr> <dbl> <lgl>
#> 1 ICU 2004-02-10 F24801 1 TRUE
#> 2 Clinical 2013-03-05 263940 1 TRUE
#> 3 Clinical 2008-06-30 E95747 1 TRUE
#> 4 Clinical 2014-08-29 414858 1 TRUE
#> 5 Clinical 2010-03-04 823896 1 TRUE
#> 6 Clinical 2016-05-09 976813 1 TRUE
#> 7 Clinical 2012-09-03 107DD1 1 TRUE
#> 8 ICU 2005-11-27 89F819 1 TRUE
#> 9 Clinical 2004-02-28 89F578 1 TRUE
#> 10 Clinical 2016-04-03 329C35 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 124 15 57 81
#> 2 ICU 47 12 35 42
#> 3 Outpatient 11 6 10 10
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 [192]
#> patient mo ward flag_episode
#> <chr> <mo> <chr> <lgl>
#> 1 F24801 B_STRPT_GRPB ICU TRUE
#> 2 263940 B_STPHY_HMNS Clinical TRUE
#> 3 E95747 B_KLBSL_PNMN Clinical TRUE
#> 4 414858 B_ESCHR_COLI Clinical TRUE
#> 5 823896 B_STPHY_CONS Clinical TRUE
#> 6 976813 B_STPHY_AURS Clinical TRUE
#> 7 107DD1 B_STPHY_CONS Clinical TRUE
#> 8 89F819 B_CRYNB ICU TRUE
#> 9 89F578 B_STPHY_CONS Clinical TRUE
#> 10 329C35 B_ESCHR_COLI Clinical TRUE
#> # … with 190 more rows
# }