R: Binomial Distribution

I. Calculating Exact Binomial Probabilities
If X~B(n, p), one may use the following mathematical formula to calculate P(X = k).

$$P(X = k)= {n \choose k} p^k (1-p)^{n-k} \:\:where \:\: {n \choose k} = \frac{n!}{k!(n-k)!}$$

Likewise, if X~B(n, p), one may use the dbinom(x, n, p) function to calculate P(X = k). In other words, the dbinom(x, n, p) function will provide the same answer as using the mathematical formula shown above.

Example 1:
If X~B(10, 0.3), use the following R code to calculate P(X = 3).
# Method 1 - Use dbinom(k, n, p)
> dbinom(3, 10,0.3)
[1] 0.2668279

# Method 2 - Use the mathematical formula...a little more complicated :)
> choose(10,3)*(0.3)^3*(0.7)^7
[1] 0.2668279

Example 2:
On the way to work, a commuter encounters one stoplight. The probability the commuter encounters a red light on the way to work is 0.3. Over the course of 20 days of commuting, find the probability that the commuter encounters exactly 8 red lights on the way to work.
> dbinom(8, 20, 0.3)
[1] 0.1143967

II. Calculating Cumulative Binomial Probabilities
If X~B(n,p), use pbinom(x, n, p) function to calculate P(X ≤ x).

Example 1:
IX~B(10, 0.3), use the following to code to find P(X ≤ 3).
> pbinom(3,10,0.3)  # pbinom(x, n, p)
[1] 0.6496107

Example 2: IX~B(10, 0.3), use the following to code to find P(X > 3).

# Method 1 - Use "lower.tail = FALSE"
> pbinom(3, 10, 0.3, lower.tail = FALSE)
[1] 0.3503893

# Method 2 - Subtract pbinom(x, n, p) from 1
> 1 - pbinom(3, 10, 0.3)
[1] 0.3503893

III. Simulating Binomial Random Variables
In statistics, one often finds the need to simulate random scenarios that are binomial. To do this, we need to use the rbinom() function. To use the rbinom() function, you need to define three parameters:

Example 1:
Let's say you wanted to simulate rolling a dice 5 times, and you wished to count the number of 3's you observe. You could simulate this experiment using the following code:
> rbinom(1,5,1/6)    ### rbinom(number of experiments, number of observations per experiment, probability of success)
[1] 2

Conclusion: The above code simulated rolling a die five times. The output of 2 means that there were 2 "successes", or 2 observed 3's as rolling a 3 was labeled as a "success".

Example 2:
Let's say you wanted to simulate a class of 30 different students flipped a coin 10 times and you asked each student to report back the number of tails they observed. You could simulate this experiment using the following code:
> rbinom(30,10,0.5)  # rbinom(number of experiments, number of observations per experiment, probability of success)
[1] 7 4 7 5 7 3 5 4 7 5 5 5 3 5 6 6 4 4 4 6 7 5 6 8 5 8 5 4 5 6

Conclusion: The above output shows the number of "successes" recorded by each of the 30 students. For example, the first student recorded seeing 7 heads, while the second student recorded seeing 4 heads.

Example 3:
Let's say you wanted to simulate a class of 30 students rolling a dice 10 times, and you wished to count the number of 3's you observe for each student. You could simulate this experiment, then create a table using the table() function to summarize your results using the following code:
> ### rbinom(number of experiments, number of observations per experiment, probability of success)
> s=rbinom(30,10,1/6)
> table(s)
s
0 1 2 3 4 5
7 9 7 4 2 1 

Conclusion: The R output creates a simple table showing, for instance, of the 30 students who rolled their dice 10 times, 7 of them observed no "successes" or rolled no 3's, while 9 students observed exactly one "success".

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.