Добавить точки на графике Случайный лес - PullRequest
0 голосов
/ 03 апреля 2019

Я использую Random Forest с пакетом caret , чтобы установить наилучшую mtry (количество факторов прогнозирования). Когда я строю модель, чтобы увидеть изменение среднеквадратичного отклонения в зависимости от mtry, я хочу добавить точку в лучшую 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

Точка положения:

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

24

min(rfmodel$results$RMSE)

2,972381

Я пытался добавить точку с этим кодом, но я мог

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

Модель можно найти здесь: https://drive.google.com/open?id=1tFFgxuCiJNC4PLMekBG7bgEziKGwMJmu

1 Ответ

2 голосов
/ 03 апреля 2019

Метод plot() в caret использует пакет lattice, а не базовую графику, поэтому lines не будет работать.

Вы можете легко получить результаты, используя метод ggplot, добавив новые слои графика. Вот два варианта:

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")

Создано в 2019-04-03 пакетом Представить (v0.2.1)

...