Loading the Mosaic Package

Let’s load the Mosaic package:

require(mosaic)



Examples from Activity #10

8) Suppose you find the life expectancy of the batteries are modeled by an exponential distribution with λ = 0.44. Graph the cdf for X.

Let’s define the cdf and plot it:

# We define the function and then set L=0.44
f <- makeFun(1 - exp(-L*x) ~ x, L=0.44)
# Plot the function
plotFun(1 - exp(-L*x) ~ x, L=0.44, x.lim=range(0,11))

plot of chunk unnamed-chunk-3


9) Calculate P(X<3), P(23).

We can do this in a couple ways. Since we already have the CDF defined, we can simply plug in the endpoints:

# P(X<3)
f(x=3)-f(x=0)
## [1] 0.7329
# P(2<x<8)
f(x=8)-f(x=2)
## [1] 0.3852

To get the last one, P(X>3), we can do either of the following:

# P(X>3) from 3 to infinity
f(x=Inf)-f(x=3)
## [1] 0.2671
# The complement of P(X<3)
1-(f(x=3)-f(x=0))
## [1] 0.2671
There’s a faster way to calculate probabilities from an exponential distribution, though. We can use the built-in exponential distribution function:

pexp(q, rate = 1, lower.tail = TRUE, log.p = FALSE)

Let’s find P(X<3) using pexp():

pexp(3, rate=0.44, lower.tail=TRUE, log.p=FALSE)
## [1] 0.7329

We can find the other probabilities just as easily:

# P(2<x<8)
pexp(8, rate=0.44, lower.tail=TRUE, log.p=FALSE) - 
    pexp(2, rate=0.44, lower.tail=TRUE, log.p=FALSE)
## [1] 0.3852
# P(X>3) using lower.tail=FALSE
pexp(3, rate=0.44, lower.tail=FALSE, log.p=FALSE)
## [1] 0.2671


10) On average, a bus arrives at a terminal every 10 minutes. What is the probability that a person must wait for more than 20 minutes for the next bus?

Let’s use the built-in exponential distribution function:

# P(x>20) with lambda = 1/10
pexp(20, rate=1/10, lower.tail=FALSE, log.p=FALSE) 
## [1] 0.1353
# Plot this distribution and shade-in the probability
x=seq(0,40,length=150)
y=dexp(x,rate=1/10)
plot(x,y,type="l",lwd=2,col="steelblue",ylab="p")
x=seq(20,40,length=150)
y=dexp(x,rate=1/10)
polygon(c(20,x,40),c(0,y,0),col="lightgray")

plot of chunk unnamed-chunk-8


12) According to the National Transportation Safety Board, there were 14 plane crashes (not all fatal) in 2007. Using this number, we can assume a plane crashes, on average, every 26 days. Given we have a plane crash on July 1, what’s the probability that we will have another crash in July??
# P(x<30) with lambda = 1/26
pexp(30, rate=1/26, lower.tail=TRUE, log.p=FALSE) 
## [1] 0.6846
# Plot this distribution and shade-in the probability
x=seq(0,60,length=150)
y=dexp(x,rate=1/26)
plot(x,y,type="l",lwd=2,col="steelblue",ylab="p")
x=seq(0,30,length=150)
y=dexp(x,rate=1/26)
polygon(c(0,x,30),c(0,y,0),col="lightgray")

plot of chunk unnamed-chunk-9