DA 410 C. C HW3
Homework 3 (30 points)
1. Chapter 6 Page 245-246: #6.27 (a) (b) (c) 2. Chapter 6 Page 246-247: #6.28 (a)
DA 410
C. C
HW3
HW 3 HELP FILE
6.27 (a)
# Input the data into R
# Calculate “between” matrices
𝑯𝑯 = 𝑛𝑛 ∑ (𝒚𝒚̅𝑖𝑖. − 𝒚𝒚̅..)(𝒚𝒚̅𝑖𝑖. − 𝒚𝒚̅..) 𝑖𝑖=1
# 𝒚𝒚̅𝑖𝑖. is the column mean of method i, 𝒚𝒚̅.. is the column mean of all samples. n is the number of samples.
𝑘𝑘
′
method1.bar <- colMeans(method1) method2.bar <- colMeans(method2) method3.bar <- colMeans(method3)
𝒚𝒚̅ 𝑖𝑖 . method.all.bar <- (method1.bar+method2.bar+method3.bar)/3
𝒚𝒚̅ . . 𝒚𝒚̅ 𝑖𝑖 . − 𝒚𝒚̅ . .
H <- 12 * unname(method1.bar.diff %*% t(method1.bar.diff) + method2.bar.diff %*% t(method2.bar.diff) + method3.bar.diff %*% t(method3.bar.diff))
method1.bar.diff <- method1.bar - method.all.bar method2.bar.diff <- method2.bar - method.all.bar method3.bar.diff <- method3.bar - method.all.bar
DA 410
C. C HW3
# Calculate “within” matrices
𝑬𝑬 = ∑ ∑ (𝒚𝒚𝑖𝑖𝑗𝑗 − 𝒚𝒚̅𝑖𝑖.)(𝒚𝒚𝑖𝑖𝑗𝑗 − 𝒚𝒚̅𝑖𝑖.)′ 𝑖𝑖=1 𝑗𝑗=1
"compute.within.matrix" <- function(data, mean) { ret <- matrix(as.numeric(0), nrow=4, ncol=4)
for (i in 1:12) {
diff <- as.numeric(unname(data[i,] - mean))
ret <- ret + diff %*% t(diff) }
return(ret) }
E <- compute.within.matrix(method1, method1.bar) + compute.within.matrix(method2, method2.bar) + compute.within.matrix(method3, method3.bar)
#Four MANOVA
# Wilks’ Test Statistic 𝛬𝛬 =
Lambda <- det(E) / det(E + H)
# Pillai Statistic 𝑉𝑉(𝑠𝑠) = 𝑡𝑡𝑡𝑡[(𝐸𝐸 + 𝐻𝐻)−1𝐻𝐻]
|𝑬𝑬| |𝑬𝑬+𝑯𝑯|
𝑘𝑘𝑛𝑛
Determinant of E/Determinant of E+H
To calculate trace of the matrix in R, you will need to install “psych” package.
> install.packages(“psych”)
DA 410 C. C HW3
> library(psych)
V.s <- tr(solve(E + H) %*% H)
# Lawley-Hotelling statistic is defined as 𝑈𝑈(𝑠𝑠) = 𝑡𝑡𝑡𝑡(𝑬𝑬−𝟏𝟏𝑯𝑯) U.s <- tr(solve(E) %*% H)
# Roy’s largest root test Ө = 𝜆𝜆1 𝜆𝜆 1+𝜆𝜆1
6.27 (b)
See textbook page 188 example 6.1.8.
6.27 (c)
Read textbook page 189-190 and see example 6.2
1 is the largest eigenvalue of matrix E
lambda.1 <- eigen(solve(E) %*% H)$values[1] theta <- lambda.1 / (1 + lambda.1)