# Chapter 6, Testing: figures for the lecture notes ch06-testing.html.
# Follows Holmes and Huber (2019), Modern Statistics for Modern Biology, Chapter 6.
#
#   Rscript ch06_testing.R             all figures and the demo data
#   Rscript ch06_testing.R fig_6_14    one figure
#
# Figures reproduced: 6.2 to 6.15 and 6.17 to 6.22. Figure 6.4 (an animation in the book) is
# drawn as a static strip of four frames of the same interpolation.
# Figures skipped:
#   6.1   photograph of a screening robot (not computed from data)
#   6.16  cartoon (not computed from data)
#
# Slow steps (DESeq2 on the airway data, IHW) are cached in code/cache/ch06-*.rds.
# Data for the browser demos are written to courses/biostat/data/ch06-*.json.

.this_dir <- local({
  a <- grep("^--file=", commandArgs(FALSE), value = TRUE)
  if (length(a)) dirname(normalizePath(sub("^--file=", "", a[1]))) else getwd()
})
source(file.path(.this_dir, "common.R"))
suppressPackageStartupMessages({
  library(patchwork)
})

CH <- 6
CACHE <- file.path(CODE_DIR, "cache")
dir.create(CACHE, showWarnings = FALSE)
cached <- function(name, expr) {
  f <- file.path(CACHE, paste0("ch06-", name, ".rds"))
  if (file.exists(f)) return(readRDS(f))
  val <- force(expr)
  saveRDS(val, f)
  val
}

# =============================================================================================
# Shared computations
# =============================================================================================

# The coin of Section 6.2: 100 tosses of a coin with P(head) = 0.6 (the book's seed and draw).
coin_flips <- function() {
  set.seed(0xdada)
  sample(c("H", "T"), size = 100, replace = TRUE, prob = c(0.6, 0.4))
}

# Rejection region of the book's construction (Section 6.2): put the outcomes k into the region in
# order of increasing null probability, as long as the total null probability stays <= alpha.
greedy_region <- function(n, p0 = 0.5, alpha = 0.05) {
  pk <- dbinom(0:n, n, p0)
  o <- order(pk)
  keep <- cumsum(pk[o]) <= alpha
  sort((0:n)[o][keep])
}

# The same region from the distribution function only (the Task in Section 6.2): for the symmetric
# fair-coin null the region is {0..a} U {n-a..n} with the largest a such that 2 P(K <= a) <= alpha.
pbinom_region <- function(n, alpha = 0.05) {
  a <- max(which(2 * pbinom(0:n, n, 0.5) <= alpha)) - 1
  c(0:a, (n - a):n)
}

# Two-sample t statistic (6.4)-(6.5), written out.
t_stat <- function(x, y) {
  n1 <- length(x); n2 <- length(y)
  s2 <- (sum((x - mean(x))^2) + sum((y - mean(y))^2)) / (n1 + n2 - 2)
  sqrt(n1 * n2 / (n1 + n2)) * (mean(x) - mean(y)) / sqrt(s2)
}

# Exact permutation distribution of the t statistic: every split of the pooled sample into groups of
# sizes n1 and n2 (choose(20, 10) = 184,756 splits for ctrl versus trt2). Vectorised over splits.
perm_t_all <- function(x, y) {
  z <- c(x, y); n1 <- length(x); N <- length(z); n2 <- N - n1
  idx <- utils::combn(N, n1)
  s1 <- colSums(matrix(z[idx], nrow = n1))
  q1 <- colSums(matrix(z[idx]^2, nrow = n1))
  S <- sum(z); Q <- sum(z^2)
  m1 <- s1 / n1; m2 <- (S - s1) / n2
  ss <- (q1 - n1 * m1^2) + (Q - q1 - n2 * m2^2)
  sqrt(n1 * n2 / N) * (m1 - m2) / sqrt(ss / (N - 2))
}

plant_pair <- function(a = "ctrl", b = "trt2") {
  list(x = PlantGrowth$weight[PlantGrowth$group == a], y = PlantGrowth$weight[PlantGrowth$group == b])
}

