library(tibble)
library(dplyr)
library(lubridate)
# Example 1a: Derive MAP
advs <- tribble(
~USUBJID, ~PARAMCD, ~PARAM, ~AVAL, ~AVALU, ~VISIT,
"01-701-1015", "DIABP", "Diastolic Blood Pressure (mmHg)", 51, "mmHg", "BASELINE",
"01-701-1015", "DIABP", "Diastolic Blood Pressure (mmHg)", 50, "mmHg", "WEEK 2",
"01-701-1015", "SYSBP", "Systolic Blood Pressure (mmHg)", 121, "mmHg", "BASELINE",
"01-701-1015", "SYSBP", "Systolic Blood Pressure (mmHg)", 121, "mmHg", "WEEK 2",
"01-701-1028", "DIABP", "Diastolic Blood Pressure (mmHg)", 79, "mmHg", "BASELINE",
"01-701-1028", "DIABP", "Diastolic Blood Pressure (mmHg)", 80, "mmHg", "WEEK 2",
"01-701-1028", "SYSBP", "Systolic Blood Pressure (mmHg)", 130, "mmHg", "BASELINE",
"01-701-1028", "SYSBP", "Systolic Blood Pressure (mmHg)", 132, "mmHg", "WEEK 2"
) %>%
mutate(
ADT = case_when(
VISIT == "BASELINE" ~ as.Date("2024-01-10"),
VISIT == "WEEK 2" ~ as.Date("2024-01-24")
),
ADTF = NA_character_
)
derive_param_computed(
advs,
by_vars = exprs(USUBJID, VISIT),
parameters = c("SYSBP", "DIABP"),
set_values_to = exprs(
AVAL = (AVAL.SYSBP + 2 * AVAL.DIABP) / 3,
PARAMCD = "MAP",
PARAM = "Mean Arterial Pressure (mmHg)",
AVALU = "mmHg",
ADT = ADT.SYSBP
)
)
# Example 1b: Using option `keep_nas = TRUE` to derive MAP in the case where some/all values
# of a variable used in the computation are missing
derive_param_computed(
advs,
by_vars = exprs(USUBJID, VISIT),
parameters = c("SYSBP", "DIABP"),
set_values_to = exprs(
AVAL = (AVAL.SYSBP + 2 * AVAL.DIABP) / 3,
PARAMCD = "MAP",
PARAM = "Mean Arterial Pressure (mmHg)",
AVALU = "mmHg",
ADT = ADT.SYSBP,
ADTF = ADTF.SYSBP
),
keep_nas = TRUE
)
# Example 2: Derive BMI where height is measured only once
advs <- tribble(
~USUBJID, ~PARAMCD, ~PARAM, ~AVAL, ~AVALU, ~VISIT,
"01-701-1015", "HEIGHT", "Height (cm)", 147.0, "cm", "SCREENING",
"01-701-1015", "WEIGHT", "Weight (kg)", 54.0, "kg", "SCREENING",
"01-701-1015", "WEIGHT", "Weight (kg)", 54.4, "kg", "BASELINE",
"01-701-1015", "WEIGHT", "Weight (kg)", 53.1, "kg", "WEEK 2",
"01-701-1028", "HEIGHT", "Height (cm)", 163.0, "cm", "SCREENING",
"01-701-1028", "WEIGHT", "Weight (kg)", 78.5, "kg", "SCREENING",
"01-701-1028", "WEIGHT", "Weight (kg)", 80.3, "kg", "BASELINE",
"01-701-1028", "WEIGHT", "Weight (kg)", 80.7, "kg", "WEEK 2"
)
derive_param_computed(
advs,
by_vars = exprs(USUBJID, VISIT),
parameters = "WEIGHT",
set_values_to = exprs(
AVAL = AVAL.WEIGHT / (AVAL.HEIGHT / 100)^2,
PARAMCD = "BMI",
PARAM = "Body Mass Index (kg/m^2)",
AVALU = "kg/m^2"
),
constant_parameters = c("HEIGHT"),
constant_by_vars = exprs(USUBJID)
)
# Example 3: Using data from an additional dataset and other variables than AVAL
qs <- tribble(
~USUBJID, ~AVISIT, ~QSTESTCD, ~QSORRES, ~QSSTRESN,
"1", "WEEK 2", "CHSF112", NA, 1,
"1", "WEEK 2", "CHSF113", "Yes", NA,
"1", "WEEK 2", "CHSF114", NA, 1,
"1", "WEEK 4", "CHSF112", NA, 2,
"1", "WEEK 4", "CHSF113", "No", NA,
"1", "WEEK 4", "CHSF114", NA, 1
)
adchsf <- tribble(
~USUBJID, ~AVISIT, ~PARAMCD, ~QSSTRESN, ~AVAL,
"1", "WEEK 2", "CHSF12", 1, 6,
"1", "WEEK 2", "CHSF14", 1, 6,
"1", "WEEK 4", "CHSF12", 2, 12,
"1", "WEEK 4", "CHSF14", 1, 6
) %>%
mutate(QSORRES = NA_character_)
derive_param_computed(
adchsf,
dataset_add = qs,
by_vars = exprs(USUBJID, AVISIT),
parameters = exprs(CHSF12, CHSF13 = QSTESTCD %in% c("CHSF113", "CHSF213"), CHSF14),
set_values_to = exprs(
AVAL = case_when(
QSORRES.CHSF13 == "Not applicable" ~ 0,
QSORRES.CHSF13 == "Yes" ~ 38,
QSORRES.CHSF13 == "No" ~ if_else(
QSSTRESN.CHSF12 > QSSTRESN.CHSF14,
25,
0
)
),
PARAMCD = "CHSF13"
)
)
# Example 4: Computing more than one variable
adlb_tbilialk <- tribble(
~USUBJID, ~PARAMCD, ~AVALC, ~ADTM, ~ADTF,
"1", "ALK2", "Y", "2021-05-13", NA_character_,
"1", "TBILI2", "Y", "2021-06-30", "D",
"2", "ALK2", "Y", "2021-12-31", "M",
"2", "TBILI2", "N", "2021-11-11", NA_character_,
"3", "ALK2", "N", "2021-04-03", NA_character_,
"3", "TBILI2", "N", "2021-04-04", NA_character_
) %>%
mutate(ADTM = ymd(ADTM))
derive_param_computed(
dataset_add = adlb_tbilialk,
by_vars = exprs(USUBJID),
parameters = c("ALK2", "TBILI2"),
set_values_to = exprs(
AVALC = if_else(AVALC.TBILI2 == "Y" & AVALC.ALK2 == "Y", "Y", "N"),
ADTM = pmax(ADTM.TBILI2, ADTM.ALK2),
ADTF = if_else(ADTM == ADTM.TBILI2, ADTF.TBILI2, ADTF.ALK2),
PARAMCD = "TB2AK2",
PARAM = "TBILI > 2 times ULN and ALKPH <= 2 times ULN"
),
keep_nas = TRUE
)
Run the code above in your browser using DataLab