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

## ----design-------------------------------------------------------------------
library(stepwedgepower)

state <- rbind(
  S1 = c(0, 0, 1, 1, 3, 3, 2, 2, 2, 2),
  S2 = c(0, 0, 0, 1, 1, 3, 3, 2, 2, 2),
  S3 = c(0, 0, 2, 2, 3, 3, 3, 3, 3, 3),
  S4 = c(0, 0, 0, 2, 2, 3, 3, 3, 3, 3),
  S5 = c(0, 0, 1, 1, 1, 3, 3, 3, 3, 3),
  S6 = c(0, 0, 2, 2, 2, 3, 3, 3, 3, 3),
  S7 = c(0, 0, 0, 0, 0, 1, 1, 3, 2, 2),
  S8 = c(0, 0, 0, 0, 0, 2, 2, 3, 1, 1)
)

design <- sw_component_design(
  clusters_per_sequence = rep(5L, 8),
  state = state
)
design

## ----assumptions--------------------------------------------------------------
assumptions <- sw_component_assumptions(
  baseline_prob = 0.15,
  treatment_or_a = 1.35,
  treatment_or_b = 1.25,
  interaction_or = 1.10,
  delay_a = 1L,
  delay_b = 1L,
  carryover_periods_a = 2L,
  carryover_periods_b = 2L,
  carryover_weights_a = c(0.60, 0.30),
  carryover_weights_b = c(0.50, 0.25),
  restart_rule_a = "reset",
  restart_rule_b = "reset",
  interaction_mode = "effective_overlap",
  icc = 0.05,
  period_effects = log(seq(1.00, 1.10, length.out = ncol(state))),
  n_per_cluster_period = 25L
)
assumptions

## ----audit--------------------------------------------------------------------
audit <- audit_component_design(design, assumptions)
audit

## ----simulate-fit-------------------------------------------------------------
one_trial <- simulate_component_swcrt(design, assumptions, seed = 123)
fit <- fit_component_model(
  one_trial,
  multiplicity = "holm",
  multiplicity_family = c("A_vs_control", "B_vs_control", "interaction")
)
fit$contrasts

## ----power, eval=FALSE--------------------------------------------------------
# power <- power_component_swcrt(
#   design,
#   assumptions,
#   nsim = 5000,
#   multiplicity = "holm",
#   multiplicity_family = c("A_vs_control", "B_vs_control", "interaction"),
#   nAGQ = 1,
#   n_cores = 4,
#   seed = 2026
# )
# summary(power)
# power$fit_diagnostics
# power$replicate_diagnostics
# 
# type1 <- type1_component_swcrt(
#   design,
#   assumptions,
#   nsim = 5000,
#   multiplicity = "holm",
#   multiplicity_family = c("A_vs_control", "B_vs_control", "interaction"),
#   nAGQ = 1,
#   n_cores = 4,
#   seed = 22026
# )
# summary(type1)

