# Chapter 7. Multivariate Analysis
# Course: Statistical Methods for Modern Biology, following Holmes and Huber (2019),
# Modern Statistics for Modern Biology, Cambridge University Press, Chapter 7.
#
# Reproduces every figure of the chapter that is computed from data or by simulation, and writes the
# small data files used by the four browser demonstrations of this chapter.
#
#   Rscript ch07_multivariate_analysis.R              all figures and the demo data
#   Rscript ch07_multivariate_analysis.R fig_7_11     one figure
#
# Figures NOT reproduced (not computed from data):
#   Figure 7.1   cartoon about dimensions (reprinted comic)
#   Figure 7.12  photograph of a shadow of a three-dimensional object (mystery silhouette)
#   Figure 7.13  hand-drawn diagram of the choices in a PCA workflow
#   Figure 7.14  photograph of another shadow of the same object
#   Figure 7.21  cartoon about eigenvectors (reprinted comic)
# Figures 7.15 and 7.16 are drawn here from the matrix of equation (7.4) (cell areas are products of
# the margins), so they are reproduced.
#
# Package note: the book draws Figures 7.3 and 7.5 with ComplexHeatmap, which is not installed here;
# they are drawn with pheatmap (same hierarchical clustering of the variables, same diverging scale).
#
# All PCA computations below are done from the singular value decomposition directly, so that the
# conventions (divisor n or n - 1, signs, scaling of the arrows) are explicit; ade4::dudi.pca is used
# where the book uses it, to show that the numbers agree.

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

suppressPackageStartupMessages({
  library(ade4)
  library(dplyr)
  library(patchwork)
  library(ggrepel)
})

CH <- 7

# ================================================================================================
# Data
# ================================================================================================
ATH_NAMES <- c("m100", "long", "weight", "high", "m400", "m110", "disc", "pole", "javel", "m1500")

load_turtles <- function() read.table(msmb_data("PaintedTurtles.txt"), header = TRUE)

# athletes_1: times and distances as recorded; athletes_2: times turned into speeds (m/s);
# athletes_3: athletes_2 standardised (mean 0, sd 1 with divisor n - 1). Holmes and Huber (2019), Section 7.2.
load_athletes <- function() {
  e <- new.env()
  data("olympic", package = "ade4", envir = e)
  a1 <- setNames(e$olympic$tab, ATH_NAMES)
  a2 <- a1
  for (v in c("m100", "m110", "m400", "m1500")) a2[[v]] <- as.numeric(sub("m", "", v)) / a1[[v]]
  a3 <- data.frame(scale(a2))
  list(a1 = a1, a2 = a2, a3 = a3, score = e$olympic$score)
}

load_rdata <- function(file, name) { e <- new.env(); load(msmb_data(file), envir = e); e[[name]] }

load_hiiragi <- function() {
  suppressPackageStartupMessages(library(Biobase))
  e <- new.env()
  data("x", package = "Hiiragi2013", envir = e)
  x <- e$x
  xwt <- x[, x$genotype == "WT"]
  sel <- order(matrixStats::rowVars(Biobase::exprs(xwt)), decreasing = TRUE)[1:100]
  xwt <- xwt[sel, ]
  grp <- as.character(xwt$sampleGroup)
  tab <- table(grp)
  list(X = t(Biobase::exprs(xwt)), group = grp, genes = as.character(Biobase::fData(xwt)$symbol),
       weight = 1 / as.numeric(tab[grp]), tab = tab)
}

# The 3 x 4 matrix of equation (7.4) and the rank-two matrix Xtwo of Section 7.6.2.
X74 <- matrix(c(780, 936, 1300, 728,
                75, 90, 125, 70,
                540, 648, 900, 504), nrow = 3, byrow = TRUE)
XTWO <- matrix(c(12.5, 35, 25, 25,
                 9, 14, 26, 18,
                 16, 21, 49, 32,
                 18, 28, 52, 36,
                 18, 10.5, 64.5, 36), ncol = 4, byrow = TRUE)

