## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(BayesianTrial)

## ----operating-characteristics------------------------------------------------
true_rates <- c(0.20, 0.40)

oc_results <- lapply(true_rates, function(theta) {
  oc_singlearm_binary(
    theta_true = theta,
    n_current = 20,
    N_total = 40,
    alpha0 = 0.2,
    beta0 = 0.8,
    theta0_null = 0.20,
    gamma_early = 0.98,
    gamma_futility = 0.50,
    gamma_final = 0.95,
    ntrial = 10000,
    seed = 666
  )
})

oc_table <- data.frame(
  true_ORR = true_rates,
  do.call(rbind, lapply(oc_results, unlist)),
  row.names = NULL
)
knitr::kable(oc_table, digits = 3)

## ----interim-posterior--------------------------------------------------------
posterior <- posterior_predictive_singlearm_binary(
  x_obs = 9,
  n_current = 20,
  N_total = 40,
  alpha0 = 0.2,
  beta0 = 0.8,
  theta0_null = 0.20
)

posterior$p_null

## ----interim-decision---------------------------------------------------------
decision <- if (posterior$p_null > 0.98) {
  "Stop for efficacy"
} else if (posterior$p_null <= 0.50) {
  "Stop for futility"
} else {
  "Continue to the final analysis"
}

decision

## ----interim-predictive-------------------------------------------------------
predictive <- posterior_predictive_singlearm_binary(
  x_obs = 9,
  n_current = 20,
  N_total = 40,
  alpha0 = 0.2,
  beta0 = 0.8,
  theta0_null = 0.20,
  gamma_final = 0.95,
  type = "predictive",
  S = 10000,
  seed = 666
)

predictive$ppos_null

