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

Ros*_*seS 5 r function r-caret

我“更新”了插入符号中的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
}
Run Code Online (Sandbox Code Playgroud)

然后是我的 trainControl 函数:

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

然后是我的模型函数:

modelSvm <- train(target ~ ., data = twoClassData, trControl = train_control, method = 'svmRadial')
Run Code Online (Sandbox Code Playgroud)

问题是当我运行此函数时,出现错误 Error in ctrl$summaryFunction(testOutput, lev, method) : Could not find function "requireNamespaceQuietStop"

即使我没有改变这部分功能。

如果我输入

twoClassSummary
Run Code Online (Sandbox Code Playgroud)

在控制台中,

环境:命名空间:插入符

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

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

提前致谢....

top*_*epo 4

该函数不是由 导出的caret,因此请改用caret::: requireNamespaceQuietStop(或使用used library)。