Link Search Menu Expand Document (external link)

How to do a hypothesis test for a population proportion

Description

When we have qualitative data, we’re often interested in performing inference on population proportions. That is, the proportion (between 0.0 and 1.0) of the population that is in a certain category with respect to the qualitative variables. Given a sample proportion, $\bar{p}$, how can we test whether the population proportion is equal to, greater than, or less than some hypothesized value?

Related tasks:

Using SciPy, in Python

View this solution alone.

We’re going to use fake data here for illustrative purposes, but you can replace our fake data with your real data in the code below.

Let’s say that we’ve hypothesized that about one-third of Bostonians are unhappy with the Red Sox’ performance. To test this hypothesis, we surveyed 460 Bostonians and found that 76 of them were unhappy with the Red Sox’ performance.

We summarize this situation with the following variables. We will do a test with a Type I error rate of $\alpha=0.05$.

1
2
3
4
n = 460               # Number of respondents in sample
x = 76                # Number of respondents in chosen subset
sample_prop = x/n     # Proportion of sample in chosen subset
population_prop = 1/3 # Hypothesized population proportion

Two-tailed test

A two-tailed test is for the null hypothesis $H_0: p = \frac13$. It can be done by directly computing the test statistic and $p$-value using tools from SciPy’s stats module.

1
2
3
4
5
import numpy as np
from scipy import stats
test_stat = ( (sample_prop - population_prop) /
    np.sqrt(population_prop*(1 - population_prop)/n) )
stats.norm.sf(abs(test_stat))*2  # p-value
1
2.0284218907806657e-14

The $p$-value is less than $\alpha$, so we can reject the null hypothesis. The proportion of Bostonians unhappy with Red Sox performance is different from $\frac13$.

Right-tailed test

A right-tailed test is for the null hypothesis $H_0: p \le \frac13$. Most of the code is the same as above, but the $p$-value is computed differently for a one-sided test. We repeat the re-used code to make it easy to copy and paste.

1
2
3
4
5
import numpy as np
from scipy import stats
test_stat = ( (sample_prop - population_prop) /
    np.sqrt(population_prop*(1 - population_prop)/n) )
stats.norm.sf(test_stat)
1
0.9999999999999899

The $p$-value is greater than $\alpha$, so we cannot reject the null hypothesis. We continue to assume that the proportion of Bostonians unhappy with Red Sox performance is less than or equal to $\frac13$.

Left-tailed test

A left-tailed test is for the null hypothesis $H_0: p\ge \frac13$. Most of the code is the same as above, but the $p$-value is computed differently yet again. We repeat the re-used code to make it easy to copy and paste.

1
2
3
4
5
import numpy as np
from scipy import stats
test_stat = ( (sample_prop - population_prop) /
    np.sqrt(population_prop*(1 - population_prop)/n) )
stats.norm.sf(abs(test_stat))
1
1.0142109453903328e-14

The $p$-value is less than $\alpha$, so we can reject the null hypothesis. The proportion of Bostonians unhappy with Red Sox performance is less than $\frac13$.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Solution, in R

View this solution alone.

We’re going to use fake data here for illustrative purposes, but you can replace our fake data with your real data in the code below.

Let’s say that we’ve hypothesized that about one-third of Bostonians are unhappy with the Red Sox’ performance. To test this hypothesis, we surveyed 460 Bostonians and found that 76 of them were unhappy with the Red Sox’ performance.

We summarize this situation with the following variables. We will do a test with a Type I error rate of $\alpha=0.05$.

1
2
3
n <- 460               # Number of respondents in sample
x <- 76                # Number of respondents in chosen subset
population_prop <- 1/3 # Hypothesized population proportion

Two-tailed test

A two-tailed test is for the null hypothesis $H_0: p = \frac13$. We use R’s prop.test() function and provide it the data from above, requesting a two-tailed test.

1
prop.test(x = x, n = n, p = population_prop, alternative = "two.sided")
1
2
3
4
5
6
7
8
9
10
	1-sample proportions test with continuity correction

data:  x out of n, null probability population_prop
X-squared = 57.75, df = 1, p-value = 2.976e-14
alternative hypothesis: true p is not equal to 0.3333333
95 percent confidence interval:
 0.1330899 0.2030664
sample estimates:
        p 
0.1652174 

The $p$-value (shown at the end of the third line of the output) is less than $\alpha$, so we can reject the null hypothesis. The proportion of Bostonians unhappy with Red Sox performance is different from $\frac13$.

R also has a binom.test() function that takes the same arguments.

Right-tailed test

A right-tailed test is for the null hypothesis $H_0: p \le \frac13$. We use R’s prop.test() function and provide it the data from above, requesting a right-tailed test.

1
prop.test(x = x, n = n, p = population_prop, alternative = "greater")
1
2
3
4
5
6
7
8
9
10
	1-sample proportions test with continuity correction

data:  x out of n, null probability population_prop
X-squared = 57.75, df = 1, p-value = 1
alternative hypothesis: true p is greater than 0.3333333
95 percent confidence interval:
 0.1377034 1.0000000
sample estimates:
        p 
0.1652174 

The $p$-value (shown at the end of the third line of the output) is greater than $\alpha$, so we cannot reject the null hypothesis. We continue to assume that the proportion of Bostonians unhappy with Red Sox performance is less than or equal to $\frac13$.

Again, binom.test() takes the same arguments.

Left-tailed test

A left-tailed test is for the null hypothesis $H_0: p\ge \frac13$. We use R’s prop.test() function and provide it the data from above, requesting a left-tailed test.

1
prop.test(x = x, n = n, p = population_prop, alternative = "less")
1
2
3
4
5
6
7
8
9
10
	1-sample proportions test with continuity correction

data:  x out of n, null probability population_prop
X-squared = 57.75, df = 1, p-value = 1.488e-14
alternative hypothesis: true p is less than 0.3333333
95 percent confidence interval:
 0.0000000 0.1967951
sample estimates:
        p 
0.1652174 

The $p$-value (shown at the end of the third line of the output) is less than $\alpha$, so we can reject the null hypothesis. The proportion of Bostonians unhappy with Red Sox performance is less than $\frac13$.

Again, binom.test() takes the same arguments.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Topics that include this task

Opportunities

This website does not yet contain a solution for this task in any of the following software packages.

  • Excel
  • Julia

If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.