The binomial distribution model is an important probability model that is used when there are two possible outcomes (hence “binomial”). In a situation in which there were more than two distinct outcomes, a multinomial probability model might be appropriate, but here we focus on the situation in which the outcome is dichotomous.
The binomial distribution model allows us to compute the probability of observing a specified number of “successes” when the process is repeated a specific number of times (e.g., in a set of patients) and the outcome for a given patient is either a success or a failure.
graph <- function(n,p){
x <- dbinom(0:n,size=n,prob=p)
barplot(x,ylim=c(0,0.4),names.arg=0:n,
main=sprintf(paste('Binomial Distribution(n,p) ',n,p,sep=',')))
}
graph(20,0.8)
graph(20,1/2)
graph(20,1/4)
graph(20,1/8)
graph(20,1/12)
graph(20,1/18)
Suppose that 80% of adults with allergies report symptomatic relief with a specific medication. If the medication is given to 10 new patients with allergies, what is the probability that it is effective in exactly seven?
graph <- function(n,p){
x <- dbinom(0:n,size=n,prob=p)
barplot(x,ylim=c(0,0.4),names.arg=0:n,
main=sprintf(paste('Binomial Distribution(n,p) ',n,p,sep=',')))
}
graph(10,0.8)
dbinom(7,10,0.8)
## [1] 0.2013266
The likelihood that a patient with a heart attack dies of the attack is 0.04 (i.e., 4 of 100 die of the attack). Suppose we have 5 patients who suffer a heart attack, what is the probability that all will survive? For this example, we will call a success a fatal attack (p = 0.04). We have n=5 patients and want to know the probability that all survive or, in other words, that none are fatal (0 successes).
graph <- function(n,p){
x <- dbinom(0:n,size=n,prob=p)
barplot(x,ylim=c(0,1),names.arg=0:n,
main=sprintf(paste('Binomial Distribution(n,p) ',n,p,sep=',')))
}
graph(5,0.04)
dbinom(0,5,0.04)
## [1] 0.8153727
one.coin <- function(){
dice <- sample(1:2, size = 1, replace = TRUE)
return(dice)
}
one.coin()
## [1] 1
one.coin()
## [1] 2
one.coin()
## [1] 1
sims <- replicate(100, one.coin())
table(sims)
## sims
## 1 2
## 53 47
table(sims)/length(sims)
## sims
## 1 2
## 0.53 0.47
plot(table(sims), xlab = 'Sum', ylab = 'Frequency', main = '100 Tossing of one Fair Coin')
sims <- replicate(10000, one.coin())
table(sims)
## sims
## 1 2
## 4909 5091
table(sims)/length(sims)
## sims
## 1 2
## 0.4909 0.5091
plot(table(sims), xlab = 'Sum', ylab = 'Frequency', main = '10000 Tossing of one Fair Coin')
# rmultinom(n, size, prob)
my_prob <- c(0.5,0.5)
number_of_experiments <- 2
number_of_samples <- 10
experiments <- rmultinom(n=number_of_experiments, size=number_of_samples, prob=my_prob)
experiments
## [,1] [,2]
## [1,] 4 6
## [2,] 6 4
df=data.frame(experiments)/number_of_samples
par(mfrow=c(1,2))
for(i in 1:2) {
barplot(df[,i],ylim=c(0,1))
}
number_of_samples <- 10000
experiments <- rmultinom(n=number_of_experiments, size=number_of_samples, prob=my_prob)
experiments
## [,1] [,2]
## [1,] 5016 5041
## [2,] 4984 4959
df=data.frame(experiments)/number_of_samples
par(mfrow=c(1,2))
for(i in 1:2) {
barplot(df[,i],ylim=c(0,1))
}