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

## ----simulate-proxy-pilot-----------------------------------------------------
set.seed(404)

n_pilot <- 200
d <- rbinom(n_pilot, size = 1, prob = 0.5)

primary <- numeric(n_pilot)
primary[d == 1] <- rbeta(sum(d == 1), shape1 = 6, shape2 = 3)
primary[d == 0] <- rbeta(sum(d == 0), shape1 = 3.5, shape2 = 3.5)

proxy_noise <- ifelse(d == 1, 0.06, 0.08) * rnorm(n_pilot)
proxy_y <- pmin(1, pmax(0, primary + proxy_noise))

round(rbind(
  proxy_treatment = c(mean = mean(proxy_y[d == 1]), variance = var(proxy_y[d == 1])),
  proxy_control = c(mean = mean(proxy_y[d == 0]), variance = var(proxy_y[d == 0]))
), 4)

## ----bridge-------------------------------------------------------------------
zeta <- c(treatment = 0.04, control = 0.06)

## ----proxy-cmr----------------------------------------------------------------
fit_proxy <- cmr_proxy(
  proxy_y = proxy_y,
  d = d,
  zeta = zeta,
  method = "bounded",
  alpha = 0.05
)

fit_proxy$pi
round(fit_proxy$rectangle, 4)
fit_proxy$zeta
fit_proxy$diagnostics$bridge

## ----proxy-vs-primary-rectangle-----------------------------------------------
round(fit_proxy$confidence_set$bridge$proxy_rectangle, 4)
round(fit_proxy$confidence_set$bridge$primary_rectangle, 4)

## ----oracle-comparison--------------------------------------------------------
fit_oracle <- cmr_two_arm(primary, d, method = "bounded")

comparison <- rbind(
  proxy = c(pi = fit_proxy$pi, U_CMR = fit_proxy$U_CMR),
  oracle_primary = c(pi = fit_oracle$pi, U_CMR = fit_oracle$U_CMR)
)

round(comparison, 4)

