title: How to get correct class, predicted class and prediction probability in Random Forest in R?
body: <p>I am using randomForest for classification of data and I am unable to understand:</p>
  
  <p>1- How can we obtain the information (preferably in a dataframe of 3 columns) which tells us the real classification in <code>testData</code> (e.g. in below example <code>Species</code> column), prediction made by random forest, and the probability score of that prediction. For example just consider the below data set and 1 case where in <code>testData</code> the Species (blinded information for random forest) was <strong>versicolor</strong> but it was predicted wrongly by classifier as <strong>virginica</strong> with a probability score of <strong>0.67</strong>. I want this kind of information but don't know how can I obtain this</p>
  
  <p>2- How can we get the confusion matrix for <code>testData</code> and <code>trainingData</code> which also gives us the class.error, like in the case when we print the model.</p>
  
  <pre><code>data(iris)
  set.seed(111)
  ind &lt;- sample(2, nrow(iris), replace = TRUE, prob=c(0.8, 0.2))
  trainData &lt;- iris[ind==1,]    
  testData &lt;- iris[ind==2,]
  #grow forest
  iris.rf &lt;- randomForest(Species ~ ., data=trainData)
  print(iris.rf)
  
  Call:
   randomForest(formula = Species ~ ., data = trainData) 
                 Type of random forest: classification
                       Number of trees: 500
  No. of variables tried at each split: 2
  
          OOB estimate of  error rate: 3.33%
  Confusion matrix:
             setosa versicolor virginica class.error
  setosa         45          0         0  0.00000000
  versicolor      0         39         1  0.02500000
  virginica       0          3        32  0.08571429
  
  **#predict using the training again...**
  iris.pred &lt;- predict(iris.rf, trainData)
  table(observed = trainData$Species, predicted = iris.pred)
  
             predicted
  observed     setosa versicolor virginica
    setosa         45          0         0
    versicolor      0         40         0
    virginica       0          0        35
  
  **#Testing on testData**
  irisPred&lt;-predict(iris.rf, newdata = testData)
  table(irisPred, testData$Species)
  
  irisPred     setosa versicolor virginica
  setosa          5          0         0
  versicolor      0          8         1
  virginica       0          2        14
  </code></pre>