proportionZTestRequiredN

fun proportionZTestRequiredN(effectSize: Double, power: Double = 0.8, alpha: Double = 0.05, alternative: Alternative = Alternative.TWO_SIDED, twoSample: Boolean = true): Int(source)

Computes the minimum sample size needed for a proportion z-test to achieve the desired power.

Given a target power and expected effectSize (Cohen's h), returns the smallest integer sample size that meets or exceeds the requested power at significance level alpha. The result is rounded up to the next whole number, with a minimum of 2.

Example:

// Per-group sample size for a two-sample proportion z-test
proportionZTestRequiredN(effectSize = 0.5, power = 0.8) // 63
// Total sample size for a one-sample proportion z-test
proportionZTestRequiredN(effectSize = 0.5, power = 0.8, twoSample = false) // 32

Return

the minimum required sample size. Always at least 2.

Parameters

effectSize

the expected Cohen's h effect size. Must be non-zero. The sign is ignored.

power

the target statistical power (probability of detecting the effect). Defaults to 0.8 (80%). Must be in (0, 1).

alpha

the significance level (Type I error rate). Defaults to 0.05 (5%). Must be in (0, 1).

alternative

the direction of the alternative hypothesis. Defaults to Alternative.TWO_SIDED.

twoSample

whether this is a two-sample test. Defaults to true. When true, the returned value is the per-group sample size. When false, it is the total sample size.

See also

to compute power at a given sample size.

to find the smallest detectable effect at a given sample size.

to compute Cohen's h from two proportions.