Skip to contents

Introduction

Here are the packages we need for this vignette:

In this vignette, we will walk through the basic procedures to collapse participants’ Urine Drug Screen (UDS) or Timeline Follow-Back (TLFB) data streams into quinary word summaries for downstream analysis. The basic steps are:

  1. calculate the induction delay (difference, in days, between the day a participant was randomized to a trial arm and the day that they received the first dose of study drug). The public.ctn0094data database has a) the day of randomization and b) the days for which study drugs were administered, relative to Day 0 (the day study consent was signed).
  2. partition the study days into weeks, then mark which weeks do not have a UDS collection event (these will be marked as “missing”).
  3. summarize the UDS data within each participant by week into symbols using quinary word logic, then collapse the symbols into a participant-specific “word”.

Rather than perform these calculations on all ~3600 participants included in public.ctn0094data and public.ctn0094extra, we will use the following 10 participants as examples:

examplePeople_int <- c(1L, 163L, 210L, 242L, 4L, 17L, 13L, 1103L, 233L, 2089L)

Calculate Induction Delay

As we mentioned above, the induction delay is the difference, in days, between the day a participant was randomized to a trial arm and the day that they received the first dose of study drug. The reason we need this is because any UDS collected or TLFB recorded on or before the induction day must be considered pre-treatment, even if the participant has already been assigned to a treatment arm.

Data Setup

The randomization day includes a first and second randomization day for all participants. For CTN-0027 and CTN-0051, these days are the same; for CTN-0030 these days are different. In order to find the induction delay, we only need the first randomization event

data_ls <- loadRawData(c("randomization", "treatment"))

data_ls$randomization <-
  data_ls$randomization %>%
  select(who, when, treatment, randomized = which) %>%
  # Remove second randomization events
  filter(randomized != 2) %>% 
  # Retain example participants
  filter(who %in% examplePeople_int)

data_ls$treatment <- 
  data_ls$treatment %>% 
  # Retain example participants
  filter(who %in% examplePeople_int)

Let’s inspect our results:

data_ls$randomization
#> # A tibble: 9 × 4
#>     who  when treatment            randomized
#>   <int> <dbl> <fct>                <fct>     
#> 1     4     1 Inpatient NR-NTX     1         
#> 2    13     4 Outpatient BUP       1         
#> 3    17    13 Methadone            1         
#> 4   163     6 Outpatient BUP       1         
#> 5   210     5 Methadone            1         
#> 6   233     4 Outpatient BUP       1         
#> 7   242     8 Inpatient NR-NTX     1         
#> 8  1103     4 Outpatient BUP + EMM 1         
#> 9  2089     2 Outpatient BUP       1

data_ls$treatment
#> # A tibble: 1,008 × 3
#>      who amount  when
#>    <int>  <dbl> <dbl>
#>  1     4      1     1
#>  2     4      1    28
#>  3     4      1    53
#>  4     4      1    81
#>  5     4      1   109
#>  6     4      1   141
#>  7    13      8     4
#>  8    13     12     5
#>  9    13     16     6
#> 10    13     16     7
#> # ℹ 998 more rows

Mark Study Days with Administered Treatment Drugs

Some participants were marked as receiving 0mg of the study drug to which they were assigned. We want to mark all the days where a participant actually got some of the assigned study drug.

treatTimeLong_df <-
  # Collapse Data List
  data_ls %>%
  reduce(.f = full_join, by = c("who", "when")) %>%
  as_tibble() %>%
  arrange(who, when) %>%
  # First pass: find any day with a dose of treatment drug
  rowwise() %>%
  mutate(
    treated = if_else(
      condition = !is.na(amount) & amount > 0,
      true = TRUE,
      false = FALSE,
      missing = FALSE
    )
  )

# Inspect results
treatTimeLong_df
#> # A tibble: 1,009 × 6
#> # Rowwise: 
#>      who  when treatment        randomized amount treated
#>    <int> <dbl> <fct>            <fct>       <dbl> <lgl>  
#>  1     4     1 Inpatient NR-NTX 1               1 TRUE   
#>  2     4    28 NA               NA              1 TRUE   
#>  3     4    53 NA               NA              1 TRUE   
#>  4     4    81 NA               NA              1 TRUE   
#>  5     4   109 NA               NA              1 TRUE   
#>  6     4   141 NA               NA              1 TRUE   
#>  7    13     4 Outpatient BUP   1               8 TRUE   
#>  8    13     5 NA               NA             12 TRUE   
#>  9    13     6 NA               NA             16 TRUE   
#> 10    13     7 NA               NA             16 TRUE   
#> # ℹ 999 more rows

Measure Difference from Randomization Day to Day of First Dose

Some participants received their first non-zero dose of their assigned study drug days after they were assigned to a treatment arm. If the subject supplies a UDS sample positive for substances of interest or records a substance use event in the TLFB after they were assigned to treatment but before they received treatment, these should not count against the efficacy of the study drug. NOTE: under the “intent to treat” paradigm, this is not the case. For example, it is challenging for some patients to start treatment with Naltrexone, so evaluating treatment efficacy fairly often requires us to include this time after treatment assignment but before induction.

