# Final Analysis ready data {#sec-ARD-final}```{r ard01, warning=FALSE, message=FALSE, echo=FALSE}## Load libraries for this chapterlibrary(readr)library(dplyr)library(tidyr)library(ggplot2)library(tidyselect)library(here)library(janitor)library(gt)library(gtExtras)library(patchwork)library(sjlabelled)## Load the datasets - make sure to load the correct ADLB2 ADSL <-readRDS(here::here("data", "IDA", "ADSL_01.rds"))ADLB <-readRDS(here::here("data", "IDA", "ADLB_02.rds"))```## Final derivations and transformationsFrom the IDA exercise a decision was made to derive a new variable based on `White Blood Count` in the absence of Neutrophiles: * WBC_NEU: WBC - NEUThe new derivation will also be checked as a candidate for pseudo-log transformation. ```{r final-ders, warning=FALSE, message=FALSE, echo=FALSE, fig.height = 3}source(here("R", "fun_ida_trans.R"))source(here("R", "fun_compare_dist_plot.R"))wbc_no_neu_01 <- ADLB |>select(USUBJID, PARAMCD, AVAL) |>filter(PARAMCD %in%c("WBC", "NEU")) |>pivot_wider(names_from ="PARAMCD", values_from ="AVAL") |>mutate(AVAL = WBC - NEU,PARAMCD ="WBC_noNEU",PARAM ="White blood count without Neutrophiles (G/L)",PARAMTYPE ="DERVIVED") |>select(-WBC, -NEU)wbc_no_neu_trans_01 <-ida_trans(wbc_no_neu_01)wbc_no_neu_trans_02 <- wbc_no_neu_trans_01$dat## plot side by sidecompare_dist_plot(wbc_no_neu_trans_02)```A visual check confirms that a pseudo-log transformation is appropriate for the newly derived variable. The new derivation and transformation is added to the analysis ready data, setting the correct indicator flags to enable final analyses and modelling. ```{r final-steps, warning=FALSE, message=FALSE, echo=FALSE, fig.height = 3}ADLB_02 <-bind_rows(ADLB, wbc_no_neu_01, wbc_no_neu_trans_02) ADLB_02 <- sjlabelled::copy_labels(ADLB_02, ADLB) ``````{r set_flags, warning=FALSE, message=FALSE, echo=FALSE} ADLB_03 <- ADLB_02 |>mutate(KEY_PRED_FL03 =case_when( PARAMCD %in%c("AGE", "WBC_noNEU", "BUN", "CREA", "PLT", "NEU") ~"Y" ),KEY_PRED_FL04 =case_when( PARAMCD %in%c("AGE", "WBC_noNEU_T", "BUN", "CREA_T", "PLT", "NEU") ~"Y" ) )attr(ADLB_03$KEY_PRED_FL03, "label") <-"Key Predictor Flag - original"attr(ADLB_03$KEY_PRED_FL04, "label") <-"Key Predictor Flag - including transformed variables"```Visual check of indicator flag and new derivation added to the analysis data set. ```{r check_flags, warning=FALSE, message=FALSE, echo=FALSE}vars <- ADLB_03 |>filter(KEY_PRED_FL03 =="Y") |>select(PARAMCD) |>distinct() |>as.list()#paste(vars, collapse = ', ')print(paste(vars, collapse =', '))```## Save final analysis ready data sets The final analysis ready data sets are then stored in `data/Analysis`. ```{r save_data, warning=FALSE, message=FALSE, echo=FALSE}saveRDS(ADSL, file =here("data","Analysis", "ADSL.rds"))saveRDS(ADLB_03, file =here("data","Analysis", "ADLB.rds"))```