Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 293
An Example of the Binomial Distribution
ОглавлениеThe example below illustrates the biniomial distribution and generates the plot in Figure 8.4.
Figure 8.4: The probability to get maximum x tails when flipping a fair coin, illustrated with the binomial distribution.
# Probability of getting 5 or less heads from 10 tosses of # a coin. pbinom(5,10,0.5) ## [1] 0.6230469 # visualize this for one to 10 numbers of tosses x <- 1:10 y <- pbinom(x,10,0.5) plot(x,y,type=“b”,col=“blue”, lwd=3, xlab=“Number of tails”, ylab=“prob of maxium x tails”, main=“Ten tosses of a coin”)# How many heads should we at least expect (with a probability # of 0.25) when a coin is tossed 10 times. qbinom(0.25,10,1/2) ## [1] 4
Similar to theNormal distribution, random draws of the Binomial distribution can be obtained via a function that starts with the letter ‘r’: rbinom()
.
rbinom()
# Find 20 random numbers of tails from and event of 10 tosses # of a coin rbinom(20,10,.5) ## [1] 5 7 2 6 7 4 6 7 3 2 5 9 5 9 5 5 5 5 5 6