The book’s worked calculations and simulations are produced by runnable R code. Published trial results are cited to their sources; figures displaying those results do not reproduce the original analyses. Nearly all of the computation lives in a companion R package, bayestrials, and the chapters call it in short chunks and draw the figures with ggplot2. This appendix describes how to install the package, how it is organized, a worked example, how to reproduce the results, the companion online lab, and the established software that practitioners use for trial design.
C.1 Installation
The book was built with R 4.5.2. The package declares R 4.1 as its minimum version. It is version 0.1.0 and exports 203 functions; it passes R CMD check, and its tests run under testthat. The source is in the bayestrials/ directory of the book repository. From a clone of the repository it installs with
The package imports only stats and parallel; mvtnorm and the other packages it can use are optional and listed under Suggests. Quarto renders the book through knitr, and each chapter begins with a hidden setup chunk that loads bayestrials and ggplot2.
C.2 How the package is organized
Functions are named in snake_case, and the prefix tells the reader which part of the book the function belongs to. Arguments follow one convention throughout (see Section A.2): alpha is the one-sided type I error, theta an effect, p0 and p1 response probabilities, n a number of patients or events, nsim the number of simulated trials, and seed an optional random seed. Functions return numbers, named vectors, data frames or named lists. They never print or plot.
Table C.1: Function families of bayestrials.
Family
What it does
Where used
Core posterior and gs_
Beta-binomial and normal posteriors (beta_post, prob_superior, normal_update), effective sample size (ess_beta, ess_normal), group sequential boundaries and crossing probabilities (gs_boundary, gs_spending_boundary, gs_crossing_prob, gs_tolls)
Permutation test, binary z test, simple, block, stratified and minimization randomization (assign_*), covariate imbalance, small simulations of two-arm trials
Simulation of Bayesian designs with interim looks, calibration of the threshold to a type I error target, operating characteristics tables (oc_simulate, oc_calibrate, oc_summarize)
The package also contains four datasets: ulcer (the four-operation ulcer trial of Chapter 11), select_design (the design of the SELECT trial, Chapter 22), ctgov_counts and ctgov_bayesian_records (counts and records of registered trials that mention Bayesian methods, retrieved from ClinicalTrials.gov). Each function has a help page with an example that runs in under a second. The files bayestrials/data-raw/map-*.md map the book’s earlier Python functions to their R replacements.
C.3 A worked example
The example calibrates a two-arm Bayesian design with three looks, at 100, 200 and 300 patients per arm and a control response probability of 0.30. The success rule is that the posterior probability of superiority exceeds a threshold \(c\) at any look. oc_calibrate returns a conservative empirical quantile of the largest posterior probability reached under the simulated null scenario, giving an empirical rejection proportion at most 0.025. The resulting threshold is then applied to the alternative in which the treatment response probability is 0.45.
The script gives a calibrated threshold of \(c =\) 0.989. The rejection proportion in the calibration run, 0.0246, is the calibration target and not an error assessment. On a fresh null run with another seed the type I error is 0.0234 and the expected total sample size is 594 (the largest possible is 600). Under the alternative the power is 0.943 and the expected total sample size is 346, because the trial stops early at the first look that crosses the threshold. These numbers are Monte Carlo estimates from 20,000 trials; the seed makes them repeatable and a different seed gives values that typically differ in the third decimal place. A full validation would repeat the fresh null run across a prespecified grid of nuisance parameter values, such as other control response probabilities. Chapter 15 explains how to report the Monte Carlo error.
C.4 Reproducibility
Every simulation either calls set.seed() in the chunk or passes seed = to the package function, so the seed is visible next to the result. Functions draw random numbers from R’s global generator after the seed is set, so a chunk is repeatable but its results depend on the order of the random calls inside it. Monte Carlo results are reported with their Monte Carlo error (Chapter 15). Quarto’s freeze mechanism (freeze: auto in _quarto.yml) stores the computed output of each chapter, so a rebuild reuses stored results unless the source of that chapter has changed, and selected chunks in the case-study and protocol chapters include a checksum of the package’s R code in their knitr cache keys. After package code or data changes, reinstall the package, remove the affected chapter’s directory under _freeze/ and its knitr cache directory, and render the chapter again. The R version and the installed package versions can be recorded with sessionInfo(), which a reader who needs exact reproduction should do.
C.5 The companion lab
An interactive tutorial accompanies the book at https://vsokolov.org/bayesian_trials_lab/. It runs in the browser and has modules that match the chapters, where the reader predicts an outcome and then checks it. The Bayesian trial simulator at https://vsokolov.org/bayesian_trials_lab/14-simulator.html lets the reader set a design (arms, prior, thresholds, interim looks) and see simulated operating characteristics, in the spirit of the simulation report discussed in Chapter 15. The full open simulator at https://vsokolov.org/bayesian_trials_lab/14-studio.html is a free tool in the style of commercial design software such as FACTS: it handles several doses, adaptive allocation, scenarios and operating characteristics tables, and is the place to explore a design before writing code. Chapters that have a matching module end with a “Try it in the lab” callout. An online encyclopedia built on this book, at https://vsokolov.org/bayesian_trials_lab/encyclopedia/, has entries for the methods, trials, regulatory documents and people in the book, each linked to the section that treats it.
C.6 Established trial-design software
The following tools are in wide use. The descriptions are brief and cover only the purpose of each tool; consult each tool’s documentation for current features and versions. The R packages below are on CRAN, except bcrm, which CRAN archived on 8 May 2026. On the machine where the book was built, rpact 4.4.0, gsDesign 3.11.0, BOIN 2.7.2, brms 2.23.0 and rstan 2.32.7 were installed; the other tools were not checked.
rpact (R package). Confirmatory adaptive design: group sequential designs, sample size reassessment and combination tests, with simulation.
gsDesign (R package). Group sequential design, including spending functions and boundaries, and power and sample size calculation. A reader can compare its boundaries with gs_boundary and gs_spending_boundary (Chapter 12).
RBesT (R package). Bayesian evidence synthesis tools: construction of meta-analytic-predictive priors, robustification, and operating characteristics for designs that use historical data. Relevant to Chapter 20.
SampleSizeProportions and SampleSizeMeans (R packages, by Joseph and Bélisle). Bayesian sample size criteria (average length, average coverage and worst outcome, ALC, ACC and WOC) for proportions and means. The lupus example of Section 10.2.4.1 uses SampleSizeProportions 1.1.3.
BOIN (R package). Design and simulation of Bayesian optimal interval phase I dose-finding trials. Relevant to Chapter 18.
trialr (R package). Bayesian clinical trial designs fitted with Stan, including dose-finding and phase II designs.
adaptr (R package). Simulation of Bayesian adaptive multi-arm trials, with response-adaptive randomization, early stopping and calibration of the stopping rules, summarized as operating characteristics. It is the closest open R counterpart to the simulation part of bayestrials for multi-arm designs.
bcrm (R package; archived on CRAN on 8 May 2026, so it no longer installs from CRAN by default). Bayesian continual reassessment method for dose finding. Relevant to Chapter 18.
Stan and brms. Stan is a probabilistic programming language for Bayesian inference by Hamiltonian Monte Carlo. brms is an R package that fits multilevel models through Stan with a formula interface. Both are used to fit analysis models when conjugate formulas are not available.
FACTS (Berry Consultants). Commercial software for designing and simulating Bayesian adaptive trials, including dose finding, response-adaptive randomization and platform designs. Simulation output of this kind is the kind regulators ask for in a CID meeting (Chapter 4).
East Horizon (Cytel). Commercial software for the design, simulation and monitoring of fixed, group sequential and adaptive trials. It is part of Cytel’s East product line.