# PCA of a numeric matrix from the SVD, with optional row weights (the ade4 convention: weighted mean,
# weighted sd with divisor sum(w), eigenvalues of Z' D Z with D = diag(w), w NOT renormalised).
# Returns eig, loadings V (c1), scores (li = Z V), variable coordinates (co = V sqrt(eig)).
pca_svd <- function(X, center = TRUE, scale = TRUE, w = rep(1 / nrow(X), nrow(X))) {
  X <- as.matrix(X)
  m <- if (center) colSums(X * w) / sum(w) else rep(0, ncol(X))
  Z <- sweep(X, 2, m)
  s <- if (scale) sqrt(colSums(Z^2 * w) / sum(w)) else rep(1, ncol(X))
  Z <- sweep(Z, 2, s, "/")
  sv <- svd(sqrt(w) * Z)
  r <- sum(sv$d > 1e-10 * sv$d[1])
  V <- sv$v[, seq_len(r), drop = FALSE]
  # sign convention: the largest-magnitude loading of each axis is positive (stable across runs)
  sg <- apply(V, 2, function(v) sign(v[which.max(abs(v))]))
  V <- sweep(V, 2, sg, "*")
  eig <- sv$d[seq_len(r)]^2
  li <- Z %*% V
  co <- sweep(V, 2, sqrt(eig), "*")
  dimnames(V) <- dimnames(co) <- list(colnames(X), paste0("PC", seq_len(r)))
  colnames(li) <- paste0("PC", seq_len(r))
  list(eig = eig, V = V, li = li, co = co, Z = Z, center = m, scale = s, w = w)
}

pct_lab <- function(eig, k) sprintf("PC%d (%.1f%%)", k, 100 * eig[k] / sum(eig))

scree_plot <- function(eig, nmax = 10, fill = COL[["blue"]], width = 0.6) {
  k <- min(nmax, length(eig))
  d <- data.frame(k = factor(seq_len(k)), pct = 100 * eig[seq_len(k)] / sum(eig))
  ggplot(d, aes(k, pct)) + geom_col(fill = fill, width = width) +
    geom_text(aes(label = sprintf("%.1f", pct)), vjust = -0.4, size = 3) +
    scale_y_continuous(expand = expansion(mult = c(0, 0.12))) +
    labs(x = "principal component", y = "percentage of total variance")
}

corr_circle <- function(co, lab = rownames(co), axes = c(1, 2), eig = NULL, col = COL[["blue"]]) {
  d <- data.frame(x = co[, axes[1]], y = co[, axes[2]], lab = lab)
  th <- seq(0, 2 * pi, length.out = 361)
  p <- ggplot(d) +
    geom_path(data = data.frame(x = cos(th), y = sin(th)), aes(x, y), colour = "grey40") +
    geom_hline(yintercept = 0, linetype = 2, colour = "grey60") +
    geom_vline(xintercept = 0, linetype = 2, colour = "grey60") +
    geom_segment(aes(x = 0, y = 0, xend = x, yend = y), colour = col,
                 arrow = arrow(length = unit(0.18, "cm"))) +
    geom_text_repel(aes(x, y, label = lab), colour = col, size = 3.4, seed = 1, min.segment.length = 0.3) +
    coord_fixed(xlim = c(-1.1, 1.1), ylim = c(-1.1, 1.1))
  if (!is.null(eig)) p <- p + labs(x = pct_lab(eig, axes[1]), y = pct_lab(eig, axes[2]))
  p
}

# Mosaic of a rank-one matrix: column widths proportional to v, row heights proportional to u, so each
# cell's area is proportional to u_i v_j = x_ij / s. Margin boxes carry the labels of u, v and s.
mosaic_rank1 <- function(X, ulab = NULL, vlab = NULL, corner = NULL, title = NULL) {
  u <- rowSums(X); v <- colSums(X)
  gap <- 0.03
  wcol <- v / sum(v) * (1 - gap * (length(v) - 1)); hrow <- u / sum(u) * (1 - gap * (length(u) - 1))
  mw <- 0.2; mh <- 0.16
  x0 <- mw + gap + c(0, cumsum(wcol + gap))[seq_along(v)]
  y1 <- 1 - c(0, cumsum(hrow + gap))[seq_along(u)]
  cells <- expand.grid(i = seq_along(u), j = seq_along(v))
  cells$xmin <- x0[cells$j]; cells$xmax <- cells$xmin + wcol[cells$j]
  cells$ymax <- y1[cells$i]; cells$ymin <- cells$ymax - hrow[cells$i]
  cells$lab <- format(X[cbind(cells$i, cells$j)], trim = TRUE)
  cells$fill <- factor(cells$j)
  top <- data.frame(xmin = c(0, x0), xmax = c(mw, x0 + wcol), ymin = 1 + gap, ymax = 1 + gap + mh,
                    lab = c(if (is.null(corner)) "" else corner, if (is.null(vlab)) rep("", length(v)) else vlab))
  left <- data.frame(xmin = 0, xmax = mw, ymin = y1 - hrow, ymax = y1,
                     lab = if (is.null(ulab)) rep("", length(u)) else ulab)
  marg <- rbind(top, left)
  pal <- c(COL[["green"]], COL[["teal"]], COL[["blue"]], COL[["purple"]], COL[["orange"]])
  p <- ggplot() +
    geom_rect(data = cells, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = fill),
              colour = "black", alpha = 0.55) +
    geom_text(data = cells, aes((xmin + xmax) / 2, (ymin + ymax) / 2, label = lab), size = 3.6) +
    geom_rect(data = marg, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), fill = "white", colour = "black") +
    geom_text(data = marg, aes((xmin + xmax) / 2, (ymin + ymax) / 2, label = lab), size = 3.3) +
    annotate("text", x = (x0 + wcol / 2), y = 1 + gap + mh + 0.06, label = paste0("v[", seq_along(v), "]"),
             parse = TRUE, size = 3.6) +
    annotate("text", x = -0.06, y = y1 - hrow / 2, label = paste0("u[", seq_along(u), "]"), parse = TRUE, size = 3.6) +
    scale_fill_manual(values = pal, guide = "none") +
    coord_fixed(clip = "off") + theme_void(base_size = 11) +
    theme(plot.margin = margin(8, 8, 8, 18), plot.title = element_text(hjust = 0.5, size = 11))
  if (!is.null(title)) p <- p + ggtitle(title)
  p
}

