Clinical Medicine & Epidemiology
Randomized clinical trial: patients → visits → labs, plus adverse events
A parallel-group trial stored the way data managers actually store it: a subject table, a visit table with repeated systolic blood pressure, a long-format lab table (one row per analyte per visit) and an adverse-event table whose row count depends on the arm.
patients (120) → visits (6) → labs (3 analytes); patients → adverse_events (Poisson, arm-dependent)
Data structure
Subject / visit / finding / event tables (as in CDISC SDTM) are the most requested structure for teaching mixed models, missing data and safety summaries.
- patientsone row = one randomized participant
- Arm, age, sex and baseline systolic blood pressure (SBP). Aggregate: number of adverse events.
- visitsone row = one scheduled visit
- Weeks 0–20. SBP falls toward an arm-specific plateau, each patient has a random slope, and later visits go missing more often when the previous value was high (MAR).
- labsone row = one analyte at one visit
- ALT, creatinine and CRP in long format with log-normal variation and a lower limit of quantification (LLOQ) at 0.3.
- adverse_eventsone row = one reported event
- Count per patient is Poisson with a higher rate in the active arm; term, severity, relatedness and onset day.
Generate the clinical trial (rct) tables
The preset is a starting point: change any formula, noise step or row count and the preview, ER diagram and relation check update.
Key relationships
sbp = parent.baseline_sbp - if(parent.arm == "active", 12, 3) * (1 - exp(-week / 6)) + group_effect(parent.patient_id, 0.15) * weekExponential approach to the treatment effect plus a patient-level random slope: group_effect gives each patient one shared draw.
missing_if: week >= 12 && lag(sbp, 1, 0) > 155 (p = 0.4)Missing at random: whether a visit is missing depends on the previous observed value, not on the missing value itself.
rows per patient ~ Poisson(if(arm == "active", 1.1, 0.7))Child row counts can depend on the parent row, so the number of adverse events differs by arm.
Common pitfalls
- Running a t-test on the last observed visit (LOCF): it ignores the dropout mechanism and understates variance.
- Pivoting long-format labs to wide without keeping units; mixing mg/dL and µmol/L creatinine is a classic merge error.
- Replacing values below the LLOQ with 0 or dropping them; treat them as left-censored, or impute LLOQ/2 and say so.
- Comparing raw adverse-event counts between arms without accounting for exposure time.
Typical analysis
Mixed model for repeated measures (MMRM) or a linear mixed model with random intercept and slope; under MAR this is valid without imputation. Adverse events: Poisson or negative-binomial regression of counts per patient by arm.
library(lme4)
patients <- read.csv("patients.csv", comment.char = "#"); visits <- read.csv("visits.csv", comment.char = "#")
d <- merge(visits, patients, by = "patient_id")
fit <- lmer(sbp ~ arm * factor(week) + baseline_sbp + (1 + week | patient_id), data = d)
summary(fit)
ae <- glm(n_adverse_events ~ arm, family = poisson, data = patients)Further reading
- Bates D, Mächler M, Bolker B, Walker S (2015). Fitting linear mixed-effects models using lme4. Journal of Statistical Software 67(1).
- National Research Council (2010). The Prevention and Treatment of Missing Data in Clinical Trials. National Academies Press.
Other relational presets
- Dose–response plates : plates (6) → wells (30 per plate) → readouts (2 per well)
- Materials tensile test : specimens (10) → load_steps (60) → strain (2 channels)
- Mouse behaviour : mice (24, 4 per cage) → sessions (8 days) → trials (20)
- All presets and the formula reference