# Two-component model of Figures 6.2 to 6.4: null score t with 4 degrees of freedom (area 2/3),
# alternative a gamma(2, 0.8) density shifted by -4 (area 1/3), cut at the null 5% quantile.
TG <- list(pi0 = 2 / 3, df = 4, shape = 2, rate = 0.8, shift = 4)
tg_f0 <- function(x) dt(x, TG$df)
tg_f1 <- function(x) dgamma(x + TG$shift, TG$shape, TG$rate)
tg_cut <- qt(0.05, TG$df)

# Airway: DESeq2 tests for the dexamethasone effect, design ~ cell + dex (Section 6.9).
airway_results <- function() cached("awde", {
  suppressPackageStartupMessages({ library(DESeq2); library(airway) })
  data("airway", package = "airway", envir = environment())
  aw <- DESeqDataSet(se = airway, design = ~ cell + dex)
  aw <- DESeq(aw, quiet = TRUE)
  res <- as.data.frame(results(aw))
  res <- res[!is.na(res$pvalue), c("baseMean", "log2FoldChange", "stat", "pvalue", "padj")]
  first <- counts(aw, normalized = TRUE)[1, ]
  list(awde = res, first_counts = first)
})

# The demo works on p-values rounded to 6 significant digits; every number the demo must reproduce
# is computed from the same rounded values.
airway_rounded <- function() {
  A <- airway_results()$awde
  data.frame(p = signif(A$pvalue, 6), bm = signif(A$baseMean, 4))
}

# Strata of Section 6.11: six groups of baseMean with breaks at its sextiles (2 significant digits).
strata_of <- function(bm) {
  cut(bm, include.lowest = TRUE, breaks = signif(quantile(bm, probs = seq(0, 1, length.out = 7)), 2))
}

ihw_result <- function() cached("ihw", {
  suppressPackageStartupMessages(library(IHW))
  A <- airway_results()$awde
  set.seed(6)
  r <- ihw(A$pvalue, A$baseMean, alpha = 0.1)
  list(rejections = rejections(r), weights = weights(r), adj = adj_pvalues(r),
       levels = weights(r, levels_only = TRUE), nbins = nbins(r), nfolds = nfolds(r))
})

# Benjamini-Hochberg step-up procedure, written out (Section 6.9.2): number of rejections.
bh_k <- function(p, phi) {
  m <- length(p); ps <- sort(p)
  ok <- which(ps <= phi * seq_len(m) / m)
  if (length(ok)) max(ok) else 0L
}

# Probability that a two-sided z-test on the running sum S_n of N(0, 1) observations rejects at least
# once when it is applied at the looks n = looks[1], looks[2], ... (Armitage et al., 1969).
# The density of S_n on the paths that have not yet rejected is carried on a grid of cells of width h
# and convolved with the N(0, n_j - n_{j-1}) increment density between looks (kernel cut at 8 SD);
# the grid reaches 8 increment SDs beyond the last boundary, so no path leaves it unseen.
# The cell width is 0.05 up to n = 100 and grows like sqrt(n) beyond (the browser demo uses the same rule).
# At each look the part of every cell beyond the boundary |s| = z sqrt(n) is absorbed (a cell cut
# by the boundary loses the fraction of its width that lies outside). The browser demo uses the
# same algorithm.
optional_stopping_exact <- function(looks, alpha = 0.05, h = 0.05 * max(1, sqrt(max(looks) / 100))) {
  zc <- qnorm(1 - alpha / 2)
  dmax <- if (length(looks) > 1) max(diff(looks)) else 1
  L <- max(zc * sqrt(max(looks)) + 8 * sqrt(dmax), 8 * sqrt(looks[1]))   # grid wide enough to see every exit
  g <- seq(0, L + h, by = h); g <- c(-rev(g[-1]), g)
  dens <- dnorm(g, sd = sqrt(looks[1]))           # density of S at the first look
  cum <- numeric(length(looks))
  stopped <- 0
  for (j in seq_along(looks)) {
    if (j > 1) {
      d <- looks[j] - looks[j - 1]
      w <- ceiling(8 * sqrt(d) / h)
      kern <- dnorm((-w:w) * h, sd = sqrt(d)) * h
      pad <- c(numeric(w), dens, numeric(w))
      dens <- as.vector(stats::filter(pad, kern, sides = 2))[(w + 1):(w + length(g))]
    }
    b <- zc * sqrt(looks[j])
    out <- pmin(pmax((abs(g) + h / 2 - b) / h, 0), 1)   # fraction of each cell beyond the boundary
    stopped <- stopped + sum(dens * out) * h
    dens <- dens * (1 - out)
    cum[j] <- stopped
  }
  cum
}

