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

## ----simulate-multiple-outcomes-----------------------------------------------
set.seed(303)

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

test_score <- numeric(n_pilot)
attendance <- numeric(n_pilot)

test_score[d == 1] <- rbeta(sum(d == 1), shape1 = 5.5, shape2 = 3.5)
test_score[d == 0] <- rbeta(sum(d == 0), shape1 = 4, shape2 = 4)

attendance[d == 1] <- rbeta(sum(d == 1), shape1 = 7, shape2 = 2.5)
attendance[d == 0] <- rbeta(sum(d == 0), shape1 = 5, shape2 = 3.5)

y <- cbind(test_score = test_score, attendance = attendance)
weights <- c(test_score = 0.7, attendance = 0.3)

by_arm_mean <- rbind(
  treatment = colMeans(y[d == 1, , drop = FALSE]),
  control = colMeans(y[d == 0, , drop = FALSE])
)
round(by_arm_mean, 3)

## ----index-cmr----------------------------------------------------------------
fit_index <- cmr_multiple_outcomes(
  y = y,
  d = d,
  weights = weights,
  estimand = "index",
  method = "bounded"
)

fit_index$pi
round(fit_index$rectangle, 4)
fit_index$estimand
fit_index$weights

## ----coprimary-cmr------------------------------------------------------------
fit_coprimary <- cmr_multiple_outcomes(
  y = y,
  d = d,
  weights = weights,
  estimand = "coprimary",
  method = "bounded"
)

fit_coprimary$pi
round(fit_coprimary$rectangle, 4)
fit_coprimary$joint_error_bound

## ----outcome-specific-bounds--------------------------------------------------
names(fit_coprimary$confidence_set$outcome_bounds)
round(fit_coprimary$confidence_set$outcome_vhat$treatment, 4)
round(fit_coprimary$confidence_set$outcome_vhat$control, 4)

## ----compare-index-coprimary--------------------------------------------------
comparison <- rbind(
  index = c(pi = fit_index$pi, U_CMR = fit_index$U_CMR),
  coprimary = c(pi = fit_coprimary$pi, U_CMR = fit_coprimary$U_CMR)
)

round(comparison, 4)

