title: TPR & FPR Curve for different classifiers - kNN, NaiveBayes, Decision Trees in R
body: <p>I'm trying to understand and plot TPR/FPR for different types of classifiers. I'm using kNN, NaiveBayes and Decision Trees in R. With kNN I'm doing the following:</p>
  
  <pre><code>clnum &lt;- as.vector(diabetes.trainingLabels[,1], mode = "numeric")
  dpknn &lt;- knn(train = diabetes.training, test = diabetes.testing, cl = clnum, k=11, prob = TRUE)
  prob &lt;- attr(dpknn, "prob")
  tstnum &lt;- as.vector(diabetes.testingLabels[,1], mode = "numeric")
  pred_knn &lt;- prediction(prob, tstnum)
  pred_knn &lt;- performance(pred_knn, "tpr", "fpr")
  plot(pred_knn, avg= "threshold", colorize=TRUE, lwd=3, main="ROC curve for Knn=11")
  </code></pre>
  
  <p>where diabetes.trainingLabels[,1] is a vector of labels (class) I want to predict, diabetes.training is the training data and diabetest.testing is the testing.data.</p>
  
  <p>Plot looks like the following:
  <a href="https://i.stack.imgur.com/ALSjP.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ALSjP.png" alt="enter image description here"></a></p>
  
  <p>The values stored in prob attribute is a numeric vector (decimal between 0 and 1). I convert the class labels factor into numbers and then I can use it with prediciton/performance function from ROCR library. Not 100% sure I'm doing it correct but at least it works.</p>
  
  <p>For the NaiveBayes and Decision Trees tho, with prob/raw parameter speciefied in predict function I don't get a single numeric vector but a vector of lists or matrix where probability for each class is specified (I guess), eg:</p>
  
  <pre><code>diabetes.model &lt;- naiveBayes(class ~ ., data = diabetesTrainset)
  diabetes.predicted &lt;- predict(diabetes.model, diabetesTestset, type="raw")
  </code></pre>
  
  <p>and diabetes.predicted is:</p>
  
  <pre><code>     tested_negative tested_positive
       [1,]    5.787252e-03       0.9942127
       [2,]    8.433584e-01       0.1566416
       [3,]    7.880800e-09       1.0000000
       [4,]    7.568920e-01       0.2431080
       [5,]    4.663958e-01       0.5336042
  </code></pre>
  
  <p>The question is how to use it to plot ROC curve and why in kNN I get one vector and for other classifieres I get them separate for both classes?</p>