inductDelay_df <-
  treatTimeLong_df %>%
  # Find the day of the first treatment
  group_by(who) %>%
  arrange(when) %>%
  filter(treated) %>%
  slice(1) %>%
  mutate(treatStart = when) %>%
  select(who, when, treatStart) %>%
  # Add the first day back to the original data
  left_join(treatTimeLong_df, ., by = c("who", "when")) %>%
  group_by(who) %>%
  fill(treatStart, .direction = "updown") %>%
  # Calculate the delay
  filter(randomized == 1) %>%
  # This sets time to be missing if the induction was not observed
  mutate(inductDelay = treatStart - when) %>%
  select(who, treatment, inductDelay) %>%
  ungroup()

# Inspect results
inductDelay_df
#> # A tibble: 9 × 3
#>     who treatment            inductDelay
#>   <int> <fct>                      <dbl>
#> 1     4 Inpatient NR-NTX               0
#> 2    13 Outpatient BUP                 0
#> 3    17 Methadone                      0
#> 4   163 Outpatient BUP                 0
#> 5   210 Methadone                      0
#> 6   233 Outpatient BUP                 0
#> 7   242 Inpatient NR-NTX               5
#> 8  1103 Outpatient BUP + EMM           0
#> 9  2089 Outpatient BUP                 0

We can see that participant 242 received their first dose of Inpatient NR-NTX 5 days after they were assigned to that treatment arm.

Let’s clean up our environment before we move on:

rm(data_ls, treatTimeLong_df)

Partition Visit Weeks and Impute Missing Visits

Now that we have the study day for which each participant received their first dose of study drug (induction day), we can partition the study days into weeks before and after induction delay as pre-study / baseline period and study period, respectively.

“Backbone” Timeline of Clinical Protocol

If we want to know which visits were missed, we first need to know which visits were required by the protocols of the three studies.

CTN-0027 and CTN-0051 Protocols

This code will create a table with the subject ID, which trial they participated in, and a column of all possible trial contact days, from 30 days prior to consent to 24 weeks after. See the function documentation for CreateProtocolHistory() for more information.

start_int <- c(`27` = -30L, `51` = -30L)
end_int   <- c(`27` = 168L, `51` = 168L)
backbone2751_df <- 
  CreateProtocolHistory(
      start_vec = start_int,
      end_vec = end_int
    ) %>% 
  filter(who %in% examplePeople_int)

# Inspect
 backbone2751_df
#> # A tibble: 1,592 × 3
#>      who project  when
#>    <int> <chr>   <int>
#>  1    13 27        -30
#>  2    13 27        -29
#>  3    13 27        -28
#>  4    13 27        -27
#>  5    13 27        -26
#>  6    13 27        -25
#>  7    13 27        -24
#>  8    13 27        -23
#>  9    13 27        -22
#> 10    13 27        -21
#> # ℹ 1,582 more rows

CTN-0030 Protocol

Because the CTN-0030 protocol was adaptive, the amount of time requested to be spent in the study will change based on the participants’ reactions to the first phase of treatment. See the function documentation for CreateCTN30ProtocolHistory() for more information.

backbone30_df <-
    randomization %>%
    full_join(everybody, by = "who") %>%
    filter(project == "30") %>%
  filter(who %in% examplePeople_int) %>% 
    CreateCTN30ProtocolHistory() %>%
    mutate(project = "30") %>%
    select(who, project, when)

# Inspect
backbone30_df
#> # A tibble: 402 × 3
#>      who project  when
#>    <int> <chr>   <dbl>
#>  1     1 30        -30
#>  2     1 30        -29
#>  3     1 30        -28
#>  4     1 30        -27
#>  5     1 30        -26
#>  6     1 30        -25
#>  7     1 30        -24
#>  8     1 30        -23
#>  9     1 30        -22
#> 10     1 30        -21
#> # ℹ 392 more rows

backbone30_df %>% 
  group_by(who) %>% 
  summarise(lastDay = max(when))
#> # A tibble: 2 × 2
#>     who lastDay
#>   <int>   <dbl>
#> 1     1      98
#> 2  1103     242

We can clearly see that the protocol length for some participants in CTN-0030 was much longer than others.

Combined “Backbone”

We can now combine these two datasets and clean up our environment.

backbone_df <-
  bind_rows(backbone2751_df, backbone30_df) %>%
    arrange(who)

rm(backbone2751_df, backbone30_df, start_int, end_int)

Mark Missing Visits

Now that we know the start of treatment and range of expected participation days per protocol, we can take the “difference” to find out when participants were supposed to visit the clinic, but did not.

Data Setup

Let’s set up the data for our example people:

data_ls <- loadRawData(c("randomization", "visit"))

data_ls$randomization <-
  data_ls$randomization %>%
  select(who, when, treatment, randomized = which) %>%
  # Remove second randomization events
  filter(randomized != 2) %>% 
  # Retain example participants
  filter(who %in% examplePeople_int)

data_ls$visit <- 
  data_ls$visit %>% 
  filter(who %in% examplePeople_int)

Add First Randomization Day

