r - 插入符号中的折叠与训练错误

标签 r r-caret

在模型调整中使用交叉验证,我从 caret::trainresults 对象获得不同的错误率,并在其 pred 对象上自己计算错误。我想了解它们为何不同,以及理想情况下如何使用折叠错误率进行模型选择、绘制模型性能等。
pred 对象包含折叠预测。文档很清楚 trainControl(..., savePredictions = "final") 保存了最佳超参数值的折叠预测:“每个重采样的保留预测应该保存多少的指标......“最终”保存了最佳超参数的预测调整参数。” (保留“所有”预测然后过滤到最佳调整值并不能解决问题。)
train 文档说 results 对象是“训练错误率的数据框...”我不确定这意味着什么,但最佳行的值始终不同于在 pred 上计算的指标。为什么它们不同,我怎样才能让它们对齐?

d <- data.frame(y = rnorm(50))
d$x1 <- rnorm(50, d$y)
d$x2 <- rnorm(50, d$y)
train_control <- caret::trainControl(method = "cv",
                                     number = 4,
                                     search = "random",
                                     savePredictions = "final")
m <- caret::train(x = d[, -1],
                     y = d$y,
                     method = "ranger",
                     trControl = train_control,
                     tuneLength = 3)
#> Loading required package: lattice
#> Loading required package: ggplot2
m
#> Random Forest 
#> 
#> 50 samples
#>  2 predictor
#> 
#> No pre-processing
#> Resampling: Cross-Validated (4 fold) 
#> Summary of sample sizes: 38, 36, 38, 38 
#> Resampling results across tuning parameters:
#> 
#>   min.node.size  mtry  splitrule   RMSE       Rsquared   MAE      
#>   1              2     maxstat     0.5981673  0.6724245  0.4993722
#>   3              1     extratrees  0.5861116  0.7010012  0.4938035
#>   4              2     maxstat     0.6017491  0.6661093  0.4999057
#> 
#> RMSE was used to select the optimal model using the smallest value.
#> The final values used for the model were mtry = 1, splitrule =
#>  extratrees and min.node.size = 3.
MLmetrics::RMSE(m$pred$pred, m$pred$obs)
#> [1] 0.609202
MLmetrics::R2_Score(m$pred$pred, m$pred$obs)
#> [1] 0.642394

reprex package (v0.2.0) 于 2018 年 4 月 9 日创建。

最佳答案

交叉验证的 RMSE 不是按照您显示的方式计算的,而是针对每个折叠然后取平均值。完整示例:

set.seed(1)
d <- data.frame(y = rnorm(50))
d$x1 <- rnorm(50, d$y)
d$x2 <- rnorm(50, d$y)
train_control <- caret::trainControl(method = "cv",
                                     number = 4,
                                     search = "random",
                                     savePredictions = "final")
set.seed(1)
m <- caret::train(x = d[, -1],
                  y = d$y,
                  method = "ranger",
                  trControl = train_control,
                  tuneLength = 3)
#output
Random Forest 

50 samples
 2 predictor

No pre-processing
Resampling: Cross-Validated (4 fold) 
Summary of sample sizes: 37, 38, 37, 38 
Resampling results across tuning parameters:

  min.node.size  mtry  splitrule   RMSE       Rsquared   MAE      
   8             1     extratrees  0.6106390  0.4360609  0.4926629
  12             2     extratrees  0.6156636  0.4294237  0.4954481
  19             2     variance    0.6472539  0.3889372  0.5217369

RMSE was used to select the optimal model using the smallest value.
The final values used for the model were mtry = 1, splitrule = extratrees and min.node.size = 8.

最佳模型的 RMSE 是 0.6106390
现在计算每个折叠和平均值的 RMSE:
m$pred %>%
  group_by(Resample) %>%
  mutate(rmse = caret::RMSE(pred, obs)) %>%
  summarise(mean = mean(rmse)) %>%
  pull(mean) %>%
  mean
#output
0.610639

m$pred %>%
  group_by(Resample) %>%
  mutate(rmse = MLmetrics::RMSE(pred, obs)) %>%
  summarise(mean = mean(rmse)) %>%
  pull(mean) %>%
  mean
#output
0.610639

关于r - 插入符号中的折叠与训练错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49743428/

相关文章:

R:将 data.frame 中的 NA 替换为另一个 dataframe 中相同位置的值

R-日期时间变量在 ifelse 后丢失格式

r - 插入符 - 调整 : Models' default parameters

R 将多个虚拟变量列合并为 1

r - 在 caret train() 中指定结果变量的正类

r - 使用 write.table 时避免在列名和行名中使用引号

xml - 使用XML和R有效地获取具有特定名称的子代数

R:如何使 switch 语句失效

R - Caret - 在模型训练中使用 ROC 而不是准确性

R: 对由 caret 包构建的逻辑模型进行 car::Anova type II 和 III 测试