1 |
x |
|
cov.fun |
|
xlab |
|
ylab |
|
qval |
|
crit |
|
plotit |
|
... |
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 | ##---- 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, cov.fun = cov.mve, xlab = "X", ylab = "Y", qval = 0.975,
crit = NULL, plotit = FALSE, ...)
{
library(MASS)
oldSeed <- .Random.seed
set.seed(12)
if (is.data.frame(x))
x = as.matrix(x)
if (is.list(x))
stop("Data cannot be stored in list mode")
nrem = nrow(as.matrix(x))
if (!is.matrix(x)) {
dis <- (x - median(x, na.rm = TRUE))^2/mad(x, na.rm = TRUE)^2
if (is.null(crit))
crit <- sqrt(qchisq(0.975, 1))
vec <- c(1:length(x))
}
if (is.matrix(x)) {
mve <- cov.fun(elimna(x))
dis <- mahalanobis(x, mve$center, mve$cov)
if (is.null(crit))
crit <- sqrt(qchisq(0.975, ncol(x)))
vec <- c(1:nrow(x))
}
dis[is.na(dis)] = 0
dis <- sqrt(dis)
chk <- ifelse(dis > crit, 1, 0)
id <- vec[chk == 1]
keep <- vec[chk == 0]
if (is.matrix(x)) {
if (ncol(x) == 2 && plotit) {
plot(x[, 1], x[, 2], xlab = xlab, ylab = ylab, type = "n")
flag <- rep(T, nrow(x))
flag[id] <- F
points(x[flag, 1], x[flag, 2])
if (sum(!flag) > 0)
points(x[!flag, 1], x[!flag, 2], pch = "*")
}
}
if (!is.matrix(x))
outval <- x[id]
if (is.matrix(x))
outval <- x[id, ]
n = nrow(as.matrix(x))
n.out = length(id)
assign(x = ".Random.seed", value = oldSeed, envir = .GlobalEnv)
list(n = n, n.out = n.out, out.val = outval, out.id = id,
keep = keep, dis = dis, crit = crit)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.