Two-Proportions Z-Test in R Programming
Last Updated :
25 Apr, 2025
Two-proportion z-test is applied to compare two proportions to determine if they are significantly different. It estimates the interval likely to contain the difference between population proportions and is based on a standard normal distribution. For a 5% two-tailed test, the critical value is 1.96. In R, the prop.test() function applies the two-proportions z-test.
Syntax:
prop.test(x, n, p = NULL, alternative = c("two.sided", "less", "greater"), correct = TRUE)
Parameters:
- x = number of successes and failures in data set.
- n = size of data set.
- p = probabilities of success. It must be in the range of 0 to 1.
- alternative = a character string specifying the alternative hypothesis.
- correct = a logical indicating whether Yates’ continuity correction should be applied where possible.
Hypotheses:
1. Null Hypothesis (H₀): The proportions are equal (pₐ = pᵦ).
2. Alternative Hypothesis (Hₐ):
- Two-sided: pₐ ≠ pᵦ (proportions are different).
- Less: pₐ < pᵦ.
- Greater: pₐ > pᵦ.
Example 1: Female Proportions in Two Student Groups
Let’s say we have two groups of students A and B. Group A with an early morning class of 400 students with 342 female students. Group B with a late class of 400 students with 290 female students. Use a 5% alpha level. We want to know, whether the proportions of females are the same in the two groups of the student. Here let's use prop.test().
r
# prop Test in R
prop.test(x = c(342, 290),
n = c(400, 400))
Output:
2-sample test for equality of proportions with continuity correction
data: c(342, 290) out of c(400, 400)
X-squared = 19.598, df = 1, p-value = 9.559e-06
alternative hypothesis: two.sided
95 percent confidence interval:
0.07177443 0.18822557
sample estimates:
prop 1 prop 2
0.855 0.725
The p-value of 9.559e-06 is below the significance level of 0.05 and therefore signifies that there is a significant difference in the proportion of females in both groups.
Testing Specific Hypotheses
To check if the proportion of females in Group A is less than or greater than in Group B:
r
# prop Test in R
prop.test(x = c(342, 290),
n = c(400, 400),
alternative = "less")
Output:
2-sample test for equality of proportions with continuity correction
data: c(342, 290) out of c(400, 400)
X-squared = 19.598, df = 1, p-value = 1
alternative hypothesis: less
95 percent confidence interval:
-1.0000000 0.1792664
sample estimates:
prop 1 prop 2
0.855 0.725
Test if Group A has more females
r
# prop Test in R
prop.test(x = c(342, 290),
n = c(400, 400),
alternative = "greater")
Output:
2-sample test for equality of proportions with continuity correction
data: c(342, 290) out of c(400, 400)
X-squared = 19.598, df = 1, p-value = 4.779e-06
alternative hypothesis: greater
95 percent confidence interval:
0.08073363 1.00000000
sample estimates:
prop 1 prop 2
0.855 0.725
Example 2: Defect Proportions in Tablet Manufacturing
ABC company manufactures tablets. For quality control, two sets of tablets were tested. In the first group, 32 out of 700 were found to contain some sort of defect. In the second group, 30 out of 400 were found to contain some sort of defect. Is the difference between the two groups significant? Use a 5% alpha level. Here let's use prop.test().
r
# prop Test in R
prop.test(x = c(32, 30), n = c(700, 400))
Output:
2-sample test for equality of proportions with continuity correction
data: c(32, 30) out of c(700, 400)
X-squared =3.5725, df = 1, p-value = 0.05874
alternative hypothesis: two.sided
95 percent confidence interval:
-0.061344109 0.002772681
sample estimates:
prop 1 prop 2
0.04571429 0.07500000
Explanation: The p-value of 0.05874 is more than 0.05, meaning there is no significant difference between the two groups' proportions of defective tablets.