# Observed power tells you nothing
# https://tidyecology.com/posts/observed-power-tells-you-nothing/
#
# 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-15

## ---- setup ----
library(ggplot2)

te_paper  <- "#f5f4ee"
te_ink    <- "#16241d"
te_body   <- "#2c3a31"
te_forest <- "#275139"
te_rust   <- "#b5534e"
te_gold   <- "#c9b458"
te_line   <- "#dad9ca"

theme_datasheet <- function() {
  theme_minimal(base_size = 12) +
    theme(plot.background  = element_rect(fill = te_paper, colour = NA),
          panel.background = element_rect(fill = te_paper, colour = NA),
          panel.grid.major = element_line(colour = te_line, linewidth = 0.3),
          panel.grid.minor = element_blank(),
          text             = element_text(colour = te_body),
          plot.title       = element_text(colour = te_ink, face = "bold"),
          axis.text        = element_text(colour = te_body))
}

# power at the effect size and variance the study happened to observe
observed_power <- function(x, y, alpha = 0.05) {
  n  <- length(x)
  sp <- sqrt(((n - 1) * var(x) + (n - 1) * var(y)) / (2 * n - 2))
  power.t.test(n = n, delta = abs(mean(x) - mean(y)), sd = sp,
               sig.level = alpha)$power
}

one_study <- function(n, true_diff) {
  x <- rnorm(n)
  y <- rnorm(n, true_diff)
  c(p = t.test(x, y, var.equal = TRUE)$p.value,
    power = observed_power(x, y),
    n = n, true_diff = true_diff)
}

set.seed(20260812)
sims <- do.call(rbind, lapply(c(0, 0.3, 0.6), function(d)
  as.data.frame(t(replicate(400, one_study(20, d))))))
nrow(sims)

## ---- rho ----
c(spearman = cor(sims$p, sims$power, method = "spearman"))

## ---- fig-curve ----
sims$truth <- factor(sims$true_diff,
                     labels = c("no true difference", "0.3", "0.6"))

ggplot(sims, aes(x = p, y = power)) +
  geom_hline(yintercept = 0.5, linetype = "dashed", colour = te_body) +
  geom_vline(xintercept = 0.05, linetype = "dashed", colour = te_body) +
  geom_point(aes(colour = truth), size = 1.5, alpha = 0.6) +
  scale_colour_manual(values = c(te_ink, te_forest, te_rust)) +
  labs(x = "p value", y = "observed power", colour = "true difference",
       title = "One curve, whatever the truth behind the data") +
  theme_datasheet() +
  theme(legend.position = "bottom")

## ---- at-threshold ----
near <- abs(sims$p - 0.05) < 0.005
round(c(studies = sum(near),
        mean_observed_power = mean(sims$power[near]),
        lowest  = min(sims$power[near]),
        highest = max(sims$power[near])), 3)

## ---- across-n ----
set.seed(4)
by_n <- do.call(rbind, lapply(c(10, 20, 60), function(n)
  as.data.frame(t(replicate(400, one_study(n, 0.4))))))
tab <- do.call(rbind, lapply(c(10, 20, 60), function(n) {
  s <- by_n[by_n$n == n & abs(by_n$p - 0.05) < 0.006, ]
  data.frame(n = n, studies = nrow(s), power_at_p_05 = mean(s$power))
}))
round(tab, 4)
pooled_rho <- cor(by_n$p, by_n$power, method = "spearman")
round(pooled_rho, 5)

## ---- high-p ----
round(c(largest_observed_power_when_p_above_half =
          max(sims$power[sims$p > 0.5])), 3)

## ---- two-studies ----
find_study <- function(n, target_p = 0.30, tol = 0.01) {
  repeat {
    x <- rnorm(n)
    y <- rnorm(n)
    tt <- t.test(x, y, var.equal = TRUE)
    if (abs(tt$p.value - target_p) < tol) return(list(x = x, y = y, tt = tt))
  }
}
set.seed(3)
small <- find_study(12)
large <- find_study(400)

two <- data.frame(
  study = c("12 per group", "400 per group"),
  p     = c(small$tt$p.value, large$tt$p.value),
  diff  = c(diff(rev(small$tt$estimate)), diff(rev(large$tt$estimate))),
  lower = c(small$tt$conf.int[1], large$tt$conf.int[1]),
  upper = c(small$tt$conf.int[2], large$tt$conf.int[2]),
  obs_power = c(observed_power(small$x, small$y),
                observed_power(large$x, large$y)))
round(two[, -1], 3)

## ---- fig-intervals ----
two$study <- factor(two$study, levels = rev(two$study))

ggplot(two, aes(y = study)) +
  annotate("rect", xmin = -0.25, xmax = 0.25, ymin = -Inf, ymax = Inf,
           fill = te_gold, alpha = 0.22) +
  geom_vline(xintercept = 0, colour = te_ink, linewidth = 0.5) +
  geom_errorbar(aes(xmin = lower, xmax = upper, colour = study),
                width = 0.12, linewidth = 0.9) +
  geom_point(aes(x = diff, colour = study), size = 3) +
  scale_colour_manual(values = c(te_forest, te_rust)) +
  labs(x = "difference between groups (standard deviations)", y = NULL,
       title = "Same p value, opposite amounts of information") +
  theme_datasheet() +
  theme(legend.position = "none")
