Here are the packages we need for this vignette:
library(public.ctn0094data)
library(public.ctn0094extra)
library(dplyr)
library(purrr)
library(tibble)
library(tidyr)
library(stringr)
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:
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).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:
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.
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
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
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:
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.
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.
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
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.
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.
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)
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
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.
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.
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:
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:
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 “_”.
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
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”
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)
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
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.
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 ____* ++++---+-------… ""
sessionInfo()
#> R version 4.4.2 (2024-10-31)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.1 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.26.so; LAPACK version 3.12.0
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
#> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: Etc/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.1
#> [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 rmarkdown_2.29
#>
#> loaded via a namespace (and not attached):
#> [1] jsonlite_1.8.9 compiler_4.4.2 tidyselect_1.2.1 jquerylib_0.1.4
#> [5] yaml_2.3.10 fastmap_1.2.0 R6_2.5.1 generics_0.1.3
#> [9] knitr_1.49 maketools_1.3.1 bslib_0.8.0 pillar_1.9.0
#> [13] rlang_1.1.4 utf8_1.2.4 cachem_1.1.0 stringi_1.8.4
#> [17] xfun_0.49 sass_0.4.9 sys_3.4.3 cli_3.6.3
#> [21] withr_3.0.2 magrittr_2.0.3 digest_0.6.37 lifecycle_1.0.4
#> [25] vctrs_0.6.5 evaluate_1.0.1 glue_1.8.0 buildtools_1.0.0
#> [29] fansi_1.0.6 tools_4.4.2 pkgconfig_2.0.3 htmltools_0.5.8.1