Power analysis by simulation, not formula
Today let's understand how to find out, before you ever collect a single row of data, whether a planned study has a fair shot at catching the effect you are looking for.
Here is the example we will use throughout. NightWell is a sleep-tracking app. It wants to test a new bedtime reminder against no reminder at all. From its existing app logs, NightWell believes the reminder adds about 15 minutes of nightly sleep on average, with a standard deviation of 50 minutes across its users. The plan is to run the test on 120 users, 60 getting the reminder and 60 not, and compare the two groups with a two-sample t-test at the usual 0.05 significance level.
Before NightWell runs this study for real, it wants an honest answer to one question. If that 15-minute effect really is there, how often would a study built exactly this way actually catch it?
That question has a name. It is called statistical power, and there is a textbook formula for some of the simplest cases. But formulas run out quickly once a study design gets even slightly more realistic. So instead of reaching for a formula first, you build power the way it actually works underneath any formula: by simulating the planned study thousands of times and counting how often it succeeds.
There are only four moves in that recipe.
Everything from here is that same loop, done once by hand first and then thousands of times at once.
What statistical power actually asks
Let's put a number on NightWell's question. Statistical power is the fraction of repeated studies, each built exactly like the one that is planned, that would correctly reject the null hypothesis when the assumed effect is genuinely there.
Read that definition again slowly, because every part of it matters. "Repeated studies" means you are not asking about the one study NightWell is about to run. You are asking about a long run of studies just like it. "Correctly reject the null hypothesis" means the test comes back significant, p below 0.05, and says there is a real difference between the two groups. And "when the assumed effect is genuinely there" means you first have to decide, as an assumption, how big a real effect you want the study to be able to catch. Here, that assumption is NightWell's own belief: a true gain of 15 minutes of nightly sleep.
So power is not a property of data you already have. It is a property of a design, asked before any data exists, under an effect you are willing to assume.
To see what one of those repeated studies looks like, simulate it. Draw 60 made-up control users and 60 made-up treatment users, where the treatment users really do sleep 15 minutes more on average, and run the exact t-test NightWell plans to run on the real data.
rnorm(60, mean = 420, sd = 50) draws 60 numbers from a normal distribution: 420 minutes of average nightly sleep for the control group, with the 50-minute spread NightWell sees across its users. The treatment group is drawn the same way, except its mean is set to 435, exactly 15 minutes above the control's 420. That 15-minute gap is baked into the simulation on purpose. It is the real effect NightWell is assuming.
t.test() then runs the same two-sample test NightWell's real study will run. It does not know the 15 minutes was baked in. It only sees the 60 plus 60 numbers and does its job.
Now look at the p-value. It came out at 0.06229, above the usual 0.05 cutoff. So this one simulated repeat fails to reject the null hypothesis, even though a real 15-minute effect is sitting inside the data by construction. One simulated repeat was simply unlucky. That does not mean the effect is not there. It means this one draw of 60 plus 60 users happened not to make the case strongly enough.