corr_heatmap <- function(R, main = "") {
  brk <- seq(-1, 1, length.out = 101)
  pal <- colorRampPalette(c("#d9822b", "#f2f2f2", "#1f5fbf"))(100)
  pheatmap::pheatmap(R, color = pal, breaks = brk, border_color = "white", silent = TRUE,
                     treeheight_row = 30, treeheight_col = 30, display_numbers = TRUE,
                     number_format = "%.1f", fontsize_number = 7, number_color = "grey20", main = main)$gtable
}

# ================================================================================================
# 7.2 What are the data?
# ================================================================================================
fig_7_2 <- function() {
  tu <- load_turtles()
  p <- GGally::ggpairs(tu[, -1], axisLabels = "show",
                       lower = list(continuous = GGally::wrap("points", size = 1.1, colour = COL[["blue"]])),
                       diag = list(continuous = GGally::wrap("barDiag", bins = 12, fill = COL[["blue"]], colour = "white"))) +
    theme_course(10)
  savefig(p, "fig-7-2", ch = CH, width = 5.6, height = 5.2)
}

fig_7_3 <- function() {
  a <- load_athletes()
  savefig(corr_heatmap(cor(a$a1)), "fig-7-3", ch = CH, width = 6.2, height = 5.4)
}

fig_7_4 <- function() {
  tu <- load_turtles()
  d <- data.frame(scale(tu[, -1]), sex = factor(tu$sex, labels = c("female", "male")))
  p <- ggplot(d, aes(width, height, colour = sex)) + geom_point(size = 2) + coord_fixed() +
    scale_colour_manual(values = c(female = COL[["orange"]], male = COL[["blue"]])) +
    labs(x = "width (standardised)", y = "height (standardised)")
  savefig(p, "fig-7-4", ch = CH, width = 5.4, height = 4.4)
}

fig_7_5 <- function() {
  a <- load_athletes()
  savefig(corr_heatmap(cor(a$a2)), "fig-7-5", ch = CH, width = 6.2, height = 5.4)
}

# ================================================================================================
# 7.3 Dimension reduction: projections, regression lines and the principal component line
# ================================================================================================
fig_7_6 <- function() {
  v <- c(2, 1) / sqrt(5); A <- c(1.1, 2.4)
  P <- sum(A * v) * v
  p <- ggplot() +
    geom_abline(intercept = 0, slope = v[2] / v[1], colour = COL[["red"]], linewidth = 1.2) +
    geom_segment(aes(x = 0, y = 0, xend = 1.35 * v[1], yend = 1.35 * v[2]), colour = COL[["blue"]],
                 linewidth = 1, arrow = arrow(length = unit(0.3, "cm"))) +
    geom_segment(aes(x = A[1], y = A[2], xend = P[1], yend = P[2]), colour = COL[["orange"]],
                 linetype = 2, linewidth = 1, arrow = arrow(length = unit(0.3, "cm"))) +
    # right-angle marker at the foot of the perpendicular
    geom_path(aes(x = P[1] + 0.16 * c(-v[1], -v[1] - v[2], -v[2]),
                  y = P[2] + 0.16 * c(-v[2], -v[2] + v[1], v[1])), colour = "grey30") +
    geom_point(aes(x = c(0, A[1], P[1]), y = c(0, A[2], P[2])), size = 2.4) +
    annotate("text", x = A[1] + 0.12, y = A[2] + 0.12, label = "A", size = 5) +
    annotate("text", x = P[1] + 0.35, y = P[2] - 0.22, label = "P[v](A)", parse = TRUE, size = 5) +
    annotate("text", x = 0.75 * v[1], y = 0.75 * v[2] + 0.22, label = "v", colour = COL[["blue"]], size = 5,
             fontface = "bold") +
    annotate("text", x = -0.15, y = 0.1, label = "O", size = 5) +
    coord_fixed(xlim = c(-0.3, 3), ylim = c(-0.3, 2.8)) + theme_void()
  savefig(p, "fig-7-6", ch = CH, width = 4.2, height = 3.8)
}

