Here, we discuss the exact binomial tests in R with interpretations, including, test statistics, p-values, critical values, and confidence intervals.
The exact binomial test in R can be performed with the
binom.test()
function from the base "stats" package.
The exact binomial test can be used to test whether the proportion of successes in the population a sample comes from is equal to a certain value (stated in the null hypothesis) or not.
In the exact binomial test, the test statistic is the number of successes which follows a binomial distribution.
Question | Is the proportion equal to \(p_0\)? | Is the proportion greater than \(p_0\)? | Is the proportion less than \(p_0\)? |
Form of Test | Two-tailed | Right-tailed test | Left-tailed test |
Null Hypothesis, \(H_0\) | \(p = p_0\) | \(p = p_0\) | \(p = p_0\) |
Alternate Hypothesis, \(H_1\) | \(p \neq p_0\) | \(p > p_0\) | \(p < p_0\) |
# Specify:
# x as the number of successes
# n as the number of trials
# Run the exact binomial test with specifications
binom.test(x = 12, n = 20, p = 0.5,
alternative = "two.sided",
conf.level = 0.95)
Exact binomial test
data: 12 and 20
number of successes = 12, number of trials = 20, p-value = 0.5034
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.3605426 0.8088099
sample estimates:
probability of success
0.6
Argument | Usage |
x | Sample count of number of successes |
n | Sample count of number of trials |
p | Population proportion value in null hypothesis |
alternative | Set alternate hypothesis as "greater", "less", or the default "two.sided" |
conf.level | Level of confidence for the test and confidence interval (default = 0.95) |
# Create object with:
# x as the number of successes
# n as the number of trials
binom_object = binom.test(x = 12, n = 20, p = 0.5,
alternative = "two.sided",
conf.level = 0.95)
# Extract a component
binom_object$statistic
number of successes
12
Test Component | Usage |
binom_object$statistic | Test-statistic: number of successes |
binom_object$p.value | P-value |
binom_object$parameter | Number of trials |
binom_object$estimate | Point estimate or sample proportion |
binom_object$conf.int | Confidence interval |
The exact binomial test has test statistic as the number of successes \(x\), in \(n\) trials.
\(x\) follows a binomial distribution (\(Binom(n, p_0)\)) when the null hypothesis is true.
\(p_0\) is the population proportion value to be tested and set in the null hypothesis,
with sample proportion \(\hat p = \frac{x}{n}\).
See the one proportion test for normal distribution and chi-squared distribution approximations.
With number of successes, \(x = 45\) in \(n = 100\) trials.
For the following null hypothesis \(H_0\), and alternative hypothesis \(H_1\), with the level of significance \(\alpha=0.05\).
\(H_0:\) the population proportion is equal to 0.5 (\(p = 0.5\)).
\(H_1:\) the population proportion is not equal to 0.5 (\(p \neq 0.5\), hence the default two-sided).
Because the level of significance is \(\alpha=0.05\), the level of confidence is \(1 - \alpha = 0.95\).
The binom.test()
function has the default
proportion as 0.5, the default alternative as
"two.sided", the default level of confidence as
0.95, hence, you do not need to specify the "p", "alternative",
and "conf.level".
Or:
Exact binomial test
data: 45 and 100
number of successes = 45, number of trials = 100, p-value = 0.3682
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.3503202 0.5527198
sample estimates:
probability of success
0.45
The sample proportion, \(\hat p = x/n\), is 0.45,
test statistic, number of successes, is 45,
the p-value, \(p\), is 0.3682,
the 95% confidence interval is [0.3503202, 0.5527198].
Note that for binom.test()
in R, the two methods may
disagree for some edge cases, as p-value is based on binomial
distribution, and confidence
interval is based on beta
distribution.
P-value: With the p-value (\(p = 0.3682\)) being greater than the level of significance 0.05, we fail to reject the null hypothesis that the population proportion is equal to 0.5.
Confidence Interval: With the null hypothesis proportion value (\(p = 0.5\)) being within the confidence interval, \([0.3503202, 0.5527198]\), we fail to reject the null hypothesis that the population proportion is equal to 0.5.
To get the critical value for an exact binomial test in R, you can
use the qbeta()
function for beta distribution to derive
the quantile associated with the given level of significance value \(\alpha\), size \(n\), and null hypothesis proportion \(p\).
Set \(x = np\)
For two-tailed test with level of significance \(\alpha\). The critical values are: floor of qbeta(\(\alpha/2\), \(x\), \(n - x + 1\))\(\times n\) and ceiling of qbeta(1-\(\alpha/2\), \(x+1\), \(n - x\))\(\times n\).
For one-tailed test with level of significance \(\alpha\). The critical value is: for left-tailed, floor of qbeta(\(\alpha\), \(x\), \(n - x + 1\))\(\times n\); and for right-tailed, ceiling of qbeta(1-\(\alpha\), \(x+1\), \(n - x\))\(\times n\).
Example:
For \(\alpha = 0.1\), \(n = 100\), and \(p = 0.4\). The \(\hat p\) and \(x\) critical values are given below.
Two-tailed:
[1] 0.317526
[1] 31
[1] 0.4870242
[1] 49
One-tailed:
[1] 0.3342374
[1] 33
[1] 0.4688521
[1] 47
With number of successes, \(x = 68\) in \(n = 100\) trials.
For the following null hypothesis \(H_0\), and alternative hypothesis \(H_1\), with the level of significance \(\alpha=0.2\).
\(H_0:\) the population proportion is equal to 0.6 (\(p = 0.6\)).
\(H_1:\) the population proportion is not equal to 0.6 (\(p \neq 0.6\), hence the default two-sided).
Because the level of significance is \(\alpha=0.2\), the level of confidence is \(1 - \alpha = 0.8\).
Exact binomial test
data: 68 and 100
number of successes = 68, number of trials = 100, p-value = 0.1253
alternative hypothesis: true probability of success is not equal to 0.6
80 percent confidence interval:
0.6128577 0.7415582
sample estimates:
probability of success
0.68
P-value: With the p-value (\(p = 0.1253\)) being less than the level of significance 0.2, we reject the null hypothesis that the population proportion is equal to 0.6.
Confidence Interval: With the null hypothesis proportion value (\(p = 0.6\)) being outside the confidence interval, \([0.6128577, 0.7415582]\), we reject the null hypothesis that the population proportion is equal to 0.6.
With number of successes, \(x = 62\) in \(n = 80\) trials.
For the following null hypothesis \(H_0\), and alternative hypothesis \(H_1\), with the level of significance \(\alpha=0.1\).
\(H_0:\) the population proportion is equal to 0.7 (\(p = 0.7\)).
\(H_1:\) the population proportion is greater than 0.7 (\(p > 0.7\), hence one-sided).
Because the level of significance is \(\alpha=0.1\), the level of confidence is \(1 - \alpha = 0.9\).
Exact binomial test
data: 62 and 80
number of successes = 62, number of trials = 80, p-value = 0.08731
alternative hypothesis: true probability of success is greater than 0.7
90 percent confidence interval:
0.7039634 1.0000000
sample estimates:
probability of success
0.775
P-value: With the p-value (\(p = 0.08731\)) being less than the level of significance 0.1, we reject the null hypothesis that the population proportion is equal to 0.7.
Confidence Interval: With the null hypothesis proportion value (\(p = 0.7\)) being outside the confidence interval, \([0.7039634, 1.0)\), we reject the null hypothesis that the population proportion is equal to 0.7.
With number of successes, \(x = 20\) in \(n = 60\) trials.
For the following null hypothesis \(H_0\), and alternative hypothesis \(H_1\), with the level of significance \(\alpha=0.05\).
\(H_0:\) the population proportion is equal to 0.4 (\(p=0.4\)).
\(H_1:\) the population proportion is less than to 0.4 (\(p < 0.4\), hence one-sided).
Because the level of significance is \(\alpha=0.05\), the level of confidence is \(1 - \alpha = 0.95\).
Exact binomial test
data: 20 and 60
number of successes = 20, number of trials = 60, p-value = 0.1786
alternative hypothesis: true probability of success is less than 0.4
95 percent confidence interval:
0.0000000 0.4464656
sample estimates:
probability of success
0.3333333
P-value: With the p-value (\(p = 0.1786\)) being greater than the level of significance 0.05, we fail to reject the null hypothesis that the population proportion is equal to 0.4.
Confidence Interval: With the null hypothesis proportion value (\(p = 0.4\)) being within the confidence interval, \((0, 0.4464656]\), we fail reject the null hypothesis that the population proportion is equal to 0.4.
Here for an exact binomial test, we show how to get the p-values,
sample proportion and confidence interval from the
binom.test()
function in R, or by written code.
x = 52; n = 100
binom_object = binom.test(x, n, p = 0.45,
alternative = "two.sided",
conf.level = 0.95)
binom_object
Exact binomial test
data: x and n
number of successes = 52, number of trials = 100, p-value = 0.161
alternative hypothesis: true probability of success is not equal to 0.45
95 percent confidence interval:
0.4177898 0.6209945
sample estimates:
probability of success
0.52
With \(X \sim Binom(n,p)\).
Two-tailed:
For sample proportion \(x/n = p_0\), \(Pvalue = 1\).
For sample proportion \(x/n > p_0\), \(Pvalue = P(X \geq x) + P(X \leq x^*)\), \(x^*\) is the largest \(\tilde x\) less than \(np\), such that \(P(X=\tilde x) \leq P(X= x)\).
For sample proportion \(x/n < p_0\), \(Pvalue = P(X \leq x) + P(X \geq x^*)\), \(x^*\) is the smallest \(\tilde x\) greater than \(np\), such that \(P(X=\tilde x) \leq P(X=x)\).
One-tailed:
For right-tail, \(Pvalue = P(X \geq x)\) or for left-tail, \(Pvalue = P(X \leq X)\).
[1] 0.1610212
Same as:
Note that the p-value depends on \(x\), the number of successes, and \(n\), the number of trials. We also use the
distribution function pbinom()
for the binomial
distribution in R.
x = 52; n = 100; prob = 0.45
# x/n = 0.52 is greater than p = 0.45.
l = pbinom(q = x-1, size = n, p = prob, lower.tail = FALSE)
df = dbinom(x = 0:(prob*n), size = n, p = prob)
u = sum(df[which(df<=dbinom(x, n, prob))])
l + u
[1] 0.1610212
\(x/n < p\) example:
x = 42; n = 100; prob = 0.45
# x/n = 0.42 is less than p = 0.45.
l = pbinom(q = x, size = n, p = prob)
df = dbinom(x = (prob*n):n, size = n, p = prob)
u = sum(df[which(df<=dbinom(x, n, prob))])
l + u
\[\hat p = \frac{x}{n}.\]
probability of success
0.52
[1] 0.52
Same as:
[1] 0.52
With \(\mathrm{B}(x, n)_{\alpha}\) as the quantile associated with \(\alpha\) in the beta distribution with shape parameters \(x\) and \(n\).
For two-tailed:
\[CI = \left[\mathrm{B}(x, n-x+1)_{\alpha/2} \;,\; \mathrm{B}(x+1, n-x)_{1-\alpha/2} \right].\]
For right one-tailed:
\[CI = \left[\mathrm{B}(x, n-x+1)_{\alpha} \;,\; 1 \right].\]
For left one-tailed:
\[CI = \left[0 \;,\; \mathrm{B}(x+1, n-x)_{1-\alpha} \right].\]
[1] 0.4177898 0.6209945
attr(,"conf.level")
[1] 0.95
[1] 0.4177898 0.6209945
Same as:
x = 52; n = 100; alpha = 0.05
l = qbeta(alpha/2, x, n - x + 1)
u = qbeta(1-alpha/2, x + 1, n - x)
c(l, u)
[1] 0.4177898 0.6209945
One tailed example:
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes