title: how to plot RMSE vs number of trees tries in bagging when using train() and cross validation in r
body: <p>I am studying this website about bagging method. <a href="https://bradleyboehmke.github.io/HOML/bagging.html" rel="nofollow noreferrer">https://bradleyboehmke.github.io/HOML/bagging.html</a></p>
  <p>I am going to use train() function with cross validation for bagging. something like below. <br>
  as far as I realized nbagg=200 tells r to try 200 trees, calculate RMSE for each and return the number of trees ( here 80 ) for which the best RMSE is achieved. <br>
  now how can I see what RMSE other nbagg values have produced in this model. like RMSE vs number of trees plot in that website ( begore introdicing cv method and train() function like plot below)</p>
  <pre><code>ames_bag2 &lt;- train(
    Sale_Price ~ .,
    data = ames_train,
    method = &quot;treebag&quot;,
    trControl = trainControl(method = &quot;cv&quot;, number = 10),
    nbagg = 200,  
    control = rpart.control(minsplit = 2, cp = 0)
  )
  ames_bag2
  ## Bagged CART 
  ## 
  ## 2054 samples
  ##   80 predictor
  ## 
  ## No pre-processing
  ## Resampling: Cross-Validated (10 fold) 
  ## Summary of sample sizes: 1849, 1848, 1848, 1849, 1849, 1847, ... 
  ## Resampling results:
  ## 
  ##   RMSE      Rsquared   MAE     
  ##   26957.06  0.8900689  16713.14
  </code></pre>
  <p><a href="https://i.stack.imgur.com/5xD8f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5xD8f.png" alt="enter image description here" /></a></p>