requireNamespaceQuietStop 在自定义 summaryFunction 插入符中导致错误

requireNamespaceQuietStop causing errors in custom summaryFunction caret

我 'updated' 插入符号中的 twoClassSummary 函数使用插入符号函数包括阴性和阳性预测值:

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 函数:

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

然后是我的模型函数:

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

问题是当我 运行 这个函数时,我得到了错误 ctrl$summaryFunction(testOutput, lev, method) 错误: 找不到函数 "requireNamespaceQuietStop"

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

如果我输入

twoClassSummary

在控制台中,

环境:namespace:caret

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

1) 如何在函数中将 R 定向到此环境?和 2) accuracy 不是预定义的插入符号函数,关于如何将 accuracy 纳入此代码的任何建议?

提前致谢....

该函数未由 caret 导出,因此请改用 caret::: requireNamespaceQuietStop(或使用已用 library)。