## Settings for RMarkdown http://yihui.name/knitr/options#chunk_options
opts_chunk$set(comment = "", warning = FALSE, message = FALSE, tidy = FALSE,
echo = TRUE, fig.width = 7, fig.height = 7)
options(width = 116, scipen = 10)
setwd("~/statistics/bio201/")
library(ggplot2)
References
## 5.6
pnorm(q = 140, mean = 124, sd = 20, lower.tail = FALSE)
[1] 0.2119
## 5.7
pnorm(q = 90, mean = 124, sd = 20, lower.tail = TRUE)
[1] 0.04457
func.1 <- function(x) dnorm(x, 124, 20)
ggplot(data = data.frame(x = 0), aes(x)) +
stat_function(fun = func.1) +
xlim(50,200) +
geom_polygon(aes(x = c(50, 90, seq(90, 50, -0.1)),
y = c( 0, 0, func.1(seq(90, 50, -0.1)) )
)
) +
geom_polygon(aes(x = c(140, seq(200, 140, -0.1)),
y = c( 0, func.1(seq(200, 140, -0.1)) )
)
)
## 5.8
pnorm(q = 140, mean = 121, sd = 19, lower.tail = FALSE)
[1] 0.1587
## 5.9
pnorm(q = 90, mean = 121, sd = 19, lower.tail = TRUE)
[1] 0.05138
func.2 <- function(x) dnorm(x, 121, 19)
ggplot(data = data.frame(x = 0), aes(x)) +
stat_function(fun = func.2) +
xlim(50,200) +
geom_polygon(aes(x = c(50, 90, seq(90, 50, -0.1)),
y = c( 0, 0, func.2(seq(90, 50, -0.1)) )
)
) +
geom_polygon(aes(x = c(140, seq(200, 140, -0.1)),
y = c( 0, func.2(seq(200, 140, -0.1)) )
)
)
## 5.31
func5.31 <- function(x) pnorm(q = x, mean = 90, sd = 38, lower.tail = TRUE)
func5.31(120) - func5.31(65)
[1] 0.5298
## 5.32
1 - func5.31(120 * 1.5)
[1] 0.008932
## 5.33
1 - func5.31(120 * 2)
[1] 0.00003951
## 5.34
(1 - func5.31(120 * 1.5))^2
[1] 0.00007978
## 5.35
pbinom(q = 75, size = 6000, prob = 1 - func5.31(120 * 1.5))
[1] 0.9978
sum(dbinom(x = 0:75, size = 6000, prob = 1 - func5.31(120 * 1.5)))
[1] 0.9978
## 5.50
func5.50 <- function(y) dbinom(x = y, size = 84, prob = 0.24)
func5.50(29)
[1] 0.00873
## 5.51
sum(func5.50(29:84))
[1] 0.01935
## 5.52 Greater than 27 thus 28. Always confirm. qbinom() function is very tricky.
qbinom(p = 0.05, size = 84, prob = 0.24, lower.tail = FALSE)
[1] 27
sum(func5.50(27:84))
[1] 0.05594
sum(func5.50(28:84))
[1] 0.03367
## By normal approximation. At least 27. Slightly off.
qnorm(p = 0.05, mean = 84 * 0.24, sd = sqrt(84 * 0.24 * (1 - 0.24)), lower.tail = FALSE)
[1] 26.6
## Graphing binomial
layout(matrix(1:2))
barplot(func5.50(0:84), names.arg = 0:84, col = rep(c(0,1), c(length(0:27),length(28:84))))
fun.approx <- function(x) dnorm(x, mean = 84 * 0.24, sd = sqrt(84 * 0.24 * (1 - 0.24)))
curve(fun.approx, xlim = c(0,84))
x.seq <- seq(84, 28, -0.1)
polygon(x = c(28, 84, x.seq),
y = c( 0, 0, fun.approx(x.seq)),
col = "grey")
## Probability of having a value greater than or equal to 288 over a 90-day period.
1 - sum(dpois(x = 0:287, lambda = 3 * 90))
[1] 0.1437
## By normal approximation. Slightly smaller
pnorm(q = 288, mean = 3 * 90, sd = sqrt(3 * 90), lower.tail = FALSE)
[1] 0.1367
## Graphing
layout(matrix(1:2))
x <- 200:350
barplot(dpois(x, lambda = 3 * 90), names.arg = x, col = rep(c(0,1), c(length(200:287),length(288:350))))
## By normal approximation mean = variance = lambda for Poisson
fun.approx <- function(x) dnorm(x, mean = 3*90, sd = sqrt(3*90))
curve(fun.approx, xlim = c(200,350))
x.seq <- seq(350, 288, -0.1)
polygon(x = c(288, 350, x.seq),
y = c( 0, 0, fun.approx(x.seq)),
col = "grey")
## 5.68
q5.68 <- pnorm(q = 140, mean = 125, sd = 15, lower.tail = FALSE)
q5.68
[1] 0.1587
## 5.69
q5.69 <- 125 - 3/10 * (142 - 123)
q5.69
[1] 119.3
## 5.70
q5.70 <- pnorm(q = 140, mean = q5.69, sd = 15, lower.tail = FALSE)
q5.70
[1] 0.08379
## 5.71
q5.68 - q5.70
[1] 0.07486