timelineRand1_df <-
    data_ls$randomization %>%
    mutate(randomized = randomized == "1") %>%
    # Join to backbone and arrange within subject by day
    full_join(backbone_df, by = c("who", "when")) %>%
    group_by(who) %>%
    arrange(when, .by_group = TRUE) %>%
    select(who, project, when, randomized)

# Inspect
timelineRand1_df
#> # A tibble: 1,994 × 4
#> # Groups:   who [10]
#>      who project  when randomized
#>    <int> <chr>   <dbl> <lgl>     
#>  1     1 30        -30 NA        
#>  2     1 30        -29 NA        
#>  3     1 30        -28 NA        
#>  4     1 30        -27 NA        
#>  5     1 30        -26 NA        
#>  6     1 30        -25 NA        
#>  7     1 30        -24 NA        
#>  8     1 30        -23 NA        
#>  9     1 30        -22 NA        
#> 10     1 30        -21 NA        
#> # ℹ 1,984 more rows

Add on the Visit Days

timelineVisit1_df <-
    data_ls$visit %>%
    select(who, when, visit, status = what) %>%
    filter(status %in% c("visit", "final")) %>%
    mutate(visit = TRUE) %>%
    select(who, when, visit) %>%
    left_join(timelineRand1_df, ., by = c("who", "when"))

# Inspect
timelineVisit1_df
#> # A tibble: 1,994 × 5
#> # Groups:   who [10]
#>      who project  when randomized visit
#>    <int> <chr>   <dbl> <lgl>      <lgl>
#>  1     1 30        -30 NA         NA   
#>  2     1 30        -29 NA         NA   
#>  3     1 30        -28 NA         NA   
#>  4     1 30        -27 NA         NA   
#>  5     1 30        -26 NA         NA   
#>  6     1 30        -25 NA         NA   
#>  7     1 30        -24 NA         NA   
#>  8     1 30        -23 NA         NA   
#>  9     1 30        -22 NA         NA   
#> 10     1 30        -21 NA         NA   
#> # ℹ 1,984 more rows

Most (6 of 7 days, on average) of the data will be missing values (shown by NA) because participants were not expected to visit the clinic more than once per week.

Impute the Missing Visits

This function will assign visit values to “missing” roughly every seven days that a visit hasn’t been recorded. See the function documentation for MarkMissing() for more information. Note: this function can take a while to run if used on thousands of participants over dozens of weeks each.

timelineMissing1_df <- MarkMissing(timelineVisit1_df) 

Clean up the Results

We aren’t showing the results from the function above because they still need to be wrangled a bit.

derived_visitImputed <-
    timelineMissing1_df %>%
  mutate(visit = as.character(visit)) %>%
    replace_na(list(visit = "", visitYM = "")) %>%
    mutate(visitImputed = paste0(visit, visitYM)) %>%
    mutate(
        visitImputed = str_replace(
            visitImputed, pattern = "TRUETRUE", replacement = "Present"
        )
    ) %>%
    select(who, when, visitImputed) %>%
    filter(visitImputed != "") %>%
    ungroup()

# Inspect
derived_visitImputed
#> # A tibble: 256 × 3
#>      who  when visitImputed
#>    <int> <dbl> <chr>       
#>  1     1     0 Missing     
#>  2     1     7 Missing     
#>  3     1    14 Missing     
#>  4     1    21 Missing     
#>  5     1    28 Missing     
#>  6     1    35 Missing     
#>  7     1    42 Missing     
#>  8     1    49 Missing     
#>  9     1    56 Missing     
#> 10     1    63 Missing     
#> # ℹ 246 more rows

Let’s now clean up our environment:

rm(
  backbone_df, data_ls, timelineMissing1_df, timelineRand1_df, timelineVisit1_df
)

Summarize Weekly Substance Use Results

This step is the most involved, so we will only show the steps necessary to complete this task for our 10 example participants. However, the full workflow is available at . The general steps for randomized trial participants in this procedure are:

  1. Determine which treatment arm to which each participant was assigned. Recall that buprenorphine can be used both as a standard treatment and an illicit substance. Therefore, having a TLFB record of or UDS positive for buprenorphine for a particular study day should not automatically be counted as “substance misuse”.
  2. For the substance(s) of interest (in our case, opioids), indicate their presence (or absence) in participants’ data streams for each study visit day. This will result in a “long” data table with one row per participant per study visit and a logical value indicating if the participant used the substance(s) of interest on that day.
  3. Create a “ticker” of study days, then partition these days into study weeks. For an “intent to treat” analysis, day 0 should be the randomization day. Otherwise, day 0 should be the induction day calculated above. Note: we will use an “intent to treat” analysis from this point on.
  4. Summarize substance use within each study week using a quinary word. For many participants, they have at least one week wherein more than one UDS sample was supplied. Our combination logic to summarize detected weekly substance use is as follows: if all UDS samples within that study week are positive for the substance(s) of interest, then the week is marked “+”. If all UDS samples are negative for the substance(s) of interest, then the week is marked “-”. If there is a mixture, the week is marked “*”. If the participant was supposed to supply a urine sample in that week but did not, then the week is marked “o”. If the participant was not supposed to supply a urine sample in that week (and did not supply one), then the week is marked “_”.
  5. Group these weekly summaries into trial phase (baseline / pre-randomization, phase 1, or phase 2 [CTN-0030 only]) and collapse into a single string.

