# 分散・共分散行列の同等性の検定
# x,y は多変量データ行列
eq.cov <- function(x, y)
{
p <- ncol(x1)
s1 <- var(x1)
s2 <- var(x2)
n1 <- nrow(x1)-1
n2 <- nrow(x2)-1
sa <- (n1*s1+n2*s2)/(n1+n2)
chi <- (1-(1/n1+1/n2-1/(n1+n2))*(2*p^2+3*p-1)/(6*p+6))*((n1+n2)*log(det(sa))-n1*log(det(s1))-n2*log(det(s2)))
df <- p*(p+1)/2
result <- c(chi, df, pchisq(chi, df, lower=F))
names(result) <- c("Statistics", "d.f.", "P value")
result
}
# 使用例
x1 <- matrix(c(
2.9,161.7,120.8,
2.3,114.8,85.2,
2,128.4,92,
3.2,149.2,97.3,
2.7,126,81.1,
4.4,133.8,107.6,
4.1,161.3,114,
2.1,111.5,77.3
), byrow=TRUE, ncol=3)
x2 <- matrix(c(
4.8,198.7,172.9,
3.6,199.3,157.9,
2,188.4,152.7,
4.9,183.6,164.2,
3.9,173.5,172.2,
4.4,184.9,163.2
), byrow=TRUE, ncol=3)
eq.cov(x, y)
# Statistics d.f. P value
# 6.9947058 6.0000000 0.3213371