I am trying to fit a KNN model and obtain a decision boundary using Auto data set in ISLR package in R.
Here I am having a difficulty to identify the decision boundary for a 3 class problem. This is my code so far.I am not getting the decision boundary.
I saw somewhere else in this website, the answer for this type of question using ggplot. But i want to get the answer in the classical way using the plot function.
library("ISLR")
trainxx=Auto[,c(1,3)]
trainyy=(Auto[,8])
n.grid1 <- 50
x1.grid1 <- seq(f = min(trainxx[, 1]), t = max(trainxx[, 1]), l = n.grid1)
x2.grid1 <- seq(f = min(trainxx[, 2]), t = max(trainxx[, 2]), l = n.grid1)
grid <- expand.grid(x1.grid1, x2.grid1)
library("class")
mod.opt <- knn(trainxx, grid, trainyy, k = 10, prob = T)
prob_knn <- attr(mod.opt, "prob")
My problem is mainly after this code segment. I am pretty much sure i have to modify the following segment . But i dont know how . Do i need to use a "nested if" here ?
prob_knn <- ifelse(mod.opt == "3", prob_knn, 1 - prob_knn)
prob_knn <- matrix(prob_knn, n.grid1, n.grid1)
plot(trainxx, col = ifelse(trainyy == "3", "green",ifelse(trainyy=="2", "red","blue")))
title(main = "plot of training data with Desicion boundary K=80")
contour(x1.grid1, x2.grid1, prob_knn, levels = 0.5, labels = "", xlab = "", ylab = "",
main = "", add = T , pch=20)
It wil be a great help if anyone can give a suggestion to solve this issue.
Basically i need something like this for a 3 class problem https://stats.stackexchange.com/questions/21572/how-to-plot-decision-boundary-of-a-k-nearest-neighbor-classifier-from-elements-o
as.factor()
for labels variables insideggplot
if you receive an error. – RLave