inst/validation/run_all.R

# Run the validation against independent routes and summarize the results.
#
# Usage (working directory: package root):
#   devtools::load_all()
#   source("inst/validation/run_all.R")
#
# 1. validate_fastsurvival.R simulates V1-V5 with FastSurvival (about 30
#    minutes). Its result results/sim_fastsurvival.csv is reused when
#    helpers.R and validate_fastsurvival.R are unchanged since it was written
#    (checked by MD5); set rerun_fastsurvival <- TRUE before sourcing to force
#    a new simulation.
# 2. compare_simulation.R compares results/sim_python.csv (V6-V10, written by
#    python/simulate_ms.py) and results/sim_fastsurvival.csv with the package.
# 3. compare_two_endpoints.R compares results/sim_python_two.csv (V3, V4, V6,
#    V7, V11 and V12 with PFS and OS together, written by
#    python/simulate_two.py) and results/danzer_check.csv
#    (python/danzer_check.py) with the package; compare_calendar.R compares
#    results/sim_python_calendar.csv (V13-V15, switching from a calendar time,
#    written by python/simulate_calendar.py) with the package.
# 4. compare_pweall.R compares with PWEALL.
# 5. compare_graph_lrstat.R compares the graphical procedure with recycling
#    of the level with lrstat::fseqbon() and lrstat::getBound().
# 6. compare_expected.R compares the expected values when the trial stops at
#    the bounds with rpact, lrstat, gsDesign, the values printed in the rpact
#    vignette and results/sim_python_expected.csv (written by
#    python/simulate_expected.py).
# 7. compare_cond_power.R compares the conditional power with
#    gsDesign::gsCP().
# Outputs in inst/validation/results/: compare_simulation.csv,
# compare_two_endpoints.csv, compare_calendar.csv, compare_pweall.csv,
# compare_graph_lrstat.csv, compare_expected.csv, compare_cond_power.csv,
# summary.csv, summary.md and perturb.csv. The run stops with an error (after
# writing the outputs) when the Python results are missing, when the
# perturbation check does not give the expected judgements, or when an id of
# explained.csv is not judged EXPLAINED.