ath_scatter <- function(a3) {
  ggplot(a3, aes(weight, disc)) + geom_point(size = 2, shape = 21, fill = "white") +
    labs(x = "weight (standardised)", y = "disc (standardised)")
}

fig_7_7 <- function() {
  a3 <- load_athletes()$a3
  p <- ath_scatter(a3) +
    geom_segment(aes(xend = weight, yend = 0), linetype = 2, colour = "grey40") +
    geom_point(aes(y = 0), colour = COL[["red"]], size = 2) + coord_fixed()
  savefig(p, "fig-7-7", ch = CH, width = 5, height = 4.6)
}

fig_7_8 <- function() {
  a3 <- load_athletes()$a3
  r1 <- lm(disc ~ weight, data = a3)
  p <- ath_scatter(a3) +
    geom_abline(intercept = coef(r1)[1], slope = coef(r1)[2], colour = COL[["blue"]], linewidth = 1.3) +
    geom_segment(aes(xend = weight, yend = fitted(r1)), colour = COL[["red"]],
                 arrow = arrow(length = unit(0.13, "cm"))) + coord_fixed()
  savefig(p, "fig-7-8", ch = CH, width = 5, height = 4.6)
}

fig_7_9 <- function() {
  a3 <- load_athletes()$a3
  r2 <- lm(weight ~ disc, data = a3)
  p <- ath_scatter(a3) +
    geom_abline(intercept = -coef(r2)[1] / coef(r2)[2], slope = 1 / coef(r2)[2], colour = COL[["green"]], linewidth = 1.3) +
    geom_segment(aes(xend = fitted(r2), yend = disc), colour = COL[["orange"]],
                 arrow = arrow(length = unit(0.13, "cm"))) + coord_fixed()
  savefig(p, "fig-7-9", ch = CH, width = 5, height = 4.6)
}

# orthogonal projections of the (weight, disc) points onto the first principal axis
pc_line <- function(a3) {
  xy <- cbind(weight = a3$weight, disc = a3$disc)   # already centred
  v1 <- svd(xy)$v[, 1]
  proj <- (xy %*% v1) %*% t(v1)
  list(v1 = v1, slope = v1[2] / v1[1], proj = proj)
}

fig_7_10 <- function() {
  a3 <- load_athletes()$a3
  pl <- pc_line(a3)
  p <- ath_scatter(a3) +
    geom_abline(intercept = 0, slope = pl$slope, colour = COL[["purple"]], linewidth = 1.3) +
    geom_segment(aes(xend = pl$proj[, 1], yend = pl$proj[, 2]), colour = "grey30") + coord_fixed()
  savefig(p, "fig-7-10", ch = CH, width = 5, height = 4.6)
}

fig_7_11 <- function() {
  a3 <- load_athletes()$a3
  r1 <- lm(disc ~ weight, data = a3); r2 <- lm(weight ~ disc, data = a3); pl <- pc_line(a3)
  lines <- data.frame(slope = c(coef(r1)[2], 1 / coef(r2)[2], pl$slope),
                      int = c(coef(r1)[1], -coef(r2)[1] / coef(r2)[2], 0),
                      line = factor(c("disc on weight", "weight on disc", "first principal component"),
                                    levels = c("disc on weight", "weight on disc", "first principal component")))
  p <- ath_scatter(a3) +
    geom_segment(aes(xend = weight, yend = fitted(r1)), colour = COL[["blue"]], alpha = 0.35) +
    geom_segment(aes(xend = fitted(r2), yend = disc), colour = COL[["green"]], alpha = 0.35) +
    geom_segment(aes(xend = pl$proj[, 1], yend = pl$proj[, 2]), colour = COL[["purple"]], alpha = 0.35) +
    geom_abline(data = lines, aes(intercept = int, slope = slope, colour = line), linewidth = 1.2) +
    scale_colour_manual(values = c(COL[["blue"]], COL[["green"]], COL[["purple"]]), name = NULL) +
    coord_fixed() + theme(legend.position = "bottom")
  savefig(p, "fig-7-11", ch = CH, width = 5.4, height = 5.4)
}

