I 'updated' the twoClassSummary function in caret to include both the negative and positive predictive values using caret functions:
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
}
And then my trainControl function:
train_control <- trainControl(method = 'repeatedcv',
number = 10, repeats = 3,
summaryFunction = testfun,
classProbs = T,
savePredictions = T)
and then my model function:
modelSvm <- train(target ~ ., data = twoClassData, trControl = train_control, method = 'svmRadial')
The problem is when I run this function, I get the error Error in ctrl$summaryFunction(testOutput, lev, method) : could not find function "requireNamespaceQuietStop"
even though I haven't change that part of the function.
if I type
twoClassSummary
in the console,
environment: namespace:caret
appears (between < > signs) at the end after the function definition and I believe this is the source of my problems.
1) How do I direct R to this environment in the function? and 2) accuracy is not a predefined caret function, any suggestions on how to work accuracy into this code?
Thanks in advance....