1 |
J |
|
K |
|
x |
|
grp |
|
JK |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (J, K, x, grp = c(1:JK), JK = J * K)
{
x = elimna(x)
if (is.matrix(x))
x <- listm(x)
xx <- list()
nvec <- NA
jk <- 0
for (j in 1:J) {
for (k in 1:K) {
jk <- jk + 1
xx[[jk]] <- x[[grp[jk]]]
if (k == 1)
nvec[j] <- length(xx[[jk]])
}
}
N <- sum(nvec)
RVALL <- matrix(0, nrow = N, K)
x <- xx
jk <- 0
rmean <- matrix(NA, nrow = J, ncol = K)
for (j in 1:J) {
RV <- matrix(0, nrow = nvec[j], ncol = K)
jk <- jk + 1
temp1 <- matrix(x[[jk]], ncol = 1)
for (k in 2:K) {
jk <- jk + 1
temp1 <- cbind(temp1, x[[jk]])
}
X <- temp1
if (j == 1)
XALL <- X
if (j > 1)
XALL <- rbind(XALL, X)
n <- nvec[j]
for (i in 1:n) {
for (ii in 1:n) {
temp3 <- sqrt(sum((X[i, ] - X[ii, ])^2))
if (temp3 != 0)
RV[i, ] <- RV[i, ] + (X[i, ] - X[ii, ])/temp3
}
RV[i, ] <- RV[i, ]/nvec[j]
if (j == 1 && i == 1)
sighat <- RV[i, ] %*% t(RV[i, ])
if (j > 1 || i > 1)
sighat <- sighat + RV[i, ] %*% t(RV[i, ])
}
}
for (i in 1:N) {
for (ii in 1:N) {
temp3 <- sqrt(sum((XALL[i, ] - XALL[ii, ])^2))
if (temp3 != 0)
RVALL[i, ] <- RVALL[i, ] + (XALL[i, ] - XALL[ii,
])/temp3
}
RVALL[i, ] <- RVALL[i, ]/N
}
bot <- 1 - nvec[1]
top <- 0
for (j in 1:J) {
bot <- bot + nvec[j]
top <- top + nvec[j]
flag <- c(bot:top)
rmean[j, ] <- apply(RVALL[flag, ], 2, mean)
}
sighat <- sighat/(N - J)
shatinv <- solve(sighat)
KW <- 0
for (j in 1:J) {
KW <- KW + nvec[j] * t(rmean[j, ]) %*% shatinv %*% rmean[j,
]
}
df <- K * (J - 1)
sig.level <- 1 - pchisq(KW, df)
list(test.stat = KW[1, 1], df = df, p.value = sig.level)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.