local({
  dir <- if (dir.exists(file.path("inst", "validation"))) {
    file.path("inst", "validation")
  } else {
    system.file("validation", package = "DesignComplexSurvival")
  }
  henv <- new.env(parent = globalenv())
  sys.source(file.path(dir, "helpers.R"), envir = henv)
  rd <- henv$val_results_dir()
  problems <- character(0)
  notes <- character(0)
  timing <- numeric(0)

  # 1. FastSurvival
  md5_in <- unname(tools::md5sum(file.path(dir, c("helpers.R", "validate_fastsurvival.R"))))
  side <- file.path(rd, "sim_fastsurvival_inputs_md5.txt")
  fs_file <- file.path(rd, "sim_fastsurvival.csv")
  force <- exists("rerun_fastsurvival", envir = globalenv()) && isTRUE(get("rerun_fastsurvival", envir = globalenv()))
  reuse <- !force && file.exists(fs_file) && file.exists(side) && identical(readLines(side), md5_in)
  if (reuse) {
    message("Reusing results/sim_fastsurvival.csv (inputs unchanged).")
    notes <- c(notes, "FastSurvival results reused (helpers.R and validate_fastsurvival.R unchanged).")
  } else {
    t0 <- proc.time()[["elapsed"]]
    fenv <- new.env(parent = henv)
    sys.source(file.path(dir, "validate_fastsurvival.R"), envir = fenv)
    timing["validate_fastsurvival"] <- proc.time()[["elapsed"]] - t0
    if (isTRUE(fenv$fs_available)) {
      writeLines(md5_in, side)
    } else {
      notes <- c(notes, "FastSurvival is not installed: V1-V5 are not simulated.")
    }
  }
  if (!file.exists(file.path(rd, "sim_python.csv"))) {
    problems <- c(problems, "results/sim_python.csv (python/simulate_ms.py) is missing.")
  }
  if (!file.exists(file.path(rd, "sim_python_two.csv"))) {
    problems <- c(problems, "results/sim_python_two.csv (python/simulate_two.py) is missing.")
  }
  if (!file.exists(file.path(rd, "danzer_check.csv"))) {
    problems <- c(problems, "results/danzer_check.csv (python/danzer_check.py) is missing.")
  }
  if (!file.exists(file.path(rd, "sim_python_calendar.csv"))) {
    problems <- c(problems, "results/sim_python_calendar.csv (python/simulate_calendar.py) is missing.")
  }
  if (!file.exists(file.path(rd, "sim_python_expected.csv"))) {
    problems <- c(problems, "results/sim_python_expected.csv (python/simulate_expected.py) is missing.")
  }

  # 2. simulations versus the package
  t0 <- proc.time()[["elapsed"]]
  senv <- new.env(parent = henv)
  sys.source(file.path(dir, "compare_simulation.R"), envir = senv)
  cs <- senv$compare_simulation()
  cs$note <- ""
  timing["compare_simulation"] <- proc.time()[["elapsed"]] - t0

  # 3. PFS and OS together versus the package
  t0 <- proc.time()[["elapsed"]]
  tenv <- new.env(parent = henv)
  sys.source(file.path(dir, "compare_two_endpoints.R"), envir = tenv)
  ct <- tenv$compare_two_endpoints()
  if (!is.null(ct)) {
    ct$note <- ""
  }
  timing["compare_two_endpoints"] <- proc.time()[["elapsed"]] - t0
  two_src <- c("python_two", "danzer")

  # 3b. switching from a calendar time versus the package
  t0 <- proc.time()[["elapsed"]]
  cenv <- new.env(parent = henv)
  sys.source(file.path(dir, "compare_calendar.R"), envir = cenv)
  cc <- cenv$compare_calendar()
  timing["compare_calendar"] <- proc.time()[["elapsed"]] - t0

  # 4. PWEALL
  t0 <- proc.time()[["elapsed"]]
  penv <- new.env(parent = henv)
  sys.source(file.path(dir, "compare_pweall.R"), envir = penv)
  cp <- penv$compare_pweall()
  timing["compare_pweall"] <- proc.time()[["elapsed"]] - t0
  if (is.null(cp)) {
    notes <- c(notes, "PWEALL is not installed: the comparison with PWEALL is not made.")
  }

  # 5. lrstat::fseqbon() (graphical procedure with recycling of the level)
  t0 <- proc.time()[["elapsed"]]
  genv <- new.env(parent = henv)
  sys.source(file.path(dir, "compare_graph_lrstat.R"), envir = genv)
  cg <- genv$compare_graph_lrstat()
  timing["compare_graph_lrstat"] <- proc.time()[["elapsed"]] - t0
  if (is.null(cg)) {
    notes <- c(notes, paste("lrstat 0.3.4 or later is not installed: the comparison with",
                            "lrstat::fseqbon() is not made."))
  }

  # 6. expected values when the trial stops at the bounds
  t0 <- proc.time()[["elapsed"]]
  eenv <- new.env(parent = henv)
  sys.source(file.path(dir, "compare_expected.R"), envir = eenv)
  ce <- eenv$compare_expected()
  timing["compare_expected"] <- proc.time()[["elapsed"]] - t0
  for (p in c("rpact", "lrstat", "gsDesign")) {
    if (!requireNamespace(p, quietly = TRUE)) {
      notes <- c(notes, paste0(p, " is not installed: the expected values are not compared with it."))
    }
  }
  exp_src <- c("rpact", "rpact_vignette", "lrstat_expected", "gsdesign_expected", "python_expected")

  # 7. conditional power, gsDesign::gsCP()
  t0 <- proc.time()[["elapsed"]]
  qenv <- new.env(parent = henv)
  sys.source(file.path(dir, "compare_cond_power.R"), envir = qenv)
  cq <- qenv$compare_cond_power()
  timing["compare_cond_power"] <- proc.time()[["elapsed"]] - t0
  if (is.null(cq)) {
    notes <- c(notes, "gsDesign is not installed: the conditional power is not compared with gsDesign::gsCP().")
  }
  cp_src <- c("gsdesign_vignette", "gsdesign_cp")
  all <- rbind(cs, ct, cc, cp, cg, ce, cq)
  ex <- henv$val_explained()
  j <- henv$val_judge(all, explained = ex$id)
  all <- cbind(all, j)
  utils::write.csv(all[!(all$source %in% c("pweall", "lrstat_fseqbon", "python_calendar", two_src,
                                            exp_src, cp_src)), ],
                   file.path(rd, "compare_simulation.csv"),
                   row.names = FALSE)
  if (!is.null(ct)) {
    utils::write.csv(all[all$source %in% two_src, ], file.path(rd, "compare_two_endpoints.csv"),
                     row.names = FALSE)
  }
  if (!is.null(cp)) {
    utils::write.csv(all[all$source == "pweall", ], file.path(rd, "compare_pweall.csv"), row.names = FALSE)
  }
  if (!is.null(cg)) {
    utils::write.csv(all[all$source == "lrstat_fseqbon", ], file.path(rd, "compare_graph_lrstat.csv"),
                     row.names = FALSE)
  }
  if (!is.null(cc)) {
    utils::write.csv(all[all$source == "python_calendar", ], file.path(rd, "compare_calendar.csv"),
                     row.names = FALSE)
  }
  if (!is.null(ce)) {
    utils::write.csv(all[all$source %in% exp_src, ], file.path(rd, "compare_expected.csv"),
                     row.names = FALSE)
  }
  if (!is.null(cq)) {
    utils::write.csv(all[all$source %in% cp_src, ], file.path(rd, "compare_cond_power.csv"),
                     row.names = FALSE)
  }
  utils::write.csv(all, file.path(rd, "summary.csv"), row.names = FALSE)
  unused <- setdiff(ex$id, all$id[all$judgement == "EXPLAINED"])
  if (length(unused)) {
    problems <- c(problems, paste("ids of explained.csv not judged EXPLAINED:", paste(unused, collapse = ", ")))
  }

  # perturbation check: package values times 1.1. A sensitive row must not
  # PASS after the change; the other rows are not required to change.
  jd <- all[all$criterion != "info", , drop = FALSE]
  pj <- henv$val_judge(jd, explained = ex$id, perturb = TRUE)
  pt <- data.frame(id = jd$id, judgement = jd$judgement, sensitive = jd$sensitive,
                   judgement_perturbed = pj$judgement,
                   expected = ifelse(jd$sensitive, "FAIL or EXPLAINED", "any"),
                   ok = !jd$sensitive | pj$judgement != "PASS", stringsAsFactors = FALSE)
  utils::write.csv(pt, file.path(rd, "perturb.csv"), row.names = FALSE)
  if (any(!pt$ok)) {
    problems <- c(problems, paste("perturbation check failed for:", paste(pt$id[!pt$ok], collapse = ", ")))
  }

  # summary.md
  fmt <- function(x, d = 6) ifelse(is.na(x), "", formatC(x, digits = d, format = "g"))
  ver <- function(p) if (requireNamespace(p, quietly = TRUE)) as.character(utils::packageVersion(p)) else "not installed"
  desc <- if (file.exists("DESCRIPTION")) read.dcf("DESCRIPTION", fields = "Version")[1, 1] else ver("DesignComplexSurvival")
  md5 <- function(f) if (file.exists(f)) unname(tools::md5sum(f)) else "missing"
  lev <- c("PASS", "EXPLAINED", "FAIL", "INFO")
  key <- paste(all$source, all$scenario, sep = " ")
  tab <- table(factor(key, levels = unique(key)), factor(all$judgement, levels = lev))
  l <- c("# Validation against independent routes", "",
         "Generated by `inst/validation/run_all.R`. Do not edit by hand.", "",
         sprintf("- Date: %s", format(Sys.time(), "%Y-%m-%d %H:%M:%S %Z")),
         sprintf("- %s; DesignComplexSurvival %s; FastSurvival %s; PWEALL %s; lrstat %s; rpact %s; gsDesign %s",
                 R.version.string, desc, ver("FastSurvival"), ver("PWEALL"), ver("lrstat"), ver("rpact"),
                 ver("gsDesign")),
         sprintf("- sim_python.csv md5 %s; sim_fastsurvival.csv md5 %s", md5(file.path(rd, "sim_python.csv")),
                 md5(fs_file)),
         sprintf("- sim_python_two.csv md5 %s; danzer_check.csv md5 %s; sim_python_calendar.csv md5 %s",
                 md5(file.path(rd, "sim_python_two.csv")), md5(file.path(rd, "danzer_check.csv")),
                 md5(file.path(rd, "sim_python_calendar.csv"))),
         sprintf("- sim_python_expected.csv md5 %s", md5(file.path(rd, "sim_python_expected.csv"))),
         sprintf("- Status: %s", if (length(problems)) "CHECKS FAILED" else "all checks passed"),
         if (length(notes)) paste("- Note:", notes), "")
  if (length(problems)) {
    l <- c(l, "## Problems", "", paste("-", problems), "")
  }
  l <- c(l, "## Judgements", "", "| Source and scenario | PASS | EXPLAINED | FAIL | INFO |", "|---|---|---|---|---|",
         sprintf("| %s | %d | %d | %d | %d |", rownames(tab), tab[, "PASS"], tab[, "EXPLAINED"], tab[, "FAIL"],
                 tab[, "INFO"]),
         sprintf("| Total | %d | %d | %d | %d |", sum(tab[, "PASS"]), sum(tab[, "EXPLAINED"]), sum(tab[, "FAIL"]),
                 sum(tab[, "INFO"])), "",
         sprintf("Run times (s): %s.", paste(names(timing), round(timing, 1), sep = " ", collapse = "; ")), "")
  line <- function(d) {
    sprintf("| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |", d$source, d$scenario, d$hypothesis,
            paste(d$look_type, d$look), fmt(d$time, 4), paste(d$quantity, d$endpoint, d$group),
            d$variant, fmt(d$simulated), fmt(d$package), ifelse(d$se_used > 0, fmt(d$diff / d$se_used, 3), fmt(d$diff, 3)))
  }
  hdr <- c("| Source | Scenario | Hypothesis | Look | Time | Quantity | Variant | Simulated / PWEALL | Package | Diff / SE (or diff) |",
           "|---|---|---|---|---|---|---|---|---|---|")
  f <- all[all$judgement == "FAIL", , drop = FALSE]
  l <- c(l, "## FAIL", "", if (nrow(f)) c(hdr, line(f)) else "None.", "")
  e <- all[all$judgement == "EXPLAINED", , drop = FALSE]
  if (nrow(e)) {
    l <- c(l, "## EXPLAINED", "", hdr, line(e), "")
  }
  pa <- all[all$source == "pweall", , drop = FALSE]
  if (nrow(pa)) {
    l <- c(l, "## PWEALL", "", hdr, line(pa), "")
  }
  inf <- all[all$judgement == "INFO" & all$source != "pweall", , drop = FALSE]
  l <- c(l, "## INFO (asymptotic approximations and other definitions)", "",
         "Simulated mean (or standard deviation, correlation, rejection rate, the estimate of Danzer et al. 2025, or the conditional power of gsDesign::gsCP() with the correlations of the events) and package value; Diff / SE is the difference divided by the Monte Carlo standard error (or the difference when there is no standard error).", "",
         hdr, line(inf), "")
  l <- c(l, "## Perturbation check", "",
         sprintf("Package values of the %d judged rows multiplied by 1.1: %d sensitive rows (the 10%% change exceeds the tolerance plus the observed difference), all expected to FAIL; %d other rows (not required to change).",
                 nrow(pt), sum(pt$sensitive), sum(!pt$sensitive)),
         sprintf("Sensitive rows judged FAIL after the change: %d of %d.", sum(pt$sensitive & pt$judgement_perturbed != "PASS"),
                 sum(pt$sensitive)), "")
  writeLines(l, file.path(rd, "summary.md"), useBytes = TRUE)
  if (length(problems)) {
    stop(paste(c("Validation checks failed:", problems), collapse = "\n  "), call. = FALSE)
  }
  message("Validation finished. Results in ", rd)
  invisible(all)
})

Try the DesignComplexSurvival package in your browser

Any scripts or data that you put into this service are public.

DesignComplexSurvival documentation built on Nov. 23, 2026, 5:07 p.m.