You may have participants who consented to join the trial but were never randomized to a treatment arm. For these individuals, if a “complete study summary” is required (that is, you must include all participants who consented, even if they never were randomized), we recommend creating a use pattern word which is either all “o” or all “_”.

Data Setup

First we build our data “backbone” as above (showing here to mirror the work done in the analysis script):

# CTN-0027 and CTN-0051
start_int <- c(`27` = -30L, `51` = -30L)
end_int   <- c(`27` = 168L, `51` = 168L) # 24 weeks
backbone2751_df <- CreateProtocolHistory(
    start_vec = start_int, end_vec = end_int
)

# CTN-0030
backbone30_df <-
    randomization %>%
    full_join(everybody, by = "who") %>%
    filter(project == "30") %>%
    CreateCTN30ProtocolHistory() %>%
    mutate(project = "30") %>%
    select(who, project, when)

# All Days
backbone_df <- bind_rows(
    backbone2751_df, backbone30_df
) %>%
    arrange(who) %>%
  mutate(project = factor(project, levels = c("27", "30", "51"))) %>% 
  filter(who %in% examplePeople_int)

rm(backbone2751_df, backbone30_df, start_int, end_int)

Now we create a data set that combines the treatment arm with the imputed visits data we calculated above.

randomized_df <-
  randomization %>%
  filter(who %in% examplePeople_int) %>% 
  mutate(randomized = as.integer(as.character(which))) %>%
  select(who, when, randomized) %>%
  left_join(everybody, by = "who") %>%
  filter( !(randomized == 2 & project %in% c("27", "51")) ) %>%
  select(-project)

udsUse2_df <-
    backbone_df %>%
    left_join(randomized_df, by = c("who", "when")) %>%
    left_join(derived_visitImputed, by = c("who", "when")) %>%
    left_join(uds, by = c("who", "when")) %>%
    # So we can use MarkUse() with UDS data (instead of all_drugs)
    mutate(source = "UDS")

This data set is an interesting one. It has one record per participant, per study day, per substance reported. So, because we are using UDS, this means that only rows which correspond to study visit days wherein a urine sample was supplied and positive for multiple substances will be duplicated. If we had used TLFB data instead, a data table in this form would be orders of magnitude longer, with many rows per participant per day. Here are visit days wherein substances were detected in the urine of participant 0017:

udsUse2_df %>% 
  filter(visitImputed == "Present") %>% 
  filter(!is.na(what)) %>% 
  filter(who == 17) %>% 
  print(n = nrow(.))
#> # A tibble: 41 × 7
#>      who project  when randomized visitImputed what           source
#>    <int> <fct>   <dbl>      <int> <chr>        <fct>          <chr> 
#>  1    17 27          0         NA Present      Amphetamine    UDS   
#>  2    17 27          0         NA Present      Benzodiazepine UDS   
#>  3    17 27          0         NA Present      Opioid         UDS   
#>  4    17 27         21         NA Present      Benzodiazepine UDS   
#>  5    17 27         28         NA Present      Benzodiazepine UDS   
#>  6    17 27         28         NA Present      Opioid         UDS   
#>  7    17 27         35         NA Present      Benzodiazepine UDS   
#>  8    17 27         35         NA Present      Opioid         UDS   
#>  9    17 27         49         NA Present      Opioid         UDS   
#> 10    17 27         56         NA Present      Benzodiazepine UDS   
#> 11    17 27         56         NA Present      Cocaine        UDS   
#> 12    17 27         56         NA Present      Opioid         UDS   
#> 13    17 27         63         NA Present      Benzodiazepine UDS   
#> 14    17 27         63         NA Present      Cocaine        UDS   
#> 15    17 27         63         NA Present      Opioid         UDS   
#> 16    17 27         63         NA Present      Thc            UDS   
#> 17    17 27         72         NA Present      Benzodiazepine UDS   
#> 18    17 27         72         NA Present      Opioid         UDS   
#> 19    17 27         77         NA Present      Benzodiazepine UDS   
#> 20    17 27         77         NA Present      Opioid         UDS   
#> 21    17 27         84         NA Present      Opioid         UDS   
#> 22    17 27         98         NA Present      Opioid         UDS   
#> 23    17 27        105         NA Present      Opioid         UDS   
#> 24    17 27        112         NA Present      Benzodiazepine UDS   
#> 25    17 27        112         NA Present      Cocaine        UDS   
#> 26    17 27        112         NA Present      Opioid         UDS   
#> 27    17 27        112         NA Present      Thc            UDS   
#> 28    17 27        119         NA Present      Benzodiazepine UDS   
#> 29    17 27        119         NA Present      Opioid         UDS   
#> 30    17 27        126         NA Present      Amphetamine    UDS   
#> 31    17 27        126         NA Present      Benzodiazepine UDS   
#> 32    17 27        126         NA Present      Opioid         UDS   
#> 33    17 27        133         NA Present      Benzodiazepine UDS   
#> 34    17 27        133         NA Present      Opioid         UDS   
#> 35    17 27        147         NA Present      Benzodiazepine UDS   
#> 36    17 27        147         NA Present      Opioid         UDS   
#> 37    17 27        156         NA Present      Benzodiazepine UDS   
#> 38    17 27        156         NA Present      Opioid         UDS   
#> 39    17 27        156         NA Present      Thc            UDS   
#> 40    17 27        161         NA Present      Benzodiazepine UDS   
#> 41    17 27        161         NA Present      Opioid         UDS

