I'm trying to understand a bit more about the caret package and ran into a roadblock that I'm unsure how to resolve.
#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)
keep getting an error:
Error in evalSummaryFunction(y, wts = weights, ctrl = trControl, lev = classLevels, :
attempt to apply non-function
I have confirmed that my DV (W.Hnd) is a factor level so a random forest would be appropriate to use for classification. My assumption is that caret doesn't know to apply to the randomForest algorithm? Other than that I have no idea.
You have quotes around "twoClassSummary", which makes this a character vector. R is trying to apply this as a function, which is causing the error.
Remove the quotes and try your script again. This should enable the function twoClassSummary to be called correctly.
#creating training parameters
control <- trainControl(method = "cv",
number = 10,
p =.8,
savePredictions = TRUE,
classProbs = TRUE,
summaryFunction = twoClassSummary)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With