Introduction to epidemiological case definitions with diyar

Many epidemiological case definitions distinguish new occurrences from repeat events. This is commonly done by specifying a period from an index event within which other events are considered part of the same occurrence.

This vignette introduces how this is done with episodes().

We begin by reviewing event_dt below. This is a sample dataset of 11 infection events and includes data on the location and type of infection.

tmp.seed <- 3
event_dt <- seq(from = as.Date("2021-01-01"), to = as.Date("2021-01-11"), by = 1)
event_dt <- data.frame(date = event_dt)
event_dt$attr_1 <- 
  c("BSI", "UTI", "RTI", "RTI", "BSI", "BSI", "BSI", "RTI", "RTI", "BSI", "RTI")
event_dt$attr_2 <- 
  c("Ward 1", "Ward 1", "Ward 3", "Ward 3", "Ward 2", "Ward 2", 
    "Ward 1", "Ward 1", "Ward 3","Ward 3", "Ward 2")
event_dt$dur <- 10
event_dt$dur[event_dt$attr_1 == 'UTI'] <- 7
event_dt$dur[event_dt$attr_1 == 'RTI'] <- 3

event_dt
#>          date attr_1 attr_2 dur
#> 1  2021-01-01    BSI Ward 1  10
#> 2  2021-01-02    UTI Ward 1   7
#> 3  2021-01-03    RTI Ward 3   3
#> 4  2021-01-04    RTI Ward 3   3
#> 5  2021-01-05    BSI Ward 2  10
#> 6  2021-01-06    BSI Ward 2  10
#> 7  2021-01-07    BSI Ward 1  10
#> 8  2021-01-08    RTI Ward 1   3
#> 9  2021-01-09    RTI Ward 3   3
#> 10 2021-01-10    BSI Ward 3  10
#> 11 2021-01-11    RTI Ward 2   3

The simplest implementations of episodes() are shown below. Episodes can also be created separately within subsets (strata) of dataset.

# A single episode  
event_dt$ep1a <- episodes(
  event_dt$date, episode_type = "fixed", group_stats = 'case_nm')
# Episodes created separately by infection type
event_dt$ep1b <- episodes(
  event_dt$date, episode_type = "fixed", group_stats = 'case_nm', 
  strata = event_dt$attr_1)
# Episodes created separately by location
event_dt$ep1c <- episodes(
  event_dt$date, episode_type = "fixed", group_stats = 'case_nm', 
  strata = event_dt$attr_2)

event_dt
#>          date attr_1 attr_2 dur    ep1a    ep1b    ep1c
#> 1  2021-01-01    BSI Ward 1  10 E.1 (C) E.1 (C) E.1 (C)
#> 2  2021-01-02    UTI Ward 1   7 E.1 (D) E.2 (S) E.1 (D)
#> 3  2021-01-03    RTI Ward 3   3 E.1 (D) E.3 (C) E.3 (C)
#> 4  2021-01-04    RTI Ward 3   3 E.1 (D) E.3 (D) E.3 (D)
#> 5  2021-01-05    BSI Ward 2  10 E.1 (D) E.1 (D) E.5 (C)
#> 6  2021-01-06    BSI Ward 2  10 E.1 (D) E.1 (D) E.5 (D)
#> 7  2021-01-07    BSI Ward 1  10 E.1 (D) E.1 (D) E.1 (D)
#> 8  2021-01-08    RTI Ward 1   3 E.1 (D) E.3 (D) E.1 (D)
#> 9  2021-01-09    RTI Ward 3   3 E.1 (D) E.3 (D) E.3 (D)
#> 10 2021-01-10    BSI Ward 3  10 E.1 (D) E.1 (D) E.3 (D)
#> 11 2021-01-11    RTI Ward 2   3 E.1 (D) E.3 (D) E.5 (D)

episodes() creates a group identifier for each occurrence. This occurrence is referred to as an episode. The index event is categorised as the case (C), while other events are categorised as duplicates (D) of that case. The group identifier is an S4 (epid) class with useful information about each group in its slots. These information can be accessed with as.data.frame() or as.list().

