Response plan to replicate a table from ADaM, with R code
Paste an agency request, SAP text and define.xml comments, get a step-by-step plan and the R script that was run to test it.
About 45 minWord and RWorked example, recomputed
What you get
# Executes the cold GOOD response plan (fda-ir-replication-instructions) step by step in R on the
# unmodified CDISC pilot ADaM files, then compares to CSR Table 14-3.01 (typed from the CSR, PDF p.49).
# Usage: Rscript replicate_14-3-01.R <folder with adam_adsl.xpt and adam_adqsadas.xpt>
# Data: CDISC SDTM/ADaM Pilot Project, used unmodified, attribution to CDISC.
#
# Where the plan left a gap (its OPEN QUESTIONS) this script had to choose; each choice is marked CHOICE.
# Needs the R packages haven and dplyr (install.packages(c("haven","dplyr")) if missing).
suppressMessages({library(haven); library(dplyr)})
d <- commandArgs(TRUE)[1]
adsl <- read_xpt(file.path(d, "adam_adsl.xpt"))
adqs <- read_xpt(file.path(d, "adam_adqsadas.xpt"))
# Plan step 1: ADSL, EFFFL == "Y"
adsl_eff <- adsl %>% filter(EFFFL == "Y") %>% select(USUBJID, TRT01P, TRT01PN, SITEGR1)
cat("Step 1 efficacy subjects in ADSL:", nrow(adsl_eff), "\n")
# Plan steps 2-3: PARAMCD == ACTOT, Week 24 (CHOICE: AVISITN == 24, open question 2), ANL01FL == "Y"; DTYPE not filtered
q <- adqs %>% filter(PARAMCD == "ACTOT", AVISITN == 24, ANL01FL == "Y") %>% select(USUBJID, AVAL, BASE, CHG, DTYPE)
cat("Step 3 rows:", nrow(q), " subjects:", n_distinct(q$USUBJID), " DTYPE:", paste(names(table(q$DTYPE)), table(q$DTYPE), collapse = "; "), "\n")
stopifnot(!any(duplicated(q$USUBJID))) # plan check: one row per subject
# Plan step 4: inner join
dd <- inner_join(adsl_eff, q, by = "USUBJID")
cat("Step 4 joined rows:", nrow(dd), " efficacy subjects with no Week 24 row:", sum(!adsl_eff$USUBJID %in% q$USUBJID), "\n")
dd$site <- factor(dd$SITEGR1)
dd$trt <- factor(dd$TRT01P, levels = c("Placebo", "Xanomeline Low Dose", "Xanomeline High Dose"))
dd$dose <- dd$TRT01PN # CHOICE: open question 4; plan names TRT01PN. Values in the file: 0, 54, 81
cat("Dose scores used:", paste(sort(unique(dd$dose)), collapse = ", "), "\n")
cat("Missing BASE or CHG:", sum(is.na(dd$BASE) | is.na(dd$CHG)), "\n")
cat("Step 5 check CHG == AVAL - BASE:", all(abs(dd$CHG - (dd$AVAL - dd$BASE)) < 1e-9), "\n")
rh <- function(x, nd) { s <- ifelse(x < 0, -1, 1); s * floor(abs(x) * 10^nd + 0.5 + 1e-9) / 10^nd } # SAS-style half up
f <- function(x, nd) formatC(rh(x, nd), format = "f", digits = nd)
# Plan step 6: descriptives
mine <- list()
for (g in levels(dd$trt)) for (v in c("BASE", "AVAL", "CHG")) {
x <- dd[[v]][dd$trt == g]
mine[[paste(g, v, "n")]] <- as.character(length(x))
mine[[paste(g, v, "meansd")]] <- sprintf("%s (%s)", f(mean(x), 1), f(sd(x), 2))
mine[[paste(g, v, "medrange")]] <- sprintf("%s (%s;%s)", f(median(x), 1), f(min(x), 0), f(max(x), 0))
}
# Plan step 7: dose-response, CHG ~ BASE + site + dose (numeric); p of the dose coefficient (two-sided t-test)
fit_dose <- lm(CHG ~ BASE + site + dose, data = dd)
p_dose <- summary(fit_dose)$coefficients["dose", "Pr(>|t|)"]
# Plan step 8-9: categorical model; differences of LS means. With no interaction the LS mean difference is the
# coefficient contrast, so site weighting (open question 9) does not change it.
fit_cat <- lm(CHG ~ BASE + site + trt, data = dd)
b <- coef(fit_cat); V <- vcov(fit_cat); dfr <- df.residual(fit_cat)
cn <- c("trtXanomeline Low Dose", "trtXanomeline High Dose")
contr <- function(w) { est <- sum(w * b[names(w)]); se <- sqrt(drop(t(w) %*% V[names(w), names(w)] %*% w))
tq <- qt(0.975, dfr); c(est = est, se = se, lo = est - tq * se, hi = est + tq * se, p = 2 * pt(-abs(est / se), dfr)) }
cmp <- list(
low_pbo = contr(setNames(1, cn[1])), # CHOICE: direction Xanomeline minus comparator
high_pbo = contr(setNames(1, cn[2])),
high_low = contr(setNames(c(1, -1), cn[c(2, 1)])))
res <- function(est) sprintf("%s (%s)", f(est["est"], 1), f(est["se"], 2))
ci <- function(est) sprintf("(%s;%s)", f(est["lo"], 1), f(est["hi"], 1))
# CSR Table 14-3.01 (PDF p.49), typed from pdftotext output
csr <- c(
"Placebo BASE n"="79","Placebo BASE meansd"="24.1 (12.19)","Placebo BASE medrange"="21.0 (5;61)",
"Placebo AVAL n"="79","Placebo AVAL meansd"="26.7 (13.79)","Placebo AVAL medrange"="24.0 (5;62)",
"Placebo CHG n"="79","Placebo CHG meansd"="2.5 (5.80)","Placebo CHG medrange"="2.0 (-11;16)",
"Xanomeline Low Dose BASE n"="81","Xanomeline Low Dose BASE meansd"="24.4 (12.92)","Xanomeline Low Dose BASE medrange"="21.0 (5;57)",
"Xanomeline Low Dose AVAL n"="81","Xanomeline Low Dose AVAL meansd"="26.4 (13.18)","Xanomeline Low Dose AVAL medrange"="25.0 (6;62)",
"Xanomeline Low Dose CHG n"="81","Xanomeline Low Dose CHG meansd"="2.0 (5.55)","Xanomeline Low Dose CHG medrange"="2.0 (-11;17)",
"Xanomeline High Dose BASE n"="74","Xanomeline High Dose BASE meansd"="21.3 (11.74)","Xanomeline High Dose BASE medrange"="18.0 (3;57)",
"Xanomeline High Dose AVAL n"="74","Xanomeline High Dose AVAL meansd"="22.8 (12.48)","Xanomeline High Dose AVAL medrange"="20.0 (3;62)",
"Xanomeline High Dose CHG n"="74","Xanomeline High Dose CHG meansd"="1.5 (4.26)","Xanomeline High Dose CHG medrange"="1.0 (-7;13)")
mine2 <- c(unlist(mine),
"p dose response" = unname(f(p_dose, 3)),
"Low-Pbo p" = unname(f(cmp$low_pbo["p"], 3)), "Low-Pbo diff (SE)" = res(cmp$low_pbo), "Low-Pbo 95% CI" = ci(cmp$low_pbo),
"High-Pbo p" = unname(f(cmp$high_pbo["p"], 3)), "High-Pbo diff (SE)" = res(cmp$high_pbo), "High-Pbo 95% CI" = ci(cmp$high_pbo),
"High-Low p" = unname(f(cmp$high_low["p"], 3)), "High-Low diff (SE)" = res(cmp$high_low), "High-Low 95% CI" = ci(cmp$high_low))
csr2 <- c(csr, "p dose response"="0.245",
"Low-Pbo p"="0.569","Low-Pbo diff (SE)"="-0.5 (0.82)","Low-Pbo 95% CI"="(-2.1;1.1)",
"High-Pbo p"="0.233","High-Pbo diff (SE)"="-1.0 (0.84)","High-Pbo 95% CI"="(-2.7;0.7)",
"High-Low p"="0.520","High-Low diff (SE)"="-0.5 (0.84)","High-Low 95% CI"="(-2.2;1.1)")
# The plan's "diff (SE)" for the CSR is printed with 1 decimal for diff and 2 for SE; compare as text.
stopifnot(identical(sort(names(csr2)), sort(names(mine2))))
ok <- mine2[names(csr2)] == csr2
for (n in names(csr2)) cat(sprintf("%-8s %-40s replicated=%-14s CSR=%s\n", ifelse(ok[n], "OK", "MISMATCH"), n, mine2[n], csr2[n]))
cat(sprintf("\n%d of %d cells match, %d mismatch.\n", sum(ok), length(ok), sum(!ok)))
cat(if (all(ok)) "RESULT: PASS\n" else "RESULT: FAIL\n")
cat("\nSensitivity (not part of the plan): dose scores 0,1,2 instead of 0,54,81 give dose-response p =",
f(tail(summary(lm(CHG ~ BASE + site + I(as.numeric(trt) - 1), data = dd))$coefficients[, 4], 1), 3), "\n")
nofl <- adqs %>% filter(PARAMCD == "ACTOT", AVISITN == 24) %>% inner_join(adsl_eff, by = "USUBJID")
cat("Sensitivity (not part of the plan): Week 24 ACTOT rows for efficacy subjects WITHOUT the ANL01FL filter:", nrow(nofl),
" subjects:", n_distinct(nofl$USUBJID), "\n")
Swipe sideways to see every column.
The script a person wrote by following the plan, with the choices the plan left open marked CHOICE.
Draft for review. Public data only.
The prompt
An agency asks how to replicate the table below from the submitted ADaM data. Draft the response plan from the
INPUT only: (A) numbered procedure with dataset, variables and the INPUT sentence that supports each step,
(B) pseudo-code a programmer could implement, (C) datasets and variables read, (D) pre-send checklist,
(E) 3 to 5 sentences of neutral cover text. State no numerical result.
Put anything missing in OPEN QUESTIONS, each once.
INPUT (request, SAP text, define.xml comments, table footnotes): [paste]Works in any chat assistant. Not tested in Microsoft 365 Copilot.
Sample input used here
Invented request plus real CDISC pilot metadata
[INVENTED for this example, not a real document. Paraphrase of an agency request, written by the page author.] "Please provide step-by-step instructions, with simplified code, showing how the primary analysis of the change from baseline in ADAS-Cog (11) at Week 24 (LOCF) was derived from the submitted ADaM datasets ADSL and ADQSADAS: the summary statistics, the test of dose response, and the pairwise treatment comparisons." [REAL: CDISC SDTM/ADaM Pilot Project, CDISCPILOT01. Variable labels read from adam_adsl.xpt and adam_adqsadas.xpt; comments quoted from adam_define.xml.] ADSL (one row per subject, 254 rows): USUBJID Unique Subject Identifier; SITEGR1 Pooled Site Group 1; TRT01P Planned Treatment for Period 01 (values Placebo, Xanomeline Low Dose, Xanomeline High Dose); TRT01PN Planned Treatment for Period 01 (N); ITTFL Intent-to-Treat Population Flag; SAFFL Safety Population Flag; EFFFL Efficacy Population Flag; COMP24FL Completers of Week 24 Population Flag. ADQSADAS (12463 rows): USUBJID; SITEGR1 Pooled Site Group 1 (comment: ADSL.SITEGR1); TRTP Planned Treatment; TRTPN Planned Treatment (N); EFFFL Efficacy Population Flag; COMP24FL; AVISIT Analysis Visit (comment: "Derived based on windowing algorithm described in SAP, Section 8.2"); AVISITN Analysis Visit (N); PARAM Parameter; PARAMCD Parameter Code (value ACTOT is labelled "Adas-Cog(11) Subscore"; ACITM01 to ACITM14 are the items); AVAL Analysis Value; BASE Baseline Value (comment: "QS.QSSTRESN when QS.QSBLFL=Y (QS.VISITNUM=3)"); CHG, label "Baseline Value" in the file, comment "AVAL - BASE"; ABLFL; ANL01FL Analysis Record Flag 01 (comment: "If multiple visits fall into the same visit window, then the one closest to the target date is chosen for analysis. These are flagged with ANL01FL=Y."); DTYPE Derivation Type (for ACTOT, comment: "Value: LOCF denotes that the LOCF imputation method was used to impute the value for the given parameter and analysis visit."). [REAL: Statistical Analysis Plan, CSR Appendix 9 (CSR PDF pages 424 to 429; SAP pp. 8, 9, 13)] SAP 6: "Efficacy population: All patients who were randomized and took drug, and have at least 1 post-baseline measure for both ADAS-Cog and CIBIC+." SAP 7.1: "Sites that enroll fewer than 3 patients in any one treatment group will be grouped together, with a new pooled site identifier assigned for the purpose of analysis." SAP 8.1: "Missing postbaseline efficacy values will be imputed using last observation carried forward (LOCF)." SAP 10.1.1: "The primary analysis of the ADAS-Cog (11) at Week 24 will use the efficacy population with LOCF imputation for any missing values at Week 24. ... an ANCOVA model will be used with the baseline score, site, and treatment included as independent variables. Treatment will be included as a continuous variable, and results for a test of dose response will be produced. Interaction terms will not be investigated. If the test for dose response is statistically significant, pairwise comparisons among the 3 groups will be performed and evaluated at a significance level of 0.05."
The request is INVENTED. Variable labels, define.xml comments and SAP sentences are from the CDISC pilot (public).
How this was checked
Result
Generated by Claude on 2026-10-21; plan executed in R the same day.
Worked example, recomputed. A script recomputed the values from the data and they matched or were flagged. Statements a script cannot test were checked by a second Claude agent.
Prompt shortened for this page. The output shown came from the earlier run of the longer prompt (2026-10-21).
The longer prompt that was checked
VERIFIED PROMPT (used for the run shown): You are a senior biostatistician preparing a response plan to an agency information request. Use only the INPUT below. Where something needed to reproduce the result is not in the INPUT, say so in OPEN QUESTIONS instead of filling it in. Do not state any numerical result. Your plan is a draft for a qualified biostatistician to implement and check. Give me: A. A numbered procedure from the submitted datasets to the requested result: step, action, dataset and variables, and which sentence of the INPUT supports it. B. A pseudo-code outline, precise enough that a programmer could implement each step in R or SAS without asking you anything (filters, record selection, model terms, how each requested statistic is obtained). No macros. C. The datasets and variables read. D. A pre-send checklist for the team. E. Neutral cover text, 3 to 5 sentences, no claims about study conclusions. OPEN QUESTIONS, each once.
A one-line prompt for comparison
Prompt: FDA sent us an information request. They want step by step instructions for how we got the primary ADAS-Cog result from the ADaM datasets, plus simplified code. Can you write the response plan?
RESPONSE PLAN: FDA information request on the primary ADAS-Cog analysis (ADaM to result) I have not seen your SAP, ADaM specs, or programs. Everything below is a template built on common conventions (ADSL, ADQS or ADADAS, MMRM on change from baseline). Items marked [CONFIRM] must be checked against your actual SAP and datasets before anything goes to FDA. 1. Triage and governance (Day 0-2) - Log the IR in the regulatory tracker, with its number, date received, and due date. Confirm the response deadline with Regulatory Affairs. - Name owners: Regulatory lead (submission and cover letter), Lead biostatistician (method and text), Independent programmer (QC), Clinical lead (context review), and QA/Medical writing. - Clarify scope with FDA through the regulatory lead if anything is ambiguous. Questions to settle: which study or studies, which ADAS-Cog version (11 or 13), which analysis (primary endpoint, timepoint), and whether they want the SAS code, the R code, or just a narrative. - Freeze the exact datasets and programs that were submitted. Record versions, checksums, and the define.xml. Do not use any post-submission data cuts. 2. Reproduce the result first (Day 2-5) - Rerun the submitted programs on the submitted ADaM. Confirm the primary estimate, CI, and p-value match the CSR and submitted tables exactly. - If they do not match, stop and escalate before writing anything. Do not send FDA code that has not reproduced the reported number. - Have the independent programmer reproduce the result from the simplified code too (step 4). ...
All 4 checks
- Check the answer key: CSR values were typed from PDF p.49. A pass does not show the submitted SAS program is what ran.
Sources
- CDISC SDTM/ADaM Pilot Project (CDISC, credit required)
Scripts
Related workflows
All workflows. How we check. Public data only. Drafts for review, not validated output.
Get one workflow a week
Unsubscribe anytime. Privacy.