1 |
train |
|
test |
|
g |
|
k |
|
prob |
|
plotit |
|
xlab |
|
ylab |
|
depthfun |
|
... |
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 | ##---- 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 (train, test, g, k = 1, prob = FALSE, plotit = FALSE,
xlab = "Group 1", ylab = "Group 2", depthfun = prodepth,
...)
{
library(class)
train = as.matrix(train)
test = as.matrix(test)
P = ncol(train)
P1 = P + 1
xall = as.data.frame(matrix(NA, nrow = nrow(train), ncol = P1))
xall[, 1:P] = train
xall[, P1] = g
xall = elimna(xall)
x1 = xall[, 1:P]
xall = as.matrix(xall)
x1 = as.matrix(x1)
g = as.vector(xall[, P1])
ids = unique(g)
x2 = elimna(test)
x1 = as.matrix(x1)
x2 = as.matrix(x2)
n = nrow(x1)
n2 = nrow(x2)
p = length(ids)
d = matrix(NA, nrow = n, ncol = p)
D = matrix(NA, nrow = n2, ncol = p)
for (i in 1:length(ids)) {
flag = g == ids[i]
d[, i] = depthfun(as.matrix(x1[flag, ]), pts = x1, ...)
D[, i] = depthfun(as.matrix(x1[flag, ]), pts = x2, ...)
}
res = NULL
res = knn(d, D, cl = as.factor(g), k = k, prob = prob)
if (plotit) {
if (p == 2) {
plot(d[, 1], d[, 2], xlab = xlab, ylab = ylab, type = "n")
flag = g == ids[1]
points(d[flag, 1], d[flag, 2])
points(d[!flag, 1], d[!flag, 2], pch = "*")
}
}
res
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.