关于r:requireNamespaceQuietStop导致自定义summary中的错误

requireNamespaceQuietStop causing errors in custom summaryFunction caret

我使用插入符号函数"更新"了插入符号中的twoClassSummary函数以包括负和正预测值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
testfun <- 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")
  data$y = as.numeric(data$obs == lvls[2])
  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]),
                   # next 3 lines are my additions and modifications
                   negPredValue(data[,"obs"], data[,"pred"], lev[2]),
                   posPredValue(data[,"obs"], data[,"pred"], lev[1]))
  names(out) <- c("ROC","Sens","Spec","NPV","PPV")
  out
}

然后是我的trainControl函数:

1
2
3
4
5
train_control <- trainControl(method = 'repeatedcv',
                              number = 10, repeats = 3,
                              summaryFunction = testfun,
                              classProbs = T,
                              savePredictions = T)

然后是我的模型函数:

1
modelSvm <- train(target ~ ., data = twoClassData, trControl = train_control, method = 'svmRadial')

问题是当我运行此功能时,出现错误
ctrl $ summaryFunction(testOutput,lev,method)中的错误:
找不到函数" requireNamespaceQuietStop"

即使我没有更改函数的那部分。

如果我键入

1
twoClassSummary

在控制台中,

环境:命名空间:插入符

出现在函数定义之后(在<>符号之间),我相信这是问题的根源。

1)如何在函数中将R定向到此环境?和
2)精度不是预定义的插入符号功能,有关如何将精度工作到此代码中的任何建议?

谢谢。...


该功能不是由caret导出的,因此请改为使用caret::: requireNamespaceQuietStop(或使用已使用的library)。