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] 64 9 31 9 1 21 40 41 9 10 15 10 60 2 15 45 51 8 60 28 26 38 54 18 48
#> [26] 49 2 66 1 20 22 68 12 43 20 61 64 14 14 65 29 37 68 1 53 67 6 24 27 68
#> [51] 8 15 1 20 13 57 1 1 32 64 50 8 14 8 63 4 65 48 21 36 4 46 8 8 61
#> [76] 36 10 43 58 11 48 32 24 38 16 12 35 21 25 2 66 9 45 61 22 36 11 21 56 44
#> [101] 42 38 8 24 54 43 56 41 60 43 29 46 19 32 13 15 42 59 62 6 9 65 31 20 23
#> [126] 19 36 24 12 5 64 42 17 34 15 51 19 68 5 24 17 27 63 53 25 39 62 13 63 47
#> [151] 33 42 3 45 65 32 55 49 11 1 7 67 26 4 12 16 56 49 63 36 10 53 47 49 12
#> [176] 59 49 33 57 61 52 43 30 40 11 22 17 62 39 66 50 56 53 41 1 10 46 29 31 30
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#> [1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
#> [13] FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
#> [25] FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
#> [37] FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE
#> [49] FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
#> [61] FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
#> [73] FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
#> [85] TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
#> [97] TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE
#> [109] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
#> [121] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
#> [133] TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
#> [145] TRUE TRUE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
#> [157] TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
#> [169] TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE
#> [181] TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
#> [193] FALSE FALSE FALSE FALSE TRUE TRUE 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: 1 × 46
#> date patient age gender ward mo PEN OXA FLC AMX
#> <date> <chr> <dbl> <chr> <chr> <mo> <rsi> <rsi> <rsi> <rsi>
#> 1 2002-06-19 402950 53 F Clinical B_STPHY_HMNS R NA S 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 E84349 2016-11-25 A FALSE
#> 2 F24801 2004-02-10 B FALSE
#> 3 889500 2009-05-08 B FALSE
#> 4 E32B78 2004-02-02 A FALSE
#> 5 8FD3C4 2002-02-24 A FALSE
#> 6 701066 2006-10-30 A FALSE
#> 7 447543 2011-03-06 A FALSE
#> 8 E27710 2011-05-19 B FALSE
#> 9 984417 2004-01-09 A FALSE
#> 10 858515 2004-05-11 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 [183]
#> ward date patient new_index new_logical
#> <chr> <date> <chr> <dbl> <lgl>
#> 1 ICU 2016-11-25 E84349 1 TRUE
#> 2 ICU 2004-02-10 F24801 1 TRUE
#> 3 Clinical 2009-05-08 889500 1 TRUE
#> 4 ICU 2004-02-02 E32B78 1 TRUE
#> 5 Clinical 2002-02-24 8FD3C4 1 TRUE
#> 6 Clinical 2006-10-30 701066 1 TRUE
#> 7 Clinical 2011-03-06 447543 1 TRUE
#> 8 Clinical 2011-05-19 E27710 1 TRUE
#> 9 ICU 2004-01-09 984417 1 TRUE
#> 10 ICU 2004-05-11 858515 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 112 15 56 74
#> 2 ICU 59 14 35 42
#> 3 Outpatient 12 7 11 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 [191]
#> patient mo ward flag_episode
#> <chr> <mo> <chr> <lgl>
#> 1 E84349 B_ESCHR_COLI ICU TRUE
#> 2 F24801 B_STRPT_GRPB ICU TRUE
#> 3 889500 B_ESCHR_COLI Clinical TRUE
#> 4 E32B78 B_GEMLL_HMLY ICU TRUE
#> 5 8FD3C4 B_STPHY_CONS Clinical TRUE
#> 6 701066 B_STPHY_CONS Clinical TRUE
#> 7 447543 B_STRPT_DYSG Clinical TRUE
#> 8 E27710 B_STRPT_GRPB Clinical TRUE
#> 9 984417 B_STPHY_AURS ICU TRUE
#> 10 858515 B_STPHY_CONS ICU TRUE
#> # … with 190 more rows
# }