#a quick classification example
x1 <- c(rnorm(20, mean=1), rnorm(20, mean=5))
x2 <- c(rnorm(20, mean=5), rnorm(20, mean=1))
y=rep(1:2,each=20)
x <- cbind(x1,x2)
train <- sample(1:40, 30)
#plot the training cases
plot(x1[train], x2[train], col=y[train]+1)
#predict the other cases
test <- (1:40)[-train]
kdist <- knn.dist(x)
preds <- knn.predict(train, test, y ,kdist, k=3, agg.meth="majority")
#add the predictions to the plot
points(x1[test], x2[test], col=as.integer(preds)+1, pch="+")
#display the confusion matrix
table(y[test], preds)
Run the code above in your browser using DataLab