Skip to content

Sample Size & Power

A power analysis answers one question before the experiment starts: how many samples per group does this design need to detect the effect you care about? Run it while you can still change the design. This page plans a two-sample t-test in R and Python, and then checks the formula against simulated experiments.

The page needs no data file. Every number comes from the design parameters or from random draws with a fixed seed.

A power analysis for a two-sample t-test links four numbers. Fix any three and the fourth follows.

Quantity Meaning Common choice
Effect size, Cohen’s d The difference between the two group means, divided by the standard deviation within a group. Set from the smallest difference that matters.
Significance level, alpha The probability that the test rejects when the true difference is zero. 0.05
Power The probability that the test rejects when the true difference equals the assumed effect. 0.80
Sample size, n The number of experimental units in each group. The unknown, most of the time.

Cohen’s d has no units. A difference of 0.8 log2 units with a standard deviation of 1.0 gives d = 0.8, and so does a difference of 40 pg/mL with a standard deviation of 50 pg/mL. Cohen proposed d = 0.2 as a small effect, d = 0.5 as a medium effect, and d = 0.8 as a large effect. This page uses those three labels for those three values and for no other value.

The sample size counts experimental units. Twenty cells from one mouse are one unit, and a design with three mice per group has n = 3 however many cells you measure.

Start with a design and ask what it can do. The code below computes the power of a two-sided, two-sample t-test with 20 units per group, alpha = 0.05, and a large effect, d = 0.8. power.t.test() takes the difference as delta and the standard deviation as sd, so d is delta / sd. statsmodels takes d directly as effect_size.

library(ggplot2)
dir.create("outputs", showWarnings = FALSE)
design <- power.t.test(n = 20, delta = 0.8, sd = 1, sig.level = 0.05,
type = "two.sample", alternative = "two.sided")
print(design)
Two-sample t test power calculation
n = 20
delta = 0.8
sd = 1
sig.level = 0.05
power = 0.6933994
alternative = two.sided
NOTE: n is number in *each* group

Both languages return a power of 0.693. Across many experiments of this design with a true d of 0.8, the test rejects in about 69% of them and misses in about 31%. That is below the usual 80% target. The note in the R output is a reminder that n is the size of each group, so this design uses 40 units in total.

Turn the question around: fix the power at 0.80 and solve for n. Both functions return a fractional n, and a group cannot hold a fraction of a sample, so round up.

target <- power.t.test(power = 0.80, delta = 0.8, sd = 1, sig.level = 0.05)
rounded <- ceiling(target$n)
achieved <- power.t.test(n = rounded, delta = 0.8, sd = 1, sig.level = 0.05)
cat(sprintf("n per group for 80%% power at d = 0.8: %.2f\n", target$n))
cat(sprintf("Power at n = %d per group: %.4f\n", rounded, achieved$power))
n per group for 80% power at d = 0.8: 25.52
Power at n = 26 per group: 0.8075

The solution is 25.52, so the design needs 26 units per group and 52 in total. At 26 per group the power is 0.8075, slightly above the target because of the rounding. Six more units per group move the power from 69% to 81%.

Repeat the calculation for the small, the medium, and the large effect.

effect_sizes <- c(0.2, 0.5, 0.8)
for (d in effect_sizes) {
needed <- power.t.test(power = 0.80, delta = d, sd = 1,
sig.level = 0.05)$n
cat(sprintf("d = %.1f: n = %.2f, round up to %d per group\n", d, needed,
ceiling(needed)))
}
d = 0.2: n = 393.41, round up to 394 per group
d = 0.5: n = 63.77, round up to 64 per group
d = 0.8: n = 25.52, round up to 26 per group

A large effect needs 26 units per group, a medium effect needs 64, and a small effect needs 394. The required n grows with the inverse square of d. From d = 0.8 to d = 0.2 the effect is four times smaller, and the sample size is about 15 times larger.

The consequence for planning is direct. An experiment sized at 26 per group for a large effect has little power against a small effect, and the curve below shows how little.

A power curve draws power against the sample size for each effect size. The horizontal axis is on a log scale so that all three curves fit in one panel.

group_sizes <- 5:500
curves <- data.frame()
for (d in effect_sizes) {
curve_power <- power.t.test(n = group_sizes, delta = d, sd = 1,
sig.level = 0.05, strict = TRUE)$power
curves <- rbind(curves, data.frame(n = group_sizes, power = curve_power,
effect = sprintf("d = %.1f", d)))
}
power_plot <- ggplot(curves, aes(x = n, y = power, color = effect)) +
geom_line(linewidth = 1) +
geom_hline(yintercept = 0.8, linetype = "dashed") +
scale_x_log10(breaks = c(5, 10, 20, 50, 100, 200, 500)) +
scale_color_manual(values = c("#e07a5f", "#69b3a2", "#1f4e5f")) +
labs(x = "Sample size per group (log scale)", y = "Power",
color = "Effect size", title = "Power of a two-sample t-test") +
theme_minimal(base_size = 13)
ggsave("outputs/power-curve-r.png", power_plot, width = 6, height = 4,
dpi = 150)

