0
votes

I am using Random Forest with caret package to set the best mtry (number of prediction factors). When I plot the model to the see the variation of the RMSE with the mtry, I want to add a point into the best mtry

ctrl <- trainControl(method = "cv", savePred=T, number = 10)
tunegrid <- expand.grid(.mtry=seq(from=2,to=nlayers(covs_processed),by=2))
# Search for the best mtry parameter
rfmodel <- train(fm, data=dat_1963@data, method = "rf", trControl = ctrl,
                 importance=TRUE, tuneGrid=tunegrid)
plot(rfmodel,main= "Tuning RF 2018")

enter image description here

Position point:

rfmodel[11][[1]]$tuneValue[[1]]

24

min(rfmodel$results$RMSE)

2.972381

I tried adding the point with this code but I could

points(rfmodel[11][[1]]$tuneValue[[1]],min(rfmodel$results$RMSE),col="red")

The model can be found here: https://drive.google.com/open?id=1tFFgxuCiJNC4PLMekBG7bgEziKGwMJmu

1
I trust this is a duplicate of stackoverflow.com/questions/31128235/…. Try the accepted solution if it works then this is a duplicate, if not I will attempt to solve the issue and answer. In any case it would be nice if a reproducible example was provided in the post with an inbuilt data set.missuse
I disagree about the duplication. This is a question about how to do this in relation to the data structure of this particular object type.topepo

1 Answers

2
votes

The plot() method in caret uses the lattice package and not base graphics so lines worn't work.

You can easily get results using the ggplot method by adding new plot layers. Here's two options:

library(caret)
#> Loading required package: lattice
#> Loading required package: ggplot2
data(BloodBrain)

theme_set(theme_bw())

ctrl <- trainControl(method = "cv", number = 10, returnResamp = "all")
set.seed(214)
rfmodel <-
  train(
    x = bbbDescr, y = logBBB,
    method = "rf",
    preProc = "zv",
    trControl = ctrl,
    tuneGrid = data.frame(mtry = 1:10)
  )

# rfmodel$resample contains the individual RMSE values per fold. Use the
# ggplot method and add another layer with those points. 

ggplot(rfmodel) + 
  geom_point(data = rfmodel$resample, alpha = .3)

# or add them as colored lines 
ggplot(rfmodel) + 
  geom_line(
    data = rfmodel$resample, 
    # For each resample, plot a different colored line
    aes(group = Resample, col = Resample),
    alpha = .3) + 
  # the legend here gets huge so stop it from being printed
  theme(legend.position = "none")

Created on 2019-04-03 by the reprex package (v0.2.1)