###### R code for calculating probabilities ### Binomial probabilities ## Example 1: Y ~ bin(n = 10, p = 0.02) # What is P(Y >= 2)? # Note P(Y >= 2) = 1 - P(Y <= 1): 1 - pbinom(1, size = 10, prob = 0.02) ## Example 2: Y ~ bin(n = 10, p = 0.40) # What is P(Y <= 7)? pbinom(7, size = 10, prob = 0.40) # What is P(Y >= 4)? # Note P(Y >= 4) = 1 - P(Y <= 3): 1 - pbinom(3, size = 10, prob = 0.40) ## Plots of these two binomial distributions: # Y ~ bin(n=10, p=0.02): plot(0:10, dbinom(0:10, size=10, prob=0.02), type='h', lwd=2, xlab='y', ylab='p(y)') # Y ~ bin(n=10, p=0.40): windows() plot(0:10, dbinom(0:10, size=10, prob=0.40), type='h', lwd=2, xlab='y', ylab='p(y)') # What is the main difference in the distributions? ############################################################################### ### Geometric probabilities ## Example 1: Y ~ geom(p = 0.75) # What is P(Y = 4)? dgeom(4-1, prob = 0.75) # What about P(Y <= 4)? # This is the probability the person will need # AT MOST 4 attempts to pass the driving test [not in notes]. pgeom(4-1, prob = 0.75) ### Negative Binomial probabilities ## Example 2: Y ~ NB(r = 3, p = 0.40) # What is P(Y = 10)? dnbinom(10-3, size = 3, prob = 0.40) # What about P(Y <= 10)? # This is the probability that AT MOST 10 employees must be checked # to find 3 with asbestos traces [not in notes]. pnbinom(10-3, size = 3, prob = 0.40) ############################################################################### ### Hypergeometric probabilities ## Example 3: Y ~ hyper(r=8, N=20, n=6): # What is P(Y = 1)? # Note (N-r) = 12: dhyper(1, 8, 12, 6) # What is P(Y <= 1)? # This is the probability of ONE OR FEWER successes in the sample. phyper(1, 8, 12, 6) ## Example 1: Y ~ hyper(r=2, N=100, n=10): # What is P(Y >= 1)? # This is the probability of ONE OR MORE successes in the sample. 1 - phyper(0, 2, 98, 10) ############################################################################### ### Poisson probabilities ## Example 1: Y ~ Pois(lambda = 2.6) # What is P(Y = 4)? dpois(4, lambda = 2.6) # What is P(Y >= 2)? # This is the probability of TWO OR MORE occurrences. 1 - ppois(1, lambda = 2.6) # What is P(3 <= Y <= 6)? # This is the probability of between 3 and 6 occurrences. ppois(6, lambda = 2.6) - ppois(2, lambda = 2.6) ## (Partial) plot of this Poisson distribution: # Y ~ Pois(lambda = 2.6): plot(0:12, dpois(0:12, lambda = 2.6), type='h', lwd=2, xlab='y', ylab='p(y)') ###############################################################################