library(tidyverse)Lab 05
MATH 361
This lab will have you work with the Binomial distribution in R. We begin with a tutorial to introduce the R commands associated with this distributions. There is also a template exercise provided as an example of how to format your answers neatly, integrating both math symbols and R code. Task 1 is an odd-numbered exercise from the textbook, so you can check your answers to help you verify dbinom() and pbinom() work as you expect.
Let’s begin by loading the necessary packages. Most of the functions in this lab are in base R, meaning they don’t require any additional packages, but we’ll load the tidyverse so we can use %>% and ggplot() when doing some additional exploration!
Tutorial
R includes many built in functions for common distributions. Commands for each follow the same basic format. The abbreviated name of the distribution is preceeded by once of four letters:
- “d” for the density function (pmf for discrete random variables)
- “p” for the cumulative distribution function (cdf)
- “q” for the quantile function (gives the x value for a given cummulative probability)
- “r” for random (generates a random number based on the distribution)
The binomial distribution pmf and cdf in R:
dbinom(x, size, prob)– returns \(f(x) = P(X = x),\) (pmf) values for a binomial random variable \(X\),pbinom(x, size, prob)– returns \(F(x) = P(X \leq x),\) values for the binomial distribution (cdf)
Note that both have three arguments defined as:
x= the value of the random variable for which a probability is computedsize= number of trials (this is what we usually refer to as \(n\))prob= probability of success on a trial (what we usually refer to as \(p\))
Note that x can be a vector, meaning you can compute multiple probabilities with a single command!
Binomial Example
Suppose \(X \sim b(10, 0.3)\). The probability that \(X = 2\) is given by
dbinom(2, 10, 0.3)[1] 0.2334744
The probability that \(X \leq 2\) is given by
pbinom(2, 10, 0.3)[1] 0.3827828
Recall that \(P(X \leq 2) = P(X = 0) + P(X = 1) + P(X = 2)\), so we could also have gotten the same result with the following code:
dbinom(0, 10, 0.3) + dbinom(1, 10, 0.3) + dbinom(2, 10, 0.3)[1] 0.3827828
We could compute the probability for all possible values of \(X = 0, 1, \dots , 10\) by making x a vector.
x <- c(0:10)
dbinom(x, 10, 0.3) [1] 0.0282475249 0.1210608210 0.2334744405 0.2668279320 0.2001209490
[6] 0.1029193452 0.0367569090 0.0090016920 0.0014467005 0.0001377810
[11] 0.0000059049
We can do a sanity check to make sure that all of those densities sum to 1. That is, to verify \(\sum_S f(x) = 1\).
sum(dbinom(x, 10, 0.3))[1] 1
We can use the vector of densities to plot the pmf
data <- data.frame(x = x,
d = dbinom(x, 10, 0.3))
ggplot(data = data) +
geom_col(aes(x = x, y = d))# a prettier version of the plot
ggplot(data = data) +
#using factor(x) tells ggplot to treat x as discrete,
#so it labels the x axis at the integers 0:10
#fill = "skyblue" changes the color of the bars
geom_col(aes(x = factor(x), y = d), fill = "skyblue") +
#changes axis labels
labs(x = "x", y = "Density f(x)") +
theme_classic()Template for Task Solution
You can use the following solution as a template for how to format your solutions on Tasks 1 & 2 below.
From Hogg, Tanis, and Zimmermann; Exercise 2.4-10
A certain type of mint has a label weight of 20.4 grams. Suppose that the probability is 0.90 that a mint weighs more than 20.7 grams. Let \(X\) equal the number of mints that weigh more than 20.7 grams in a sample of eight mints selected at random.
- Find the mean and standard deviation of \(X\)
- Find the probability that exactly 8 mints weigh more than 20.7 grams
- Find the probability that no more that 6 mints weigh more than 20.7 grams
ANSWER
[OPTIONAL BUT ENCOURAGED: to help us verify that our answers are reasonable, we can first plot the distribution]
\(X \sim b(8, 0.9)\)
#create vector of possible x values
x <- c(0:8)
#create vector of corresponding densities
d <- dbinom(x, 8, 0.9)
#combine into a data frame for plotting
data <- data.frame(x, d)
ggplot(data = data) +
geom_col(aes(x = factor(x), y = d), fill = "navyblue") +
labs(x = "x", y = "Density f(x)") +
theme_classic()Part a
\[\mu = np = 8 \cdot 0.9\] \[\sigma = \sqrt{np(1-p)} = \sqrt{8 \cdot 0.9 \cdot 0.1}\]
mu <- 8*0.9
sigma <- sqrt(8*0.9*0.1)
solution <- data.frame(mu, sigma)
solution mu sigma
1 7.2 0.8485281
Part b
\(P(X = 8)\)
dbinom(8, size = 8, prob = 0.9)[1] 0.4304672
Part c
\(P(X \leq 6)\)
pbinom(6, size = 8, prob = 0.9)[1] 0.1868953
Task 1
From Hogg, Tanis, and Zimmermann; Exercise 2.4-5
In a lab experiment involving inorganic syntheses of molecular precursors to organometallic ceramics, the final step of a five-step reaction involves the formation of a metal–metal bond. The probability of such a bond forming is \(p = 0.20\). Let \(X\) equal the number of successful reactions out of \(n = 25\) such experiments.
- Find the probability that \(X\) is at most 4.
- Find the probability that \(X\) is at least 5.
- Find the probability that \(X\) is equal to 6.
- Give the mean, variance, and standard deviation of \(X\).
ANSWER
#create vector of possible x values
x <- c(0:25)
#create vector of corresponding densities
d <- dbinom(x, 25, 0.2)
#combine into a data frame for plotting
data <- data.frame(x, d)
ggplot(data = data) +
geom_col(aes(x = factor(x), y = d), fill = "navyblue") +
labs(x = "x", y = "Density f(x)") +
theme_classic()Part a
pbinom(4, size = 25, prob = 0.2)[1] 0.4206743
Part b
1 - pbinom(4, size = 25, prob = 0.2)[1] 0.5793257
Part c
dbinom(6, size = 25, prob = 0.2)[1] 0.1633459
Part d
mu <- 25 * 0.20
var <- 25 * 0.20 * (1 - 0.20)
sigma <- sqrt(var)
solution <- data.frame(mu, var, sigma)
solution mu var sigma
1 5 4 2
Task 2
From Hogg, Tanis, and Zimmermann; Exercise 2.4-6
It is believed that approximately 75% of American youth now have insurance due to the health care law. Suppose this is true, and let \(X\) equal the number of American youth in a random sample of \(n = 15\) with private health insurance.
- Find the probability that \(X\) is at least 10.
- Find the probability that \(X\) is at most 10.
- Find the probability that \(X\) is equal to 10.
- Give the mean, variance, and standard deviation of \(X\).
ANSWER
#create vector of possible x values
x <- c(0:15)
#create vector of corresponding densities
d <- dbinom(x, 15, 0.75)
#combine into a data frame for plotting
data <- data.frame(x, d)
ggplot(data = data) +
geom_col(aes(x = factor(x), y = d), fill = "navyblue") +
labs(x = "x", y = "Density f(x)") +
theme_classic()Part a
1 - pbinom(9, size = 15, prob = 0.75)[1] 0.8516319
Part b
pbinom(10, size = 15, prob = 0.75)[1] 0.3135141
Part c
dbinom(10, size = 15, prob = 0.75)[1] 0.165146
Part d
mu <- 15 * 0.75
var <- 15 * 0.75 * (1 - 0.75)
sigma <- sqrt(var)
solution <- data.frame(mu, var, sigma)
solution mu var sigma
1 11.25 2.8125 1.677051