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

## ----designs------------------------------------------------------------------
library(stepwedgepower)

swd <- sw_batched_design(
  c(5L, 5L),
  rbind(
    `Group 1` = c("0", "1", "1+2", "1+2"),
    `Group 2` = c("0", "0", "1", "1+2")
  ),
  batch = c("Batch 1", "Batch 1")
)

bswd <- sw_batched_design(
  c(2L, 2L, 3L, 3L),
  rbind(
    `Group 1` = c("0", "1", "1+2", "1+2", "-"),
    `Group 2` = c("0", "0", "1", "1+2", "-"),
    `Group 3` = c("-", "0", "1", "1+2", "1+2"),
    `Group 4` = c("-", "0", "0", "1", "1+2")
  ),
  batch = c("Batch 1", "Batch 1", "Batch 2", "Batch 2")
)

swd
bswd

## ----metadata-----------------------------------------------------------------
bswd$batch_start_period
bswd$batch_delay
bswd$batch_gap_from_previous
bswd$time_on_trial

## ----assumptions--------------------------------------------------------------
assumptions <- sw_batched_assumptions(
  baseline_prob = 0.15,
  treatment_or_a = 1.35,
  treatment_or_b = 1.25,
  interaction_mode = "none",
  icc = 0.05,
  n_per_cluster_period = 25L,
  time_model = "calendar",
  time_effects = log(seq(1.00, 1.08, length.out = 5))
)

rbind(
  SWD = component_resource_summary(swd, assumptions),
  BSWD = component_resource_summary(bswd, assumptions)
)

## ----audits-------------------------------------------------------------------
contrasts <- c("A_vs_control", "AB_vs_A", "AB_vs_control")

calendar_audit <- audit_batched_design(
  bswd, assumptions, "calendar", contrasts, include_interaction = FALSE
)
trial_time_audit <- audit_batched_design(
  bswd, assumptions, "time_on_trial", contrasts,
  include_interaction = FALSE
)
separate_audit <- audit_batched_design(
  bswd, assumptions, "separate", contrasts, include_interaction = FALSE
)

calendar_audit
trial_time_audit
separate_audit

## ----simulate-fit-------------------------------------------------------------
one_trial <- simulate_batched_swcrt(bswd, assumptions, seed = 123)
one_fit <- fit_batched_model(
  one_trial,
  time_model = "calendar",
  contrasts = contrasts,
  include_interaction = FALSE,
  multiplicity = "holm"
)
one_fit$contrasts

## ----compare, eval=FALSE------------------------------------------------------
# comparison <- compare_swd_bswd(
#   swd, bswd, assumptions,
#   nsim = 5000,
#   contrasts = contrasts,
#   include_interaction = FALSE,
#   multiplicity = "holm",
#   multiplicity_family = contrasts,
#   nAGQ = 1,
#   n_cores = 4,
#   seed = 2026,
#   warn_on_design = FALSE
# )
# 
# comparison$resource_check
# comparison$comparison
# comparison$fit_diagnostics
# 
# model_comparison <- compare_batched_time_models(
#   bswd, assumptions,
#   nsim = 5000,
#   contrasts = contrasts,
#   include_interaction = FALSE,
#   multiplicity = "holm",
#   multiplicity_family = contrasts,
#   nAGQ = 1,
#   n_cores = 4,
#   seed = 2026,
#   warn_on_design = FALSE
# )
# model_comparison$comparison

