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

## ----simulate-binary-pilot----------------------------------------------------
set.seed(202)

n_pilot <- 220
d <- rbinom(n_pilot, size = 1, prob = 0.5)
response_prob <- ifelse(d == 1, 0.38, 0.55)
y <- rbinom(n_pilot, size = 1, prob = response_prob)

table(treatment = d, outcome = y)

## ----bernoulli-cmr------------------------------------------------------------
fit_auto <- cmr_binary(y, d, method = "auto", alpha = 0.05)

fit_auto$method
fit_auto$pi
round(fit_auto$rectangle, 4)
fit_auto$pilot$n

## ----binary-methods-----------------------------------------------------------
methods <- c("auto", "bernoulli", "bernoulli_exact", "bounded", "mtr")

comparison <- do.call(rbind, lapply(methods, function(method) {
  fit <- cmr_binary(y, d, method = method, alpha = 0.05)
  c(
    pi = fit$pi,
    U_CMR = fit$U_CMR,
    v_l1 = fit$rectangle[["v_l1"]],
    v_u1 = fit$rectangle[["v_u1"]],
    v_l0 = fit$rectangle[["v_l0"]],
    v_u0 = fit$rectangle[["v_u0"]]
  )
}))
rownames(comparison) <- methods

round(comparison, 4)

## ----folded-counts------------------------------------------------------------
fit_auto$confidence_set$treatment$statistic
fit_auto$confidence_set$control$statistic

