$npx -y skills add RConsortium/pharma-skills --skill clinical-trial-ipd-simEnd-to-end R/pharmaverse workflow to simulate individual patient data (IPD) for a registered clinical trial using a g-formula causal-DAG simulator. Given an NCT ID with posted protocol + results, derives SDTM-style CRFs, builds evidence-based structural causal models, parameteriz
| 1 | # Clinical Trial IPD Causal-DAG Simulator |
| 2 | |
| 3 | End-to-end R workflow for generating individual-patient-level CRF data for a |
| 4 | registered clinical trial. The output is a set of CSV CRFs whose marginal |
| 5 | statistics match the published trial results AND whose joint distribution |
| 6 | follows an explicit causal DAG, so the data are usable for downstream |
| 7 | causal-inference methodology. |
| 8 | |
| 9 | The implementation target is R-first and pharmaverse-compatible. The Python |
| 10 | FLAURA2 build used during development remains a worked example of the causal |
| 11 | logic, but new work should create an R package-style project unless the user |
| 12 | explicitly asks for Python. See [r_implementation.md](r_implementation.md) |
| 13 | for the R module layout and package roles. |
| 14 | |
| 15 | This skill generalizes the FLAURA2 causal-DAG workflow into an |
| 16 | R/pharmaverse implementation target. |
| 17 | |
| 18 | ## When to use |
| 19 | |
| 20 | The user provides an NCT ID **and** the trial has both a posted protocol and |
| 21 | posted results. Examples: |
| 22 | |
| 23 | - "Simulate IPD for NCT04035486" → use this skill |
| 24 | - "Generate CRFs for the AEGEAN trial" → look up NCT, then use this skill |
| 25 | - "Create a digital-twin cohort for trial X" → use this skill |
| 26 | - "I need synthetic patients matching the published KM curves of …" → use this skill |
| 27 | |
| 28 | Do **not** use when the user only wants summary-level reconstruction (Cox HR, |
| 29 | KM medians, AE proportions). For that, reach for tabular reconstruction |
| 30 | methods (e.g., reconstructed IPD from KM digitization). |
| 31 | |
| 32 | ## Required environment |
| 33 | |
| 34 | - R 4.3+ with `renv` for dependency locking. |
| 35 | - Core simulation/analysis: `dplyr`, `tidyr`, `purrr`, `readr`, `tibble`, |
| 36 | `lubridate`, `stringr`, `rlang`, `survival`, `flexsurv`, `broom`, `jsonlite`. |
| 37 | - Pharmaverse/CDISC stack: |
| 38 | - `sdtm.oak` for SDTM-oriented CRF/domain construction from generated source records. |
| 39 | - `admiral` for ADaM derivations such as ADSL, ADAE, ADLB, ADTTE. |
| 40 | - `haven` for reading/writing SAS transport-adjacent inputs and SAS datasets when needed. |
| 41 | - `tidytlg` for TLG generation from ADaM outputs. |
| 42 | - `xportr` for XPT metadata handling and transport export. |
| 43 | - `datasetjson` for Dataset-JSON export when requested. |
| 44 | - Network access for the ClinicalTrials.gov API (`https://clinicaltrials.gov/api/v2/studies/{nct}`) |
| 45 | - Citation-capable literature and standards lookup for causal-parent |
| 46 | evidence, natural-history rates, priors, CTCAE, and RECIST references. |
| 47 | Record stable source identifiers for all non-CTGov evidence; do not rely |
| 48 | on model memory for cited claims. |
| 49 | - `WebFetch` or equivalent only for fetching specific known URLs |
| 50 | (the ClinicalTrials.gov API, a protocol PDF, a publication supplement) |
| 51 | |
| 52 | ## Workflow — six steps |
| 53 | |
| 54 | ### Step 1 · Intake from ClinicalTrials.gov |
| 55 | |
| 56 | 1. Fetch the trial JSON record: |
| 57 | `https://clinicaltrials.gov/api/v2/studies/{NCT_ID}?format=json` |
| 58 | 2. Extract: |
| 59 | - **Design**: arms, randomization ratio, stratification factors, primary/secondary endpoints |
| 60 | - **Eligibility**: inclusion/exclusion criteria → baseline population priors |
| 61 | - **Outcomes table**: per-arm event counts, medians, hazard ratios with 95% CIs |
| 62 | - **Adverse events table**: per-arm SOC/PT counts at any-grade, Gr ≥3, SAE |
| 63 | - **Protocol document URL** (under `ProtocolSection.IPDSharingStatementModule` or `LargeDocumentsModule`) |
| 64 | 3. Locate the **schedule of activities (SoA)** in the protocol PDF/HTML. |
| 65 | If the SoA cannot be parsed automatically, ask the user to point at the |
| 66 | relevant pages, or scaffold a default visit grid based on the dosing schedule. |
| 67 | If ClinicalTrials.gov does not provide a protocol, search for the protocol |
| 68 | or supplementary appendix in the New England Journal of Medicine (NEJM). |
| 69 | If neither source has a protocol, ask the user to provide one and do not |
| 70 | proceed until it is available. |
| 71 | 4. Persist the intake to `intake/{NCT_ID}.json` so later steps don't re-fetch. |
| 72 | |
| 73 | **Output of step 1**: structured trial summary with design, endpoint targets, |
| 74 | AE targets, and SoA visit grid. |
| 75 | |
| 76 | ### Step 2 · CRF derivation from the protocol |
| 77 | |
| 78 | Read the protocol's SoA and generate a CDISC-style CRF schema: |
| 79 | |
| 80 | | Form | Visits collected | Variables | Source | |
| 81 | |---|---|---|---| |
| 82 | | Demographics | Screening | AGE, SEX, RACE, COUNTRY, ARM | Protocol §X | |
| 83 | | Cancer/Disease History | Screening | Diagnosis, stage, |