1 |
J |
|
K |
|
x |
|
alpha |
|
nboot |
|
grp |
|
est |
|
... |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | ##---- 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, alpha = 0.05, nboot = NA, grp = NA, est = onestep,
...)
{
if (is.data.frame(x))
x = as.matrix(x)
JK <- J * K
if (is.matrix(x))
x <- listm(x)
if (!is.na(grp)) {
yy <- x
for (j in 1:length(grp)) x[[j]] <- yy[[grp[j]]]
}
if (!is.list(x))
stop("Data must be stored in list mode or a matrix.")
for (j in 1:JK) {
xx <- x[[j]]
x[[j]] <- xx[!is.na(xx)]
}
ij <- matrix(c(rep(1, J)), 1, J)
ik <- matrix(c(rep(1, K)), 1, K)
jm1 <- J - 1
cj <- diag(1, jm1, J)
for (i in 1:jm1) cj[i, i + 1] <- 0 - 1
km1 <- K - 1
ck <- diag(1, km1, K)
for (i in 1:km1) ck[i, i + 1] <- 0 - 1
conA <- t(kron(cj, ik))
conB <- t(kron(ij, ck))
conAB <- t(kron(cj, ck))
ncon <- max(nrow(conA), nrow(conB), nrow(conAB))
if (JK != length(x)) {
print("Warning: The number of groups does not match")
print("the number of contrast coefficients.")
}
if (is.na(nboot)) {
nboot <- 5000
if (ncon <= 4)
nboot <- 2000
}
m1 <- matrix(0, nrow = JK, ncol = nboot)
set.seed(2)
print("Taking bootstrap samples. Please wait.")
for (j in 1:JK) {
paste("Working on group ", j)
data <- matrix(sample(x[[j]], size = length(x[[j]]) *
nboot, replace = TRUE), nrow = nboot)
m1[j, ] <- apply(data, 1, est, ...)
}
bootA <- matrix(0, ncol(conA), nboot)
bootB <- matrix(0, ncol(conB), nboot)
bootAB <- matrix(0, ncol(conAB), nboot)
testA <- NA
testB <- NA
testAB <- NA
testvecA <- NA
testvecB <- NA
testvecAB <- NA
for (d in 1:ncol(conA)) {
bootA[d, ] <- apply(m1, 2, trimpartt, conA[, d])
testA[d] <- sum((bootA[d, ] > 0))/nboot
testA[d] <- min(testA[d], 1 - testA[d])
}
for (d in 1:ncol(conB)) {
bootB[d, ] <- apply(m1, 2, trimpartt, conB[, d])
testB[d] <- sum((bootB[d, ] > 0))/nboot
testB[d] <- min(testB[d], 1 - testB[d])
}
for (d in 1:ncol(conAB)) {
bootAB[d, ] <- apply(m1, 2, trimpartt, conAB[, d])
testAB[d] <- sum((bootAB[d, ] > 0))/nboot
testAB[d] <- min(testAB[d], 1 - testAB[d])
}
Jm <- J - 1
Km <- K - 1
JKm <- (J - 1) * (K - 1)
dvecA <- alpha/c(1:Jm)
dvecB <- alpha/c(1:Km)
dvecAB <- alpha/c(1:JKm)
testA <- (0 - 1) * sort(-2 * testA)
testB <- (0 - 1) * sort(-2 * testB)
testAB <- (0 - 1) * sort(-2 * testAB)
sig <- sum((testA < dvecA[1:Jm]))
if (sig > 0)
print("Significant result obtained for Factor A: Reject")
if (sig == 0)
print("No significant result Factor A: Fail to reject")
sig <- sum((testB < dvecB[1:Km]))
if (sig > 0)
print("Significant result obtained for Factor B: Reject")
if (sig == 0)
print("No significant result Factor B: Fail to reject")
sig <- sum((testAB < dvecAB[1:JKm]))
if (sig > 0)
print("Significant Interaction: Reject")
if (sig == 0)
print("No significant Interaction: Fail to reject")
list(testA = testA, crit.vecA = dvecA, testB = testB, crit.vecB = dvecB,
testAB = testAB, crit.vecAB = dvecAB)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.