我正在使用带有自定义拟合指标的插入符号,但我不仅需要最大化这个指标,还需要最大化它的置信区间的下限。所以我想最大化像mean(metric) - k * stddev(metric)
. 我知道如何手动执行此操作,但有没有办法告诉插入符号使用此功能自动选择最佳参数?
slonopotam
问问题
5203 次
2 回答
5
是的,您可以通过“trainControl”对象的“summaryFunction”参数定义自己的选择指标,然后使用调用的“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.
于 2013-05-31T21:42:08.443 回答
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)
于 2015-03-27T09:25:25.877 回答