Participant’s Assigned Treatment

Which substances are considered “approved” and which are “illicit”?

nonStudyOpioids_ls <- list(
  "Buprenorphine" = c("Opioid", "Methadone"),
  "Methadone"     = c("Opioid", "Buprenorphine"),
  "Naltrexone"    = c("Opioid", "Methadone", "Buprenorphine"),
  "Not treated"   = c("Opioid", "Methadone", "Buprenorphine")
)

Now we extract the treatment groups for each clinical trial, so that we can mark “illicit” buprenorphine and methadone appropriately.

treatGroups_ls <-
  public.ctn0094data::randomization %>%
  filter(who %in% examplePeople_int) %>% 
  filter(which == 1) %>%
  left_join(everybody, by = "who") %>%
  select(who, treatment) %>%
  mutate(
    treat_drug = case_when(
      str_detect(treatment, "BUP") ~ "Buprenorphine",
      treatment == "Methadone" ~ "Methadone",
      treatment == "Inpatient NR-NTX" ~ "Naltrexone"
    )
  ) %>%
  select(-treatment) %>%
  split(f = .$treat_drug) %>%
  map(.f = "who")

# Inspect
treatGroups_ls
#> $Buprenorphine
#> [1]   13  163  233 1103 2089
#> 
#> $Methadone
#> [1]  17 210
#> 
#> $Naltrexone
#> [1]   4 242

This shows us which participants were assigned to buprenorphine, methadone, and naltrexone, respectively. We can now compare the substances present in urine against the substances the participants are supposed to have in their urine. For more information, see the documentation for the MarkUse() function.

opioidUse_df <-
  udsUse2_df %>%
  mutate(
    treat_group = case_when(
      who %in% treatGroups_ls$Buprenorphine ~ "Buprenorphine",
      who %in% treatGroups_ls$Methadone     ~ "Methadone",
      who %in% treatGroups_ls$Naltrexone    ~ "Naltrexone",
      TRUE                                  ~ "Not treated"
    )
  ) %>%
  split(f = .$treat_group) %>%
  # List of data in alphabetical order, so the non-study drugs ls should match
  map2(
    .y = nonStudyOpioids_ls,
    .f = ~{
      # REQUIRES "source" COLUMN
      MarkUse(
        targetDrugs_char = .y,
        drugs_df = .x,
        # because we have participants with no recorded UDS; in practice DO NOT
        #   use this command
        retainEmptyRows = TRUE
      ) 
    }
  ) %>%
  bind_rows() %>%
  mutate(
    udsOpioid = case_when(
       is.na(when) ~ NA,
      !is.na(when) ~ TRUE
    )
  ) %>% 
  select(who, when, udsOpioid)
#> Warning: The following drugs were not matched: Methadone. Please check for
#> possible spelling/capitalization errors.
#> Warning: The following drugs were not matched: Buprenorphine. Please check for
#> possible spelling/capitalization errors.
#> Warning: The following drugs were not matched: Opioid, Methadone. Please check
#> for possible spelling/capitalization errors.
#> Warning: No matching drugs found. If you are using the default data set, please
#>   see the help file for a list of possible drug choices.

All of the drugs marked above would still be individual rows, so we want to get back to “one row per person per day”

timelineUDS_df <-
    udsUse2_df %>%
    left_join(opioidUse_df, by = c("who", "when")) %>%
    select(-what, -source) %>%
  # 2,089 rows to 1,994
    distinct()

rm(
  derived_visitImputed, opioidUse_df, randomized_df, treatGroups_ls, udsUse2_df
)

Counting Days Since Randomization

Do we state that by definition any person who wasn’t randomized is an early treatment failure? In the sense of evaluating treatment efficacy, yes; in evaluating the subject, no. Regardless, no matter the treatment outcome definition, participants without a randomization date will be listed as treatment failures under the “intent to treat” paradigm.

wasRandomized_int <-
    timelineUDS_df %>%
    group_by(who) %>%
    summarise(randomized = any(randomized %in% 1:2)) %>%
    filter(randomized) %>%
    pull(who)
notRandomized_int <-
    timelineUDS_df %>%
    filter( !(who %in% wasRandomized_int) ) %>%
    pull(who) %>%
    unique()

# Was randomized:
wasRandomized_int
#> [1]    4   13   17  163  210  233  242 1103 2089

# Wasn't
notRandomized_int
#> [1] 1

Now we need a Study Day ticker (for randomized subjects only). Recall that CTN-0030 trial participants potentially have 2 randomization days.