# Probability that the smallest of K two-sided p-values is <= alpha when the K test statistics are
# standard normal with common correlation rho >= 0 (Z_i = sqrt(rho) U + sqrt(1 - rho) E_i).
# Conditionally on U the statistics are independent, so P(all |Z_i| <= c) is a one-dimensional integral,
# done here by Simpson's rule on [-8, 8] with 2,000 intervals (as in the browser demo).
best_of_k_fpr <- function(K, rho, alpha = 0.05) {
  cc <- qnorm(1 - alpha / 2)
  if (rho == 0) return(1 - (1 - alpha)^K)
  u <- seq(-8, 8, length.out = 2001); hh <- u[2] - u[1]
  inner <- pnorm((cc - sqrt(rho) * u) / sqrt(1 - rho)) - pnorm((-cc - sqrt(rho) * u) / sqrt(1 - rho))
  wts <- c(1, rep(c(4, 2), length.out = 1999), 1)
  1 - sum(wts * dnorm(u) * inner^K) * hh / 3
}

# =============================================================================================
# Figures 6.2 to 6.4: a score x, its four outcomes, and the same picture on the p-value scale
# =============================================================================================

OUT_COL <- c("True positive" = "#e31a1c", "False negative" = "#fb9a99",
             "False positive" = "#a6cee3", "True negative" = "#1f78b4")

# Long data frame for the two stacked components on a given scale; `ux` maps x to the plotted
# coordinate and `jac` is its derivative (change of variables for the densities).
tg_frame <- function(ux, jac, xs = seq(-4, 4, length.out = 801)) {
  f0 <- TG$pi0 * tg_f0(xs) / jac(xs)
  f1 <- (1 - TG$pi0) * tg_f1(xs) / jac(xs)
  u <- ux(xs); cu <- ux(tg_cut)
  left <- u <= cu
  rbind(
    data.frame(u = u, y = f0, comp = "null (negatives)", out = ifelse(left, "False positive", "True negative")),
    data.frame(u = u, y = f1, comp = "alternative (positives)", out = ifelse(left, "True positive", "False negative"))
  ) |> transform(comp = factor(comp, levels = c("alternative (positives)", "null (negatives)")))
}

tg_plot <- function(d, cut, xlab) {
  ggplot(d, aes(u, y)) +
    geom_area(aes(fill = out, group = interaction(comp, out)), position = "identity", alpha = 0.95) +
    geom_line(aes(group = comp), linewidth = 0.3) +
    geom_vline(xintercept = cut, linewidth = 0.9) +
    facet_grid(comp ~ ., switch = "y") +
    scale_fill_manual(values = OUT_COL, name = NULL) +
    labs(x = xlab, y = NULL) +
    theme(legend.position = "bottom", axis.text.y = element_blank(), axis.ticks.y = element_blank(),
          strip.text.y.left = element_text(angle = 0), strip.placement = "outside")
}

fig_6_2 <- function() {
  d <- tg_frame(identity, function(x) rep(1, length(x)))
  savefig(tg_plot(d, tg_cut, "score x"), "fig-6-2", CH, 6.4, 3.6)
}

fig_6_3 <- function() {
  xs <- qt(seq(0.0005, 0.9995, length.out = 801), TG$df)    # even grid on the p-value scale
  d <- tg_frame(function(x) pt(x, TG$df), tg_f0, xs)
  savefig(tg_plot(d, 0.05, "p = F0(x)"), "fig-6-3", CH, 6.4, 3.6)
}

