Appendix E: Sample Size and Power with the pwr Package
A statistically significant result tells you that your sample produced an extreme-enough test statistic to reject the null hypothesis. But the question comes before the study: how large does the sample need to be to have a reasonable chance of detecting the effect you’re looking for, if it exists?
That question is answered by power analysis. This appendix shows how to do power analysis in R using the pwr package, covering the four tests used most often in this course.
Key concepts
Statistical power is the probability of correctly rejecting the null hypothesis when the alternative is true — the probability of detecting a real effect. Conventionally, a power of 0.80 (80%) is the minimum acceptable threshold.
The four quantities in a power analysis are tightly linked. For a given test and design, specify any three and the pwr package solves for the fourth:
| Quantity | Symbol | Typical role |
|---|---|---|
| Sample size | n | Solve for this (planning) |
| Effect size | d, f, f², w, r | Specified from theory or prior work |
| Significance level | α | Usually fixed at .05 |
| Power | 1 − β | Usually targeted at .80 |
Effect size conventions (Cohen, 1988):
| Test | Small | Medium | Large |
|---|---|---|---|
| t-test (Cohen’s d) | 0.20 | 0.50 | 0.80 |
| ANOVA (f) | 0.10 | 0.25 | 0.40 |
| Correlation (r) | .10 | .30 | .50 |
| Chi-square (w) | .10 | .30 | .50 |
The small/medium/large conventions were established by Jacob Cohen as rough guides when no other information is available — he explicitly described them as conventions of last resort. If you have a prior estimate of the effect size from published work, a pilot study, or domain knowledge, use that instead. In many applied areas (clinical psychology, public health), even a “small” d of 0.20 represents a practically important effect, and you would power accordingly.
Install and load
install.packages("pwr")
library(pwr)pwr.t.test() — t-tests
Independent-samples t-test: how many per group?
# How many participants per group to detect d = 0.50 with 80% power?
pwr.t.test(
d = 0.50, # medium effect
sig.level = 0.05,
power = 0.80,
type = "two.sample"
)Output:
Two-sample t test power calculation
n = 63.76561
d = 0.5
sig.level = 0.05
power = 0.8
alternative = two.sided
NOTE: n is number in *each* group
You need 64 participants per group (128 total) to detect a medium effect (d = 0.50) at 80% power. Round up — always use the ceiling, not the floor.
Paired-samples t-test
For a paired design, use type = "paired" and supply the effect size on the difference scores (same d formula, but computed from the differences).
pwr.t.test(
d = 0.50,
sig.level = 0.05,
power = 0.80,
type = "paired"
)
# n = 34 pairs (paired designs are more efficient)One-sample t-test
pwr.t.test(
d = 0.50,
sig.level = 0.05,
power = 0.80,
type = "one.sample"
)
# n = 34Compute power for a given n
If you already have a fixed sample size (e.g., archive data, a convenience sample), leave out power and supply n:
pwr.t.test(
n = 40,
d = 0.50,
sig.level = 0.05,
type = "two.sample"
)
# power = 0.598 — only 60% power with 40 per groupSample size table: independent t-test
The table below shows required n per group at 80% power, α = .05, two-tailed.
| Effect size (d) | Label | n per group | Total N |
|---|---|---|---|
| 0.20 | Small | 394 | 788 |
| 0.35 | Small–medium | 130 | 260 |
| 0.50 | Medium | 64 | 128 |
| 0.65 | Medium–large | 39 | 78 |
| 0.80 | Large | 26 | 52 |
Generate this programmatically:
library(pwr)
ds <- c(0.20, 0.35, 0.50, 0.65, 0.80)
ns <- sapply(ds, function(d) {
ceiling(pwr.t.test(d = d, sig.level = 0.05, power = 0.80,
type = "two.sample")$n)
})
data.frame(d = ds, n_per_group = ns, total_N = 2 * ns)pwr.f2.test() — ANOVA and regression
pwr.f2.test() uses Cohen’s f² as the effect size. This is different from f (used by pwr.anova.test()).
Converting η² to f²:
\[f^2 = \frac{\eta^2}{1 - \eta^2}\]
# η² = .06 (medium) → f² = 0.06 / 0.94 = 0.064
f2 <- 0.06 / (1 - 0.06)
# One-way ANOVA: k = 3 groups
# u = df between = k - 1 = 2
pwr.f2.test(
u = 2, # numerator df (k - 1 for one-way ANOVA)
f2 = f2,
sig.level = 0.05,
power = 0.80
)Output includes v (denominator df). To get N:
\[N = u + v + 1\]
result <- pwr.f2.test(u = 2, f2 = f2, sig.level = 0.05, power = 0.80)
N <- ceiling(result$u + result$v + 1)
N # total sample size neededFor multiple regression
For a multiple regression model with k predictors:
# R² = .15, k = 3 predictors
f2 <- 0.15 / (1 - 0.15)
pwr.f2.test(
u = 3, # numerator df = k (number of predictors)
f2 = f2,
sig.level = 0.05,
power = 0.80
)
# v gives denominator df; N = u + v + 1pwr.chisq.test() — Chi-square
The effect size for chi-square is Cohen’s w:
\[w = \sqrt{\chi^2 / N}\]
Use the uncorrected χ² here (chisq.test(table, correct = FALSE)). For a 2×2 table, w then equals Cramér’s V.
# Chi-square test: 2×3 table, medium effect (w = 0.30)
pwr.chisq.test(
w = 0.30,
df = (2 - 1) * (3 - 1), # (rows - 1)(cols - 1) = 2
sig.level = 0.05,
power = 0.80
)For a 2×2 table:
pwr.chisq.test(
w = 0.30,
df = 1,
sig.level = 0.05,
power = 0.80
)
# n ≈ 88pwr.r.test() — Correlation
# Detect r = 0.30 (medium correlation) with 80% power
pwr.r.test(
r = 0.30,
sig.level = 0.05,
power = 0.80
)
# n ≈ 85 pairs of observationsA complete worked example
You’re planning an independent-samples t-test to compare anxiety scores between a therapy group and a waitlist control. From a published meta-analysis, the expected effect size for this type of intervention is d ≈ 0.45.
library(pwr)
# Step 1: Determine n per group
result <- pwr.t.test(
d = 0.45,
sig.level = 0.05,
power = 0.80,
type = "two.sample"
)
n_per_group <- ceiling(result$n)
n_per_group # = 79 per group, 158 total
# Step 2: Assess sensitivity — what effect can we detect with n = 50 per group?
pwr.t.test(
n = 50,
sig.level = 0.05,
power = 0.80,
type = "two.sample"
)
# d = 0.566 — you'd need at least a medium-to-large effect
# to reach 80% power with only 50 per group
# Step 3: Plot power curve
ds <- seq(0.20, 1.0, by = 0.05)
powers <- sapply(ds, function(d) {
pwr.t.test(n = 50, d = d, sig.level = 0.05, type = "two.sample")$power
})
plot(ds, powers,
type = "l", lwd = 2,
xlab = "Effect size (d)",
ylab = "Power",
main = "Power curve: n = 50 per group, α = .05")
abline(h = 0.80, lty = 2, col = "red")
abline(v = 0.566, lty = 2, col = "blue")The power curve shows visually where your study has adequate power and where it falls short.
Common mistakes
Using the wrong effect size metric. Cohen’s d, f, f², w, and r are not interchangeable. Match the metric to the test.
Forgetting to round up. Always use ceiling(), not round(). A sample size of 63.77 means you need 64 participants, not 63.
Using conventions without checking. If published work in your area consistently reports d = 0.25, powering for d = 0.50 (medium) will leave your study underpowered.
Post-hoc power analysis. Computing power after a non-significant result and reporting “power = 0.30 — the study was underpowered” is circular: power computed from the observed effect size is just a transformation of the p-value and adds no new information. Use power analysis for planning, not for explaining away non-significant results.
Quick-reference: pwr function summary
| Test | Function | Effect size argument |
|---|---|---|
| One-sample t | pwr.t.test(type = "one.sample") |
d |
| Independent t | pwr.t.test(type = "two.sample") |
d |
| Paired t | pwr.t.test(type = "paired") |
d |
| One-way ANOVA | pwr.anova.test() |
f (Cohen’s f) |
| ANOVA / Regression | pwr.f2.test() |
f2 (Cohen’s f²) |
| Correlation | pwr.r.test() |
r |
| Chi-square | pwr.chisq.test() |
w |
For every function, supply the design information (such as k or degrees of freedom) and leave out the quantity you want to solve for. To solve for α instead, write sig.level = NULL explicitly, because it defaults to .05.