|
| 1 | +## ----r setup, message=FALSE, warning=FALSE, results='hold'-------------------- |
| 2 | +library(admiral) |
| 3 | +library(admiralonco) |
| 4 | +library(metacore) |
| 5 | +library(metatools) |
| 6 | +library(xportr) |
| 7 | +library(pharmaversesdtm) |
| 8 | +library(pharmaverseadam) |
| 9 | +library(dplyr) |
| 10 | +library(lubridate) |
| 11 | +library(stringr) |
| 12 | + |
| 13 | +## ----r read-specs------------------------------------------------------------- |
| 14 | +metacore <- spec_to_metacore("./metadata/onco_spec.xlsx") %>% |
| 15 | + select_dataset("ADRS") |
| 16 | + |
| 17 | +## ----r load-data-------------------------------------------------------------- |
| 18 | +data("adsl") |
| 19 | +data("rs_onco_recist") |
| 20 | +data("tu_onco_recist") |
| 21 | + |
| 22 | +rs <- rs_onco_recist |
| 23 | +tu <- tu_onco_recist |
| 24 | + |
| 25 | +# Convert blanks to NA |
| 26 | +rs <- convert_blanks_to_na(rs) |
| 27 | +tu <- convert_blanks_to_na(tu) |
| 28 | + |
| 29 | +## ----r merge-adsl-rs---------------------------------------------------------- |
| 30 | +adsl_vars <- exprs(RANDDT) |
| 31 | +adrs_merged <- derive_vars_merged( |
| 32 | + rs, |
| 33 | + dataset_add = adsl, |
| 34 | + new_vars = adsl_vars, |
| 35 | + by_vars = exprs(STUDYID, USUBJID) |
| 36 | +) |
| 37 | + |
| 38 | +## ----r set-param-details------------------------------------------------------ |
| 39 | +adrs_ovr <- adrs_merged %>% |
| 40 | + filter(RSEVAL == "INVESTIGATOR" & RSTESTCD == "OVRLRESP") %>% |
| 41 | + mutate( |
| 42 | + PARAMCD = "OVR", |
| 43 | + PARAM = "Overall Response by Investigator", |
| 44 | + PARCAT1 = "Tumor Response", |
| 45 | + PARCAT2 = "Investigator", |
| 46 | + PARCAT3 = "RECIST 1.1" |
| 47 | + ) |
| 48 | + |
| 49 | +## ----r impute-dates----------------------------------------------------------- |
| 50 | +adrs_imputed <- adrs_ovr %>% |
| 51 | + derive_vars_dt( |
| 52 | + dtc = RSDTC, |
| 53 | + new_vars_prefix = "A", |
| 54 | + highest_imputation = "D", |
| 55 | + date_imputation = "last" |
| 56 | + ) %>% |
| 57 | + mutate(AVISIT = VISIT) |
| 58 | + |
| 59 | +## ----r derive-aval------------------------------------------------------------ |
| 60 | +adrs_aval <- adrs_imputed %>% |
| 61 | + mutate( |
| 62 | + AVALC = RSSTRESC, |
| 63 | + AVAL = aval_resp(AVALC) |
| 64 | + ) |
| 65 | + |
| 66 | +## ----r flag-worst-assessment-------------------------------------------------- |
| 67 | +worst_resp <- function(arg) { |
| 68 | + case_when( |
| 69 | + arg == "NE" ~ 1, |
| 70 | + arg == "CR" ~ 2, |
| 71 | + arg == "PR" ~ 3, |
| 72 | + arg == "SD" ~ 4, |
| 73 | + arg == "NON-CR/NON-PD" ~ 5, |
| 74 | + arg == "PD" ~ 6, |
| 75 | + TRUE ~ 0 |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +adrs_anl01fl <- adrs_aval %>% |
| 80 | + restrict_derivation( |
| 81 | + derivation = derive_var_extreme_flag, |
| 82 | + args = params( |
| 83 | + by_vars = exprs(STUDYID, USUBJID, ADT), |
| 84 | + order = exprs(worst_resp(AVALC), RSSEQ), |
| 85 | + new_var = ANL01FL, |
| 86 | + mode = "last" |
| 87 | + ), |
| 88 | + filter = !is.na(AVAL) & ADT >= RANDDT |
| 89 | + ) |
| 90 | + |
| 91 | +## ----r derive-pd-------------------------------------------------------------- |
| 92 | +adrs_pd <- adrs_anl01fl %>% |
| 93 | + derive_extreme_records( |
| 94 | + dataset_ref = adsl, |
| 95 | + dataset_add = adrs_anl01fl, |
| 96 | + by_vars = exprs(STUDYID, USUBJID), |
| 97 | + filter_add = PARAMCD == "OVR" & AVALC == "PD" & ANL01FL == "Y", |
| 98 | + order = exprs(ADT, RSSEQ), |
| 99 | + mode = "first", |
| 100 | + exist_flag = AVALC, |
| 101 | + false_value = "N", |
| 102 | + set_values_to = exprs( |
| 103 | + PARAMCD = "PD", |
| 104 | + PARAM = "Disease Progression by Investigator", |
| 105 | + PARCAT1 = "Tumor Response", |
| 106 | + PARCAT2 = "Investigator", |
| 107 | + PARCAT3 = "RECIST 1.1", |
| 108 | + AVAL = yn_to_numeric(AVALC), |
| 109 | + ANL01FL = "Y" |
| 110 | + ) |
| 111 | + ) |
| 112 | + |
| 113 | +## ----r derive-death----------------------------------------------------------- |
| 114 | +adsldth <- adsl %>% |
| 115 | + select(STUDYID, USUBJID, DTHDT, !!!adsl_vars) |
| 116 | + |
| 117 | +adrs_death <- adrs_pd %>% |
| 118 | + derive_extreme_records( |
| 119 | + dataset_ref = adsldth, |
| 120 | + dataset_add = adsldth, |
| 121 | + by_vars = exprs(STUDYID, USUBJID), |
| 122 | + filter_add = !is.na(DTHDT), |
| 123 | + exist_flag = AVALC, |
| 124 | + false_value = "N", |
| 125 | + set_values_to = exprs( |
| 126 | + PARAMCD = "DEATH", |
| 127 | + PARAM = "Death", |
| 128 | + PARCAT1 = "Reference Event", |
| 129 | + AVAL = yn_to_numeric(AVALC), |
| 130 | + ANL01FL = "Y", |
| 131 | + ADT = DTHDT |
| 132 | + ) |
| 133 | + ) %>% |
| 134 | + select(-DTHDT) |
| 135 | + |
| 136 | +## ----r derive-lsta------------------------------------------------------------ |
| 137 | +adrs_lsta <- adrs_death %>% |
| 138 | + derive_extreme_records( |
| 139 | + dataset_ref = adsl, |
| 140 | + dataset_add = adrs_death, |
| 141 | + by_vars = exprs(STUDYID, USUBJID), |
| 142 | + filter_add = PARAMCD == "OVR" & ANL01FL == "Y", |
| 143 | + order = exprs(ADT, RSSEQ), |
| 144 | + mode = "last", |
| 145 | + set_values_to = exprs( |
| 146 | + PARAMCD = "LSTA", |
| 147 | + PARAM = "Last Disease Assessment by Investigator", |
| 148 | + PARCAT1 = "Tumor Response", |
| 149 | + PARCAT2 = "Investigator", |
| 150 | + PARCAT3 = "RECIST 1.1", |
| 151 | + ANL01FL = "Y" |
| 152 | + ) |
| 153 | + ) |
| 154 | + |
| 155 | +## ----r apply-metadata-check, message=FALSE, warning=FALSE--------------------- |
| 156 | +adrs_checked <- adrs_lsta %>% |
| 157 | + add_variables(metacore) %>% # Add variables specified in the metadata |
| 158 | + drop_unspec_vars(metacore) %>% # Drop variables not specified in metadata |
| 159 | + check_variables(metacore) %>% # Check all variables specified are present and no more |
| 160 | + check_ct_data(metacore) %>% # Check controlled terminology |
| 161 | + order_cols(metacore) %>% # Order columns according to metadata |
| 162 | + sort_by_key(metacore) # Sort rows by sort keys |
| 163 | + |
| 164 | +## ----r xportr----------------------------------------------------------------- |
| 165 | +dir <- tempdir() # Specify the directory for saving the XPT file |
| 166 | + |
| 167 | +adrs_xpt <- adrs_checked %>% |
| 168 | + xportr_type(metacore, domain = "ADRS") %>% # Coerce variable types to match metadata |
| 169 | + xportr_length(metacore) %>% # Assign variable lengths from metadata |
| 170 | + xportr_label(metacore) %>% # Assign variable labels from metadata |
| 171 | + xportr_format(metacore) %>% # Assign variable formats from metadata |
| 172 | + xportr_df_label(metacore) %>% # Assign dataset labels from metadata |
| 173 | + xportr_write(file.path(dir, "adrs.xpt")) # Write the XPT file |
| 174 | + |
0 commit comments