# qc-demo.R # Independent double-programming demo comparing two derivations with {diffdf}. # # Run with: # Rscript qc-demo.R suppressPackageStartupMessages({ library(diffdf) }) # Production derivation of a minimal ADSL-like dataset from SDTM DM. derive_adsl_prod <- function(dm = pharmaversesdtm::dm) { df <- data.frame( STUDYID = dm$STUDYID, USUBJID = dm$USUBJID, AGE = dm$AGE, AGEGR1 = ifelse(dm$AGE < 65, "<65", ">=65"), SEX = dm$SEX, RACE = dm$RACE, TRT01A = dm$ACTARM, stringsAsFactors = FALSE ) attr(df$AGEGR1, "label") <- "Pooled Age Group 1" attr(df$TRT01A, "label") <- "Actual Treatment Arm" df } # QC derivation with a planted boundary discrepancy (<= 65 vs < 65). derive_adsl_qc_buggy <- function(dm = pharmaversesdtm::dm) { df <- data.frame( STUDYID = dm$STUDYID, USUBJID = dm$USUBJID, AGE = dm$AGE, AGEGR1 = ifelse(dm$AGE <= 65, "<65", ">=65"), SEX = dm$SEX, RACE = dm$RACE, TRT01A = dm$ACTARM, stringsAsFactors = FALSE ) attr(df$AGEGR1, "label") <- "Pooled Age Group 1" attr(df$TRT01A, "label") <- "Actual Treatment Arm" df } # Fixed QC derivation resolving the discrepancy to match the specification. derive_adsl_qc_clean <- function(dm = pharmaversesdtm::dm) { df <- data.frame( STUDYID = dm$STUDYID, USUBJID = dm$USUBJID, AGE = dm$AGE, AGEGR1 = ifelse(dm$AGE < 65, "<65", ">=65"), SEX = dm$SEX, RACE = dm$RACE, TRT01A = dm$ACTARM, stringsAsFactors = FALSE ) attr(df$AGEGR1, "label") <- "Pooled Age Group 1" attr(df$TRT01A, "label") <- "Actual Treatment Arm" df } # Reconcile primary and QC datasets using diffdf keyed by USUBJID. compare_adsl <- function( base, compare, keys = "USUBJID", tolerance = sqrt(.Machine$double.eps) ) { diffdf( base = base, compare = compare, keys = keys, suppress_warnings = TRUE, tolerance = tolerance ) } if (sys.nframe() == 0L) { dm <- pharmaversesdtm::dm prod <- derive_adsl_prod(dm) qc_buggy <- derive_adsl_qc_buggy(dm) qc_clean <- derive_adsl_qc_clean(dm) cat("=== Round 1: Primary vs QC with Planted Boundary Bug ===\n") diff_round1 <- compare_adsl(prod, qc_buggy) print(diff_round1) cat(sprintf("Has issues: %s\n\n", diffdf_has_issues(diff_round1))) cat("Issue rows subset from production dataset:\n") print(diffdf_issuerows(prod, diff_round1, vars = "AGEGR1")) cat("\n") cat("=== Round 2: Primary vs Fixed QC ===\n") diff_round2 <- compare_adsl(prod, qc_clean) print(diff_round2) cat(sprintf("Has issues: %s\n", diffdf_has_issues(diff_round2))) }