title: How does caret calculate sensitivity and specificity in resamples?
body: <p>Recently, when I use caret package to run my model, I found sensitivity and specificity from resample of its train object are different from those calculated manually for each fold. 
  Let me use the GermanCredit data as an example.</p>
  
  <pre><code>library(caret)
  data("GermanCredit")
  form = as.formula('credit_risk~amount+savings+installment_rate+age+housing+number_credits')
  train.control &lt;- trainControl(method="cv", 
                             number=5,
                             summaryFunction = twoClassSummary,
                             classProbs = TRUE,
                             savePredictions='all')
  rf = train(form, data=GermanCredit,  method = 'rf',
             metric = 'ROC', trControl=train.control)
  
  print(rf$resample)
  </code></pre>
  
  <p>We got:</p>
  
  <pre><code>ROC         Sens        Spec        Resample
  0.6239881   0.9428571   0.13333333  Fold1   
  0.6603571   0.9714286   0.08333333  Fold2   
  0.6622619   0.9642857   0.06666667  Fold5   
  0.6502381   0.9928571   0.10000000  Fold4   
  0.7072619   0.9714286   0.16666667  Fold3
  </code></pre>
  
  <p>As you can see, for fold 1, sensitivity and specificity are 0.94 and 0.13 respectively. </p>
  
  <p>Now if we just take resamples from Fold1, and use confusionMatrix to calculate the metrics, we got the result below:</p>
  
  <pre><code>resamp.1 = rf$pred %&gt;% filter(Resample=='Fold1')
  cm=confusionMatrix(resamp.1$pred, resamp.1$obs)
  print(cm) 
  
  Confusion Matrix and Statistics
  
            Reference
  Prediction good bad
        good  366 135
        bad    54  45
  
                 Accuracy : 0.685          
                   95% CI : (0.6462, 0.722)
      No Information Rate : 0.7            
      P-Value [Acc &gt; NIR] : 0.8018         
  
                    Kappa : 0.1393         
   Mcnemar's Test P-Value : 5.915e-09      
  
              Sensitivity : 0.8714         
              Specificity : 0.2500         
           Pos Pred Value : 0.7305         
           Neg Pred Value : 0.4545         
               Prevalence : 0.7000         
           Detection Rate : 0.6100         
     Detection Prevalence : 0.8350         
        Balanced Accuracy : 0.5607         
  
         'Positive' Class : good
  </code></pre>
  
  <p>As you can notice, sensitivity and specificity are 0.87 and 0.25 respectively.  Compared with those from resamples output directly,  the numbers are totally different!! Same thing happens with other folds.</p>
  
  <p>Did I do something wrong? Or is caret doing something differently? Thanks.</p>