Power against sample size per group for d = 0.2, 0.5 and 0.8, drawn with ggplot2. The curves cross the dashed 80% line at larger sample sizes as d falls.

Each curve crosses the dashed line at the n from the last output: 26 for d = 0.8, 64 for d = 0.5, and 394 for d = 0.2. At 20 per group the d = 0.2 curve has barely left 0.05, the rate at which the test rejects when there is no effect at all.

In R, strict = TRUE counts rejections in both tails, which is what statsmodels computes. The setting changes the result only where power is close to alpha, at the left end of the d = 0.2 curve.

Power is a statement about repeated experiments, and a computer can run them. The code below draws 5000 experiments with 20 units per group and a true difference of 0.8 standard deviations, runs the t-test on each one, and counts how often p falls below 0.05. It uses the pooled-variance t-test, which is the test that both power functions describe.

set.seed(2026)
n_simulations <- 5000
rejected <- logical(n_simulations)
for (i in seq_len(n_simulations)) {
control <- rnorm(20, mean = 0, sd = 1)
treated <- rnorm(20, mean = 0.8, sd = 1)
test <- t.test(treated, control, var.equal = TRUE)
rejected[i] <- test$p.value < 0.05
}
simulated_power <- mean(rejected)
standard_error <- sqrt(simulated_power * (1 - simulated_power) /
n_simulations)
cat(sprintf("Rejected in %d of %d experiments\n", sum(rejected),
n_simulations))
cat(sprintf("Simulated power: %.3f (Monte Carlo SE %.3f)\n",
simulated_power, standard_error))
cat(sprintf("Formula power: %.3f\n", design$power))
Rejected in 3398 of 5000 experiments
Simulated power: 0.680 (Monte Carlo SE 0.007)
Formula power: 0.693

In R, 3398 of the 5000 simulated experiments reject, a simulated power of 0.680. In Python, 3486 reject, a simulated power of 0.697. The formula gives 0.693. R and Python use different random number generators, so the same seed gives different draws. The Monte Carlo standard error is 0.007 in R and 0.006 in Python. The R estimate sits about two standard errors below the formula value, and the Python estimate sits less than one above it. A longer simulation shrinks that error.

This is what 69% power means. Each of these 5000 experiments has a true effect of d = 0.8, and about three in ten of them end with a p-value above 0.05. One such result is no evidence that the effect is absent.

The same loop works for a design that has no formula, such as groups with unequal variances or a skewed measurement. Change the lines that draw the data and the line that runs the test.

The effect size is the one input you cannot look up, and the answer depends on it more than on anything else. There are three sources for it.

  • The smallest effect of interest is the smallest difference that would change a decision. It needs no data, and it is the easiest choice to defend.
  • Pilot data give an estimate of the difference and of the standard deviation. A pilot with a few samples per group estimates both poorly, so treat its d as a rough guide.
  • A published study of a similar system gives an estimate too. Studies with large estimates are more likely to be published, so plan for a smaller effect than the one reported.

Cohen’s labels are conventions for the case where you know nothing else. A “large” d of 0.8 is no promise about your assay. Compute d from a difference and a standard deviation in the units of your measurement, and take the standard deviation between experimental units. A standard deviation between technical replicates is smaller, gives a larger d, and returns a sample size that is too low.

The n from a power analysis is the number of units you analyze. Add the units you expect to lose to failed libraries, dropouts, or quality filters.

Do the calculation before the experiment. Power computed after the experiment from the observed effect is a transformation of the p-value and adds no information to it.

A screen that tests thousands of genes uses a stricter threshold per gene than 0.05, and a stricter threshold needs more samples at the same d. Rerun the calculation with the threshold you will apply. Multiple Testing explains how that threshold is set.

More samples are one way to raise power. A design that removes variation is another: a paired design, or a covariate that explains part of the spread, reduces the noise that the difference has to stand out from.

Cohen, 1992 gives the conventional small, medium, and large values of d and of the effect size indexes for other common tests, with the sample sizes that reach 80% power.

Hoenig and Heisey, 2001 show that power computed from the observed effect is a function of the p-value, and that it cannot help to interpret a nonsignificant result.

The R power.t.test reference defines delta, sd, and strict, and covers the one-sample and paired forms that this page does not use.

The statsmodels TTestIndPower reference documents ratio, the argument for groups of unequal size, and the plot_power method.

Hypothesis Testing runs Welch’s version of the two-group test that this page plans. Estimation Statistics reports the effect and its interval once the data are in, and Multiple Testing covers the threshold for a study with many hypotheses.