# ================================================================================================
# 7.6 Rank-one matrices and the SVD (mosaics of the matrix of equation 7.4)
# ================================================================================================
fig_7_15 <- function() {
  savefig(mosaic_rank1(X74), "fig-7-15", ch = CH, width = 4.4, height = 4)
}

fig_7_16 <- function() {
  s <- svd(X74)
  u1 <- abs(s$u[, 1]); v1 <- abs(s$v[, 1])   # the SVD fixes the vectors up to a common sign
  pa <- mosaic_rank1(X74, ulab = c(52, 5, 36), vlab = c(15, 18, 25, 14), title = "(a)")
  pb <- mosaic_rank1(X74, ulab = c(26, 2.5, 18), vlab = c(30, 36, 50, 28), title = "(b)")
  pc <- mosaic_rank1(X74, ulab = sprintf("%.2f", u1), vlab = sprintf("%.2f", v1),
                     corner = sprintf("%.1f", s$d[1]), title = "(c)")
  savefig(pa + pb + pc, "fig-7-16", ch = CH, width = 9.6, height = 3.6)
}

# ================================================================================================
# 7.7 Plotting the observations in the principal plane
# ================================================================================================
fig_7_17 <- function() {
  a3 <- load_athletes()$a3
  xy <- cbind(disc = a3$disc, weight = a3$weight)
  s <- svd(xy)
  sc <- sweep(s$u, 2, s$d, "*")
  sc[, 1] <- sc[, 1] * sign(s$v[1, 1])   # PC1 increases with the throws
  sc[, 2] <- sc[, 2] * sign(s$v[1, 2])
  d <- data.frame(PC1 = sc[, 1], PC2 = sc[, 2])
  base <- ggplot(d, aes(PC1, PC2)) + geom_point() +
    geom_hline(yintercept = 0, colour = COL[["purple"]], linewidth = 1.3, alpha = 0.5) +
    coord_fixed(xlim = c(-3.5, 3.5), ylim = c(-1.6, 1.6)) + labs(x = "PC1", y = "PC2")
  pa <- base + geom_segment(aes(xend = PC1, yend = 0), colour = COL[["red"]]) +
    geom_point(aes(y = 0), colour = COL[["red"]], size = 1.6) + ggtitle("(a)")
  pb <- base + geom_vline(xintercept = 0, colour = COL[["teal"]], linewidth = 1.3, alpha = 0.5) +
    geom_segment(aes(xend = 0, yend = PC2), colour = COL[["blue"]]) +
    geom_point(aes(x = 0), colour = COL[["blue"]], size = 1.6) + ggtitle("(b)")
  savefig(pa + pb, "fig-7-17", ch = CH, width = 9, height = 2.9)
}

turtle_pca <- function() {
  tu <- load_turtles()
  pca_svd(tu[, -1])      # standardised, divisor n (as princomp and dudi.pca)
}

fig_7_18 <- function() {
  pt <- turtle_pca()
  savefig(scree_plot(pt$eig, width = 0.4), "fig-7-18", ch = CH, width = 4.2, height = 3.4)
}

fig_7_19 <- function() {
  tu <- load_turtles(); pt <- turtle_pca()
  d <- data.frame(pt$li[, 1:2], sex = factor(tu$sex, labels = c("female", "male")))
  k <- 0.9 * max(abs(pt$li[, 1:2])) / max(abs(pt$co[, 1:2]))   # arrow stretch factor, for display only
  ar <- data.frame(x = k * pt$co[, 1], y = k * pt$co[, 2], lab = rownames(pt$co))
  p <- ggplot(d, aes(PC1, PC2)) +
    geom_hline(yintercept = 0, linetype = 2, colour = "grey60") + geom_vline(xintercept = 0, linetype = 2, colour = "grey60") +
    geom_point(aes(colour = sex, shape = sex), size = 2) +
    geom_segment(data = ar, aes(0, 0, xend = x, yend = y), colour = "grey20", arrow = arrow(length = unit(0.18, "cm"))) +
    geom_text_repel(data = ar, aes(x, y, label = lab), size = 3.4, seed = 1, bg.color = "white", bg.r = 0.15) +
    scale_colour_manual(values = c(female = COL[["orange"]], male = COL[["blue"]])) +
    coord_fixed() + labs(x = pct_lab(pt$eig, 1), y = pct_lab(pt$eig, 2))
  savefig(p, "fig-7-19", ch = CH, width = 7, height = 2.8)
}

