## ----study-data, include=FALSE------------------------------------------------
library(ggplot2)
study_dir <- system.file("shiny", "benchmark", package = "DDESONN", mustWork = TRUE)
study <- new.env()
sys.source(file.path(study_dir, "studies.R"), envir = study)
results <- read.csv(file.path(study_dir, "data", "100SeedsDDESONNvsKerasCleanSplit.csv"))
reductions <- c("Sum vs. sum" = "sum", "Mean vs. mean" = "mean")
cumulative <- do.call(rbind, lapply(names(reductions), function(label) {
  pairs <- study$study_clean_pairs(results, reductions[[label]])
  transform(study$study_seed_summary(pairs, cumulative = TRUE), comparison = label)
}))
cumulative$comparison <- factor(cumulative$comparison, levels = names(reductions))
overall <- subset(cumulative, seeds == 100)

## ----accuracy-summary, echo=FALSE---------------------------------------------
knitr::kable(data.frame(
  Comparison = overall$comparison,
  `DDESONN (%)` = overall$DDESONN_pct,
  `Keras (%)` = overall$Keras_pct,
  `Difference (pp)` = overall$advantage_pp,
  `95% paired interval (pp)` = sprintf("%.2f to %.2f", overall$low_pp, overall$high_pp),
  `DDESONN wins / ties / losses` = with(overall, paste(wins, ties, losses, sep = " / ")),
  check.names = FALSE
), digits = 2, row.names = FALSE)

## ----accuracy-distributions, echo=FALSE, fig.width=7, fig.height=3.6, fig.alt="Boxplots of held-out accuracy for DDESONN and Keras in both matched reduction settings."----
results$comparison <- factor(results$reduction, levels = reductions, labels = names(reductions))
results$framework <- factor(results$framework, levels = c("ddesonn", "keras"),
                            labels = c("DDESONN", "Keras"))
ggplot(results, aes(framework, accuracy * 100, fill = framework)) +
  geom_boxplot(width = 0.55) +
  facet_wrap(~ comparison) +
  scale_fill_manual(values = c(DDESONN = "#087f8c", Keras = "#b65c28"), guide = "none") +
  labs(x = NULL, y = "Held-out accuracy (%)") +
  theme_minimal()

## ----cumulative-precision, echo=FALSE, fig.width=7, fig.height=3.6, fig.alt="Cumulative DDESONN minus Keras accuracy with pointwise 95 percent intervals for both reductions."----
ggplot(cumulative, aes(seeds, advantage_pp)) +
  geom_ribbon(aes(ymin = low_pp, ymax = high_pp), fill = "#087f8c", alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  geom_line(color = "#087f8c") +
  geom_point(color = "#087f8c") +
  facet_wrap(~ comparison) +
  labs(x = "Training seeds per condition", y = "DDESONN − Keras accuracy (pp)") +
  theme_minimal()