We can implement more complex case definitions by changing different aspects of the episode tracking process. These are explained below.

Type of episodes

Fixed episodes

These are episodes where a fixed period from the index case is used to distinguish one occurrence of an outcome from another. Events that occur outside of this fixed period (relative to the index event) are classified as separate episodes and given different group IDs.

In the example below, every event after the fourth day will be assigned to a different episode, each with its own case event.

# 3-day difference fixed episodes 
event_dt$ep2 <- episodes(
  event_dt$date, case_length = 3, episode_type = "fixed", 
  group_stats = 'case_nm')

Rolling episodes

For rolling episodes, the duration distinguishing one occurrence from another is not fixed. Instead, it is continuously extended if a new event falls within the recurrence window. This chaining process continues indefinitely until there are no more events within the recurrence window or it is stopped by the rolls_max argument.

# 3-day difference rolling episodes 
event_dt$ep3 <- episodes(
  event_dt$date, case_length = 3, episode_type = "rolling", 
  group_stats = 'case_nm')
event_dt
#>          date attr_1 attr_2 dur    ep1a    ep1b    ep1c     ep2     ep3
#> 1  2021-01-01    BSI Ward 1  10 E.1 (C) E.1 (C) E.1 (C) E.1 (C) E.1 (C)
#> 2  2021-01-02    UTI Ward 1   7 E.1 (D) E.2 (S) E.1 (D) E.1 (D) E.1 (D)
#> 3  2021-01-03    RTI Ward 3   3 E.1 (D) E.3 (C) E.3 (C) E.1 (D) E.1 (D)
#> 4  2021-01-04    RTI Ward 3   3 E.1 (D) E.3 (D) E.3 (D) E.1 (D) E.1 (D)
#> 5  2021-01-05    BSI Ward 2  10 E.1 (D) E.1 (D) E.5 (C) E.5 (C) E.1 (R)
#> 6  2021-01-06    BSI Ward 2  10 E.1 (D) E.1 (D) E.5 (D) E.5 (D) E.1 (D)
#> 7  2021-01-07    BSI Ward 1  10 E.1 (D) E.1 (D) E.1 (D) E.5 (D) E.1 (D)
#> 8  2021-01-08    RTI Ward 1   3 E.1 (D) E.3 (D) E.1 (D) E.5 (D) E.1 (R)
#> 9  2021-01-09    RTI Ward 3   3 E.1 (D) E.3 (D) E.3 (D) E.9 (C) E.1 (D)
#> 10 2021-01-10    BSI Ward 3  10 E.1 (D) E.1 (D) E.3 (D) E.9 (D) E.1 (D)
#> 11 2021-01-11    RTI Ward 2   3 E.1 (D) E.3 (D) E.5 (D) E.9 (D) E.1 (R)

In the example above, we see the introduction of recurrent events (R). A recurrent event is the first event within the recurrent window and categorised separately from duplicate events (D).

Note that case_length and recurrence_length are interpreted as unit-differences. For example, a case_length of 3 is means a difference of 3 units (see help(epid_unit)) between event dates, not 3 calendar days between events.

Selecting index events

We can control which record is used as the reference point (i.e. index) to check for others within its window of occurrence or recurrence. By default, this is always the earliest event within a strata. However, there are several ways to change this to suit different case definitions.

Direction of episode tracking

from_last determines if episodes tracking proceeds from the earliest to the most recent event or vice versa. Consequentially, this allows the selection of either the earliest or most recent event as the index event.

# 3-day difference rolling episodes tracked in reverse
event_dt$ep5 <- episodes(
  event_dt$date, 3, episode_type = "rolling", from_last = TRUE, 
  group_stats = 'case_nm')

Custom sorting

A custom preference for selecting index events can be provided to custom_sort. To demonstrate this, the example below uses "RTI" infections as index events and then track episode forward or backward in time from the selected index event.

