Error: attempt to apply non-function when using caret package

Error: attempt to apply non-function when using caret package

我想更多地了解 caret 软件包,运行 遇到一个我不确定如何解决的障碍。

#loading up libraries
library(MASS)
library(caret)
library(randomForest)
data(survey)
data<-survey

#create training and test set
split <- createDataPartition(data$W.Hnd, p=.8)[[1]]
train<-data[split,]
test<-data[-split,]


#creating training parameters
control <- trainControl(method = "cv",
                        number = 10, 
                        p =.8, 
                        savePredictions = TRUE, 
                        classProbs = TRUE, 
                        summaryFunction = "twoClassSummary")

#fitting and tuning model
tuningGrid <- data.frame(.mtry = floor(seq(1 , ncol(train) , length = 6)))
rf_tune <- train(W.Hnd ~ . , 
            data=train, 
            method = "rf" ,
            metric = "ROC",
            trControl = control)

不断出现错误:

Error in evalSummaryFunction(y, wts = weights, ctrl = trControl, lev = classLevels,  : 
  attempt to apply non-function

我已经确认我的 DV (W.Hnd) 是一个因子级别,因此 运行dom 森林适合用于分类。我的假设是 caret 不知道应用于 randomForest 算法?除此之外我不知道。

"twoClassSummary" 周围有引号,这使它成为一个字符向量。 R 试图将此作为函数应用,这导致了错误。

删除引号并重试您的脚本。这应该能够使函数 twoClassSummary 被正确调用。

#creating training parameters
control <- trainControl(method = "cv",
                        number = 10, 
                        p =.8, 
                        savePredictions = TRUE, 
                        classProbs = TRUE, 
                        summaryFunction = twoClassSummary)