## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4.5,
  fig.align = "center"
)

## ----load-package-------------------------------------------------------------
library(RCTCovAdj)

## ----small-trial--------------------------------------------------------------
set.seed(20260904)
n <- 600L
x <- rnorm(n)
a <- rbinom(n, size = 1L, prob = 0.5)
y <- 0.5 * a + (2 - 3 * a) * x + rnorm(n)

trial <- data.frame(outcome = y, treatment = a, x = x)
head(trial)

## ----fixed-adjustment---------------------------------------------------------
fit <- rct_adjust(
  outcome = trial$outcome,
  treatment = trial$treatment,
  covariates = trial["x"],
  allocation = 0.5,
  methods = c("unadjusted", "ancova", "interacted")
)
fit

## ----crossfit-adjustment------------------------------------------------------
fit_cf <- rct_crossfit(
  outcome = trial$outcome,
  treatment = trial$treatment,
  covariates = trial["x"],
  allocation = 0.5,
  learner = "linear",
  folds = 2L,
  seed = 2718L
)
fit_cf

## ----quadratic-crossfit-------------------------------------------------------
nonlinear_trial <- simulate_rct_case("A", n = 600L, seed = 314L)
rct_crossfit(
  outcome = nonlinear_trial$outcome,
  treatment = nonlinear_trial$treatment,
  covariates = nonlinear_trial["x"],
  allocation = 0.5,
  learner = "quadratic",
  folds = 2L,
  seed = 2718L
)

## ----hc2-diagnostic-----------------------------------------------------------
rct_adjust(
  outcome = trial$outcome,
  treatment = trial$treatment,
  covariates = trial["x"],
  allocation = 0.5,
  methods = "interacted",
  diagnostic_hc2 = TRUE
)

## ----licorice-authorized, eval=FALSE------------------------------------------
# licorice <- read_licorice_data(file.choose())
# licorice_fit <- analyze_licorice(licorice)
# licorice_fit$estimates
# plot_licorice_results(licorice_fit)

## ----licorice-medicaldata, eval=FALSE-----------------------------------------
# licorice <- prepare_licorice_data()
# licorice_fit <- analyze_licorice(licorice)

## ----sample-size-example------------------------------------------------------
variance <- c(
  unadjusted = 12.5,
  interacted_linear = 8.5,
  efficient_quadratic = 4.0
)

rct_sample_size(
  effect = 0.5,
  variance = variance,
  power = 0.80,
  alpha = 0.05,
  alternative = "two.sided"
)

## ----power-design, fig.alt="Two-panel normal-approximation power curves under balanced nonlinear Case A and unequal-allocation Case C, comparing unadjusted, interacted, and efficient quadratic or pooled ANCOVA analyses; symbols mark 80 percent power.", fig.cap="Normal-approximation power under planning Cases A and C. Symbols mark the smallest integer total sample sizes attaining 80% power for each analysis."----
design <- paper_power_design()
design$sample_sizes
plot_power_design(design)

