いいえ、twoClassSummary
は、わずか0.5の標準閾値の値を返し、0と1の間のすべての閾値を評価しません。 twoClassSummary
は次のように定義されて
:私の文を確認するには
function (data, lev = NULL, model = NULL)
{
lvls <- levels(data$obs)
if (length(lvls) > 2)
stop(paste("Your outcome has", length(lvls), "levels. The twoClassSummary() function isn't appropriate."))
requireNamespaceQuietStop("ModelMetrics")
if (!all(levels(data[, "pred"]) == lvls))
stop("levels of observed and predicted data do not match")
rocAUC <- ModelMetrics::auc(ifelse(data$obs == lev[2], 0,
1), data[, lvls[1]])
out <- c(rocAUC, sensitivity(data[, "pred"], data[, "obs"],
lev[1]), specificity(data[, "pred"], data[, "obs"], lev[2]))
names(out) <- c("ROC", "Sens", "Spec")
out
}
、私はexcplicitly 0.5にしきい値を設定し、その両方の値がスペック(オリジナルの特異性を参照するカスタムsummaryFunction
と、次の例を試してみてくださいtwoClassSummary)とSPEC2(手動で0.5に設定された閾値との特異性)によって報告はまったく同じになります。また
# load libraries
library(caret)
library(mlbench)
# load the dataset
data(PimaIndiansDiabetes)
# define custom summaryFunction
customSummary <- function (data, lev = NULL, model = NULL){
spec <- specificity(data[, "pred"], data[, "obs"], lev[2])
pred <- factor(ifelse(data[, "neg"] > 0.5, "neg", "pos"))
spec2 <- specificity(pred, data[, "obs"], "pos")
out <- c(spec, spec2)
names(out) <- c("Spec", "Spec2")
out
}
# prepare resampling method
control <- trainControl(method="cv",
number=5,
classProbs=TRUE,
summaryFunction=customSummary)
set.seed(7)
fit <- train(diabetes~.,
data=PimaIndiansDiabetes,
method="rpart",
tuneLength= 5,
metric="Spec",
trControl=control)
print(fit)
CART
768 samples
8 predictor
2 classes: 'neg', 'pos'
No pre-processing
Resampling: Cross-Validated (5 fold)
Summary of sample sizes: 615, 615, 614, 614, 614
Resampling results across tuning parameters:
cp Spec Spec2
0.01305970 0.5749825 0.5749825
0.01492537 0.5411600 0.5411600
0.01741294 0.5596785 0.5596785
0.10447761 0.4932215 0.4932215
0.24253731 0.2837177 0.2837177
Spec was used to select the optimal model using the largest value.
The final value used for the model was cp = 0.0130597.
、あなたが設定するための最大の特異性を計算するためにキャレットたい場合任意のしきい値のハイパーパラメータの値を報告し、その値を報告する場合は、0.05のステップで0.1から0.95のすべてのしきい値を試して、次のようなcustom summaryFunctionを定義できます。
# define custom summaryFunction
customSummary <- function (data, lev = NULL, model = NULL){
spec <- specificity(data[, "pred"], data[, "obs"], lev[2])
pred <- factor(ifelse(data[, "neg"] > 0.5, "neg", "pos"))
spec2 <- specificity(pred, data[, "obs"], "pos")
speclist <- as.numeric()
for(i in seq(0.1, 0.95, 0.05)){
predi <- factor(ifelse(data[, "neg"] > i, "neg", "pos"))
singlespec <- specificity(predi, data[, "obs"], "pos")
speclist <- c(speclist, singlespec)
}
max(speclist) -> specmax
out <- c(spec, spec2, specmax)
names(out) <- c("Spec", "Spec2", "Specmax")
out
}