fig_6_4 <- function() {
  lam <- c(0, 1 / 3, 2 / 3, 1)
  xs <- qt(seq(0.0005, 0.9995, length.out = 241), TG$df)
  xs <- sort(unique(c(seq(-4, 4, length.out = 241), xs[xs > -4 & xs < 4], tg_cut)))
  d <- do.call(rbind, lapply(lam, function(l) {
    ux <- function(x) (1 - l) * (x + 4) / 8 + l * pt(x, TG$df)      # rescale x to [0, 1] first
    jac <- function(x) (1 - l) / 8 + l * tg_f0(x)
    cbind(tg_frame(ux, jac, xs), frame = sprintf("lambda = %s", c("0", "1/3", "2/3", "1")[match(l, lam)]),
          cut = ux(tg_cut))
  }))
  d$frame <- factor(d$frame, levels = unique(d$frame))
  cuts <- unique(d[, c("frame", "cut")])
  p <- ggplot(d, aes(u, y)) +
    geom_area(aes(fill = out, group = interaction(comp, out)), position = "identity", alpha = 0.95) +
    geom_line(aes(group = comp), linewidth = 0.3) +
    geom_vline(data = cuts, aes(xintercept = cut), linewidth = 0.8) +
    facet_grid(comp ~ frame, scales = "free_y", switch = "y") +
    scale_fill_manual(values = OUT_COL, name = NULL) +
    scale_x_continuous(breaks = c(0, 0.5, 1)) +
    labs(x = "u = (1 - lambda) (x + 4)/8 + lambda F0(x)", y = NULL) +
    theme(legend.position = "bottom", axis.text.y = element_blank(), axis.ticks.y = element_blank(),
          strip.text.y.left = element_text(angle = 0), strip.placement = "outside")
  savefig(p, "fig-6-4", CH, 8.4, 3.8)
}

# =============================================================================================
# Section 6.2: the binomial test
# =============================================================================================

fig_6_5 <- function() {
  k <- sum(coin_flips() == "H")
  d <- data.frame(k = 0:100, p = dbinom(0:100, 100, 0.5))
  p <- ggplot(d, aes(k, p)) + geom_col(width = 0.8, fill = "grey35") +
    geom_vline(xintercept = k, colour = COL[["blue"]], linewidth = 0.8) +
    labs(x = "number of heads k", y = "P(K = k)")
  savefig(p, "fig-6-5", CH, 4.6, 3.2)
}

fig_6_6 <- function() {
  set.seed(66)
  k <- sum(coin_flips() == "H")
  sims <- rbinom(10000, 100, 0.5)
  d <- as.data.frame(table(factor(sims, levels = 0:100)))
  d$k <- as.integer(as.character(d$Var1))
  p <- ggplot(d, aes(k, Freq)) + geom_col(width = 0.8, fill = "grey35") +
    geom_vline(xintercept = k, colour = COL[["blue"]], linewidth = 0.8) +
    labs(x = "number of heads in 100 simulated tosses", y = "count (of 10,000)")
  savefig(p, "fig-6-6", CH, 4.6, 3.2)
}

fig_6_7 <- function() {
  k <- sum(coin_flips() == "H")
  rr <- greedy_region(100)
  d <- data.frame(k = 0:100, p = dbinom(0:100, 100, 0.5), reject = (0:100) %in% rr)
  p <- ggplot(d, aes(k, p, fill = reject)) + geom_col(width = 0.8) +
    scale_fill_manual(values = c(`TRUE` = COL[["red"]], `FALSE` = "grey60"), guide = "none") +
    geom_vline(xintercept = k, colour = COL[["blue"]], linewidth = 0.8) +
    labs(x = "number of heads k", y = "P(K = k)")
  savefig(p, "fig-6-7", CH, 4.6, 3.2)
}

# =============================================================================================
# Section 6.5: the t-test and the permutation test
# =============================================================================================

fig_6_8 <- function() {
  p <- ggplot(PlantGrowth, aes(group, weight, colour = group)) +
    ggbeeswarm::geom_beeswarm(size = 2, cex = 3) +
    scale_colour_manual(values = unname(COL[c("blue", "orange", "green")]), guide = "none") +
    labs(x = NULL, y = "dried weight")
  savefig(p, "fig-6-8", CH, 4.2, 3.4)
}

tnull_cols <- c(low = COL[["blue"]], high = COL[["red"]], none = "grey60")

