## —-echo=FALSE———————————————————-
set.seed(2339)
## ————————————————————————
x <- c(1612, 1352, 1256, 922, 1560, 1456, 2324)
y <- c(1082, 1300, 1092, 1040, 910 , 1248, 1092, 1040, 1092, 1288)
## ------------------------------------------------------------------------
t.test(x, y, var.equal = TRUE)
## ------------------------------------------------------------------------
var.test(x, y)
## ------------------------------------------------------------------------
t.test(x, y)
## ----fig.width=6, fig.height=4-------------------------------------------
par(mfrow = c(1, 2))
qqnorm(x, main = "X", col = 4)
qqline(x, lty = 2)
qqnorm(y, main = "Y", col = 4)
qqline(y, lty = 2)
## ------------------------------------------------------------------------
# Wilcoxon test with normal approximation
wilcox.test(x, y, exact = FALSE)
## ----results='hide'------------------------------------------------------
y2 <- y
y2[5] <- 470
wilcox.test(x, y2, exact = FALSE)
## ------------------------------------------------------------------------
butterfat <- read.table("butterfat.txt")[, 1]
x.bar <- mean(butterfat)
s <- sd(butterfat)
## ------------------------------------------------------------------------
b <- c(0, seq(374, 624, 50), 1000) # class boundaries
O <- table(cut(butterfat, breaks = b)) # observed counts
O
## ------------------------------------------------------------------------
prob <- rep(0, 7)
for (k in 1:7)
prob[k] <- pnorm(b[k + 1], x.bar, s) - pnorm(b[k], x.bar, s)
E <- prob * length(butterfat) # expected frequencies
## ------------------------------------------------------------------------
C <- sum((O - E)^2 / E) # chi-squared statistic
d1 <- length(O) # number of classes
qchisq(0.95, d1 - 3) # critical value
1 - pchisq(C, d1 - 3) # p-value
cbind(O, E)
## ------------------------------------------------------------------------
chisq.test(O, p = prob)
## ------------------------------------------------------------------------
1 - pchisq(1.0144, d1 - 3)
## ------------------------------------------------------------------------
X.raw <- c(171, 93, 82, 62, 54, 11, 31, 55, 43, 11, 27, 57)
X <- matrix(X.raw, nrow = 4, byrow = TRUE)
test <- chisq.test(X)
test
## ------------------------------------------------------------------------
test$expected
## ------------------------------------------------------------------------
E.1 <- sum(X[, 1]) * sum(X[1, ]) / sum(X)
E.1
O.1 <- 171
(O.1 - E.1)^2 / E.1