如何为每个调整参数绘制误差线?

时间:2018-02-21 04:25:26

标签: r r-caret

训练模型后,我想了解列车RMSE的方差。一种方法是将误差条添加到调整参数与交叉验证误差的图中。

如果我的训练方法有train,我可以向trControl图添加误差线吗?我必须首先获得model$results手动执行此操作吗?下面显示的是一个最小的工作示例。我训练一个模型,并希望为每个调整参数绘制误差条。

library(tidyverse)
library(ElemStatLearn)
library(caret)


data(prostate)

dtrain = prostate %>% filter(train) %>% select(-train)
dtest = prostate %>% filter(!train) %>% select(-train)



cv = trainControl(method = 'cv', number = 10)

set.seed(0)
model = train(lpsa ~ .,
              data = dtrain,
              method = 'pcr',
              trControl = cv,
              tuneLength = 8)

#No Error bars even though 10-fold CV?
plot(model)

1 个答案:

答案 0 :(得分:0)

您可以使用model$results对象手动绘制误差线:

所需数据位于 ncomp RMSE Rsquared MAE RMSESD RsquaredSD MAESD 1 1 1.0354695 0.3532009 0.8847237 0.3973763 0.2996081 0.3558122 2 2 1.0571604 0.3546154 0.8983677 0.4268048 0.2884902 0.3682752 3 3 1.0546954 0.3446711 0.8945127 0.4429895 0.3191984 0.4101867 4 4 0.8761915 0.5676269 0.7259782 0.2582520 0.1701669 0.2354457 5 5 0.8251083 0.6212784 0.6755912 0.2087671 0.1721623 0.1595105 6 6 0.7956971 0.6377419 0.6539849 0.1827476 0.1400881 0.1433317 7 7 0.7822073 0.6789155 0.6330582 0.1834283 0.1413476 0.1636086 library(ggplot2) ggplot(model$results) + geom_line(aes(x = ncomp, y = RMSE)) + geom_point(aes(x = ncomp, y = RMSE)) + geom_errorbar(aes(x = ncomp, ymin = RMSE - RMSESD, ymax = RMSE + RMSESD), width = 0.3)+ theme_bw()

LPSTR

enter image description here

如果使用了多个调整参数,您可以将第一个参数映射到x轴,第二个参数映射到颜色,第三个参数映射到构面网格。