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.

Exact Binomial Tests & Hypotheses
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\)

Sample Steps to Run an Exact Binomial Test:

# 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 
Table of Some Exact Binomial Tests Arguments in R
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)

Creating an Exact Binomial Test Object:

# 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 
Table of Some Exact Binomial Test Object Outputs in R
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

1 Test Statistic for Exact Binomial Test in R

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.

2 Simple Exact Binomial Test in R

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".

binom.test(45, 100, p = 0.5,
           alternative = "two.sided",
           conf.level = 0.95)

Or:

binom.test(45, 100)

    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].

Interpretation:

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.

3 Exact Binomial Test Critical Value in R

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:

n = 100; p = 0.4; x = n*p
qbeta(0.05, x, n - x + 1); floor(qbeta(0.05, x, n - x + 1)*n)
[1] 0.317526
[1] 31
qbeta(0.95, x + 1, n - x); ceiling(qbeta(0.95, x + 1, n - x)*n)
[1] 0.4870242
[1] 49

One-tailed:

#Left
qbeta(0.1, x, n - x + 1); floor(qbeta(0.1, x, n - x + 1)*n)
[1] 0.3342374
[1] 33
#Right
qbeta(0.9, x + 1, n - x); ceiling(qbeta(0.9, x + 1, n - x)*n)
[1] 0.4688521
[1] 47

4 Two-tailed Exact Binomial Test in R

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\).

binom.test(68, 100, p = 0.6,
           alternative = "two.sided",
           conf.level = 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 

Interpretation:

  • 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.

5 One-tailed Exact Binomial Test in R

Right Tailed Test

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\).

binom.test(62, 80, p = 0.7,
           alternative = "greater",
           conf.level = 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 

Interpretation:

  • 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.

Left Tailed Test

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\).

binom.test(20, 60, p = 0.4, 
           alternative = "less",
           conf.level = 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 

Interpretation:

  • 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.

6 Exact Binomial Test: P-value, Estimate & Confidence Interval in R

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 

To get the p-value:

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)\).

binom_object$p.value
[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

To get the point estimate or sample proportion:

\[\hat p = \frac{x}{n}.\]

binom_object$estimate
probability of success 
                  0.52 
# to remove name probability of success
unname(binom_object$estimate)
[1] 0.52

Same as:

x = 52; n = 100; x/n
[1] 0.52

To get the confidence interval for \(p\):

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].\]

binom_object$conf.int
[1] 0.4177898 0.6209945
attr(,"conf.level")
[1] 0.95
# to remove attr conf.level
binom_object$conf.int[1:2]
[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:

x = 52; n = 100; alpha = 0.05
# Right Tail
l = qbeta(alpha, x, n - x + 1)
c(l, 1)
# Left Tail
u = qbeta(1-alpha, x + 1, n - x)
c(0, u)

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes