Two-proportion Z-test
The two-proportion Z-test is a statistical hypothesis test for assessing whether two groups differ in the proportion of a binary outcome, in such a significant way that is beyond chance. For example, the proportion of patients responding positively to a treatment in a clinical trial versus control, the defect rate in quality control for two production lines, or the click-through rate in an A/B test of two alternative webpage designs.
The test is appropriate when each observation is independent from another, can be classified as a success or failure and the sample sizes are large enough that the sampling distribution of each sample proportion is well approximated by the central limit theorem. Under those conditions the observed difference of sample proportions can be converted to a standardized z-statistic and compared to the standard normal distribution to obtain p-values or form confidence intervals for the difference in proportions. This article explains the z-statistic and pooled versus unpooled variance choices, describes confidence-interval and sample-size / minimum-detectable-effect calculations, and notes common alternatives and caveats.
Definition
The two-proportion Z-test or two-sample proportion Z-test is a statistical method used to determine whether the difference between the proportions of two groups, coming from a binomial distribution is statistically significant. This approach relies on the observation that the sample proportions follow a normal distribution under the Central Limit Theorem, allowing the construction of a z-test for hypothesis testing and confidence interval estimation. It is used in various fields to compare success rates, response rates, or other proportions across different groups.Hypothesis test
The z-test for comparing two proportions is a frequentist statistical hypothesis test used to evaluate whether two independent samples have different population proportions for a binary outcome. Under mild regularity conditions, the sample proportions are approximately normally distributed under the central limit theorem, which permits using a z-statistic constructed from the difference of sample proportions and an estimated standard error.The test involves two competing hypotheses:
- null hypothesis : The proportions in the two populations are equal, i.e.,.
- alternative hypothesis : The proportions in the two populations are not equal, i.e., or / .
The z-test determines statistical significance by comparing the calculated z-statistic to a critical value. E.g., for a significance level of we reject the null hypothesis if . Or, alternatively, by computing the p-value and rejecting the null hypothesis if.
Confidence interval
The confidence interval for the difference between two proportions, based on the definitions above, is:where is the critical value of the standard normal distribution.This interval provides a range of plausible values for the true difference between population proportions.
Notice how the variance estimation is different between the hypothesis testing and the confidence intervals. The first uses a pooled variance, while the second has to estimate the variance using each sample separately. This difference may lead to slightly different results if using the confidence interval as an alternative to the hypothesis testing method.
Sample size determination and Minimum detectable effect
Sample size determination is the act of choosing the number of observations to include in each group for running the statistical test. For the Two-proportion Z-test, this is closely-related with deciding on the minimum detectable effect.For finding the required sample size, we define that,, then:
The minimum detectable effect or MDE is the smallest difference between two proportions that a statistical test can detect for a chosen type I error level, statistical power, and sample sizes. It is commonly used in study design to determine whether the sample sizes allows for a test with sufficient sensitivity to detect meaningful differences.
The MDE for when using the z-test formula for comparing two proportions, incorporating critical values for and, and the standard errors of the proportions:where is critical value for the significance level, is quantile for the desired power, and is when assuming the null is correct.
The MDE depends on the sample sizes, baseline proportions, and test parameters. When the baseline proportions are not known, they need to be assumed or roughly estimated from a small study. Larger samples or smaller power requirements leads to a smaller MDE, making the test more sensitive to smaller differences. Researchers may use the MDE to assess the feasibility of detecting meaningful differences before conducting a study.
The Minimal Detectable Effect is the smallest difference, denoted as, that satisfies two essential criteria in hypothesis testing:
- The null hypothesis is rejected at the specified significance level.
- Statistical power is achieved under the alternative hypothesis.
The first criterion establishes the critical value required to reject the null hypothesis. The second criterion specifies how far the alternative distribution must be from to ensure that the probability of exceeding it under the alternative hypothesis is at least.
Condition 1: Rejecting
Under the null hypothesis, the test statistic is based on the pooled standard error :
might be estimated.
To reject, the observed difference must exceed the critical threshold after properly inflating it to the SE:
If the MDE is defined solely as, the statistical power would be only 50% because the alternative distribution is symmetric about the threshold. To achieve a higher power level, an additional component is required in the MDE calculation.
Condition 2: Achieving power
Under the alternative hypothesis, the standard error is.
It means that if the alternative distribution was centered around some value, then the minimal must be at least larger than to ensure that the probability of detecting the difference under the alternative hypothesis is at least.
Combining conditions
To meet both conditions, the total detectable difference incorporates components from both the null and alternative distributions. The MDE is defined as:
By summing the critical thresholds from the null and adding to it the relevant quantile from the alternative distributions, the MDE ensures the test satisfies the dual requirements of rejecting at significance level and achieving statistical power of at least.
Assumptions and conditions
To ensure valid results, the following assumptions must be met:- Independent random samples: The samples must be drawn independently from the populations of interest.
- Large sample sizes: Typically, should exceed 20.
- Success or failure condition:
- # and
- # and
Relation to other statistical methods
Using the z-test confidence intervals for hypothesis testing would give the same results as the chi-squared test for a two-by-two contingency table. Fisher's exact test is more suitable for when the sample sizes are small.Treatment of 2-by-2 contingency table has been investigated as early as the 19th century, with further work during the 20th century.
Alternatives to the asymptotic method described includes continuity correction, as well as modification that is similar to Wilson score interval.
Notice that:
- When one or more cell counts are small, prefer exact tests or exact confidence intervals.
- For paired or matched binary data use McNemar's test rather than the two-sample z-test.
- The choice between pooled and unpooled variance matters: pooled variance is appropriate for hypothesis testing of equality, whereas the unpooled variance is used for confidence intervals.
- Multiple testing, selection effects, and nonrandom sampling can invalidate p-values and CIs; these design issues should be addressed in the study methods.
Example
Suppose group 1 has 120 successes out of 1000 trials and group 2 has 150 successes out of 1000 trials. The pooled proportion is. The pooled standard error isThe z-statistic isgiving a two-sided p-value of about 0.0497. An approximate 95% confidence interval for the difference using the unpooled standard error isBecause the 95% CI excludes 0 and the p-value is ≈0.0497, the difference is statistically significant at the 5% level by the usual large-sample criteria.
Software implementation
Implementations are available in many statistical environments. See below for implementation details in some popular languages. Other implementations also exists for SPSS, SAS, and Minitab.R
Useprop.test with continuity correction disabled:prop.test, n = c
Output includes z-test equivalent results: chi-squared statistic, p-value, and confidence interval:
2-sample test for equality of proportions without continuity correction
data: c out of c
X-squared = 3.8536, df = 1, p-value = 0.04964
alternative hypothesis: two.sided
95 percent confidence interval:
-5.992397e-02 -7.602882e-05
sample estimates:
prop 1 prop 2
0.12 0.15
Python
Useproportions_ztest from statsmodels:from statsmodels.stats.proportion import proportions_ztest
z, p = proportions_ztest
- For CI: from statsmodels.stats.proportion import proportions_diff_confint_indep
SQL
Direct implementation of the formulas from above, using Presto flavour of SQLWITH input_data AS / ) AS p_pooled
FROM AS t
),
stats_computed AS AS se_p2_minus_p1,
SQRT * AS pooled_se,
inverse_normal_cdf AS z_975 -- for 95% CI
FROM input_data
SELECT
n_1,
n_2,
ROUND AS p_1,
ROUND AS p_2,
ROUND AS p2_minus_p1,
ROUND AS se_p2_minus_p1,
ROUND AS p2_minus_p1_ci_lower,
ROUND AS p2_minus_p1_ci_upper,
ROUND AS p_value
FROM stats_computed;