r - 在随机森林 mtry 图中添加点
问题描述
我正在使用带有插入符号包的随机森林来设置最佳 mtry(预测因子的数量)。当我绘制模型以查看 RMSE 与 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")
定位点:
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
解决方案
中的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")
由reprex 包(v0.2.1)于 2019 年 4 月 3 日创建
推荐阅读
- apache-spark - 读入火花时将字符串列转换为时间戳
- puppeteer - Puppeteer 的流类型定义阻止使用默认导出?
- java - 在Java中每X秒发送一次UDP消息
- java - 使用 SingleColumnValueFilter 在 hbase 过滤器中不比较 int 值
- cron - 无法使用指定时间的 cron 包执行 cron 作业
- ionic4 - 当离子选择器打开时,各个列项目都堆叠在一起
- python-3.x - 检测数据框中的文件结尾(列和行)并删除所有额外的
- c++ - 声明数组时,它们的内存地址有什么值
- c++ - 当屏幕缩放不是 100% 时,将 GDI+ 图元文件保存为 BMP 裁剪
- java - 从 Excel 中的表格生成图形