From ccff2e5d5d254be369917974ad01d752860c1a1b Mon Sep 17 00:00:00 2001 From: Jeff Dickinson Date: Wed, 14 Jan 2026 17:31:21 -0600 Subject: [PATCH 1/3] #134 update with derive_var_nfrlt --- adam/adpc.R | 25 ++++++++++++++++--------- adam/adpc.qmd | 38 +++++++++++++++++++------------------- adam/adppk.R | 22 +++++++++++++++------- adam/adppk.qmd | 26 +++++++++++++++++--------- 4 files changed, 67 insertions(+), 44 deletions(-) diff --git a/adam/adpc.R b/adam/adpc.R index 4adf128a..c0543d17 100644 --- a/adam/adpc.R +++ b/adam/adpc.R @@ -56,8 +56,14 @@ pc_dates <- pc %>% # Derive event ID and nominal relative time from first dose (NFRLT) mutate( EVID = 0, - DRUG = PCTEST, - NFRLT = if_else(PCTPTNUM < 0, 0, PCTPTNUM), .after = USUBJID + DRUG = PCTEST + ) %>% + derive_var_nfrlt( + new_var = NFRLT, + new_var_unit = FRLTU, + out_unit = "HOURS", + tpt_var = PCTPT, + visit_day = VISITDY ) ## ----r------------------------------------------------------------------------ @@ -85,11 +91,13 @@ ex_dates <- ex %>% ) %>% # Derive event ID and nominal relative time from first dose (NFRLT) mutate( - EVID = 1, - NFRLT = case_when( - VISITDY == 1 ~ 0, - TRUE ~ 24 * VISITDY - ) + EVID = 1 + ) %>% + derive_var_nfrlt( + new_var = NFRLT, + new_var_unit = FRLTU, + out_unit = "HOURS", + visit_day = VISITDY ) %>% # Set missing end dates to start date mutate(AENDTM = case_when( @@ -240,6 +248,7 @@ adpc_arrlt <- bind_rows(adpc_nom_next, ex_exp) %>% # Derive Actual Relative Time from Reference Dose (ARRLT) derive_vars_duration( new_var = ARRLT, + new_var_unit = RRLTU, start_date = ADTM_prev, end_date = ADTM, out_unit = "hours", @@ -330,8 +339,6 @@ adpc_aval <- adpc_nrrlt %>% ) %>% # Derive relative time units mutate( - FRLTU = "h", - RRLTU = "h", # Derive PARAMCD PARAMCD = coalesce(PCTESTCD, "DOSE"), ALLOQ = PCLLOQ, diff --git a/adam/adpc.qmd b/adam/adpc.qmd index 7a06c8f1..8decac79 100644 --- a/adam/adpc.qmd +++ b/adam/adpc.qmd @@ -16,7 +16,7 @@ The Non-compartmental analysis (NCA) ADaM uses the CDISC Implementation Guide (< ## First Load Packages -First we will load the packages required for our project. We will use `{admiral}` for the creation of analysis data. `{admiral}` requires `{dplyr}`, `{lubridate}` and `{stringr}`. Find other `{admiral}` functions and related variables by searching [admiraldiscovery](). We will use `{metacore}` and `{metatools}` to store and manipulate metadata from our specifications. We will use `{xportr}` to perform checks on the final data and export to a transport file. +First we will load the packages required for our project. We will use `{admiral}` for the creation of analysis data. `{admiral}` requires `{dplyr}`, `{lubridate}` and `{stringr}`. Find other `{admiral}` functions and related variables by searching [admiraldiscovery](https://pharmaverse.github.io/admiraldiscovery/articles/reactable.html). We will use `{metacore}` and `{metatools}` to store and manipulate metadata from our specifications. We will use `{xportr}` to perform checks on the final data and export to a transport file. The source SDTM data will come from the CDISC pilot study data stored in `{pharmaversesdtm}`. @@ -71,7 +71,7 @@ vs <- convert_blanks_to_na(vs) ### Derive PC Dates -Here we use `{admiral}` functions for working with dates and we will also create a nominal time from first dose `NFRLT` for `PC` data based on `PCTPTNUM`. +Here we use `{admiral}` functions for working with dates and we will also create a nominal time from first dose `NFRLT` for `PC` data using `derive_var_nfrlt()`. ```{r} #| label: PC Dates @@ -101,8 +101,14 @@ pc_dates <- pc %>% # Derive event ID and nominal relative time from first dose (NFRLT) mutate( EVID = 0, - DRUG = PCTEST, - NFRLT = if_else(PCTPTNUM < 0, 0, PCTPTNUM), .after = USUBJID + DRUG = PCTEST + ) %>% + derive_var_nfrlt( + new_var = NFRLT, + new_var_unit = FRLTU, + out_unit = "HOURS", + tpt_var = PCTPT, + visit_day = VISITDY ) ``` @@ -112,7 +118,7 @@ print_df(pc_dates %>% select(USUBJID, PCTEST, ADTM, VISIT, PCTPT, NFRLT)) ### Get Dosing Information -Here we also create nominal time from first dose `NFRLT` for `EX` data based on `VISITDY`. +Here we also create nominal time from first dose `NFRLT` for `EX` data based on `VISITDY` using `derive_var_nfrlt()`. ```{r} #| label: Dosing @@ -141,11 +147,13 @@ ex_dates <- ex %>% ) %>% # Derive event ID and nominal relative time from first dose (NFRLT) mutate( - EVID = 1, - NFRLT = case_when( - VISITDY == 1 ~ 0, - TRUE ~ 24 * VISITDY - ) + EVID = 1 + ) %>% + derive_var_nfrlt( + new_var = NFRLT, + new_var_unit = FRLTU, + out_unit = "HOURS", + visit_day = VISITDY ) %>% # Set missing end dates to start date mutate(AENDTM = case_when( @@ -230,8 +238,6 @@ adpc_first_dose <- pc_dates %>% ) ``` - - ```{r eval=TRUE, echo=FALSE, purl=FALSE} print_df(adpc_first_dose %>% select(USUBJID, FANLDTM, NFRLT, ADTM, AVISITN, AVISIT, PCTPT)) ``` @@ -318,7 +324,6 @@ adpc_nom_next <- adpc_nom_prev %>% print_df(adpc_nom_prev %>% select(USUBJID, NFRLT, AVISIT, PCTPT, NFRLT_prev)) ``` - ### Combine PC and EX Data Combine `PC` and `EX` records and derive the additional relative time variables. @@ -348,6 +353,7 @@ adpc_arrlt <- bind_rows(adpc_nom_next, ex_exp) %>% # Derive Actual Relative Time from Reference Dose (ARRLT) derive_vars_duration( new_var = ARRLT, + new_var_unit = RRLTU, start_date = ADTM_prev, end_date = ADTM, out_unit = "hours", @@ -406,12 +412,10 @@ adpc_nrrlt <- adpc_arrlt %>% ) ``` - ```{r eval=TRUE, echo=FALSE, purl=FALSE} print_df(adpc_nrrlt %>% select(USUBJID, NFRLT, NRRLT, AVISIT, PCTPT)) ``` - ### Derive Analysis Variables Here we derive the analysis variables such as `AVAL` and `ATPTREF`. @@ -458,8 +462,6 @@ adpc_aval <- adpc_nrrlt %>% ) %>% # Derive relative time units mutate( - FRLTU = "h", - RRLTU = "h", # Derive PARAMCD PARAMCD = coalesce(PCTESTCD, "DOSE"), ALLOQ = PCLLOQ, @@ -512,12 +514,10 @@ dtype <- adpc_aval %>% derive_vars_dtm_to_tm(exprs(PCRFTDTM)) ``` - ```{r eval=TRUE, echo=FALSE, purl=FALSE} print_df(dtype %>% select(USUBJID, DTYPE, BASETYPE, ATPT, NFRLT, NRRLT, AFRLT, ARRLT)) ``` - ### Combine Original and DTYPE Copy Now the duplicated records are combined with the original records. diff --git a/adam/adppk.R b/adam/adppk.R index 3842fb19..91faf209 100644 --- a/adam/adppk.R +++ b/adam/adppk.R @@ -59,8 +59,14 @@ pc_dates <- pc %>% # Derive event ID and nominal relative time from first dose (NFRLT) mutate( EVID = 0, - DRUG = PCTEST, - NFRLT = if_else(PCTPTNUM < 0, 0, PCTPTNUM), .after = USUBJID + DRUG = PCTEST + ) %>% + derive_var_nfrlt( + new_var = NFRLT, + new_var_unit = FRLTU, + out_unit = "HOURS", + tpt_var = PCTPT, + visit_day = VISITDY ) ## ----r------------------------------------------------------------------------ @@ -90,11 +96,13 @@ ex_dates <- ex %>% ) %>% # Derive event ID and nominal relative time from first dose (NFRLT) mutate( - EVID = 1, - NFRLT = case_when( - VISITDY == 1 ~ 0, - TRUE ~ 24 * VISITDY - ) + EVID = 1 + ) %>% + derive_var_nfrlt( + new_var = NFRLT, + new_var_unit = FRLTU, + out_unit = "HOURS", + visit_day = VISITDY ) %>% # Set missing end dates to start date mutate(AENDTM = case_when( diff --git a/adam/adppk.qmd b/adam/adppk.qmd index 5ceac193..8d6433d8 100644 --- a/adam/adppk.qmd +++ b/adam/adppk.qmd @@ -73,7 +73,7 @@ lb <- convert_blanks_to_na(lb) ### Derive PC Dates -Here we use `{admiral}` functions for working with dates and we will also create a nominal time from first dose `NFRLT` for `PC` data based on `PCTPTNUM`. +Here we use `{admiral}` functions for working with dates and we will also create a nominal time from first dose `NFRLT` for `PC` data using `derive_var_nfrlt()`. ```{r} #| label: PC Dates @@ -103,8 +103,14 @@ pc_dates <- pc %>% # Derive event ID and nominal relative time from first dose (NFRLT) mutate( EVID = 0, - DRUG = PCTEST, - NFRLT = if_else(PCTPTNUM < 0, 0, PCTPTNUM), .after = USUBJID + DRUG = PCTEST + ) %>% + derive_var_nfrlt( + new_var = NFRLT, + new_var_unit = FRLTU, + out_unit = "HOURS", + tpt_var = PCTPT, + visit_day = VISITDY ) ``` @@ -115,7 +121,7 @@ print_df(pc_dates %>% select(USUBJID, PCTEST, ADTM, VISIT, PCTPT, NFRLT)) ### Get Dosing Information -Here we also create nominal time from first dose `NFRLT` for `EX` data based on `VISITDY`. +Here we also create nominal time from first dose `NFRLT` for `EX` data based on `VISITDY` using `derive_var_nfrlt()`. ```{r} #| label: Dosing @@ -145,11 +151,13 @@ ex_dates <- ex %>% ) %>% # Derive event ID and nominal relative time from first dose (NFRLT) mutate( - EVID = 1, - NFRLT = case_when( - VISITDY == 1 ~ 0, - TRUE ~ 24 * VISITDY - ) + EVID = 1 + ) %>% + derive_var_nfrlt( + new_var = NFRLT, + new_var_unit = FRLTU, + out_unit = "HOURS", + visit_day = VISITDY ) %>% # Set missing end dates to start date mutate(AENDTM = case_when( From 05feee784b81e2d56a8c3a5fc8b86b2dba83c3a5 Mon Sep 17 00:00:00 2001 From: Jeff Dickinson Date: Wed, 14 Jan 2026 17:38:48 -0600 Subject: [PATCH 2/3] #134 spelling and units --- adam/adpc.R | 6 +++--- adam/adpc.qmd | 6 +++--- adam/adppk.R | 4 ++-- adam/adppk.qmd | 4 ++-- inst/WORDLIST | 2 ++ 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/adam/adpc.R b/adam/adpc.R index c0543d17..f285f844 100644 --- a/adam/adpc.R +++ b/adam/adpc.R @@ -241,7 +241,7 @@ adpc_arrlt <- bind_rows(adpc_nom_next, ex_exp) %>% new_var = AFRLT, start_date = FANLDTM, end_date = ADTM, - out_unit = "hours", + out_unit = "HOURS", floor_in = FALSE, add_one = FALSE ) %>% @@ -251,7 +251,7 @@ adpc_arrlt <- bind_rows(adpc_nom_next, ex_exp) %>% new_var_unit = RRLTU, start_date = ADTM_prev, end_date = ADTM, - out_unit = "hours", + out_unit = "HOURS", floor_in = FALSE, add_one = FALSE ) %>% @@ -260,7 +260,7 @@ adpc_arrlt <- bind_rows(adpc_nom_next, ex_exp) %>% new_var = AXRLT, start_date = ADTM_next, end_date = ADTM, - out_unit = "hours", + out_unit = "HOURS", floor_in = FALSE, add_one = FALSE ) %>% diff --git a/adam/adpc.qmd b/adam/adpc.qmd index 8decac79..bb4fb247 100644 --- a/adam/adpc.qmd +++ b/adam/adpc.qmd @@ -346,7 +346,7 @@ adpc_arrlt <- bind_rows(adpc_nom_next, ex_exp) %>% new_var = AFRLT, start_date = FANLDTM, end_date = ADTM, - out_unit = "hours", + out_unit = "HOURS", floor_in = FALSE, add_one = FALSE ) %>% @@ -356,7 +356,7 @@ adpc_arrlt <- bind_rows(adpc_nom_next, ex_exp) %>% new_var_unit = RRLTU, start_date = ADTM_prev, end_date = ADTM, - out_unit = "hours", + out_unit = "HOURS", floor_in = FALSE, add_one = FALSE ) %>% @@ -365,7 +365,7 @@ adpc_arrlt <- bind_rows(adpc_nom_next, ex_exp) %>% new_var = AXRLT, start_date = ADTM_next, end_date = ADTM, - out_unit = "hours", + out_unit = "HOURS", floor_in = FALSE, add_one = FALSE ) %>% diff --git a/adam/adppk.R b/adam/adppk.R index 91faf209..8e628673 100644 --- a/adam/adppk.R +++ b/adam/adppk.R @@ -219,7 +219,7 @@ adppk_aprlt <- bind_rows(adppk_nom_prev, ex_exp) %>% new_var = AFRLT, start_date = FANLDTM, end_date = ADTM, - out_unit = "hours", + out_unit = "HOURS", floor_in = FALSE, add_one = FALSE ) %>% @@ -228,7 +228,7 @@ adppk_aprlt <- bind_rows(adppk_nom_prev, ex_exp) %>% new_var = APRLT, start_date = ADTM_prev, end_date = ADTM, - out_unit = "hours", + out_unit = "HOURS", floor_in = FALSE, add_one = FALSE ) %>% diff --git a/adam/adppk.qmd b/adam/adppk.qmd index 8d6433d8..242bff3e 100644 --- a/adam/adppk.qmd +++ b/adam/adppk.qmd @@ -336,7 +336,7 @@ adppk_aprlt <- bind_rows(adppk_nom_prev, ex_exp) %>% new_var = AFRLT, start_date = FANLDTM, end_date = ADTM, - out_unit = "hours", + out_unit = "HOURS", floor_in = FALSE, add_one = FALSE ) %>% @@ -345,7 +345,7 @@ adppk_aprlt <- bind_rows(adppk_nom_prev, ex_exp) %>% new_var = APRLT, start_date = ADTM_prev, end_date = ADTM, - out_unit = "hours", + out_unit = "HOURS", floor_in = FALSE, add_one = FALSE ) %>% diff --git a/inst/WORDLIST b/inst/WORDLIST index a57298a7..4ee2a1e6 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -464,6 +464,7 @@ NCA nDate nDose nestcolor +nfrlt NFRLT nFrom nLTRs @@ -733,6 +734,7 @@ TMPTC toc topleft toupper +tpt TPT TPTNUM traceback From fa05d0fcf00f7bd4a77344dd62c8b1b1dd03fca2 Mon Sep 17 00:00:00 2001 From: Jeff Dickinson Date: Tue, 3 Feb 2026 10:05:01 -0600 Subject: [PATCH 3/3] #134 also fix typo in BSA and warning from create_var_from_codelist() --- adam/adpc.R | 2 +- adam/adpc.qmd | 2 +- adam/adppk.R | 6 +++--- adam/adppk.qmd | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/adam/adpc.R b/adam/adpc.R index f285f844..83e6cc2b 100644 --- a/adam/adpc.R +++ b/adam/adpc.R @@ -422,7 +422,7 @@ adpc_aseq <- adpc_chg %>% create_var_from_codelist(metacore, input_var = PARAMCD, out_var = PARAMN) ## ----r------------------------------------------------------------------------ -#---- Derive additional baselines from VS ---- +# ---- Derive additional baselines from VS ---- adpc_baselines <- adpc_aseq %>% derive_vars_merged( diff --git a/adam/adpc.qmd b/adam/adpc.qmd index bb4fb247..0276bb87 100644 --- a/adam/adpc.qmd +++ b/adam/adpc.qmd @@ -585,7 +585,7 @@ Here we derive additional baseline values from `VS` for baseline height `HTBL` a ```{r} #| label: Baselines -#---- Derive additional baselines from VS ---- +# ---- Derive additional baselines from VS ---- adpc_baselines <- adpc_aseq %>% derive_vars_merged( diff --git a/adam/adppk.R b/adam/adppk.R index 8e628673..b0acc5f6 100644 --- a/adam/adppk.R +++ b/adam/adppk.R @@ -335,7 +335,7 @@ adppk_aseq <- adppk_aval %>% ) ## ----r------------------------------------------------------------------------ -#---- Derive Covariates ---- +# ---- Derive Covariates ---- # Include numeric values for STUDYIDN, USUBJIDN, SEXN, RACEN etc. covar <- adsl %>% @@ -392,7 +392,7 @@ covar_vslb <- covar %>% BMIBL = compute_bmi(height = HTBL, weight = WTBL), BSABL = compute_bsa( height = HTBL, - weight = HTBL, + weight = WTBL, method = "Mosteller" ), CRCLBL = compute_egfr( @@ -421,7 +421,7 @@ adppk_prefinal <- adppk_aseq %>% RECSEQ = row_number(), EXCLFCOM = "None" ) %>% - create_var_from_codelist(metacore, input_var = DVID, out_var = DVIDN) %>% + create_var_from_codelist(metacore, input_var = DVID, out_var = DVIDN, strict = FALSE) %>% create_var_from_codelist(metacore, input_var = EXCLFCOM, out_var = EXCLF) ## ----r------------------------------------------------------------------------ diff --git a/adam/adppk.qmd b/adam/adppk.qmd index 242bff3e..9efd130c 100644 --- a/adam/adppk.qmd +++ b/adam/adppk.qmd @@ -482,7 +482,7 @@ In this step we will create our numeric covariates using the `metatools::create_ ```{r} #| label: Covariates -#---- Derive Covariates ---- +# ---- Derive Covariates ---- # Include numeric values for STUDYIDN, USUBJIDN, SEXN, RACEN etc. covar <- adsl %>% @@ -551,7 +551,7 @@ covar_vslb <- covar %>% BMIBL = compute_bmi(height = HTBL, weight = WTBL), BSABL = compute_bsa( height = HTBL, - weight = HTBL, + weight = WTBL, method = "Mosteller" ), CRCLBL = compute_egfr( @@ -593,7 +593,7 @@ adppk_prefinal <- adppk_aseq %>% RECSEQ = row_number(), EXCLFCOM = "None" ) %>% - create_var_from_codelist(metacore, input_var = DVID, out_var = DVIDN) %>% + create_var_from_codelist(metacore, input_var = DVID, out_var = DVIDN, strict = FALSE) %>% create_var_from_codelist(metacore, input_var = EXCLFCOM, out_var = EXCLF) ```