title: Getting the decision boundary for KNN classifier using R
body: <p>I am trying to fit a KNN model and obtain a decision boundary using Auto data set in ISLR package in R. </p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <pre><code> library("ISLR")
  
  trainxx=Auto[,c(1,3)]
  trainyy=(Auto[,8])
  
  n.grid1 &lt;- 50
  
  x1.grid1 &lt;- seq(f = min(trainxx[, 1]), t = max(trainxx[, 1]), l = n.grid1)
  x2.grid1 &lt;- seq(f = min(trainxx[, 2]), t = max(trainxx[, 2]), l = n.grid1)
  grid &lt;- expand.grid(x1.grid1, x2.grid1)
  
  library("class")
  mod.opt &lt;- knn(trainxx, grid, trainyy, k = 10, prob = T)
  
  prob_knn &lt;- attr(mod.opt, "prob") 
  </code></pre>
  
  <p>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 ? </p>
  
  <pre><code>prob_knn &lt;- ifelse(mod.opt == "3", prob_knn, 1 - prob_knn) 
  
  
  
  prob_knn &lt;- 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)
  </code></pre>
  
  <p>It wil be a great help if anyone can give a suggestion to solve this issue.</p>
  
  <p>Basically i need something like this for a 3 class problem
  <a href="https://stats.stackexchange.com/questions/21572/how-to-plot-decision-boundary-of-a-k-nearest-neighbor-classifier-from-elements-o">https://stats.stackexchange.com/questions/21572/how-to-plot-decision-boundary-of-a-k-nearest-neighbor-classifier-from-elements-o</a></p>