fig_6_9 <- function() {
  pr <- plant_pair()
  tobs <- t_stat(pr$x, pr$y)
  tp <- perm_t_all(pr$x, pr$y)
  q <- quantile(tp, c(0.025, 0.975), type = 1)
  br <- seq(-5, 5, by = 0.2)
  h <- hist(pmin(pmax(tp, -5), 5), breaks = br, plot = FALSE)
  d <- data.frame(mid = h$mids, n = h$counts)
  d$reg <- ifelse(d$mid <= q[1], "low", ifelse(d$mid > q[2], "high", "none"))
  p <- ggplot(d, aes(mid, n, fill = reg)) + geom_col(width = 0.2) +
    geom_vline(xintercept = tobs, colour = "#101010") +
    scale_fill_manual(values = tnull_cols, guide = "none") +
    coord_cartesian(xlim = c(-5, 5)) +
    labs(x = "t (all 184,756 relabellings)", y = "count")
  savefig(p, "fig-6-9", CH, 4.6, 3.0)
}

fig_6_10 <- function() {
  pr <- plant_pair()
  tobs <- t_stat(pr$x, pr$y)
  q <- qt(c(0.025, 0.975), 18)
  d <- data.frame(t = seq(-5, 5, by = 0.01))
  d$dens <- dt(d$t, 18)
  d$reg <- ifelse(d$t <= q[1], "low", ifelse(d$t > q[2], "high", "none"))
  p <- ggplot(d, aes(t, dens)) +
    geom_area(aes(fill = reg, group = reg), position = "identity") +
    geom_line(linewidth = 0.3) +
    geom_vline(xintercept = tobs, colour = "#101010") +
    scale_fill_manual(values = tnull_cols, guide = "none") +
    labs(x = "t", y = "density of t with 18 df")
  savefig(p, "fig-6-10", CH, 4.6, 3.0)
}

# =============================================================================================
# Section 6.8: the family-wise error rate
# =============================================================================================

fig_6_11 <- function() {
  m <- 10000
  d <- data.frame(alpha = seq(0, 7e-6, length.out = 200))
  d$p <- 1 - (1 - d$alpha)^m
  a_sidak <- 1 - 0.95^(1 / m)
  p <- ggplot(d, aes(alpha, p)) + geom_line() +
    geom_hline(yintercept = 0.05, colour = COL[["red"]]) +
    geom_vline(xintercept = 0.05 / m, colour = COL[["grey"]], linetype = "dashed") +
    annotate("point", x = a_sidak, y = 0.05, colour = COL[["red"]]) +
    labs(x = expression("per-test level " * alpha), y = "P(at least one false rejection)")
  savefig(p, "fig-6-11", CH, 4.6, 3.2)
}

# =============================================================================================
# Sections 6.9 and 6.10: airway p-values, FDR, Benjamini-Hochberg, two-groups model
# =============================================================================================

fig_6_12 <- function() {
  A <- airway_results()$awde
  p <- ggplot(A, aes(pvalue)) + geom_histogram(binwidth = 0.025, boundary = 0, fill = "grey35") +
    labs(x = "p-value", y = "number of genes")
  savefig(p, "fig-6-12", CH, 4.6, 3.2)
}

fig_6_13 <- function() {
  A <- airway_results()$awde
  a <- 0.025
  pi0 <- 2 * mean(A$pvalue > 0.5)
  p <- ggplot(A, aes(pvalue)) + geom_histogram(binwidth = a, boundary = 0, fill = "grey35") +
    geom_hline(yintercept = pi0 * a * nrow(A), colour = COL[["blue"]], linewidth = 0.8) +
    geom_vline(xintercept = a, colour = COL[["red"]], linewidth = 0.8) +
    labs(x = "p-value", y = "number of genes")
  savefig(p, "fig-6-13", CH, 4.6, 3.2)
}

fig_6_14 <- function() {
  A <- airway_results()$awde
  m <- nrow(A); phi <- 0.1
  d <- data.frame(rank = seq_len(7000), p = sort(A$pvalue)[1:7000])
  k <- bh_k(A$pvalue, phi)
  p <- ggplot(d, aes(rank, p)) + geom_line() +
    geom_abline(slope = phi / m, intercept = 0, colour = COL[["red"]]) +
    geom_vline(xintercept = k, colour = COL[["grey"]], linetype = "dashed") +
    labs(x = "rank i", y = expression(p[(i)]))
  savefig(p, "fig-6-14", CH, 4.6, 3.2)
}

