title: Error in Confusion Matrix : the data and reference factors must have the same number of levels
body: <p>I've trained a Linear Regression model with R caret. I'm now trying to generate a confusion matrix and keep getting the following error:</p>
  
  <p>Error in confusionMatrix.default(pred, testing$Final) : 
  the data and reference factors must have the same number of levels</p>
  
  <pre><code>EnglishMarks &lt;- read.csv("E:/Subject Wise Data/EnglishMarks.csv", 
  header=TRUE)
  inTrain&lt;-createDataPartition(y=EnglishMarks$Final,p=0.7,list=FALSE)
  training&lt;-EnglishMarks[inTrain,]
  testing&lt;-EnglishMarks[-inTrain,]
  predictionsTree &lt;- predict(treeFit, testdata)
  confusionMatrix(predictionsTree, testdata$catgeory)
  modFit&lt;-train(Final~UT1+UT2+HalfYearly+UT3+UT4,method="lm",data=training)
  pred&lt;-format(round(predict(modFit,testing)))              
  confusionMatrix(pred,testing$Final)
  </code></pre>
  
  <p>The error occurs when generating the confusion matrix. The levels are the same on both objects. I cant figure out what the problem is. Their structure and levels are given below. They should be the same. Any help would be greatly appreciated as its making me cracked!!</p>
  
  <pre><code>&gt; str(pred)
  chr [1:148] "85" "84" "87" "65" "88" "84" "82" "84" "65" "78" "78" "88" "85"  
  "86" "77" ...
  &gt; str(testing$Final)
  int [1:148] 88 85 86 70 85 85 79 85 62 77 ...
  
  &gt; levels(pred)
  NULL
  &gt; levels(testing$Final)
  NULL
  </code></pre>