fig_7_20 <- function() {
  pt <- turtle_pca()
  savefig(corr_circle(pt$co, eig = pt$eig), "fig-7-20", ch = CH, width = 4.4, height = 4.4)
}

athlete_pca <- function() pca_svd(load_athletes()$a3)

fig_7_22 <- function() {
  savefig(scree_plot(athlete_pca()$eig, width = 0.4), "fig-7-22", ch = CH, width = 5, height = 3.4)
}

fig_7_23 <- function() {
  pa <- athlete_pca()
  savefig(corr_circle(pa$co, eig = pa$eig), "fig-7-23", ch = CH, width = 5, height = 5)
}

fig_7_24 <- function() {
  pa <- athlete_pca()
  d <- data.frame(pa$li[, 1:2], lab = seq_len(nrow(pa$li)))
  p <- ggplot(d, aes(PC1, PC2, label = lab)) +
    geom_hline(yintercept = 0, linetype = 2, colour = "grey60") + geom_vline(xintercept = 0, linetype = 2, colour = "grey60") +
    geom_point(colour = COL[["blue"]]) + geom_text_repel(size = 3.2, seed = 1, colour = COL[["blue"]]) +
    coord_fixed() + labs(x = pct_lab(pa$eig, 1), y = pct_lab(pa$eig, 2))
  savefig(p, "fig-7-24", ch = CH, width = 6, height = 4.8)
}

fig_7_25 <- function() {
  a <- load_athletes(); pa <- athlete_pca()
  d <- data.frame(pc1 = pa$li[, 1], score = a$score, lab = seq_along(a$score))
  p <- ggplot(d, aes(pc1, score)) +
    geom_smooth(method = "lm", formula = y ~ x, se = FALSE, colour = COL[["blue"]]) +
    geom_text(aes(label = lab), size = 3.3) + labs(x = "first principal component (score of athlete)", y = "decathlon points (1988)")
  savefig(p, "fig-7-25", ch = CH, width = 5.4, height = 4)
}

# ================================================================================================
# 7.7.3 Choosing k; 7.8 PCA with extra information
# ================================================================================================
fig_7_26 <- function() {
  S7 <- load_rdata("screep7.RData", "screep7")
  savefig(scree_plot(pca_svd(S7)$eig, width = 0.5), "fig-7-26", ch = CH, width = 4.4, height = 3.6)
}

tcell_data <- function() {
  M <- load_rdata("Msig3transp.RData", "Msig3transp")
  ids <- rownames(M)
  list(M = M, ids = ids, celltype = factor(substr(ids, 7, 9), levels = c("NAI", "MEM", "EFF"),
                                           labels = c("naive", "memory", "effector")))
}

fig_7_27 <- function() {
  td <- tcell_data()
  savefig(scree_plot(pca_svd(td$M)$eig, nmax = 10), "fig-7-27", ch = CH, width = 5, height = 3.4)
}

fig_7_28 <- function() {
  td <- tcell_data(); pt <- pca_svd(td$M)
  d <- data.frame(pt$li[, 1:2], type = td$celltype, id = td$ids)
  p <- ggplot(d, aes(PC1, PC2, colour = type)) +
    geom_hline(yintercept = 0, linetype = 2, colour = "grey60") + geom_vline(xintercept = 0, linetype = 2, colour = "grey60") +
    geom_point(size = 3.2, alpha = 0.85) +
    scale_colour_manual(values = c(naive = COL[["green"]], memory = COL[["orange"]], effector = COL[["purple"]]),
                        name = "cell type") +
    coord_fixed() + labs(x = pct_lab(pt$eig, 1), y = pct_lab(pt$eig, 2))
  savefig(p, "fig-7-28", ch = CH, width = 6.4, height = 3.6)
}

mat1_pca <- function() {
  mat1 <- load_rdata("mat1xcms.RData", "mat1")
  list(p = pca_svd(t(mat1)), labels = colnames(mat1))
}

fig_7_29 <- function() {
  savefig(scree_plot(mat1_pca()$p$eig, width = 0.6), "fig-7-29", ch = CH, width = 5, height = 3.4)
}

