title: Variation on "How to plot decision boundary of a k-nearest neighbor classifier from Elements of Statistical Learning?"
body: <p>This is a question related to <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>
  
  <p>For completeness, here's the original example from that link:</p>
  
  <pre><code>library(ElemStatLearn)
  require(class)
  x &lt;- mixture.example$x
  g &lt;- mixture.example$y
  xnew &lt;- mixture.example$xnew
  mod15 &lt;- knn(x, xnew, g, k=15, prob=TRUE)
  prob &lt;- attr(mod15, "prob")
  prob &lt;- ifelse(mod15=="1", prob, 1-prob)
  px1 &lt;- mixture.example$px1
  px2 &lt;- mixture.example$px2
  prob15 &lt;- matrix(prob, length(px1), length(px2))
  par(mar=rep(2,4))
  contour(px1, px2, prob15, levels=0.5, labels="", xlab="", ylab="", main=
          "15-nearest neighbour", axes=FALSE)
  points(x, col=ifelse(g==1, "coral", "cornflowerblue"))
  gd &lt;- expand.grid(x=px1, y=px2)
  points(gd, pch=".", cex=1.2, col=ifelse(prob15&gt;0.5, "coral", "cornflowerblue"))
  box()
  </code></pre>
  
  <p>I've been playing with that example, and would like to try to make it work with three classes. I can change some values of g with something like</p>
  
  <pre><code>g[8:16] &lt;- 2
  </code></pre>
  
  <p>just to pretend that there are some samples which are from a third class. I can't make the plot work, though. I guess I need to change the lines that deal with the proportion of votes for winning class:</p>
  
  <pre><code>prob &lt;- attr(mod15, "prob")
  prob &lt;- ifelse(mod15=="1", prob, 1-prob)
  </code></pre>
  
  <p>and also the levels on the contour:</p>
  
  <pre><code>contour(px1, px2, prob15, levels=0.5, labels="", xlab="", ylab="", main=
  "15-nearest neighbour", axes=FALSE)
  </code></pre>
  
  <p>I am also not sure contour is the right tool for this. One alternative that works is to create a matrix of data that covers the region I'm interested, classify each point of this matrix and plot those with a large marker and different colors, similar to what is being done with the points(gd...) bit.</p>
  
  <p>The final purpose is to be able to show different decision boundaries generated by different classifiers. Can someone point me to the right direction?</p>
  
  <p>thanks 
  Rafael</p>