# Flagship worked example: one synthetic bioequivalence study

Use the [point-and-click toolkit setup](https://patrickiben.github.io/biometrics-ai-portfolio/TOOLKIT_SETUP.html) to create a private project from the examples, restore the study packages, check Quarto and rebuild this synthetic study. Source adaptation is separate from reproduction; the generated SAP settings file is overwritten by the run.

One synthetic early-phase study, SIMBE01 (a single-dose 2x2 crossover bioequivalence study of
a test and a reference tablet, N = 40), taken through every lifecycle stage and regenerated by
one script. Page: [`index.html`](index.html).

**Synthetic data only.** The product, the study and every participant are simulated. Nothing
here is real trial data, a validated system or a regulatory deliverable.

## Stages

| Stage | Folder | What runs | Job evidence (resume) |
|---|---|---|---|
| 1 Design | [`01_design/`](01_design/protocol_statistics.html) | PowerTOST sample size at 80% and 90% power with a sensitivity grid and dropout inflation; blockrand permuted-block schedule (TR/RT, seed 2026) with a balance check; protocol statistics excerpt with ICH M11 headings | Led: protocol statistical sections and randomization schedules |
| 2 SAP | [`02_sap/`](02_sap/sap_excerpt.html) | Estimand (five ICH E9(R1) attributes, intercurrent-event strategies), analysis sets, the ICH M13A model, BLQ and missing-data rules, [mock shells](02_sap/mock_shells.html); settings in `analysis_parameters.json` feed the code | Led: analysis plans and mock TLFs |
| 3 ADaM | [`03_adam/`](03_adam/index.html) | SDTM-like DM, EX, PC, AE, VS, DS; ADSL, ADPC, ADPP, ADAE with admiral 1.5.0 (ad_adpc.R and ad_adpp.R templates; NFRLT/AFRLT); specification in Pinnacle 21 layout (xlsx) and JSON read by `metacore::spec_to_metacore()`; metatools checks; XPT v5 with xportr; Dataset-JSON v1.1 (not accepted by FDA as of Oct 2026) | Supported: created or reviewed CDISC analysis specifications. Gap: define.xml and ADRG are not authored |
| 4 Analysis | [`04_analysis/`](04_analysis/index.html) | PKNCA NCA with BLQ kept as zero (ICH M13A), with its effect against the PKNCA default shown; the SAP parameter rules (adjusted R-squared, AUC(0-inf) extrapolation) applied with PKNCA `exclude()`; the M13A GLM with all four effect tests and the 90% CI; supplementary analyses, including one without the participants whose Cmax is at the first post-dose sample (M13A 2.1.8.1); six rtables/tern tables to RTF (`formatters::export_as_rtf()`) and HTML; figures; a [Quarto report](04_analysis/report/report.html) | Led: PK analyses for crossover and BE studies |
| 5 QC | [`05_qc/`](05_qc/qc_ledger.html) | Same-author re-derivation: NonCompart vs PKNCA (including the BLQ scenario and the parameter-rule flags), the BE model by a closed-form period-difference t-test and by `nlme::lme()`, base-R rebuilds of ADPC times, population flags and AE counts, all compared with diffdf at stated tolerances; two controls that must fail in a known way | Supported: reviewed outputs. Not independent double programming |
| 6 CSR | [`06_csr/`](06_csr/csr_section11.html) | Section 11-style text with every number read from an output, filled into fixed sentence templates (written with an AI coding assistant; no language model runs in the pipeline), checked by the `tlf_interpret` number-grounding validator, with controls that show what it catches and what it cannot | Supported: statistical input to CSRs; Led: final statistical reports |

## Run

```sh
Rscript -e 'install.packages("renv"); renv::restore(project = "study", prompt = FALSE)'
Rscript study/run_all.R          # from the repository root; QUARTO=/path/to/quarto if not on PATH
```

`run_all.R` loads `study/renv/library` when it exists, runs the stages in order, and renders the
report with Quarto 1.10.19. Two consecutive runs on R 4.6.0 (macOS) produced byte-identical files.
XPT, XLSX and Dataset-JSON files carry write timestamps, so they are replaced only when their
content changes. `.github/workflows/study.yml` runs the script on Ubuntu with r-lib/actions v2 and
fails if a committed output changes (figures and the rendered report are excluded from that byte
check because PNG rendering depends on system fonts). It runs on GitHub on every push and pull
request that touches `study/` or `sim_lifecycle/`; it first passed on main on 25 October 2026.
`R/snapshot_lock.R` rewrites `renv.lock`.

## Builds on `sim_lifecycle/`

The SDTM-like layer reuses `sim_lifecycle/priors.R` (demographics, PK and adverse-event priors)
and the one-compartment model in `sim_lifecycle/simulate_adam.R`, with the same design (2x2,
N = 40, seed 2026). It adds the randomization schedule, calendar dates, actual sampling times
and six documented scenarios (a withdrawal, emesis after dosing, a pre-dose carry-over value
below the 5% rule, a missed sample, a late sample and a BLQ result between quantifiable
samples). `sim_lifecycle/` itself is unchanged.

## Not covered

define.xml and ADRG authoring; Pinnacle 21 or CDISC CORE conformance; conformant SDTM;
independent double programming by a second person; a validated (21 CFR Part 11) system; a
regulatory submission package; interim analyses and a DMC; SAS (the SAS TLF library is not
executed here).
