插入符号ref + gbm + ROC

时间:2014-04-29 14:22:39

标签: r roc r-caret gbm

我正在尝试使用插入符号包中的rfe函数,但我不能使用ROC指标使其适用于gbm模型。

我在那里找到了一些见解:

Feature Selection in caret rfe + sum with ROC

http://www.cybaea.net/Blogs/Feature-selection-Using-the-caret-package.html

我结束了这段代码:

gbmFuncs <- treebagFuncs
gbmFuncs$fit <- function (x, y, first, last, ...) {
  library("gbm")
  n.levels <- length(unique(y))
  if ( n.levels == 2 ) {
    distribution = "bernoulli"
  } else {
    distribution = "gaussian"
  }
  gbm.fit(x, y, distribution = distribution, ...)
}
gbmFuncs$pred <- function (object, x) {
  n.trees <- suppressWarnings(gbm.perf(object,
                                       plot.it = FALSE,
                                       method = "OOB"))
  if ( n.trees <= 0 ) n.trees <- object$n.trees
  predict(object, x, n.trees = n.trees, type = "link")
}

control <- rfeControl(functions = gbmFuncs, method = "cv", verbose = TRUE, returnResamp="final", 
                  number = 5)
trainctrl <- trainControl(classProbs= TRUE,
                          summaryFunction = twoClassSummary)

gbmFit_bernoulli_sel <- rfe(data_model[x, -as.numeric(y)+2,
                            sizes=c(10, 15, 20, 30, 40, 50), rfeControl = control, verbose = FALSE,
                        interaction.depth = 14, n.trees = 10000, shrinkage = .01, metric="ROC", 
                        trControl = trainctrl)

但是我收到了这个错误:

Error in { : 
  task 1 failed - "argument inutilisé (trControl = list(method = "boot", number = 25, repeats = 25, p = 0.75, initialWindow = NULL, horizon = 1, fixedWindow = TRUE, verboseIter = FALSE, returnData = TRUE, returnResamp = "final", savePredictions = FALSE, classProbs = TRUE, summaryFunction = 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

修改

使用此代码:

caretFuncs$summary <- twoClassSummary
controlrfe <- rfeControl(functions = caretFuncs, method = "cv", number = 3, verbose = TRUE)
gbmGrid <- expand.grid(interaction.depth = 5, n.trees = 1000, shrinkage = .01)
confroltrain <- trainControl(method = "none", classProbs=T, summaryFunction =     twoClassSummary, verbose = TRUE)
gbmFit_bernoulli_sel <- rfe(data_model[,-ncol(data_model)], data_model[,ncol(data_model)],
                            sizes=c(10,15), rfeControl = controlrfe, metric="ROC",
                            trControl = confroltrain, tuneGrid=gbmGrid, method="gbm")

我必须使用train函数,因为当我使用gbmFuncs时,我显然遇到了一些问题,因为gbm.fit需要一个数字目标变量,但ROC度量评估需要一个因子。

谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您正试图将trControl传递给gbm.fit。连接(三个)点=]

尝试删除trControl = trainctrl

最高