How big a sample do you actually need

Today let's work out how many people a study actually needs, before it ever collects a single row of data.

Here's the question we'll use throughout. A sleep lab wants to test whether a digital wind-down routine, a dimming screen plus a short breathing animation before bed, helps people fall asleep faster than no intervention at all. A wearable tracker records how many minutes each person takes to fall asleep.

Checking whether a planned study has a fair shot at catching a real effect means simulating the study thousands of times and counting how often it comes back significant. That share is the study's power.

Today we turn that around. Instead of fixing the sample size and asking what power it buys you, we fix the power we want, usually 0.80, and ask how big the sample needs to be to get there.

Here's the shape of that answer, before we plug in this study's own numbers.

Switch between small, medium and large effects and watch the curve shift, and the sample size marked for 80% power move with it.

Turning the power simulation around: fix power, solve for n

Here's the idea in one line. Instead of fixing a sample size and asking how much power it buys, fix the power you want and search for the smallest sample size that reaches it.

The search itself reuses the simulate-and-count loop: pick a candidate sample size n, simulate the sleep study at that n many times, run the exact test the real study will use, and record the share of simulated repeats that came back significant. That share is the estimated power at that n. Repeat this for a whole grid of candidate n values, and read off the smallest one whose power clears the target.

Let's try it on the sleep study itself. The clinician's own numbers are: a population standard deviation of 25 minutes in sleep-onset latency, and a smallest worthwhile improvement of 10 minutes, the gap the study actually cares about catching. Sweep candidate sample sizes per group from 10 up to 200, in steps of 10.

RInteractive R
# Turn the power simulation around: sweep a grid of sample sizes and estimate power at each one set.seed(1) mwd <- 10 # smallest worthwhile difference in minutes sd_total <- 25 # population standard deviation in minutes alpha <- 0.05 n_grid <- seq(10, 200, by = 10) power_at_n <- sapply(n_grid, function(n) { rejected <- replicate(2000, { treatment <- rnorm(n, mean = mwd, sd = sd_total) control <- rnorm(n, mean = 0, sd = sd_total) t.test(treatment, control)$p.value < alpha }) mean(rejected) }) data.frame(n = n_grid, power = round(power_at_n, 3)) #> n power #> 1 10 0.133 #> 2 20 0.222 #> 3 30 0.340 #> 4 40 0.430 #> 5 50 0.506 #> 6 60 0.582 #> 7 70 0.650 #> 8 80 0.717 #> 9 90 0.760 #> 10 100 0.796 #> 11 110 0.840 #> 12 120 0.869 #> 13 130 0.892 #> 14 140 0.919 #> 15 150 0.940 #> 16 160 0.941 #> 17 170 0.954 #> 18 180 0.964 #> 19 190 0.976 #> 20 200 0.980

  

Each row of treatment and control is one simulated repeat of the sleep study at that n, built with the true 10-minute effect baked in by construction. t.test() runs the exact comparison the real study would run, and replicate(2000, ...) does that 2000 times per n, so mean(rejected) is the share of repeats that came back significant, the estimated power.

Reading down the table, power is still at 0.796 at n = 100 per group, just short of 0.80, and crosses it at n = 110, where it reaches 0.840. So on this grid, 110 per group looks like the answer.

But is 110 really the right number, or is that just this grid's resolution showing through, since it only checked every 10th value? A finer sweep next pins it down exactly, and checks it against a formula built for exactly this case.