Custom sorting and the direction of episode tracking are implemented separately. This allows both features to be combined to achieve specific outcomes or case definitions. As a result, the episodes which event belongs to can change in line with the requested settings.

# Custom preference
custom_pref <- ifelse(event_dt$attr_1 == "RTI", 1, 2)
# Track episode forward in time from index events
event_dt$ep6 <- episodes(
  event_dt$date, 3, custom_sort = custom_pref, skip_order = 1, 
  group_stats = 'case_nm')
# Track episode backward in time from index events
event_dt$ep7 <- episodes(
  event_dt$date, 3, custom_sort = custom_pref, skip_order = 1, from_last = TRUE, 
   group_stats = 'case_nm')

event_dt
#>          date attr_1 attr_2 dur    ep1a    ep1b    ep1c     ep2     ep3
#> 1  2021-01-01    BSI Ward 1  10 E.1 (C) E.1 (C) E.1 (C) E.1 (C) E.1 (C)
#> 2  2021-01-02    UTI Ward 1   7 E.1 (D) E.2 (S) E.1 (D) E.1 (D) E.1 (D)
#> 3  2021-01-03    RTI Ward 3   3 E.1 (D) E.3 (C) E.3 (C) E.1 (D) E.1 (D)
#> 4  2021-01-04    RTI Ward 3   3 E.1 (D) E.3 (D) E.3 (D) E.1 (D) E.1 (D)
#> 5  2021-01-05    BSI Ward 2  10 E.1 (D) E.1 (D) E.5 (C) E.5 (C) E.1 (R)
#> 6  2021-01-06    BSI Ward 2  10 E.1 (D) E.1 (D) E.5 (D) E.5 (D) E.1 (D)
#> 7  2021-01-07    BSI Ward 1  10 E.1 (D) E.1 (D) E.1 (D) E.5 (D) E.1 (D)
#> 8  2021-01-08    RTI Ward 1   3 E.1 (D) E.3 (D) E.1 (D) E.5 (D) E.1 (R)
#> 9  2021-01-09    RTI Ward 3   3 E.1 (D) E.3 (D) E.3 (D) E.9 (C) E.1 (D)
#> 10 2021-01-10    BSI Ward 3  10 E.1 (D) E.1 (D) E.3 (D) E.9 (D) E.1 (D)
#> 11 2021-01-11    RTI Ward 2   3 E.1 (D) E.3 (D) E.5 (D) E.9 (D) E.1 (R)
#>         ep5     ep6      ep7
#> 1  E.11 (R) E.1 (S) E.04 (D)
#> 2  E.11 (D) E.2 (S) E.04 (D)
#> 3  E.11 (D) E.3 (C) E.04 (D)
#> 4  E.11 (R) E.3 (D) E.04 (C)
#> 5  E.11 (D) E.3 (D) E.05 (S)
#> 6  E.11 (D) E.3 (D) E.06 (S)
#> 7  E.11 (R) E.7 (S) E.07 (S)
#> 8  E.11 (D) E.8 (C) E.11 (D)
#> 9  E.11 (D) E.8 (D) E.11 (D)
#> 10 E.11 (D) E.8 (D) E.11 (D)
#> 11 E.11 (C) E.8 (D) E.11 (C)

It can also be useful to pair skip_order with custom_sort. skip_order will stop the episode tracking process when all events up to rank-n of your custom_sort has been tracked.

Episode duration

Episode duration is primarily controlled by case_length. However, episodes() offers more flexible ways to define episode duration and recurrence periods for complex case definitions.

For example;

  • A different duration for recurrence from the case_length can be passed to recurrence_length.
  • A range ahead or before, and inclusive or not, of the index event can be used through a number_line() object.
  • Multiple durations from an index event can also be checked by providing a list of lengths or ranges as shown in the example below.
  • A different case_length or recurrence_length can be used for different events.