timelineUDS2_df <-
    timelineUDS_df %>%
    filter(who %in% wasRandomized_int) %>%
    group_by(who) %>%
  filter(!is.na(randomized)) %>%
    mutate(
        whenRandomized1 = case_when(randomized == 1 ~ when),
        whenRandomized2 = case_when(randomized == 2 ~ when)
    ) %>%
    select(who, when, whenRandomized1, whenRandomized2) %>%
    left_join(timelineUDS_df, ., by = c("who", "when")) %>%
    filter(who %in% wasRandomized_int) %>%
    # Add back in the groupings BEFORE the fill()
    group_by(who) %>%
    fill(whenRandomized1, .direction = "updown") %>%
    fill(whenRandomized2, .direction = "updown") %>%
    mutate(daysSinceRand1 = when - whenRandomized1) %>%
    mutate(daysSinceRand2 = when - whenRandomized2) %>%
    select(-whenRandomized1, -whenRandomized2)

Symbolically Summarize Participant UDS by Study Week

We puts the first day of “study week 1” on the day after randomization, not on the day of consent, but consent could have been signed also on the same day as randomization.

weeklyUse_df <-
  timelineUDS2_df %>%
  # The (daysSinceRand1 - 1) adjustment is to ensure that the first study week
  #   is a full 7 days, since "day 0" is the day before randomization. The "+1"
  #   at the end is to shift the study week label such that "week 0" is the 
  #   week *BEFORE* treatment, rather than the first week of treatment. So, the
  #   randomization day is the last day of "week 0" (the pre-treatment period).
  mutate(studyWeek1 = (daysSinceRand1 - 1) %/% 7 + 1) %>%
  mutate(studyWeek2 = (daysSinceRand2 - 1) %/% 7 + 1) %>%
  group_by(who, studyWeek1) %>%
  # There are some study weeks with multiple UDS, so we count the number of
  #   positive and negative UDS per week.
  summarise(
    nPosUDS  = sum(udsOpioid == 1, na.rm = TRUE),
    nNegUDS  = sum(visitImputed == "Present" & is.na(udsOpioid), na.rm = TRUE),
    nMissing = sum(visitImputed == "Missing", na.rm = TRUE),
    randWk1  = sum(randomized == 1, na.rm = TRUE) > 0,
    randWk2  = sum(randomized == 2 & project == "30", na.rm = TRUE) > 0
  ) %>%
  ungroup()

Now we assign a single symbol to represent the UDS results for each participant week. The symbols are (+) for only positive UDS, (–) only negative UDS, (*) at least one of each positive and negative UDS, (o) UDS required but not collected, or (_) UDS not required.

useByWeekRandomized_df <-
  weeklyUse_df %>%
    mutate(
        udsStatus = case_when(
            # If we see a positive UDS and no negative UDS, it's positive
            nPosUDS > 0  & nNegUDS == 0 ~ "+",
            # If we see a negative UDS and no positive UDS, it's negative
            nPosUDS == 0 & nNegUDS > 0  ~ "-",
            # If we see both positive and negative UDS in a single week, it's both
            #   (note that we can recode all "B"s to be "+" as necessary)
            nPosUDS > 0  & nNegUDS > 0  ~ "*",
            # If we don't have any UDS in a week after randomization, it's missing
            # UPDATE 2022-03-08: I had this as a 0 originally, and I was using this
            #   in context of consent, not randomization. This was wrong.
            nPosUDS == 0 & nNegUDS == 0 & studyWeek1 >= 1 ~ "o",
            # If none of the above are true, but we still have a missing value as
            #   marked by the MarkMissing() function, then it's missing
            nMissing > 0 ~ "o",
            # If none of the above conditions are true (probably because it's a week
            #   before randomization but not during a baseline visit for consent),
            #   then leave it blank (pre-study)
            TRUE ~ "_"
        )
    ) %>%
  group_by(who) %>%
  # For CTN-0030, Phase II could have started on any day of the week, even in
  #   the middle of a treatment week. If we try to start counting Phase II
  #   weeks the day after treatment arms are switched, we can end up with the
  #   last "week" of Phase I not having 7 days. I'm going to leave the first
  #   week of Phase II as whatever week the switch happened in.
  mutate(
    rand1Active = studyWeek1 > 0,
    # This returns 0 for any week before the Phase II randomization, and 1 for
    #   the Phase II randomization week and all subsequent weeks (because the
    #   randWk2 column is 1 only for the week of second randomization and 0 
    #   all other rows).
    rand2Active = cumsum(randWk2),
    trialPhase  = rand1Active + rand2Active
  ) %>%
    select(
      who, studyWeek = studyWeek1, randWk1, randWk2, udsStatus, trialPhase
    )

We then split these rows by trial phase, and collapse the weekly symbols into a single string.

weeklyOpioidPatterns_df <-
  useByWeekRandomized_df %>%
  mutate(
    phase = case_when(
      trialPhase == 0L ~ "Baseline",
      trialPhase == 1L ~ "Phase_1",
      trialPhase == 2L ~ "Phase_2"
    )
  ) %>%
  group_by(who, phase) %>%
  summarise(
    usePattern = paste0(udsStatus, collapse = "")
  ) %>%
  pivot_wider(names_from = "phase", values_from = "usePattern")

