title: Different results with randomForest() and caret's randomForest (method = "rf")
body: <p>I am new to caret, and I just want to ensure that I fully understand what it’s doing. Towards that end, I’ve been attempting to replicate the results I get from a randomForest() model using caret’s train() function for method="rf". Unfortunately, I haven’t been able to get matching results, and I’m wondering what I’m overlooking.</p>
  
  <p>I’ll also add that given that randomForest uses bootstrapping to generate samples to fit each of the ntrees, and estimates error based on out-of-bag predictions, I’m a little fuzzy on the difference between specifying "oob" and "boot" in the trainControl function call. These options generate different results, but neither matches the randomForest() model.</p>
  
  <p>Although I’ve read the caret Package website (<a href="http://topepo.github.io/caret/index.html" rel="noreferrer">http://topepo.github.io/caret/index.html</a>), as well as various StackOverflow questions that seem potentially relevant, but I haven’t been able to figure out why the caret method = "rf" model produces different results from randomForest(). Thank you very much for any insight you might be able to offer.</p>
  
  <p>Here’s a replicable example, using the CO2 dataset from the MASS package.</p>
  
  <pre><code>library(MASS)
  data(CO2)
  
  library(randomForest)
  set.seed(1)
  rf.model &lt;- randomForest(uptake ~ ., 
                         data = CO2,
                         ntree = 50,
                         nodesize = 5,
                         mtry=2,
                         importance=TRUE, 
                         metric="RMSE")
  
  library(caret)
  set.seed(1)
  caret.oob.model &lt;- train(uptake ~ ., 
                       data = CO2,
                       method="rf",
                       ntree=50,
                       tuneGrid=data.frame(mtry=2),
                       nodesize = 5,
                       importance=TRUE, 
                       metric="RMSE",
                       trControl = trainControl(method="oob"),
                       allowParallel=FALSE)
  
  set.seed(1)
  caret.boot.model &lt;- train(uptake ~ ., 
                       data = CO2,
                       method="rf",
                       ntree=50,
                       tuneGrid=data.frame(mtry=2),
                       nodesize = 5,
                       importance=TRUE, 
                       metric="RMSE",
                       trControl=trainControl(method="boot", number=50),
                       allowParallel=FALSE)
  
   print(rf.model)
   print(caret.oob.model$finalModel) 
   print(caret.boot.model$finalModel)
  </code></pre>
  
  <p>Produces the following:</p>
  
  <blockquote>
    <p>print(rf.model) </p>
  </blockquote>
  
  <pre><code>      Mean of squared residuals: 9.380421
                  % Var explained: 91.88
  </code></pre>
  
  <blockquote>
    <p>print(caret.oob.model$finalModel) </p>
  </blockquote>
  
  <pre><code>      Mean of squared residuals: 38.3598
                  % Var explained: 66.81
  </code></pre>
  
  <blockquote>
    <p>print(caret.boot.model$finalModel)</p>
  </blockquote>
  
  <pre><code>      Mean of squared residuals: 42.56646
                  % Var explained: 63.16
  </code></pre>
  
  <p>And the code to look at variable importance:</p>
  
  <pre><code>importance(rf.model)
  
  importance(caret.oob.model$finalModel)
  
  importance(caret.boot.model$finalModel)
  </code></pre>