使用插入符号使用标准偏差选择调整参数

时间:2013-05-21 13:28:27

标签: r r-caret

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

2 个答案:

答案 0 :(得分:4)

是的,您可以通过" summaryFunction"定义您自己的选择指标。您的" trainControl"的参数对象,然后使用" metric"您对train()的调用参数。关于此的详细信息在"备选绩效指标"关于插入符号模型调整页面的部分: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. 

答案 1 :(得分:0)

在插入符号帮助列车功能方面有更多基本示例:

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)