Each of these options for controlling episode duration and recurrence periods can be combined with preferential selection of index events in order to implement a variety of case definitions.

# 1-day difference occurrence period with 4-day difference recurrence periods
event_dt$ep8i <- episodes(
  event_dt$date, case_length = 1, recurrence_length = 4,  
  episode_type = "rolling", group_stats = 'case_nm')

# Rolling episodes with different periods of 
# occurrence and recurrence based on infection type
event_dt$ep8ii <- episodes(
  event_dt$date, case_length = event_dt$dur, episode_type = "rolling",
  group_stats = 'case_nm')

# Fixed episode within +/- 3 day-difference occurrence period from a
# preferentially selected index event 
event_dt$ep8ii <- episodes(
  event_dt$date, case_length = number_line(-3, 3), group_stats = 'case_nm', 
  episode_type = "fixed", custom_sort = custom_pref, skip_order = 1)

# Fixed episodes using occurrence windows of:
# -2 to -1, 4 to 5, and 7 to 9 days from the index event.
# Events falling between these windows are skipped.
event_dt$ep9 <- episodes(
  event_dt$date,  group_stats = 'case_nm',
  case_length = list(number_line(-2, -1), number_line(4, 5), number_line(7, 9)), 
  episode_type = "fixed", custom_sort = custom_pref, skip_order = 1, 
  skip_if_b4_lengths = TRUE)
event_dt
#>          date attr_1 attr_2 dur    ep1a    ep1b    ep1c     ep2     ep3
#> 1  2021-01-01    BSI Ward 1  10 E.1 (C) E.1 (C) E.1 (C) E.1 (C) E.1 (C)
#> 2  2021-01-02    UTI Ward 1   7 E.1 (D) E.2 (S) E.1 (D) E.1 (D) E.1 (D)
#> 3  2021-01-03    RTI Ward 3   3 E.1 (D) E.3 (C) E.3 (C) E.1 (D) E.1 (D)
#> 4  2021-01-04    RTI Ward 3   3 E.1 (D) E.3 (D) E.3 (D) E.1 (D) E.1 (D)
#> 5  2021-01-05    BSI Ward 2  10 E.1 (D) E.1 (D) E.5 (C) E.5 (C) E.1 (R)
#> 6  2021-01-06    BSI Ward 2  10 E.1 (D) E.1 (D) E.5 (D) E.5 (D) E.1 (D)
#> 7  2021-01-07    BSI Ward 1  10 E.1 (D) E.1 (D) E.1 (D) E.5 (D) E.1 (D)
#> 8  2021-01-08    RTI Ward 1   3 E.1 (D) E.3 (D) E.1 (D) E.5 (D) E.1 (R)
#> 9  2021-01-09    RTI Ward 3   3 E.1 (D) E.3 (D) E.3 (D) E.9 (C) E.1 (D)
#> 10 2021-01-10    BSI Ward 3  10 E.1 (D) E.1 (D) E.3 (D) E.9 (D) E.1 (D)
#> 11 2021-01-11    RTI Ward 2   3 E.1 (D) E.3 (D) E.5 (D) E.9 (D) E.1 (R)
#>         ep5     ep6      ep7    ep8i   ep8ii     ep9
#> 1  E.11 (R) E.1 (S) E.04 (D) E.1 (C) E.3 (D) E.3 (D)
#> 2  E.11 (D) E.2 (S) E.04 (D) E.1 (D) E.3 (D) E.3 (D)
#> 3  E.11 (D) E.3 (C) E.04 (D) E.1 (R) E.3 (C) E.3 (C)
#> 4  E.11 (R) E.3 (D) E.04 (C) E.1 (D) E.3 (D) E.4 (C)
#> 5  E.11 (D) E.3 (D) E.05 (S) E.1 (D) E.3 (D) E.5 (S)
#> 6  E.11 (D) E.3 (D) E.06 (S) E.1 (D) E.3 (D) E.6 (S)
#> 7  E.11 (R) E.7 (S) E.07 (S) E.1 (R) E.8 (D) E.3 (D)
#> 8  E.11 (D) E.8 (C) E.11 (D) E.1 (D) E.8 (C) E.3 (D)
#> 9  E.11 (D) E.8 (D) E.11 (D) E.1 (D) E.8 (D) E.4 (D)
#> 10 E.11 (D) E.8 (D) E.11 (D) E.1 (D) E.8 (D) E.3 (D)
#> 11 E.11 (C) E.8 (D) E.11 (C) E.1 (R) E.8 (D) E.3 (D)

