# How to Perform a Binomial Test in R

binomial test compares a sample proportion to a hypothesized proportion. The test has the following null and alternative hypotheses:

H0: π = p (the population proportion π is equal to some value p)

HA: π ≠ p (the population proportion π is not equal to some value p)

The test can also be performed with a one-tailed alternative that the true population proportion is greater than or less than some value p.

To perform a binomial test in R, you can use the following function:

binom.test(x, n, p)

where:

• x: number of successes
• n: number of trials
• p: probability of success on a given trial

The following examples illustrate how to use this function in R to perform binomial tests.

Example 1: Two-tailed Binomial Test

You want to determine whether or not a die lands on the number “3” during 1/6 of the rolls so you roll the die 24 times and it lands on “3” a total of 9 times.  Perform a Binomial test to determine if the die actually lands on “3” during 1/6 of rolls.

```#perform two-tailed Binomial test
binom.test(9, 24, 1/6)

#output
Exact binomial test

data:  9 and 24
number of successes = 9, number of trials = 24, p-value = 0.01176
alternative hypothesis: true probability of success is not equal to 0.1666667
95 percent confidence interval:
0.1879929 0.5940636
sample estimates:
probability of success
0.375
```

The p-value of the test is 0.01176. Since this is less than 0.05, we can reject the null hypothesis and conclude that there is evidence to say the die does not land on the number “3” during 1/6 of the rolls.

Example 2: Left-tailed Binomial Test

You want to determine whether or not a coin is less likely to land on heads compared to tails so you flip the coin 30 times and find that it lands on heads just 11 times. Perform a Binomial test to determine if the coin is actually less likely to land on heads compared to tails.

```#perform left-tailed Binomial test
binom.test(11, 30, 0.5, alternative="less")

#output
Exact binomial test

data:  11 and 30
number of successes = 11, number of trials = 30, p-value = 0.1002
alternative hypothesis: true probability of success is less than 0.5
95 percent confidence interval:
0.0000000 0.5330863
sample estimates:
probability of success
0.3666667 ```

The p-value of the test is 0.1002. Since this is not less than 0.05, we fail to reject the null hypothesis. We do not have sufficient evidence to say that the coin is less likely to land on heads compared to tails.

Example 3: Right-tailed Binomial Test

A shop makes widgets with 80% effectiveness. They implement a new system that they hope will improve the rate of effectiveness. They randomly select 50 widgets from a recent production run and find that 46 of them are effective. Perform a binomial test to determine if the new system leads to higher effectiveness.

```#perform right-tailed Binomial test
binom.test(46, 50, 0.8, alternative="greater")

#output
Exact binomial test

data:  46 and 50
number of successes = 46, number of trials = 50, p-value = 0.0185
alternative hypothesis: true probability of success is greater than 0.8
95 percent confidence interval:
0.8262088 1.0000000
sample estimates:
probability of success
0.92
```

The p-value of the test is 0.0185. Since this is less than 0.05, we reject the null hypothesis. We have sufficient evidence to say that the new system produces effective widgets at a higher rate than 80%.