title: missing values in object - Random Forest Confusion Matrix in R
body: <p>I'm trying to obtain the confusion matrix after a fitting a model with no success. Using the same code and decision tree, instead, there was no problem. That's my code:</p>
  
  <pre><code>library(caret)
  library(randomForest)
  
  training &lt;- read.csv("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv", na.strings=c("#DIV/0!"), row.names = 1)
  
  to_exclude &lt;- nearZeroVar(training)
  training &lt;- training[, -to_exclude]
  
  set.seed(1234)
  train_idx &lt;- createDataPartition(training$classe, p = 0.8, list = FALSE)
  train &lt;- training[train_idx,]
  validation &lt;- training[-train_idx,]
  
  rf_model &lt;- randomForest(classe ~ . , data=train, method="class")
  rf_validation &lt;- predict(rf_model, validation, type="class")
  
  confusionMatrix(rf_validation, validation$classe)
  </code></pre>
  
  <p>That's the error:</p>
  
  <blockquote>
    <p>Error in na.fail.default(list(classe = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  : 
      missing values in object</p>
  </blockquote>
  
  <p>I also try this:</p>
  
  <pre><code>table(rf_validation, validation$classe)
  </code></pre>
  
  <p>Which resulted in the same error.
  If I use:</p>
  
  <pre><code>dt_model &lt;- rpart(classe ~ ., data=train, method="class")
  </code></pre>
  
  <p>Instead, everything works fine.</p>
  
  <p>What am I missing?</p>