Episodes from intervals

Episodes can be built from events with start and end times (event intervals) instead of a single point (date, time, numeric) in time. It should be noted that the reference point for event intervals is their end point. Therefore, a case_length of 1 would be interpreted as 1-unit difference after the end point of the event.

By default, events that occur within (but excluding the end point of) index event interval will not be captured. To capture them, use index_window() to get the range required to capture such events, and supply it to case_length or recurrence_length. See the examples below.

# Sample hospital stays
data(hospital_admissions)
event_int <- hospital_admissions['rd_id']
event_int$admin_period <- number_line(
    hospital_admissions$admin_dt, hospital_admissions$discharge_dt)
                                  
# Group overlapping hospital stays
event_int$ep1 <- episodes(
  date = event_int$admin_period, 
  case_length = index_window(event_int$admin_period),
  group_stats = 'case_nm')

# Group overlapping hospital stays and those within
# 20-day difference after the end point of the index hospital stay 
event_int$ep2 <- episodes(
  date = event_int$admin_period, 
  case_length = expand_number_line(
    index_window(event_int$admin_period), 20, "right"))
event_int
#>    rd_id             admin_period     ep1                              ep2
#> 1      1 2019-01-01 == 2019-01-01 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 2      2 2019-01-01 -> 2019-01-10 E.2 (C) E.2 2019-01-01 -> 2019-01-31 (C)
#> 3      3 2019-01-10 -> 2019-01-13 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 4      4 2019-01-05 -> 2019-01-06 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 5      5 2019-01-05 -> 2019-01-15 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 6      6 2019-01-07 -> 2019-01-15 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 7      7 2019-01-04 -> 2019-01-13 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 8      8 2019-01-20 -> 2019-01-30 E.8 (C) E.2 2019-01-01 -> 2019-01-31 (D)
#> 9      9 2019-01-26 -> 2019-01-31 E.8 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 10    10 2019-01-01 -> 2019-01-10 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 11    11 2019-01-20 -> 2019-01-30 E.8 (D) E.2 2019-01-01 -> 2019-01-31 (D)

Overlap methods for event intervals

We can request that only event intervals which overlap in specific ways be built into the same episode. The overlap methods are passed to case_overlap_methods and recurrence_overlap_methods.

Currently, diyar recognises 7 main mutually exclusive ways a pair of intervals can overlap. Multiple methods can be requested by separating each with a vertical bar. Please see vignette("number_line") and help(overlaps) for more details about this.

# Wrapper function for a fixed episode
episodes_wf <- function(x){
  epids <- episodes(date = event_int$admin_period, 
                    sn = event_int$rd_id, 
                    case_length = index_window(event_int$admin_period), 
                    case_overlap_methods = x,
                    group_stats = 'case_nm')
  return(epids)
}

# Methods 
methods <- list(
  # Identical intervals
  exact = "exact",
  # Intervals with their start or end points within another
  across = "across",
  # Intervals with aligned start points
  aligns_start = "aligns_start",
  # Intervals with aligned end points
  aligns_end = "aligns_end",
  # Intervals with start points that align with the end point of another, and vice versa
  chain = "chain",
  # Intervals occurring completely within others
  inbetween = "inbetween",
  # A combination of `chain` and `inbetween` methods
  cb1 = "chain|inbetween",
  # A combination of `exact`, `chain` and `inbetween` methods
  cb2 = "exact|chain|inbetween",
  # A combination of `across`, `chain` and `aligns_end` methods
  cb3 = "across|chain|aligns_end"
)

