# Checking a bootstrap: when resampling fails
# https://tidyecology.com/posts/checking-a-bootstrap/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-10-14

## ---- setup ----
suppressMessages(library(ggplot2))
te_ink <- "#275139"; te_green <- "#4e7a5a"; te_gold <- "#c98a1b"
te_rose <- "#a5524a"; te_slate <- "#3f5c7a"; te_grey <- "#8a8a80"
theme_te <- function(base_size = 12) {
  theme_minimal(base_size = base_size) +
    theme(text = element_text(colour = "#2f3e35"),
          plot.title = element_text(face = "bold", colour = "#233a2c", size = rel(1.0)),
          plot.subtitle = element_text(colour = "#54655b", size = rel(0.84)),
          panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = "#e7e3d8", linewidth = 0.3),
          plot.background = element_rect(fill = "#f5f4ee", colour = NA),
          panel.background = element_rect(fill = "#f5f4ee", colour = NA),
          axis.text = element_text(colour = "#54655b"),
          legend.position = "bottom", plot.margin = margin(8, 12, 8, 12))
}

## ---- gran ----
set.seed(2450)
n1 <- 12
x1 <- round(rlnorm(n1, log(20), 0.5), 1)
B <- 4000
boot_mean <- replicate(B, mean(sample(x1, n1, replace = TRUE)))
boot_med  <- replicate(B, median(sample(x1, n1, replace = TRUE)))
d_mean <- length(unique(round(boot_mean, 6)))   # distinct resampled means
d_med  <- length(unique(boot_med))              # distinct resampled medians
atom_med <- max(table(boot_med)) / B            # largest single spike

## ---- fig-gran ----
pA <- ggplot(data.frame(m = boot_mean), aes(m)) +
  geom_histogram(bins = 40, fill = "#dfe8e0", colour = "#c4d3c6", linewidth = 0.2) +
  labs(title = "Mean: smooth", subtitle = sprintf("%d distinct resampled values", d_mean),
       x = "resampled mean", y = "count") +
  theme_te()
pB <- ggplot(data.frame(m = boot_med), aes(m)) +
  geom_histogram(bins = 40, fill = "#e4d3bf", colour = "#c99a6a", linewidth = 0.2) +
  labs(title = "Median: lumpy", subtitle = sprintf("%d distinct resampled values", d_med),
       x = "resampled median", y = "count") +
  theme_te()
grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = grid::grid.layout(1, 2)))
print(pA, vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 1))
print(pB, vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 2))

## ---- maxdemo ----
set.seed(2451)
n2 <- 40; theta <- 1
x2 <- runif(n2, 0, theta)
mx <- max(x2)
boot_max <- replicate(B, max(sample(x2, n2, replace = TRUE)))
atom <- mean(boot_max == mx)                    # share of resamples pinned at the max
atom_theory <- 1 - (1 - 1/n2)^n2                # exact for n = 40; the limit is 1 - 1/e = 0.632
ci_boot <- quantile(boot_max, c(.025, .975))    # bootstrap percentile interval
ci_exact <- c(mx, mx / (0.05)^(1/n2))           # exact 95% interval from max/theta ~ Beta(n,1)

## ---- covmax ----
covmax <- function(R = 2000, Bb = 500) {
  hb <- 0; he <- 0
  for (r in seq_len(R)) {
    xx <- runif(n2, 0, 1); m <- max(xx)
    bb <- replicate(Bb, max(sample(xx, n2, TRUE)))
    hb <- hb + (1 >= quantile(bb, .025) & 1 <= quantile(bb, .975))
    ce <- c(m, m / (0.05)^(1/n2)); he <- he + (1 >= ce[1] & 1 <= ce[2])
  }
  c(bootstrap = hb / R, exact = he / R)
}
set.seed(22)
cmax <- covmax()

## ---- fig-max ----
pA <- ggplot(data.frame(m = boot_max), aes(m)) +
  geom_histogram(bins = 50, fill = te_grey, colour = "#c4d3c6", linewidth = 0.2) +
  geom_vline(xintercept = mx, colour = te_gold, linetype = "dashed", linewidth = 0.7) +
  geom_vline(xintercept = theta, colour = te_ink, linewidth = 0.9) +
  annotate("text", x = mx - 0.004, y = Inf, label = "sample maximum", hjust = 1, vjust = 1.6,
           size = 3, colour = te_gold) +
  annotate("text", x = theta + 0.004, y = Inf, label = "true bound", hjust = 0, vjust = 3.0,
           size = 3, colour = te_ink) +
  coord_cartesian(xlim = c(min(boot_max), 1.08)) +
  labs(title = "Bootstrap of the maximum", subtitle = "A spike that cannot reach the truth",
       x = "resampled maximum", y = "count") +
  theme_te()
dcm <- data.frame(method = factor(c("bootstrap", "exact"), levels = c("bootstrap", "exact")),
                  cov = as.numeric(cmax), col = c(te_rose, te_ink))
pB <- ggplot(dcm, aes(method, cov, fill = col)) +
  geom_col(width = 0.6) +
  geom_hline(yintercept = 0.95, linetype = "dashed", colour = "#233a2c", linewidth = 0.6) +
  geom_text(aes(label = sprintf("%.2f", cov)), vjust = -0.5, size = 3.1, colour = "#233a2c") +
  scale_fill_identity() +
  coord_cartesian(ylim = c(0, 1.05)) +
  labs(title = "Coverage of the true bound", subtitle = "Nominal 95% (dashed)",
       x = NULL, y = "coverage") +
  theme_te()
grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = grid::grid.layout(1, 2)))
print(pA, vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 1))
print(pB, vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 2))

## ---- covvar ----
nn <- 20; sig2 <- 1; zc <- qnorm(c(.025, .975))
one_var <- function(v) mean((v - mean(v))^2)
cov_pb <- function(R = 1500, Bb = 600) {
  hp <- 0; hb <- 0
  for (r in seq_len(R)) {
    xx <- rnorm(nn, 0, 1); th <- one_var(xx)
    bv <- replicate(Bb, one_var(sample(xx, nn, TRUE)))
    hp <- hp + (sig2 >= quantile(bv, .025) & sig2 <= quantile(bv, .975))
    z0 <- qnorm(mean(bv < th))
    jk <- sapply(seq_len(nn), function(i) one_var(xx[-i])); jm <- mean(jk)
    a  <- sum((jm - jk)^3) / (6 * (sum((jm - jk)^2))^1.5)
    ad <- pnorm(z0 + (z0 + zc) / (1 - a * (z0 + zc)))
    cb <- quantile(bv, ad); hb <- hb + (sig2 >= cb[1] & sig2 <= cb[2])
  }
  c(percentile = hp / R, bca = hb / R)
}
set.seed(50)
cvar <- cov_pb()

## ---- fig-cov ----
dcv <- data.frame(method = factor(c("percentile", "bias-corrected"),
                                  levels = c("percentile", "bias-corrected")),
                  cov = as.numeric(cvar), col = c(te_slate, te_ink))
ggplot(dcv, aes(method, cov, fill = col)) +
  geom_col(width = 0.6) +
  geom_hline(yintercept = 0.95, linetype = "dashed", colour = "#233a2c", linewidth = 0.6) +
  geom_text(aes(label = sprintf("%.2f", cov)), vjust = -0.5, size = 3.1, colour = "#233a2c") +
  scale_fill_identity() +
  coord_cartesian(ylim = c(0.6, 0.98)) +
  labs(title = "Neither reaches 95%", subtitle = "Variance interval coverage",
       x = NULL, y = "coverage") +
  theme_te()
