Getting to know the binomial distribution functions in R: Simulates a random draw from a binomial distribution. This function has three arguments: the number of random draws, the number of coins being flipped on each draw, and the probability of a heads as the outcome.As before, heads will be assigned a value of 1 and tails a value of 0.
rbinom(1,1,.5)
## [1] 0
Simulates 10 flips of a single coin.
rbinom(10,1,.5)
## [1] 0 1 1 1 1 0 0 1 0 0
Simulates one draw of ten coins. This counts the number of heads out of 10 flips.
rbinom(1,10,.5)
## [1] 4
Simulates 10 draws with each having 10 coins and reports the number of heads from each draw. You can think of the number of draws as the number of replicates and the number of coins as the sample size in each replicate.
rbinom(10,10,.5)
## [1] 5 7 7 6 3 5 8 7 7 4
Simulates 10 draws of 10 coins, each with an 80% chance of producing a head instead of a 50% chance. These biased coins are still considered random variables because the outcome is random.
rbinom(10,10,.8)
## [1] 8 7 9 8 9 7 7 8 8 9
Simulates 100000 draws, each with 10 fair coins, and saves the outcome as ‘flips’.
flips<-rbinom(100000, 10, .5)
Finds the fraction of draws where 5 heads occurred, which reflects the probability that this outcome will occur.
mean(flips==5)
## [1] 0.24376
Finds the cumulative probability that heads will occur 4 or fewer times during each draw. This is referred to as the cumulative density because it sums up the probability that the number of heads is 0, 1, 2, 3, or 4 out of 10 flips.
mean(flips<=4)
## [1] 0.37723
Estimates the exact probability density at a given point. The arguments here are the density being estimated (5 heads), the number of coins (10), and the probability of producing a head (0.5).
dbinom(5,10, .5)
## [1] 0.2460938
Estimates the exact cumulative probability density for a given range. The arguments here are the density being estimated (4 or fewer heads), the number of coins (10), and the probability of producing a head (0.5).
pbinom(4,10,.5)
## [1] 0.3769531
rbinom(10, 1, .7)
## [1] 1 1 0 1 1 1 1 1 1 1
flips<-rbinom(100, 10, 0.7)
table(flips)/100
## flips
## 3 4 5 6 7 8 9 10
## 0.01 0.03 0.10 0.15 0.24 0.30 0.14 0.03
hist(flips, freq = FALSE) #how does the y-axis change between theset two?
hist(flips)
dbinom(2, 10, 0.3) #Note that the word problem lists probability of getting tails
## [1] 0.2334744
mean(rbinom(10000, 10, 0.3)==2)
## [1] 0.2357
1-pbinom(4, 10, .3) #Note that we have to subtract from 1 to get the right range
## [1] 0.1502683
pbinom(4, 10, .3, lower.tail = FALSE) #same as the line above
## [1] 0.1502683
mean(rbinom(10000, 10, .3)>= 5)
## [1] 0.1523
r10<-mean(rbinom(10, 10, 0.3)==2)
r100<-mean(rbinom(100, 10, 0.3)==2)
r1000<-mean(rbinom(1000, 10, 0.3)==2)
r10000<-mean(rbinom(10000, 10, 0.3)==2)
r100000<-mean(rbinom(100000, 10, 0.3)==2)
r<-c(r10, r100, r1000, r10000, r100000)
n<-c(10, 100, 1000, 10000, 100000)
plot(n, r, xlab="Number of replicates", ylab="Probability of 70% flips coming up tails")
plot(log(n), r, xlab="Log of number of replicates", ylab="Probability that 2 heads will arise from 10 coin flips")
A<-rbinom(1000, 1, 0.5)
B<-rbinom(1000, 1, 0.3)
mean(A & B) #Answer is around 15.2%
## [1] 0.147
C<-rbinom(1000, 1, 0.7)
mean(A & B & C) #Answer is around 10.5%
## [1] 0.103
A<-rbinom(1000, 1, 0.4)
B<-rbinom(1000, 1, 0.75)
mean(A|B) #Answer is around 86.1%
## [1] 0.843
X<-rbinom(100000, 10, 0.3)
Y<-rbinom(100000, 10, 0.65)
mean(X<=5|Y<=5)
## [1] 0.96512
prob_X_less<-pbinom(5, 10, 0.3)
prob_Y_less<-pbinom(5, 10, 0.65)
prob_X_less + prob_Y_less - prob_X_less*prob_Y_less
## [1] 0.9644174