epids <- event_int['admin_period']
for(m in methods){
  epids[[m]] <- episodes_wf(m)
}

# Use `schema()` to visualise each.
epids
#>                admin_period   exact   across aligns_start aligns_end    chain
#> 1  2019-01-01 == 2019-01-01 E.1 (C) E.01 (C)     E.02 (D)   E.01 (C) E.01 (C)
#> 2  2019-01-01 -> 2019-01-10 E.2 (C) E.02 (C)     E.02 (C)   E.02 (C) E.02 (C)
#> 3  2019-01-10 -> 2019-01-13 E.3 (C) E.03 (C)     E.03 (C)   E.07 (D) E.02 (D)
#> 4  2019-01-05 -> 2019-01-06 E.4 (C) E.04 (C)     E.05 (D)   E.04 (C) E.04 (C)
#> 5  2019-01-05 -> 2019-01-15 E.5 (C) E.02 (D)     E.05 (C)   E.05 (C) E.05 (C)
#> 6  2019-01-07 -> 2019-01-15 E.6 (C) E.02 (D)     E.06 (C)   E.05 (D) E.06 (C)
#> 7  2019-01-04 -> 2019-01-13 E.7 (C) E.02 (D)     E.07 (C)   E.07 (C) E.07 (C)
#> 8  2019-01-20 -> 2019-01-30 E.8 (C) E.08 (C)     E.08 (C)   E.08 (C) E.08 (C)
#> 9  2019-01-26 -> 2019-01-31 E.9 (C) E.08 (D)     E.09 (C)   E.09 (C) E.09 (C)
#> 10 2019-01-01 -> 2019-01-10 E.2 (D) E.10 (C)     E.10 (C)   E.10 (C) E.10 (C)
#> 11 2019-01-20 -> 2019-01-30 E.8 (D) E.11 (C)     E.11 (C)   E.11 (C) E.11 (C)
#>    inbetween chain|inbetween exact|chain|inbetween across|chain|aligns_end
#> 1   E.01 (C)        E.01 (C)               E.1 (C)                E.01 (C)
#> 2   E.02 (C)        E.02 (C)               E.2 (C)                E.02 (C)
#> 3   E.05 (D)        E.02 (D)               E.2 (D)                E.02 (D)
#> 4   E.02 (D)        E.02 (D)               E.2 (D)                E.04 (C)
#> 5   E.05 (C)        E.05 (C)               E.5 (C)                E.02 (D)
#> 6   E.06 (C)        E.06 (C)               E.6 (C)                E.02 (D)
#> 7   E.07 (C)        E.07 (C)               E.7 (C)                E.02 (D)
#> 8   E.08 (C)        E.08 (C)               E.8 (C)                E.08 (C)
#> 9   E.09 (C)        E.09 (C)               E.9 (C)                E.08 (D)
#> 10  E.10 (C)        E.10 (C)               E.2 (D)                E.10 (C)
#> 11  E.11 (C)        E.11 (C)               E.8 (D)                E.11 (C)

Combining case_overlap_methods and recurrence_overlap_methods with case_length and recurrence__length gives a lot of flexibility in determining how event intervals should overlap. For example, the examples below are different ways of the achieving the same outcome of linking the first two records.

# Group hospital stays with the same start point (aligns_start) or 
# same start and end points (exact).
event_int$ep3 <- episodes(
  date = event_int$admin_period, 
  case_length = index_window(event_int$admin_period),
  case_overlap_methods = 'aligns_start|exact', group_stats = 'case_nm')

# Group hospital stays that overlap at any point within 9-day difference
# before the end point of the index event interval.
event_int$ep4 <- episodes(
  date = event_int$admin_period, case_length = number_line(-9,-9),
  case_overlap_methods = 'overlap', group_stats = 'case_nm')

