title: subscript out of bounds Error, Random Forest Model
body: <p>I'm trying to use the random forest model to predict Gender based on Height, Weight and Number of siblings. I've gotten the data from a much larger data set that contains dozens of variables, but I've cleaned it into this "clean" data.frame with omitted NA values and only the 4 variables I care about, the last column being Gender.</p>
  
  <p>I've tried fiddling with the code and searching everywhere but I can't find a concrete fix.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>ind &lt;- sample(nrow(clean),0.8*nrow(clean))
  train &lt;- clean[ind,]
  test &lt;- clean[-ind,]
  
  
  rf &lt;- randomForest(Gender ~ ., data = train[,1:4], ntree = 20)
  
  pred &lt;- predict(rf, newdata = test[,-c(length(test))])
  cm &lt;- table(test$Gender, pred)
  cm
  </code></pre>
  
  <p>and here's the output:</p>
  
  <pre><code>Error in `[.default`(table(observed = y, predicted = out.class), levels(y), : subscript out of bounds
  Traceback:
  
  1. randomForest(Gender ~ ., data = train[, 1:4], ntree = 20)
  2. randomForest.formula(Gender ~ ., data = train[, 1:4], ntree = 20)
  3. randomForest.default(m, y, ...)
  4. table(observed = y, predicted = out.class)[levels(y), levels(y)]
  5. `[.table`(table(observed = y, predicted = out.class), levels(y), 
   .     levels(y))
  6. NextMethod()
  </code></pre>