## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4.6,
  fig.align = "center"
)

## ----load-package-------------------------------------------------------------
library(RCTCovAdj)

## ----case-table---------------------------------------------------------------
data("simulation_cases", package = "RCTCovAdj")
simulation_cases

## ----analytic-benchmarks------------------------------------------------------
with(
  simulation_cases[simulation_cases$case == "A", ],
  rct_variance_factors(
    allocation = pi,
    beta_control = b0,
    beta_treatment = b1,
    quadratic = quad,
    noise_variance = 1
  )
)

## ----one-dataset--------------------------------------------------------------
trial_a <- simulate_rct_case(case = "A", n = 200L, seed = 123L)
str(trial_a)

rct_adjust(
  outcome = trial_a$outcome,
  treatment = trial_a$treatment,
  covariates = trial_a["x"],
  allocation = 0.5,
  methods = c("unadjusted", "ancova", "interacted")
)

## ----one-dataset-crossfit-----------------------------------------------------
rct_crossfit(
  outcome = trial_a$outcome,
  treatment = trial_a$treatment,
  covariates = trial_a["x"],
  allocation = 0.5,
  learner = "quadratic",
  folds = 2L,
  seed = 456L
)

## ----full-simulation, eval=FALSE----------------------------------------------
# full_output_dir <- tempfile("rctcovadj-full-")
# full_files <- reproduce_paper_simulations(
#   output_dir = full_output_dir,
#   reps = 4000L,
#   sample_sizes = c(200L, 800L),
#   seed = 20260903L
# )
# unlink(full_output_dir, recursive = TRUE)

## ----quick-simulation, eval=FALSE---------------------------------------------
# quick <- run_paper_simulations(
#   reps = 32L,
#   sample_sizes = 200L,
#   seed = 20260903L
# )

## ----archived-results---------------------------------------------------------
data("paper_population_benchmarks", package = "RCTCovAdj")
data("paper_simulation_results", package = "RCTCovAdj")

head(paper_population_benchmarks)
head(paper_simulation_results)

## ----simulation-figure, fig.width=7.1, fig.height=5.2, dev.args=list(pointsize=13.5), fig.alt="Four panels show empirical root-n variance relative to the semiparametric efficiency bound for six estimators in Cases A through D. Filled circles have vertical 95 percent Monte Carlo intervals, and open diamonds show first-order theory.", fig.cap="Empirical variance relative to the semiparametric bound at n=800. Bars are 95% Monte Carlo intervals for the empirical ratios; open diamonds are the first-order ratios."----
plot_simulation_results(
  results = paper_simulation_results,
  benchmarks = paper_population_benchmarks,
  metric = "efficiency"
)

## ----restricted-trial-analysis, eval=FALSE------------------------------------
# source_data <- read_licorice_data(file.choose())
# trial_results <- analyze_licorice(source_data)
# trial_results$estimates
# plot_licorice_results(trial_results)

## ----medicaldata-trial-analysis, eval=FALSE-----------------------------------
# source_data <- prepare_licorice_data()
# trial_results <- analyze_licorice(source_data)

## ----design-results-----------------------------------------------------------
data("paper_power_design_results", package = "RCTCovAdj")
paper_power_design_results

## ----rebuild-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."----
recomputed_design <- paper_power_design()
recomputed_design$sample_sizes
plot_power_design(recomputed_design)

## ----replication-scripts------------------------------------------------------
replication_dir <- system.file("replication", package = "RCTCovAdj")
list.files(replication_dir, pattern = "[.]R$")