event_int
#>    rd_id             admin_period     ep1                              ep2
#> 1      1 2019-01-01 == 2019-01-01 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 2      2 2019-01-01 -> 2019-01-10 E.2 (C) E.2 2019-01-01 -> 2019-01-31 (C)
#> 3      3 2019-01-10 -> 2019-01-13 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 4      4 2019-01-05 -> 2019-01-06 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 5      5 2019-01-05 -> 2019-01-15 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 6      6 2019-01-07 -> 2019-01-15 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 7      7 2019-01-04 -> 2019-01-13 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 8      8 2019-01-20 -> 2019-01-30 E.8 (C) E.2 2019-01-01 -> 2019-01-31 (D)
#> 9      9 2019-01-26 -> 2019-01-31 E.8 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 10    10 2019-01-01 -> 2019-01-10 E.2 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#> 11    11 2019-01-20 -> 2019-01-30 E.8 (D) E.2 2019-01-01 -> 2019-01-31 (D)
#>        ep3     ep4
#> 1  E.2 (D) E.2 (D)
#> 2  E.2 (C) E.2 (C)
#> 3  E.3 (C) E.3 (C)
#> 4  E.5 (D) E.5 (D)
#> 5  E.5 (C) E.5 (C)
#> 6  E.6 (C) E.6 (C)
#> 7  E.7 (C) E.7 (C)
#> 8  E.8 (C) E.8 (C)
#> 9  E.9 (C) E.9 (C)
#> 10 E.2 (D) E.2 (D)
#> 11 E.8 (D) E.8 (D)

Additional match criteria

episodes() can apply additional match criteria when building episodes. This means only events that fall within requested period of occurrence or recurrence, and also meets the specified conditions will be assigned to the same episode. The match criteria are created as a sub_criteria object and passed to case_sub_criteria and recurrence_sub_criteria. Please see vignette("links") for more details about sub_criteria objects.

In the example below, we specify that only index events that are an "RTI" or occurred in "Ward 3" can start an episode.

# Sub-criteria 1 - RTI or Ward 2 index events only
match_funx1 <- function(x, y) y == "RTI"
match_funx2 <- function(x, y) y == "Ward 2"
sub_cri_1 <- sub_criteria(
  event_dt$attr_1, event_dt$attr_2, operator = "or", 
  match_funcs = c(match_funx1, match_funx2))

event_dt$ep10 <- episodes(
  event_dt$date, case_length = 5, case_sub_criteria = sub_cri_1)

schema(event_dt$ep10, seed = tmp.seed, show_labels = c("length_arrow"),
       custom_label = paste0(event_dt$attr_1, " in ", event_dt$attr_2))

Alternatively, we can request that "BSI" events or those that occurred in "Ward 1" should not be linked to any episode. Note that they will still be able to start episodes of their own unless otherwise specified as with event_dt$ep10.

# Sub-criteria 2 - Exclude BSI and Ward 1 events 
match_funx3 <- function(x, y) !(x$attr_1 == "BSI" | x$attr_2 == "Ward 1")
sub_cri_2 <- sub_criteria(
  attrs(attr_1 = event_dt$attr_1, 
        attr_2 = event_dt$attr_2), 
        operator = "and", match_funcs = match_funx3, equal_funcs = diyar::true)

event_dt$ep11 <- episodes(
  event_dt$date, case_length = 5, case_sub_criteria = sub_cri_2)

schema(event_dt$ep11, seed = tmp.seed, show_labels = c("length_arrow"),
       custom_label = paste0(event_dt$attr_1, " in ", event_dt$attr_2))

We can even combine both conditions into a nested condition.

combined_sub_cri <- sub_criteria(sub_cri_1, sub_cri_2, operator = "and")
event_dt$ep12 <- episodes(
  event_dt$date, case_length = 5, case_sub_criteria = combined_sub_cri)

schema(event_dt$ep12, seed = tmp.seed, show_labels = c("length_arrow"),
       custom_label = paste0(event_dt$attr_1, " in ", event_dt$attr_2))