1 | cmba2(x, csteps = 5, ii = 1)
|
x |
|
csteps |
|
ii |
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 | ##---- 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 (x, csteps = 5, ii = 1)
{
n <- dim(x)[1]
p <- dim(x)[2]
mds <- matrix(nrow = n, ncol = 8, 0)
covs <- var(x)
mns <- apply(x, 2, mean)
cmd <- sqrt(mahalanobis(x, mns, covs))
for (i in 1:csteps) {
md2 <- mahalanobis(x, mns, covs)
medd2 <- median(md2)
mns <- apply(x[md2 <= medd2, ], 2, mean)
covs <- var(x[md2 <= medd2, ])
}
mds[, 8] <- sqrt(mahalanobis(x, mns, covs))
covb <- covs
mnb <- mns
critb <- prod(diag(chol(covb)))
covv <- diag(p)
med <- apply(x, 2, median)
md2 <- mahalanobis(x, center = med, covv)
smd2 <- sort(md2)
val <- p + 3
tem <- 1:7
tem[1] <- smd2[val + floor(0.02 * n)]
tem[2] <- smd2[val + floor(0.05 * n)]
tem[3] <- smd2[val + floor(0.1 * n)]
tem[4] <- smd2[val + floor(0.2 * n)]
tem[5] <- smd2[val + floor(0.3 * n)]
tem[6] <- smd2[val + floor(0.4 * n)]
tem[7] <- median(md2)
medd2 <- tem[7]
for (j in ii:7) {
val2 <- tem[j]
mns <- apply(x[md2 <= val2, ], 2, mean)
covs <- var(x[md2 <= val2, ])
for (i in 1:csteps) {
md2 <- mahalanobis(x, mns, covs)
medd2 <- median(md2)
mns <- apply(x[md2 <= medd2, ], 2, mean)
covs <- var(x[md2 <= medd2, ])
}
mds[, j] <- sqrt(mahalanobis(x, mns, covs))
plot(cmd, mds[, j])
identify(cmd, mds[, j])
crit <- prod(diag(chol(covs)))
if (crit < critb) {
critb <- crit
covb <- covs
mnb <- mns
}
}
pairs(mds)
rd2 <- mahalanobis(x, mnb, covb)
const <- median(rd2)/(qchisq(0.5, p))
covb <- const * covb
list(center = mnb, cov = covb, mds = mds)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.