## ----setup--------------------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(cmrdesign)

## ----multiarm-----------------------------------------------------------------
set.seed(505)

n_per_arm <- 180
arm <- rep(c(0, 1, 2), each = n_per_arm)
y <- c(
  rbeta(n_per_arm, shape1 = 4, shape2 = 4),
  rbeta(n_per_arm, shape1 = 2.5, shape2 = 6),
  rbeta(n_per_arm, shape1 = 5, shape2 = 3)
)

fit_multi <- cmr_multiarm(y, arm, alpha = 0.05, method = "bounded")

round(fit_multi$pi, 3)
fit_multi$U_CMR
fit_multi$method
fit_multi$pilot$n

## ----multiarm-counts----------------------------------------------------------
multi_counts <- realize_allocation(fit_multi, n_main = 1200)

multi_counts$counts
multi_counts$realized_U_CMR

## ----stratified---------------------------------------------------------------
set.seed(506)

n_per_cell <- 100
strata <- rep(c("urban", "rural"), each = 2 * n_per_cell)
d <- rep(c(rep(1, n_per_cell), rep(0, n_per_cell)), times = 2)
y <- c(
  rbeta(n_per_cell, shape1 = 2, shape2 = 5),
  rbeta(n_per_cell, shape1 = 4, shape2 = 4),
  rbeta(n_per_cell, shape1 = 3, shape2 = 6),
  rbeta(n_per_cell, shape1 = 5, shape2 = 4)
)
strata_share <- c(urban = 0.55, rural = 0.45)

fit_strata <- cmr_stratified(
  y = y,
  d = d,
  strata = strata,
  strata_share = strata_share,
  alpha = 0.05,
  method = "bounded"
)

round(fit_strata$pi_matrix, 3)
round(fit_strata$sampling_margin, 3)
round(fit_strata$treatment_margin, 3)
fit_strata$U_CMR

## ----stratified-counts--------------------------------------------------------
strata_counts <- realize_allocation(
  fit_strata,
  strata_counts = c(urban = 550, rural = 450)
)

strata_counts$counts

## ----unbounded----------------------------------------------------------------
set.seed(507)

n_per_arm <- 1500
d <- c(rep(1, n_per_arm), rep(0, n_per_arm))
y <- c(
  0.20 + 1.10 * rt(n_per_arm, df = 8),
  0.00 + 0.70 * rt(n_per_arm, df = 8)
)

fit_unbounded <- cmr_unbounded(
  y = y,
  d = d,
  psi = c(treatment = 6, control = 6),
  alpha = 0.10
)

fit_unbounded$pi
fit_unbounded$U_CMR
fit_unbounded$pilot[c("rho", "b", "active", "status")]

