Prompt
How do I calculate sample size for an experiment?
Latest observation
To calculate sample size for an experiment, you usually need these ingredients:
-
Significance level ((\alpha))
Commonly 0.05. This is the chance of a false positive. -
Power ((1-\beta))
Commonly 0.8 or 0.9. This is the chance of detecting a real effect. -
Effect size
The smallest difference you care about detecting. -
Variability
Standard deviation for continuous outcomes, or baseline conversion rate for proportions. -
Test type / design
For example: two-sample t-test, paired test, chi-square test, regression, etc.
Basic idea
Sample size is determined by:
- how small an effect you want to detect,
- how noisy the data are,
- how strict you want to be about false positives,
- how much power you want.
Common formulas
1) Comparing two means
If you want to compare two independent groups:
[ n_{\text{per group}} = \frac{2\sigma^2 (z_{1-\alpha/2} + z_{1-\beta})^2}{\delta^2} ]
Where:
- (\sigma) = standard deviation
- (\delta) = difference in means you want to detect
- (z_{1-\alpha/2}) = z-value for your significance level
- (z_{1-\beta}) = z-value for your power
Example values:
- (\alpha = 0.05 \Rightarrow z_{1-\alpha/2} = 1.96)
- power = 0.8 (\Rightarrow z_{1-\beta} = 0.84)
So:
[ n_{\text{per group}} \approx \frac{2\sigma^2 (1.96 + 0.84)^2}{\delta^2} ]
2) Comparing two proportions
For A/B tests with binary outcomes:
[ n_{\text{per group}} = \frac{\left(z_{1-\alpha/2}\sqrt{2\bar p(1-\bar p)} + z_{1-\beta}\sqrt{p_1(1-p_1)+p_2(1-p_2)}\right)^2}{(p_2-p_1)^2} ]
Where:
- (p_1) = baseline proportion
- (p_2) = expected proportion after treatment
- (\bar p = (p_1+p_2)/2)
Practical steps
- Define your primary outcome.
- Decide the minimum effect you care about.
- Estimate variability from prior data or a pilot.
- Choose (\alpha) and power.
- Use the relevant formula or a power calculator/software.
Tools
You can use:
- R:
power.t.test,pwr - Python:
statsmodels.stats.power - Online power calculators
- Spreadsheet tools for rough estimates
Example
Suppose:
- You want to detect a mean difference of 5 units
- Standard deviation is 20
- (\alpha = 0.05)
- power = 0.8
Then:
[ n \approx \frac{2(20^2)(1.96+0.84)^2}{5^2} ]
[ n \approx \frac{2(400)(7.84)}{25} \approx 251 ]
So you’d need about 251 participants per group.
If you want, I can calculate the sample size for your specific experiment if you tell me:
- outcome type (mean or proportion),
- expected baseline,
- minimum detectable effect,
- standard deviation or baseline rate,
- desired power,
- significance level.