gpt4 book ai didi

r - 错误: attempt to apply non-function when using caret package

转载 作者:行者123 更新时间:2023-11-30 08:51:38 26 4
gpt4 key购买 nike

我试图更多地了解 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) 是一个因子水平,因此随机森林适合用于分类。我的假设是 caret 不知道适用于 randomForest 算法?除此之外我不知道。

最佳答案

您在“twoClassSummary”周围有引号,这使其成为字符向量。 R 试图将其作为函数应用,这导致了错误。

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

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

关于r - 错误: attempt to apply non-function when using caret package,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32020965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com