fig_6_15 <- function() {
  A <- airway_results()$awde
  f <- A[A$baseMean >= 1, ]
  f <- f[order(f$pvalue), ]
  f$omp <- 1 - f$pvalue
  f$N <- nrow(f) - seq_len(nrow(f))
  jj <- round(nrow(f) * c(1, 0.5))
  slope <- diff(f$N[jj]) / diff(f$omp[jj])
  p <- ggplot(f, aes(omp, N)) + geom_point(size = 0.15) +
    geom_abline(intercept = 0, slope = slope, colour = COL[["red"]]) +
    geom_hline(yintercept = slope, linetype = "dotted") +
    geom_vline(xintercept = 1, linetype = "dotted") +
    annotate("text", x = 0, y = slope, label = format(round(slope), big.mark = ","), hjust = -0.1, vjust = -0.3) +
    labs(x = expression(1 - p[i]), y = expression(N(p[i])))
  savefig(p, "fig-6-15", CH, 4.6, 3.6, dense = TRUE)
}

# Two-groups model of Figure 6.17: pi0 = 0.6, alternative Beta(1, 7) density, cutoff 0.1.
LF <- list(pi0 = 0.6, b = 7, t0 = 0.1)
lf_f <- function(t) LF$pi0 + (1 - LF$pi0) * dbeta(t, 1, LF$b)
lf_F <- function(t) LF$pi0 * t + (1 - LF$pi0) * pbeta(t, 1, LF$b)

fig_6_17 <- function() {
  t <- seq(0, 1, length.out = 501)
  t0 <- LF$t0
  pal <- c(nullL = "#bdbdbd", nullR = "#f0f0f0", altL = "#1f78b4", altR = "#a6cee3")
  dd <- function(y, y0) {
    rbind(data.frame(t = t[t <= t0], lo = 0, hi = y0[t <= t0], k = "nullL"),
          data.frame(t = t[t >= t0], lo = 0, hi = y0[t >= t0], k = "nullR"),
          data.frame(t = t[t <= t0], lo = y0[t <= t0], hi = y[t <= t0], k = "altL"),
          data.frame(t = t[t >= t0], lo = y0[t >= t0], hi = y[t >= t0], k = "altR"))
  }
  f <- lf_f(t); f0 <- rep(LF$pi0, length(t))
  F <- lf_F(t); F0 <- LF$pi0 * t
  segs <- function(y0v, yv) data.frame(x = t0, y = c(0, y0v), yend = c(y0v, yv), k = c("n", "a"))
  top <- ggplot(dd(f, f0)) + geom_ribbon(aes(t, ymin = lo, ymax = hi, fill = k, group = k)) +
    geom_line(data = data.frame(t = t, f = f), aes(t, f)) +
    geom_segment(data = segs(LF$pi0, lf_f(t0)), aes(x = x, xend = x, y = y, yend = yend, colour = k), linewidth = 1.4) +
    scale_fill_manual(values = pal, guide = "none") +
    scale_colour_manual(values = c(n = "#fb9a99", a = "#e31a1c"), guide = "none") +
    scale_y_continuous(breaks = c(0, LF$pi0, 1, 2, 3), labels = c("0", "pi0 = 0.6", "1", "2", "3")) +
    coord_cartesian(ylim = c(0, f[1]), expand = FALSE) + labs(x = NULL, y = "density f(p)")
  bot <- ggplot(dd(F, F0)) + geom_ribbon(aes(t, ymin = lo, ymax = hi, fill = k, group = k)) +
    geom_line(data = data.frame(t = t, F = F), aes(t, F)) +
    geom_segment(data = segs(LF$pi0 * t0, lf_F(t0)), aes(x = x, xend = x, y = y, yend = yend, colour = k), linewidth = 1.4) +
    scale_fill_manual(values = pal, guide = "none") +
    scale_colour_manual(values = c(n = "#fb9a99", a = "#e31a1c"), guide = "none") +
    coord_cartesian(ylim = c(0, 1), expand = FALSE) + labs(x = "p", y = "distribution F(p)")
  savefig(top / bot, "fig-6-17", CH, 4.2, 6.0)
}