fig_7_30 <- function() {
  m <- mat1_pca()
  d <- data.frame(m$p$li[, 1:2], label = m$labels, number = substr(m$labels, 3, 4),
                  type = factor(substr(m$labels, 1, 2), labels = c("knockout", "wild type")))
  base <- ggplot(d, aes(PC1, PC2, colour = type, label = label)) +
    geom_hline(yintercept = 0, linetype = 2, colour = "grey60") + geom_vline(xintercept = 0, linetype = 2, colour = "grey60") +
    geom_point(size = 2.6) + geom_text_repel(size = 3, seed = 3, show.legend = FALSE) +
    scale_colour_manual(values = c(knockout = COL[["red"]], "wild type" = COL[["teal"]]), name = NULL) +
    labs(x = pct_lab(m$p$eig, 1), y = pct_lab(m$p$eig, 2))
  pa <- base + ggtitle("(a)")
  pb <- base + geom_line(aes(group = number), colour = "grey30") + ggtitle("(b) pairs joined")
  savefig(pa + pb + plot_layout(guides = "collect") & theme(legend.position = "bottom"),
          "fig-7-30", ch = CH, width = 9, height = 4.4)
}

wine_data <- function() {
  list(wine = load_rdata("wine.RData", "wine"), class = load_rdata("wineClass.RData", "wine.class"))
}

fig_7_31 <- function() {
  w <- wine_data()
  g <- pheatmap::pheatmap(1 - cor(w$wine), color = colorRampPalette(c("#1f5fbf", "#f2f2f2", "#d9822b"))(100),
                          breaks = seq(0, 2, length.out = 101), treeheight_row = 6, treeheight_col = 30,
                          border_color = "white", silent = TRUE)$gtable
  savefig(g, "fig-7-31", ch = CH, width = 6, height = 5.2)
}

fig_7_32 <- function() {
  w <- wine_data(); pw <- pca_svd(w$wine)
  d <- data.frame(pw$li[, 1:2], class = w$class)
  k <- 3.2
  ar <- data.frame(x = k * pw$co[, 1], y = k * pw$co[, 2], lab = rownames(pw$co))
  p <- ggplot(d, aes(PC1, PC2)) +
    geom_hline(yintercept = 0, linetype = 2, colour = "grey70") + geom_vline(xintercept = 0, linetype = 2, colour = "grey70") +
    geom_point(aes(colour = class), size = 1.6, alpha = 0.8) +
    stat_ellipse(aes(colour = class, fill = class), type = "norm", level = 0.69, geom = "polygon", alpha = 0.12) +
    geom_segment(data = ar, aes(0, 0, xend = x, yend = y), colour = COL[["purple"]], arrow = arrow(length = unit(0.15, "cm"))) +
    geom_text_repel(data = ar, aes(x, y, label = lab), colour = COL[["purple"]], size = 3.1, seed = 2,
                    bg.color = "white", bg.r = 0.15) +
    scale_colour_manual(values = c(barolo = COL[["red"]], grignolino = COL[["green"]], barbera = COL[["blue"]])) +
    scale_fill_manual(values = c(barolo = COL[["red"]], grignolino = COL[["green"]], barbera = COL[["blue"]])) +
    coord_fixed() + labs(x = pct_lab(pw$eig, 1), y = pct_lab(pw$eig, 2))
  savefig(p, "fig-7-32", ch = CH, width = 6.6, height = 5.4)
}

hiiragi_pca <- function() {
  h <- load_hiiragi()
  list(h = h, p = pca_svd(h$X, w = h$weight))
}

fig_7_33 <- function() {
  hp <- hiiragi_pca()
  savefig(scree_plot(hp$p$eig, nmax = 10), "fig-7-33", ch = CH, width = 5, height = 3.4)
}

fig_7_34 <- function() {
  hp <- hiiragi_pca()
  d <- data.frame(hp$p$li[, 1:2], group = hp$h$group)
  p <- ggplot(d, aes(PC1, PC2, colour = group)) +
    geom_hline(yintercept = 0, linetype = 2, colour = "grey60") + geom_vline(xintercept = 0, linetype = 2, colour = "grey60") +
    geom_point(size = 2.2) +
    scale_colour_manual(values = unname(COL[c("orange", "blue", "green", "purple", "red")]), name = "sample group") +
    coord_fixed() + labs(x = pct_lab(hp$p$eig, 1), y = pct_lab(hp$p$eig, 2))
  savefig(p, "fig-7-34", ch = CH, width = 6.4, height = 4.4)
}

# ================================================================================================
# Exercises: Figure 7.35 (correlated bivariate normal data and its principal component rotation)
# ================================================================================================
sim2d_data <- function() {
  set.seed(2024)
  s1 <- 2.5; s2 <- 0.8; rho <- 0.9
  Sig <- matrix(c(s1^2, s1 * s2 * rho, s1 * s2 * rho, s2^2), 2)
  X <- MASS::mvrnorm(50, mu = c(1, 2), Sigma = Sig)
  colnames(X) <- c("X1", "X2")
  X
}

