Binomial and Poisson distributions in R. Example. A fair coin is

Binomial and Poisson distributions in R. Example. A fair coin is tossed 20 times. Find the probability it… a. Lands heads 10 times or fewer. b. Lands heads 11 times or more. c. lands heads exactly 13 times. If X = the number of heads in 20 tosses, X has a binomial distribution with p = .5. The solution to (a) is pbinom(c(10), size=20, prob=0.5, lower.tail=TRUE) [1] 0.5880985 pbinom specifies a cumulative probability with a binomial distribution, c(10) is a vector with one element (we want the case with 10 heads), size is the number of trials in question, prob is the probability of success, and lower.tail = TRUE means we want the probability of 10 heads or fewer (the lower tail of the distribution). The solution to (b) could be found by taking 1 – (a) (since b is the complement of part a), or… > pbinom(c(10), size=20, prob=0.5, lower.tail=FALSE) [1] 0.4119015 Setting lower.tail = FALSE means we want the upper tail of the distribution, which will ignore the point at X = 10. To do (c), > dbinom(c(13), size=20, prob=.5) [1] 0.07392883 dbinom will tell the probability of getting exactly the specified number of successful trials (the prefix d is used because you’re asking for the ‘density’ of the binomial distribution at a certain point). Example. A professor is teaching a small upper‐level class of 6 students. The professor believes there is a 30% probability that any given student will get a grade of A in the class. Find the probabilities that exactly 0, exactly 1, …, and exactly 6 students will get a grade of A, assuming each student is independent. Then, if X = the number of students getting an A, find the mean of X. Solution. You could just type in dbinom(c(0), size=6, prob=.3), dbinom(c(1), size=6, prob=.3), …, dbinom(c(6), size=6, prob=.3), but there is a better way. > dbinom(c(0:6), size=6, prob=.3) [1] 0.117649 0.302526 0.324135 0.185220 0.059535 0.010206 0.000729 The notation 0:6 is just a quick way of saying all the integers between 0 and 6 (including 0 and 6). The output tells us that the probability 0 people will get an A is .117669, the probability exactly one person will get an A is .302526, …, and the probability that all 6 students will get A’s is .000729. If you wanted to get fancy and create some output that looks nice, > #X = number of students getting an A > #prob.X = probability exactly X students get an A > X = 0:6 > prob.X= dbinom(c(0:6), size=6, prob=.3) > grade.dist = data.frame(X, prob.X) > grade.dist X prob.X 1 0 0.117649 2 1 0.302526 3 2 0.324135 4 3 0.185220 5 4 0.059535 6 5 0.010206 7 6 0.000729 This also lets us easily find the mean of X. > weighted.mean(X, prob.X) [1] 1.8 Example. In 8 hours, 16 students showed up to the Math Assistance Center looking for help in College Algebra. Assuming this is a Poisson process, estimate the probability that… a. Three or fewer students would show up in a given hour. b. Four or more students would show up in a given hour. c. Exactly three students would show up in a given hour. Here, 16
8
2
So part (a) is: > ppois(c(3), lambda=2, lower.tail=TRUE) [1] 0.8571235 Note the similarity to the binomial calculation. Part (b) would be either 1‐.8571235 = .1428765 or > ppois(c(3), lambda=2, lower.tail=FALSE) [1] 0.1428765 Like the binomial case, part(c) is: > dpois(c(3), lambda=2) [1] 0.180447