## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse   = TRUE,
  comment    = "#>",
  fig.width  = 7,
  fig.height = 4.5,
  out.width  = "100%",
  dpi        = 96
)
library(SingleArmMRCT)

## -----------------------------------------------------------------------------
lambda  <- log(2) / 10   # treatment arm: median survival = 10
lambda0 <- log(2) / 5    # historical control: median survival = 5
t_a     <- 3             # accrual period
t_f     <- 10            # follow-up period
# True HR = lambda / lambda0 = 0.5

## -----------------------------------------------------------------------------
result_f <- rcp1armHazardRatio(
  lambda         = lambda,
  lambda0        = lambda0,
  Nj             = c(20, 80),
  t_a            = t_a,
  t_f            = t_f,
  lambda_dropout = NULL,
  PI             = 0.5,
  approach       = "formula"
)
print(result_f)

## -----------------------------------------------------------------------------
result_s <- rcp1armHazardRatio(
  lambda         = lambda,
  lambda0        = lambda0,
  Nj             = c(20, 80),
  t_a            = t_a,
  t_f            = t_f,
  lambda_dropout = NULL,
  PI             = 0.5,
  approach       = "simulation",
  nsim           = 10000,
  seed           = 1
)
print(result_s)

## -----------------------------------------------------------------------------
result_dropout <- rcp1armHazardRatio(
  lambda         = lambda,
  lambda0        = lambda0,
  Nj             = c(20, 80),
  t_a            = t_a,
  t_f            = t_f,
  lambda_dropout = 0.05,
  PI             = 0.5,
  approach       = "formula"
)
print(result_dropout)

## ----fig.height=6, fig.alt="Grid plot of RCP versus f1 for a hazard ratio endpoint with HR = 0.5, showing Method 1 on log-HR and linear-HR scales and Method 2 across N = 20, 40, 100"----
plot_rcp1armHazardRatio(
  lambda    = lambda,
  lambda0   = lambda0,
  t_a       = t_a,
  t_f       = t_f,
  PI        = 0.5,
  N_vec     = c(20, 40, 100),
  J         = 3,
  nsim      = 5000,
  seed      = 1,
  base_size = 8
)

## -----------------------------------------------------------------------------
t_eval <- 8
S0     <- exp(-log(2) * t_eval / 5)
cat(sprintf("True S(%g) = %.4f,  S0 = %.4f,  delta = %.4f\n",
            t_eval, exp(-lambda * t_eval), S0,
            exp(-lambda * t_eval) - S0))

## -----------------------------------------------------------------------------
result_f <- rcp1armMilestoneSurvival(
  lambda         = lambda,
  t_eval         = t_eval,
  S0             = S0,
  Nj             = c(20, 80),
  t_a            = t_a,
  t_f            = t_f,
  lambda_dropout = NULL,
  PI             = 0.5,
  approach       = "formula"
)
print(result_f)

## -----------------------------------------------------------------------------
result_s <- rcp1armMilestoneSurvival(
  lambda         = lambda,
  t_eval         = t_eval,
  S0             = S0,
  Nj             = c(20, 80),
  t_a            = t_a,
  t_f            = t_f,
  lambda_dropout = NULL,
  PI             = 0.5,
  approach       = "simulation",
  nsim           = 10000,
  seed           = 1
)
print(result_s)

## ----fig.alt="Line plot of RCP versus f1 for a milestone survival endpoint at t_eval = 8, showing Method 1 and Method 2 across N = 20, 40, 100"----
plot_rcp1armMilestoneSurvival(
  lambda    = lambda,
  t_eval    = t_eval,
  S0        = S0,
  t_a       = t_a,
  t_f       = t_f,
  PI        = 0.5,
  N_vec     = c(20, 40, 100),
  J         = 3,
  nsim      = 5000,
  seed      = 1,
  base_size = 8
)

## -----------------------------------------------------------------------------
tau_star <- 8
mu0      <- (1 - exp(-lambda0 * tau_star)) / lambda0
mu_est   <- (1 - exp(-lambda  * tau_star)) / lambda
cat(sprintf("True RMST = %.4f,  mu0 = %.4f,  delta = %.4f\n",
            mu_est, mu0, mu_est - mu0))

## -----------------------------------------------------------------------------
result_f <- rcp1armRMST(
  lambda         = lambda,
  tau_star       = tau_star,
  mu0            = mu0,
  Nj             = c(20, 80),
  t_a            = t_a,
  t_f            = t_f,
  lambda_dropout = NULL,
  PI             = 0.5,
  approach       = "formula"
)
print(result_f)

## -----------------------------------------------------------------------------
result_s <- rcp1armRMST(
  lambda         = lambda,
  tau_star       = tau_star,
  mu0            = mu0,
  Nj             = c(20, 80),
  t_a            = t_a,
  t_f            = t_f,
  lambda_dropout = NULL,
  PI             = 0.5,
  approach       = "simulation",
  nsim           = 10000,
  seed           = 1
)
print(result_s)

## ----fig.alt="Line plot of RCP versus f1 for an RMST endpoint with tau_star = 8, showing Method 1 and Method 2 across N = 20, 40, 100"----
plot_rcp1armRMST(
  lambda    = lambda,
  tau_star  = tau_star,
  mu0       = mu0,
  t_a       = t_a,
  t_f       = t_f,
  PI        = 0.5,
  N_vec     = c(20, 40, 100),
  J         = 3,
  nsim      = 5000,
  seed      = 1,
  base_size = 8
)

## ----echo=FALSE---------------------------------------------------------------
tbl <- data.frame(
  Endpoint = c("Hazard Ratio", "Milestone Survival", "RMST"),
  `Effect parameter` = c(
    "$\\log(HR) = \\log(\\lambda/\\lambda_0)$ (Method 1, log-HR scale); $1 - HR = 1 - \\lambda/\\lambda_0$ (Method 1, linear-HR scale)",
    "$\\delta = e^{-\\lambda t_{\\text{eval}}} - S_0$",
    "$\\delta = \\mu(\\tau^*) - \\mu_0(\\tau^*)$"
  ),
  `Benefit direction` = c(
    "$\\widehat{HR}_j < 1$",
    "$\\hat{S}_j(t) > S_0$",
    "$\\hat{\\mu}_j > \\mu_0$"
  ),
  `Variance basis` = c(
    "Expected events via $\\phi$ (Wu 2015)",
    "Greenwood's formula",
    "Squared survival difference integral"
  ),
  `Closed-form condition` = c(
    "Always",
    "$t_{\\text{eval}} \\leq t_f$",
    "$\\tau^* \\leq t_f$"
  ),
  check.names = FALSE
)
knitr::kable(tbl, align = "lllll")

