Skip to content

Instantly share code, notes, and snippets.

@fdabl
Created June 21, 2025 06:51
Show Gist options
  • Save fdabl/7fa83cffedf6652e63b3c1eba79ea70c to your computer and use it in GitHub Desktop.
Save fdabl/7fa83cffedf6652e63b3c1eba79ea70c to your computer and use it in GitHub Desktop.
Power analysis two proportions
library(pwr)
library(dplyr)
library(ggplot2)
compute_sample_size <- function(
p1, p2, power = 0.8, sig.level = 0.05, alternative = 'two.sided'
) {
h <- ES.h(p1, p2) # Cohen's h
result <- pwr.2p.test(
h = h, power = power,
sig.level = sig.level, alternative = alternative
)
ceiling(result$n)
}
compute_sample_size <- Vectorize(compute_sample_size)
get_df <- function(baselines, effect_sizes, alternative = 'two.sided') {
df <- expand.grid('baseline' = baselines, 'effect_size' = effect_sizes)
# floating point comparison issue below otherwise
df$baseline <- round(df$baseline, 3)
df$effect_size <- round(df$effect_size, 3)
df$n <- compute_sample_size(
df$baseline, df$baseline + df$effect_size, alternative = alternative
)
df$type <- ifelse(alternative == 'two.sided', alternative, 'one.sided')
df
}
baselines <- seq(0.10, 0.45, 0.05)
effect_sizes <- seq(0.05, 0.15, 0.01)
df <- rbind(
get_df(baselines, effect_sizes),
get_df(baselines, effect_sizes, alternative = 'less')
)
p <- ggplot(df, aes(x = effect_size * 100, y = n, color = factor(baseline))) +
geom_line(size = 1.2) +
scale_color_brewer(name = 'Baseline (unclustered)', palette = 'Set2') +
scale_x_continuous(breaks = seq(5, 15, 1)) +
scale_y_continuous(breaks = seq(0, 1600, 200), limits = c(0, 1600)) +
facet_wrap(~ type) +
labs(
title = 'Required Sample Size per Group vs. Effect Size',
x = 'Effect Size (Percentage Point Increase)',
y = 'Sample Size per Group'
) +
theme_minimal() +
theme(
legend.position = 'top',
plot.title = element_text(size = 14, hjust = 0.50)
)
p
tab <- df %>%
filter(
baseline %in% c(0.15, 0.30, 0.45),
effect_size %in% c(0.05, 0.10, 0.15)
)
tab
tab2 <- df %>%
filter(
baseline %in% c(0.15, 0.30, 0.45),
effect_size %in% c(0.05, 0.06, 0.07, 0.08, 0.09, 0.10)
)
tab2
@fdabl
Copy link
Author

fdabl commented Jun 21, 2025

power_results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment