# Dependence and the Benjamini-Yekutieli correction
# https://tidyecology.com/posts/dependence-and-fdr-benjamini-yekutieli/
#
# 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-08

## ---- setup ----
library(ggplot2)
te_ink<-"#16241d"; te_body<-"#2c3a31"; te_forest<-"#275139"; te_faint<-"#5d6b61"
te_paper<-"#f5f4ee"; te_line<-"#dad9ca"
c_bh<-"#275139"; c_by<-"#93a87f"; c_raw<-"#b5534e"
theme_te <- function(base=13) theme_minimal(base_size=base) +
  theme(plot.background=element_rect(fill=te_paper,colour=NA),
        panel.background=element_rect(fill=te_paper,colour=NA),
        panel.grid.minor=element_blank(),
        panel.grid.major=element_line(colour=te_line,linewidth=.3),
        axis.text=element_text(colour=te_body), axis.title=element_text(colour=te_ink),
        plot.title=element_text(colour=te_ink,face="bold"),
        plot.subtitle=element_text(colour=te_faint),
        legend.text=element_text(colour=te_body))
BH <- function(p, q = 0.05){
  m <- length(p); o <- order(p); s <- p[o]
  below <- s <= (seq_len(m) / m) * q
  k <- if(any(below)) max(which(below)) else 0
  rej <- rep(FALSE, m); if(k > 0) rej[o[seq_len(k)]] <- TRUE
  rej
}

## ---- setup-sim ----
m <- 200; m1 <- 40; m0 <- m - m1; mu <- 3; q <- 0.05
nonnull <- c(rep(TRUE, m1), rep(FALSE, m0))
simz <- function(rho){
  e <- rnorm(m); g <- rnorm(1)
  sqrt(1 - rho) * e + sqrt(rho) * g + ifelse(nonnull, mu, 0)
}

## ---- mc-bh ----
run_bh <- function(rho, seed, R = 3000){
  set.seed(seed); fdp <- numeric(R)
  for(r in 1:R){
    z <- simz(rho); p <- 2 * pnorm(-abs(z))
    rb <- BH(p, q)
    fdp[r] <- if(sum(rb) == 0) 0 else sum(rb & !nonnull) / sum(rb)
  }
  fdp
}
fdp_ind <- run_bh(0.0, seed = 808)
fdp_dep <- run_bh(0.5, seed = 809)
c(FDR_independent = round(mean(fdp_ind), 4), SD_independent = round(sd(fdp_ind), 3),
  FDR_dependent   = round(mean(fdp_dep), 4), SD_dependent   = round(sd(fdp_dep), 3))

## ---- fig-fdp-density ----
d <- data.frame(FDP = c(fdp_ind, fdp_dep),
  dep = rep(c("independent","positive dependence (rho=0.5)"), each = length(fdp_ind)))
p_fdp <- ggplot(d, aes(FDP, fill = dep, colour = dep)) +
  geom_density(alpha = .35, linewidth = .7, bw = 0.01) +
  geom_vline(xintercept = q, linetype = "dashed", colour = te_faint) +
  scale_fill_manual(values = c(c_bh, c_raw), name = NULL) +
  scale_colour_manual(values = c(c_bh, c_raw), name = NULL) +
  coord_cartesian(xlim = c(0, 0.35)) +
  labs(x = "realised false discovery proportion (per study)", y = "density",
       title = "Dependence keeps BH's FDR under q but inflates its variance") +
  theme_te() + theme(legend.position = "inside", legend.position.inside = c(.72, .8))
p_fdp

## ---- thumb ----
ggsave("thumbnail.png",
       p_fdp + labs(title = NULL) +
         theme(plot.margin = margin(8, 14, 8, 8)),
       width = 7.5, height = 5.4, dpi = 100, bg = te_paper)

## ---- by-fun ----
BY <- function(p, q = 0.05){
  m <- length(p); cm <- sum(1 / (1:m)); o <- order(p); s <- p[o]
  below <- s <= (seq_len(m) / (m * cm)) * q
  k <- if(any(below)) max(which(below)) else 0
  rej <- rep(FALSE, m); if(k > 0) rej[o[seq_len(k)]] <- TRUE
  rej
}
cm <- sum(1 / (1:m))
set.seed(5); pc <- runif(60)^1.5
c(harmonic_cm = round(cm, 3), matches_p.adjust = identical(BY(pc, 0.1), p.adjust(pc, "BY") <= 0.1))

## ---- mc-by ----
run_both <- function(rho, seed, R = 3000){
  set.seed(seed); pow_bh <- pow_by <- fdr_by <- numeric(R)
  for(r in 1:R){
    z <- simz(rho); p <- 2 * pnorm(-abs(z))
    rb <- BH(p, q); ry <- BY(p, q)
    pow_bh[r] <- mean(rb[nonnull]); pow_by[r] <- mean(ry[nonnull])
    fdr_by[r] <- if(sum(ry) == 0) 0 else sum(ry & !nonnull) / sum(ry)
  }
  c(power_bh = mean(pow_bh), power_by = mean(pow_by), fdr_by = mean(fdr_by))
}
res <- run_both(0.0, seed = 808)
round(res, 4)

## ---- fig-power-fdr ----
db <- data.frame(method = factor(c("BH","BY"), levels = c("BH","BY")),
  power = c(res["power_bh"], res["power_by"]),
  fdr   = c(mean(fdp_ind), res["fdr_by"]))
ggplot(db, aes(method, power, fill = method)) +
  geom_col(width = .55) +
  geom_text(aes(label = sprintf("power %.2f\nFDR %.3f", power, fdr)),
            vjust = -0.2, size = 3.3, colour = te_ink, lineheight = .95) +
  scale_fill_manual(values = c(c_bh, c_by), guide = "none") +
  scale_y_continuous(limits = c(0, 0.8), expand = expansion(mult = c(0, .18))) +
  labs(x = NULL, y = "power",
       title = "Benjamini-Yekutieli is safe under any dependence, at a power cost") +
  theme_te() + theme(plot.title.position = "plot")