weeklyOpioidPatterns_df
#> # A tibble: 9 × 4
#> # Groups:   who [9]
#>     who Baseline Phase_1                  Phase_2                  
#>   <int> <chr>    <chr>                    <chr>                    
#> 1     4 ____-    --------------------o-oo NA                       
#> 2    13 ____*    -------o---o-ooooooooooo NA                       
#> 3    17 _____+-  o-++*++++++-++++++-+++-  NA                       
#> 4   163 _____*   ----oo---o--o*-o-------o NA                       
#> 5   210 _____*   ++++++++-+++-----o-----o NA                       
#> 6   233 ____*    +++++++++++++++++++++++o NA                       
#> 7   242 ____+_   -----------------------  NA                       
#> 8  1103 ____+    +o--oo--o                *+-+---o---------o-o-o-o-
#> 9  2089 ____*    ++++---+---------------- NA

Notice that we have the opioid UDS results for each subject by week split into the various phases of the clinical trial. These summaries are in quinary word format, following the work in Odom et al. (2023) [https://www.ncbi.nlm.nih.gov/pmc/articles/PMC10490938/].

Our last step for the randomized participants is to mark the study weeks when these phases start and end.

derived_weeklyOpioidPatternRand <-
  useByWeekRandomized_df %>%
  mutate(
    randWeek1  = randWk1 * studyWeek,
    randWeek2  = randWk2 * studyWeek
  ) %>%
  summarise(
    startWeek  = min(studyWeek),
    randWeek1  = max(randWeek1),
    randWeek2  = if_else( all(randWeek2 == 0), NA_real_, max(randWeek2) ),
    endWeek    = max(studyWeek)
  ) %>%
  # View this smaller data set before joining. In practice, you can comment out
  #   this print() command.
  print() %>% 
  left_join(weeklyOpioidPatterns_df, by = "who")
#> # A tibble: 9 × 5
#>     who startWeek randWeek1 randWeek2 endWeek
#>   <int>     <dbl>     <dbl>     <dbl>   <dbl>
#> 1     4        -4         0        NA      24
#> 2    13        -4         0        NA      24
#> 3    17        -6         0        NA      23
#> 4   163        -5         0        NA      24
#> 5   210        -5         0        NA      24
#> 6   233        -4         0        NA      24
#> 7   242        -5         0        NA      23
#> 8  1103        -4         0        10      34
#> 9  2089        -4         0        NA      24

Symbolically Summarize Non-Randomized Participants UDS by Study Week

There are some strange cases where non-randomized participants also submitted UDS samples during the clinical trial. We will also create quinary words for these participants, but note that most of the UDS results will be missing (symbolized by “o”).

nonRandUDS_df <-
    backbone_df %>%
    filter(who %in% notRandomized_int) %>%
    left_join(uds) %>%
    # MarkUse() requires a "source" column
    mutate(source = "UDS") %>%
    MarkUse(
        targetDrugs_char = nonStudyOpioids_ls$`Not treated`,
        drugs_df = .
    ) %>%
  mutate(
    udsOpioid = case_when(
      !is.na(when) ~ TRUE,
       is.na(when) ~ NA
    )
  ) %>% 
    select(who, when, udsOpioid) %>%
  # Uneccessary here, but some UDS records are duplicated
    distinct()

Although not present in this small data example, there are positive UDS events among the non-randomized. We then construct a “study week” ticker to mark such weekly UDS results.

timelineNonRandUDS_df <-
    backbone_df %>%
    filter(who %in% notRandomized_int) %>%
    left_join(nonRandUDS_df, by = c("who", "when")) %>%
  # Because this week moves off of the consent date, there is no reason to add
  #   a `(week - 1)` adjustment
  mutate(studyWeek = when %/% 7 + 1) %>%
    group_by(who, studyWeek) %>%
    summarise(
        posUDS  = sum(udsOpioid == 1, na.rm = TRUE) > 0
    )

For these participants who were not randomized at all, they will only have “Phase I” missing values.

weeklyNonRandPatterns_df <-
  timelineNonRandUDS_df %>%
  mutate(
    udsStatus = case_when(
      # If they are positive, they are positive
      posUDS ~ "+",
      # If they aren't positive and it's after the consent week, they are
      #   missing (because they weren't randomized)
      !posUDS & studyWeek >= 1 ~ "o",
      # If they aren't positive and it's on or before the consent week, then
      #   leave it blank (pre-study)
      !posUDS & studyWeek < 1 ~ "_"
    )
  ) %>%
  mutate(
    phase = case_when(
      studyWeek <  1 ~ "Baseline",
      studyWeek >= 1 ~ "Phase_1"
    )
  ) %>%
  # Again, this print is unecessary, but here it make clear what we are doing
  print() %>% 
  group_by(who, phase) %>%
  summarise(
    usePattern = paste0(udsStatus, collapse = "")
  ) %>%
  pivot_wider(names_from = "phase", values_from = "usePattern") %>%
  ungroup()
#> # A tibble: 20 × 5
#> # Groups:   who [1]
#>      who studyWeek posUDS udsStatus phase   
#>    <int>     <dbl> <lgl>  <chr>     <chr>   
#>  1     1        -4 FALSE  _         Baseline
#>  2     1        -3 FALSE  _         Baseline
#>  3     1        -2 FALSE  _         Baseline
#>  4     1        -1 FALSE  _         Baseline
#>  5     1         0 FALSE  _         Baseline
#>  6     1         1 FALSE  o         Phase_1 
#>  7     1         2 FALSE  o         Phase_1 
#>  8     1         3 FALSE  o         Phase_1 
#>  9     1         4 FALSE  o         Phase_1 
#> 10     1         5 FALSE  o         Phase_1 
#> 11     1         6 FALSE  o         Phase_1 
#> 12     1         7 FALSE  o         Phase_1 
#> 13     1         8 FALSE  o         Phase_1 
#> 14     1         9 FALSE  o         Phase_1 
#> 15     1        10 FALSE  o         Phase_1 
#> 16     1        11 FALSE  o         Phase_1 
#> 17     1        12 FALSE  o         Phase_1 
#> 18     1        13 FALSE  o         Phase_1 
#> 19     1        14 FALSE  o         Phase_1 
#> 20     1        15 FALSE  o         Phase_1

weeklyNonRandPatterns_df
#> # A tibble: 1 × 3
#>     who Baseline Phase_1        
#>   <int> <chr>    <chr>          
#> 1     1 _____    ooooooooooooooo

Finally, we add in the start and end weeks for these two phases.

derived_weeklyOpioidPatternNonRand <-
  timelineNonRandUDS_df %>%
  group_by(who) %>%
  summarise(
    startWeek  = min(studyWeek),
    randWeek1  = NA_real_,
    randWeek2  = NA_real_,
    endWeek    = max(studyWeek)
  ) %>%
  left_join(weeklyNonRandPatterns_df, by = "who")

Final Product

At the end of this very long process, we have this:

derived_weeklyOpioidPattern <-
    derived_weeklyOpioidPatternRand %>%
    bind_rows(derived_weeklyOpioidPatternNonRand) %>%
    arrange(who) %>%
  replace_na(list(Phase_2 = ""))

derived_weeklyOpioidPattern
#> # A tibble: 10 × 8
#>      who startWeek randWeek1 randWeek2 endWeek Baseline Phase_1          Phase_2
#>    <int>     <dbl>     <dbl>     <dbl>   <dbl> <chr>    <chr>            <chr>  
#>  1     1        -4        NA        NA      15 _____    ooooooooooooooo  ""     
#>  2     4        -4         0        NA      24 ____-    ---------------… ""     
#>  3    13        -4         0        NA      24 ____*    -------o---o-oo… ""     
#>  4    17        -6         0        NA      23 _____+-  o-++*++++++-+++… ""     
#>  5   163        -5         0        NA      24 _____*   ----oo---o--o*-… ""     
#>  6   210        -5         0        NA      24 _____*   ++++++++-+++---… ""     
#>  7   233        -4         0        NA      24 ____*    +++++++++++++++… ""     
#>  8   242        -5         0        NA      23 ____+_   ---------------… ""     
#>  9  1103        -4         0        10      34 ____+    +o--oo--o        "*+-+-…
#> 10  2089        -4         0        NA      24 ____*    ++++---+-------… ""

Wrapping Up

sessionInfo()
#> R version 4.3.2 (2023-10-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.3 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0
#> 
#> locale:
#>  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
#>  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
#>  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
#> [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
#> 
#> time zone: UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] stringr_1.5.1             tidyr_1.3.0              
#> [3] tibble_3.2.1              purrr_1.0.2              
#> [5] dplyr_1.1.4               public.ctn0094extra_1.0.4
#> [7] public.ctn0094data_1.0.6 
#> 
#> loaded via a namespace (and not attached):
#>  [1] jsonlite_1.8.7    compiler_4.3.2    tidyselect_1.2.0  jquerylib_0.1.4  
#>  [5] systemfonts_1.0.5 textshaping_0.3.7 yaml_2.3.7        fastmap_1.1.1    
#>  [9] R6_2.5.1          generics_0.1.3    knitr_1.45        desc_1.4.2       
#> [13] rprojroot_2.0.4   bslib_0.5.1       pillar_1.9.0      rlang_1.1.2      
#> [17] utf8_1.2.4        cachem_1.0.8      stringi_1.8.1     xfun_0.41        
#> [21] fs_1.6.3          sass_0.4.7        memoise_2.0.1     cli_3.6.1        
#> [25] withr_2.5.2       pkgdown_2.0.7     magrittr_2.0.3    digest_0.6.33    
#> [29] lifecycle_1.0.4   vctrs_0.6.4       evaluate_0.23     glue_1.6.2       
#> [33] ragg_1.2.6        fansi_1.0.5       rmarkdown_2.25    tools_4.3.2      
#> [37] pkgconfig_2.0.3   htmltools_0.5.7