Loading the Mosaic Package

Let’s load the Mosaic package:

require(mosaic)



Examples from Activity #9

8. Given f(x)=6x(1-x), find P(x≤.25)

We first must define the function:

# We define the function and then set a=6
f <- makeFun(a*x - a*x^2 ~ x, a=6)
# Plot the function
plotFun(a*x - a*x^2 ~ x, a=6, x.lim=range(0,1))

plot of chunk unnamed-chunk-3

We can then integrate this function to find F(0.25)-F(0)

F = antiD(a*x - a*x^2 ~ x, a=6)
F(x=0.25)-F(x=0)
## [1] 0.1562


11. Find P(0.25 ≤ x ≤ 0.75)
F = antiD(a*x - a*x^2 ~ x, a=6)
F(x=0.75)-F(x=0.25)
## [1] 0.6875


11. Find the median
F = antiD(a*x - a*x^2 ~ x, a=6)
F(x=0.75)-F(x=0.25)
## [1] 0.6875

F = antiD(ax - ax^2 ~ x, a=6) F(x=0.5)-F(x=0)

We need to find b, such that F(b)-F(0) = 0.50. We already know:
F(x) = a * 1/2 * x^2 - a * 1/3 * x^3 + C
F(0) = 0

So we’re, in essence, trying to solve:
0.50 = a * 1/2 * x^2 - a * 1/3 * x^3
where a = 6

We can subtract 0.50 from both sides to get:
a * 1/2 * x^2 - a * 1/3 * x^3 - 0.50 = 0
where a = 6

We now need to find the zero(s) of this function. The Mosaic Package has a function to find zeros, but we need to tell the computer where to look:

# We define the function and then set a=6
g <- makeFun(a*1/2 * x^2 - a * 1/3 * x^3 - 0.50 ~ x, a=6)

# Plot the function to estimate the zero visually
plotFun(a*1/2 * x^2 - a * 1/3 * x^3 - 0.50 ~ x, a=6, x.lim=range(0,1))
plotFun(0 ~ x, add=TRUE, col="black")

plot of chunk unnamed-chunk-7

It looks like the zero is between 0.4 and 0.6 (ok, we all know it’s 0.5… play along anyway).

To find the zero, we use:

# We tell the computer to look somewhere between 0 and 1
findZeros(a*1/2 * x^2 - a * 1/3 * x^3 - 0.50 ~ x, a=6, x.lim = range(0, 1))
##     x
## 1 0.5


11. Find the 25th percentile
# We tell the computer to look somewhere between 0 and 1
findZeros(a*1/2 * x^2 - a * 1/3 * x^3 - 0.25 ~ x, a=6, x.lim = range(0, 1))
##        x
## 1 0.3263