title: R: Confusion matrix in RF model returns error: data` and `reference` should be factors with the same levels
body: <p>I am new player in R and want to solve binary classification task.</p>
  
  <p>Dataset has factor variable LABELS with 2 classes: first - 0, second - 1. The next image shows actual head of it:
  <a href="https://i.stack.imgur.com/HViMR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HViMR.png" alt="unbalanced_dataset"></a>
  TimeDate column - it's just index.
  Class distribution is defined as:</p>
  
  <pre><code>print("the number of values with % in factor variable - LABELS:")
  percentage &lt;- prop.table(table(dataset$LABELS)) * 100
  cbind(freq=table(dataset$LABELS), percentage=percentage)
  </code></pre>
  
  <p>Result of class distribution:
  <a href="https://i.stack.imgur.com/hkMVw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hkMVw.png" alt="classes"></a></p>
  
  <p>Also I know that Slot2 column is calculated based on formula:</p>
  
  <pre><code>Slot2 = Var3 - Slot3 + Slot4
  </code></pre>
  
  <p>The features Var1,Var2,Var3,Var4 were selected after analysis the correlation matrix. </p>
  
  <p>Before start the modeling i divided dataset to train and test parts.
  I tried to build Random forest Model for binary classification task used the next code:</p>
  
  <pre><code>rf2 &lt;- randomForest(LABELS ~ Var1 + Var2  + Var3 + Var4, 
                      data=train, ntree = 100,
                      mtry = 4, importance = TRUE)
  print(rf2)
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>  Call:
       randomForest(formula = LABELS ~ Var1 + Var2  + Var3 + Var4,
       data = train, ntree = 100,      mtry = 4, importance = TRUE) 
  
   Type of random forest: classification
   Number of trees: 100
   No. of variables tried at each split: 4
  
   OOB estimate of  error rate: 0.16%
  
   Confusion matrix:
             0      1 class.error
      0 164957    341 0.002062941
      1    280 233739 0.001196484
  </code></pre>
  
  <p>When I tried to do predict:</p>
  
  <pre><code># Prediction &amp; Confusion Matrix - train data
  p1 &lt;- predict(rf2, train, type="prob")
  print("Prediction &amp; Confusion Matrix - train data")
  confusionMatrix(p1, train$LABELS)
  
  # # Prediction &amp; Confusion Matrix - test data
  p2 &lt;- predict(rf2, test, type="prob")
  print("Prediction &amp; Confusion Matrix - test data")
  confusionMatrix(p2, test$LABELS)
  </code></pre>
  
  <p>I received an error in R: </p>
  
  <pre><code>[1] "Prediction &amp; Confusion Matrix - train data"
  Error: `data` and `reference` should be factors with the same levels.
  Traceback:
  
  1. confusionMatrix(p1, train$LABELS)
  2. confusionMatrix.default(p1, train$LABELS)
  3. stop("`data` and `reference` should be factors with the same levels.", 
   .     call. = FALSE)
  </code></pre>
  
  <p>Also I have already tried to fix it by using idea from the following questions:</p>
  
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/24801452/error-in-confusionmatrix-the-data-and-reference-factors-must-have-the-same-numbe">Error in ConfusionMatrix the data and reference factors must have the same number of levels R CARET</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/30002013/error-in-confusion-matrix-the-data-and-reference-factors-must-have-the-same-nu/30147711">Error in Confusion Matrix : the data and reference factors must have the same number of levels</a></p></li>
  </ol>
  
  <p>but it doesn't help in my case.</p>
  
  <p>Could you please help me with this error?</p>
  
  <p>I'll be appreciate for any ideas and comments.Thank you in advance.</p>