# =============================================================================================
# Section 6.11: independent hypothesis weighting
# =============================================================================================

fig_6_18 <- function() {
  A <- airway_results()$awde
  p <- ggplot(A, aes(asinh(baseMean))) + geom_histogram(bins = 60, fill = "grey35") +
    labs(x = "asinh(baseMean)", y = "number of genes")
  savefig(p, "fig-6-18", CH, 4.6, 3.0)
}

fig_6_19 <- function() {
  A <- airway_results()$awde
  p <- ggplot(A, aes(rank(baseMean), -log10(pvalue))) + geom_hex(bins = 60) +
    scale_fill_viridis_c(trans = "log10", guide = "none") +
    labs(x = "rank of baseMean", y = expression(-log[10]~p))
  savefig(p, "fig-6-19", CH, 4.6, 3.4)
}

fig_6_20 <- function() {
  A <- airway_results()$awde
  A$stratum <- strata_of(A$baseMean)
  p <- ggplot(A, aes(pvalue)) + facet_wrap(~ stratum, nrow = 3) +
    geom_histogram(binwidth = 0.025, boundary = 0, fill = "grey35") +
    labs(x = "p-value", y = "number of genes")
  savefig(p, "fig-6-20", CH, 6.0, 5.0)
}

fig_6_21 <- function() {
  A <- airway_results()$awde
  A$stratum <- strata_of(A$baseMean)
  # ECDF evaluated on a fine grid (identical curve at plotting resolution, much smaller SVG)
  g <- c(0, sort(unique(c(seq(0, 1, by = 0.002), 10^seq(-12, -3, by = 0.05)))))
  d <- do.call(rbind, lapply(split(A$pvalue, A$stratum), function(p) {
    data.frame(p = g, F = ecdf(p)(g))
  }))
  d$stratum <- factor(rep(levels(A$stratum), each = length(g)), levels = levels(A$stratum))
  p <- ggplot(d, aes(p, F, colour = stratum)) + geom_step() +
    scale_colour_viridis_d(end = 0.9, name = "baseMean stratum") +
    labs(x = "p-value", y = "empirical distribution function") +
    theme(legend.position = "bottom") + guides(colour = guide_legend(nrow = 2))
  savefig(p, "fig-6-21", CH, 5.6, 4.4)
}

fig_6_22 <- function() {
  W <- ihw_result()$levels
  d <- data.frame(stratum = rep(seq_len(nrow(W)), ncol(W)), w = as.vector(W),
                  fold = factor(rep(seq_len(ncol(W)), each = nrow(W))))
  p <- ggplot(d, aes(stratum, w, colour = fold)) + geom_line() + geom_point(size = 1) +
    scale_colour_viridis_d(end = 0.9) +
    labs(x = "stratum of baseMean (low to high)", y = "weight")
  savefig(p, "fig-6-22", CH, 5.0, 3.0)
}

# =============================================================================================
# Data for the browser demonstrations
# =============================================================================================

export_demo_data <- function() {
  export_json(list(weight = PlantGrowth$weight, group = as.character(PlantGrowth$group)), "ch06-plantgrowth.json")
  R <- airway_rounded()
  export_json(list(p = R$p, baseMean = R$bm), "ch06-airway.json")
  W <- ihw_result()$weights
  export_json(list(w = signif(W, 5)), "ch06-airway-ihw.json")
}

run_figures(list(
  fig_6_2 = fig_6_2, fig_6_3 = fig_6_3, fig_6_4 = fig_6_4, fig_6_5 = fig_6_5, fig_6_6 = fig_6_6,
  fig_6_7 = fig_6_7, fig_6_8 = fig_6_8, fig_6_9 = fig_6_9, fig_6_10 = fig_6_10, fig_6_11 = fig_6_11,
  fig_6_12 = fig_6_12, fig_6_13 = fig_6_13, fig_6_14 = fig_6_14, fig_6_15 = fig_6_15,
  fig_6_17 = fig_6_17, fig_6_18 = fig_6_18, fig_6_19 = fig_6_19, fig_6_20 = fig_6_20,
  fig_6_21 = fig_6_21, fig_6_22 = fig_6_22, data = export_demo_data
))
