## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4)

## ----setup--------------------------------------------------------------------
library(DesignComplexSurvival)

## ----helpers, include = FALSE-------------------------------------------------
nf <- function(x) format(x, big.mark = ",")

## ----read---------------------------------------------------------------------
judgements <- c("PASS", "EXPLAINED", "FAIL", "INFO")
read_results <- function(folder) {
  utils::read.csv(system.file(folder, "results", "summary.csv",
                              package = "DesignComplexSurvival"))
}
rp <- read_results("reproduce")
vd <- read_results("validation")
count <- function(x, by) {
  tab <- as.data.frame.matrix(table(x[[by]], factor(x$judgement, levels = judgements)))
  tab$Total <- rowSums(tab)
  tab
}

## ----reproduce-table----------------------------------------------------------
knitr::kable(count(rp, "source"))

## ----validation-table---------------------------------------------------------
labels <- c(fastsurvival = "FastSurvival simulation",
            python = "Python simulation",
            python_two = "Python simulation, PFS and OS together",
            python_calendar = "Python simulation, switching from a calendar time",
            danzer = "Estimator of Danzer et al. (2025)",
            pweall = "PWEALL",
            lrstat_fseqbon = "lrstat::fseqbon()",
            rpact = "rpact::getPowerSurvival(), expected values",
            rpact_vignette = "Values printed in the rpact vignette",
            lrstat_expected = "lrstat::lrpower(), expected values",
            gsdesign_expected = "gsDesign::gsProbability(), expected values",
            python_expected = "Python simulation, expected values",
            gsdesign_cp = "gsDesign::gsCP(), conditional power",
            gsdesign_vignette = "Values printed in the gsDesign vignette")
tab <- count(vd, "source")
n_scen <- tapply(vd$scenario, vd$source, function(x) length(unique(x)))
tab <- cbind(Route = ifelse(is.na(labels[rownames(tab)]), rownames(tab),
                            labels[rownames(tab)]),
             Scenarios = as.vector(n_scen[rownames(tab)]), tab)
names(tab)[names(tab) == "Scenarios"] <- "Scenarios or designs"
knitr::kable(tab, row.names = FALSE)

## ----validation-criteria------------------------------------------------------
judged <- vd[vd$judgement != "INFO", ]
crit <- as.data.frame(table(criterion = judged$criterion, judgement = judged$judgement))
knitr::kable(crit[crit$Freq > 0, ], row.names = FALSE)

## ----info-z, fig.alt = "Histogram of the differences between the package and the simulations in standard errors for the rows that are not judged"----
info <- vd[vd$judgement == "INFO" & is.finite(vd$se_used) & vd$se_used > 0, ]
z <- info$diff / info$se_used
hist(z, breaks = 40, main = "", xlab = "(simulation - package) / standard error")

## ----info-largest-------------------------------------------------------------
big <- info[abs(z) > 4, ]
big$variant[big$variant == ""] <- "(none)"
table(quantity = big$quantity)
sort(table(variant = big$variant), decreasing = TRUE)
big$z <- big$diff / big$se_used
head(big[order(-abs(big$z)), c("source", "scenario", "hypothesis", "quantity",
                               "endpoint", "variant", "simulated", "package", "z")], 10)

