# Rosner, chapter 5 # ================================= # Example 5.6 hypertension mu <- 80 sigma <- 12 xs <- seq(40,120, length=200) plot(xs, dnorm(xs, mean=mu, sd=sigma), type="l", col="red", xlab="DBP", ylab="density", main="pdf of DBP") # ================================= # Example 5.11 X ~ N(0, 1) mu <- 0 sigma <- 1 xs <- seq(-3, 3, length=200) plot(xs, pnorm(xs, mean=mu, sd=sigma), type="l", col="red", xlab="x", ylab="density", main="cdf of X ~ N(0,1)") # P(X <= 1.96), P(X <= 1) pnorm(1.96) pnorm(1) # ================================= # Example 5.12 X ~ N(0, 1) # P(X <= -1.96) pnorm(-1.96) # ================================= # Example 5.13 X ~ N(0, 1) # P(-1 <= X <= 1.5) pnorm(1.5) - pnorm(-1) # ================================= # Example 5.14 pulmonary disease # X ~ N(0, 1) # P(X <= -1.5) pnorm(-1.5) # ================================= # Example 5.15 pulmonary disease # X ~ N(0, 1) # P(-1.5 <= X <= 1.5) pnorm(1.5) - pnorm(-1.5) # ================================= # Example 5.16 X ~ N(0, 1) # P(-1.5 <= X <= 1.5) pnorm(1.45) - pnorm(0) # ================================= # Example 5.17 X ~ N(0, 1) # P(X <= 2.824) pnorm(2.824) # ================================= # Example 5.18 percentiles, X ~ N(0, 1) qnorm(.975) qnorm(.95) qnorm(.5) qnorm(.025) # ================================= # Example 5.19 percentiles, X ~ N(0, 1) qnorm(.85) # ================================= # Example 5.20 percentiles, X ~ N(mu=80, sigma=sqrt(144)) # P(90 < X < 100) mu <- 80 sigma <- sqrt(144) a <- 90 b <- 100 pnorm((b-mu)/sigma) - pnorm((a-mu)/sigma) # Example 5.21 botany # P(12 < X) where X ~ N(8,4) mu <- 8 sigma <- sqrt(4) a <- 12 1 - pnorm((a-mu)/sigma) # ================================= # Example 5.22 cerebrovascular disease # P(X < 40) where X ~ N(75,17^2) mu <- 75 sigma <- 17 a <- 40 pnorm((a-mu)/sigma) # ================================= # Example 5.23 ophthalmology # P(X < 40) where X ~ N(75,17^2) mu <- 16 sigma <- 3 a <- 12 b <- 20 pnorm((b-mu)/sigma) - pnorm((a-mu)/sigma) # continuity correction pnorm((b + 0.5 - mu)/sigma) - pnorm((a - 0.5 - mu)/sigma) # ================================= # Example 5.24 hypertension # X.05 and X.95 where X ~ N(80,144) # X.05 == mu + Z.05 * sigma # X.95 == mu + Z.95 * sigma mu <- 80 sigma <- sqrt(144) X.05 <- mu + qnorm(.05) * sigma; X.05 X.95 <- mu + qnorm(.95) * sigma; X.95 # ================================= # Example 5.28 renal disease X1.mu <- 1.3 X2.mu <- 1.5 X1.var <- .25 X2.var <- .25 # X3 == 0.5 * X1 + 0.5 * X2 X3.mu <- 0.5 * X1.mu + 0.5 * X2.mu; X3.mu X3.var <- 0.5^2 * X1.var + 0.5^2 * X2.var; X3.var # X3 ~ N(mu=1.4, var=0.125) # ================================= # Example 5.33 # P(7 <= X <= 12) where X ~ Binomial(n=25, p=.4) n <- 25 p <- .4 mu <- n * p var <- n * p * (1-p) sigma <- sqrt(var) a <- 7 b <- 12 # using continuity correction pnorm(b+0.5, mean=mu, sd=sigma) - pnorm(a-0.5, mean=mu, sd=sigma) # check sum(dbinom(a:b, size=n, prob=p)) # ================================= # Example 5.34 infectious disease # P(50 <= X <= 75) where X ~ Binomial(n=100, p=.6) n <- 100 p <- .6 a <- 50 b <- 75 # exact sum(dbinom(a:b, size=n, prob=p)) # normal approximation mu <- n * p var <- n * p * (1-p) sigma <- sqrt(var) # using continuity correction pnorm(b+0.5, mean=mu, sd=sigma) - pnorm(a-0.5, mean=mu, sd=sigma) # ================================= # Example 5.35 infectious disease # P(X <= 49 || 76 <= X) where X ~ Binomial(n=100, p=.6) n <- 100 p <- .6 a <- 49 b <- 76 # exact : low sum(dbinom(0:a, size=n, prob=p)) # exact : high sum(dbinom(b:n, size=n, prob=p)) # normal approximation mu <- n * p; mu var <- n * p * (1-p); var sigma <- sqrt(var); sigma # using continuity correction # exact : low pnorm(a+0.5, mean=mu, sd=sigma) pnorm((a+0.5-mu)/sigma) # exact : high 1 - pnorm(b-0.5, mean=mu, sd=sigma) 1 - pnorm((b-0.5-mu)/sigma) # ================================= # Example 5.36 bacteriology # X ~ Poisson(mu=lambda * a) # P(20 <= X) lambda <- 0.1 area <- 100 mu <- lambda * area; mu # exact 1 - ppois(19, mu) # normal approximation mean <- mu var <- mu sigma <- sqrt(var) # continuity correction 1 - pnorm((20 - 0.5 - mean)/sigma) # ================================= # binomial distributions, p=.1 par(mfrow=c(2,2)) plot(dbinom(0:5, size=10, prob=.1), pch=19, col="dark red", xlab="k", ylab="P(X==k)") title(main="binomial distributions:") plot(dbinom(0:7, size=20, prob=.1), pch=19, col="dark red", xlab="k", ylab="P(X==k)") title(main="n=10,20,50,100 and p=.1") plot(dbinom(0:12, size=50, prob=.1), pch=19, col="dark red", xlab="k", ylab="P(X==k)") plot(dbinom(0:20, size=100, prob=.1), pch=19, col="dark red", xlab="k", ylab="P(X==k)") # ================================= # binomial distributions, p=.2 par(mfrow=c(2,2)) plot(dbinom(0:7, size=10, prob=.2), pch=19, col="dark red", xlab="k", ylab="P(X==k)") title(main="binomial distributions:") plot(dbinom(0:10, size=20, prob=.2), pch=19, col="dark red", xlab="k", ylab="P(X==k)") title(main="n=10,20,50,100 and p=.2") plot(dbinom(2:18, size=50, prob=.2), pch=19, col="dark red", xlab="k", ylab="P(X==k)") plot(dbinom(10:30, size=100, prob=.2), pch=19, col="dark red", xlab="k", ylab="P(X==k)") # ================================= # Poisson distributions, mu=2,5,10,20 par(mfrow=c(2,2)) plot(dpois(0:8, lambda=2), pch=19, col="dark red", xlab="k", ylab="P(X==k)") title(main="Poisson distributions:") plot(dpois(0:12, lambda=5), pch=19, col="dark red", xlab="k", ylab="P(X==k)") title(main="mu=2,5,10,20") plot(dpois(0:20, lambda=10), pch=19, col="dark red", xlab="k", ylab="P(X==k)") plot(dpois(6:34, lambda=20), pch=19, col="dark red", xlab="k", ylab="P(X==k)")