【问题标题】:Selecting tuning parameters with caret using standard deviation of custom metric使用自定义指标的标准偏差选择带有插入符号的调整参数
【发布时间】:2016-04-28 01:21:14
【问题描述】:

我正在使用带有自定义拟合指标的插入符号,但我不仅需要最大化这个指标,还需要最大化它的置信区间的下限。所以我想最大化像mean(metric) - k * stddev(metric) 这样的东西。我知道如何手动执行此操作,但有没有办法告诉插入符号使用此功能自动选择最佳参数?

【问题讨论】:

  • 我已将此问题标记为离题,因此请将其迁移到 Stack Overflow

标签: r r-caret


【解决方案1】:

是的,您可以通过“trainControl”对象的“summaryFunction”参数定义自己的选择指标,然后使用调用train() 的“metric”参数定义自己的选择指标。这方面的详细信息在插入符号模型调整页面的“替代性能指标”部分中有很好的记录:http://caret.r-forge.r-project.org/training.html

我认为您没有为任何人提供足够的信息来准确地编写您正在寻找的内容,但这里是一个使用 twoClassSummary 函数中的代码的示例:

> library(caret)
> data(Titanic)
> 
> #an example custom function
> roc <- function (data, lev = NULL, model = NULL) {
+   require(pROC)
+   if (!all(levels(data[, "pred"]) == levels(data[, "obs"]))) 
+     stop("levels of observed and predicted data do not match")
+   rocObject <- try(pROC:::roc(data$obs, data[, lev[1]]), silent = TRUE)
+   rocAUC <- if (class(rocObject)[1] == "try-error") 
+     NA
+   else rocObject$auc
+   out <- c(rocAUC, sensitivity(data[, "pred"], data[, "obs"], lev[1]), specificity(data[, "pred"], data[, "obs"], lev[2]))
+   names(out) <- c("ROC", "Sens", "Spec")
+   out
+ }
> 
> #your train control specs
> tc <- trainControl(method="cv",classProb=TRUE,summaryFunction=roc)
> #yoru model with selection metric specificed
> train(Survived~.,data=data.frame(Titanic),method="rf",trControl=tc,metric="ROC")
32 samples
 4 predictors
 2 classes: 'No', 'Yes' 

No pre-processing
Resampling: Cross-Validation (10 fold) 

Summary of sample sizes: 28, 29, 30, 30, 28, 28, ... 

Resampling results across tuning parameters:

  mtry  ROC    Sens  Spec  ROC SD  Sens SD  Spec SD
  2     0.9    0.2   0.25  0.175   0.35     0.425  
  4     0.85   0.4   0.6   0.211   0.459    0.459  
  6     0.875  0.35  0.6   0.212   0.412    0.459  

ROC was used to select the optimal model using  the largest value.
The final value used for the model was mtry = 2. 

【讨论】:

  • 嗨,你能告诉我如何使用这个 summaryFunction 来选择仅基于特异性的分类器吗?我想要这个,因为我的积极事件数据非常罕见。
  • 只要使用metric="Spec"
【解决方案2】:

在插入符号的帮助中有更基本的例子:

madSummary <- function (data,
                        lev = NULL,
                        model = NULL) {
  out <- mad(data$obs - data$pred, 
             na.rm = TRUE)  
  names(out) <- "MAD"
  out
}

robustControl <- trainControl(summaryFunction = madSummary)
marsGrid <- expand.grid(degree = 1, nprune = (1:10) * 2)

earthFit <- train(medv ~ .,
                  data = BostonHousing, 
                  method = "earth",
                  tuneGrid = marsGrid,
                  metric = "MAD",
                  maximize = FALSE,
                  trControl = robustControl)

【讨论】:

    猜你喜欢
    • 2016-10-06
    • 2013-11-15
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 2014-08-21
    • 2021-10-16
    • 2016-07-20
    • 2017-03-19
    相关资源
    最近更新 更多