# WCRBayesDesign

Bayesian Two-Stage Adaptive Design for Single-Arm Survival Trials

## Overview

**WCRBayesDesign** provides simulation and optimization tools for
Bayesian two-stage adaptive single-arm trials with time-to-event
endpoints.  All statistical calculations are carried out in a
transformed-time space induced by a reference survival function
$S_0$, while key results are reported on the original time scale
for clinical interpretability.

## Installation

```r
# Install from source
install.packages("WCRBayesDesign_1.0.0.tar.gz", repos = NULL, type = "source")
```

## Quick Start

```r
library(WCRBayesDesign)

# Step 1 - Search feasible interim-analysis parameters
res_pIA <- find_Nw_pIA(
  tau = 24, theta_L = 0.62, theta_alt = 0.80,
  alpha_target = 0.05, beta_target = 0.20,
  rate = 5/12, X_grid = c(1, 2, 3),
  S0_dist = "weibull",
  S0_par = list(k = 1.2, lambda = 0.08)
)

# Step 2 - Optimise the two-stage design
opt <- two_stage_optimize_design(
  NwX_pIA_results = res_pIA,
  rate = 5/12, tau = 24,
  theta_L = 0.62, theta_alt = 0.80,
  alpha_target = 0.05, beta_target = 0.20,
  nsim = 2000,
  S0_dist = "weibull",
  S0_par = list(k = 1.2, lambda = 0.08),
  optimize = "ESS", ncores = 4
)

# Step 3 - Evaluate operating characteristics
oc <- oc_two_stage(
  N = 50, Nw = 30, X = 2,
  pIA = 0.5, pF = 0.05,
  rate = 5/12, tau = 24,
  theta_L = 0.62, theta_alt = 0.80,
  a0 = 0.01, b0 = 0.01, nsim = 5000,
  S0_dist = "weibull",
  S0_par = list(k = 1.2, lambda = 0.08)
)
```

## Exported Functions

| Function | Purpose |
|----------|---------|
| `find_Nw_pIA()` | Search for feasible interim-analysis parameters |
| `two_stage_optimize_design()` | Optimise sample size and decision thresholds |
| `oc_two_stage()` | Evaluate operating characteristics via simulation |
| `run_two_stage_trial()` | Simulate a single two-stage trial |
| `conduct()` | Perform Bayesian interim or final analysis |
| `S0_weibull()` | Weibull reference survival function |
| `S0_inverse()` | Inverse baseline survival function |
| `delta_from_theta_goal()` | Convert survival-probability target to delta |
| `stats_transformed()` | Compute transformed sufficient statistics |

## Supported Baseline Distributions

- Exponential (`S0_dist = "exp"`)
- Weibull (`S0_dist = "weibull"`)
- Log-normal (`S0_dist = "lognormal"`)
- Log-logistic (`S0_dist = "loglogistic"`)
- User-supplied (`S0_dist = "custom"`)

## License

GPL-3

## Authors

- Zhongheng Cai (maintainer) — zhonghengcai123@gmail.com
- Haitao Pan — Haitao.Pan@STJUDE.ORG