fig_7_35 <- function() {
  X <- sim2d_data()
  pc <- princomp(X)
  sc <- pc$scores; sc[, 1] <- sc[, 1] * sign(pc$loadings[1, 1])
  pa <- ggplot(data.frame(X), aes(X1, X2)) + geom_point(colour = COL[["blue"]]) + ggtitle("(a) original data")
  pb <- ggplot(data.frame(pc1 = sc[, 1], pc2 = sc[, 2]), aes(pc1, pc2)) + geom_point(colour = COL[["blue"]]) +
    coord_fixed() + ggtitle("(b) principal component rotation")
  savefig(pa + pb + plot_layout(widths = c(1, 1.6)), "fig-7-35", ch = CH, width = 8.4, height = 3.4)
}

# ================================================================================================
# Data for the browser demonstrations
# ================================================================================================
data_json <- function() {
  a <- load_athletes()
  export_json(list(names = ATH_NAMES, raw = unname(as.matrix(a$a1)), score = a$score,
                   units = c("s", "m", "m", "m", "s", "s", "m", "m", "m", "s")), "ch07-athletes.json")
  tu <- load_turtles()
  export_json(list(names = names(tu)[-1], X = unname(as.matrix(tu[, -1])), group = tu$sex), "ch07-turtles.json")
  w <- wine_data()
  export_json(list(names = names(w$wine), X = unname(as.matrix(w$wine)), group = as.character(w$class)), "ch07-wine.json")
  td <- tcell_data()
  export_json(list(names = colnames(td$M), X = unname(round(as.matrix(td$M), 4)), group = as.character(td$celltype),
                   ids = td$ids), "ch07-tcells.json")
  h <- load_hiiragi()
  export_json(list(names = h$genes, X = unname(round(h$X, 4)), group = h$group), "ch07-hiiragi.json")
  mat1 <- load_rdata("mat1xcms.RData", "mat1")
  export_json(list(names = rownames(mat1), X = unname(signif(t(mat1), 6)), ids = colnames(mat1),
                   group = ifelse(substr(colnames(mat1), 1, 2) == "ko", "knockout", "wild type")), "ch07-mat1.json")
  S7 <- load_rdata("screep7.RData", "screep7")
  nm7 <- if (is.null(colnames(S7))) paste0("V", seq_len(ncol(S7))) else colnames(S7)
  export_json(list(names = nm7, X = unname(round(as.matrix(S7), 4))), "ch07-screep7.json")
  # Exercise 7.1: a rank-one matrix plus N(1, 1) noise, and a rank-two matrix plus small noise
  set.seed(71)
  X1 <- outer(seq(2, 30, by = 2), seq(3, 12, by = 3))
  noisy1 <- X1 + matrix(rnorm(60, 1), 15, 4)
  Y2 <- outer(rnorm(30), rnorm(4)) + outer(rnorm(30), rnorm(4))
  noisy2 <- Y2 + matrix(rnorm(30 * 4, sd = 0.05), 30, 4)
  export_json(list(X74 = X74, Xtwo = XTWO, noisy1 = round(noisy1, 4), noisy2 = round(noisy2, 4)),
              "ch07-matrices.json")
}

if (!exists("CH07_NO_RUN")) {
  run_figures(list(
    fig_7_2 = fig_7_2, fig_7_3 = fig_7_3, fig_7_4 = fig_7_4, fig_7_5 = fig_7_5, fig_7_6 = fig_7_6,
    fig_7_7 = fig_7_7, fig_7_8 = fig_7_8, fig_7_9 = fig_7_9, fig_7_10 = fig_7_10, fig_7_11 = fig_7_11,
    fig_7_15 = fig_7_15, fig_7_16 = fig_7_16, fig_7_17 = fig_7_17, fig_7_18 = fig_7_18, fig_7_19 = fig_7_19,
    fig_7_20 = fig_7_20, fig_7_22 = fig_7_22, fig_7_23 = fig_7_23, fig_7_24 = fig_7_24, fig_7_25 = fig_7_25,
    fig_7_26 = fig_7_26, fig_7_27 = fig_7_27, fig_7_28 = fig_7_28, fig_7_29 = fig_7_29, fig_7_30 = fig_7_30,
    fig_7_31 = fig_7_31, fig_7_32 = fig_7_32, fig_7_33 = fig_7_33, fig_7_34 = fig_7_34, fig_7_35 = fig_7_35,
    data_json = data_json
  ))
}
