title: R - what command to generate confusion matrix using as input results from rpart() and predict()?
body: <p>What command should I use in R to perform a confusion matrix after having used <code>rpart()</code> and <code>predict()</code> commands to generate a prediction model?</p>
  
  <pre><code># Grow tree
  library(rpart)
  fit &lt;- rpart(activity ~ ., method="class", data=train.data)
  
  printcp(fit) # display the results
  plotcp(fit) # visualize cross-validation results
  summary(fit) # detailed summary of splits
  
  # Prune the tree (in my case is exactly the same as the initial model)
  pfit &lt;- prune(fit, cp=0.10) # from cptable
  pfit &lt;- prune(fit,cp=fit$cptable[which.min(fit$cptable[,"xerror"]),"CP"])
  
  # Predict using the test dataset
  pred1 &lt;- predict(fit, test.data, type="class")
  
  # Show re-substitution error
  table(train.data$activity, predict(fit, type="class"))
  
  # Accuracy rate
  sum(test.data$activity==pred1)/length(pred1)
  </code></pre>
  
  <p>I would like to summarise in a clear way True Positives, False Negatives, False Positives and True Negatives. It would be great also to have in the same matrix Sensitivity, Specificity, Positive Predictive Value and Negative Predictive Value.</p>
  
  <p><img src="https://i.stack.imgur.com/3eGlc.png" alt="Relationships among terms">
  Source: <a href="http://en.wikipedia.org/wiki/Sensitivity_and_specificity" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Sensitivity_and_specificity</a></p>