topepo / caret

caret (Classification And Regression Training) R package that contains misc functions for training and plotting classification and regression models
http://topepo.github.io/caret/index.html
1.61k stars 634 forks source link

Something is wrong; all the Accuracy metric values are missing #1329

Open Haitham-Hajjo opened 1 year ago

Haitham-Hajjo commented 1 year ago

Hi' I'm trying to implement random forest algorithm using the Caret package. In this code I'm trying to find the optimal mtry and ntree parameters for my data.

I run this code based on this tutorial: https://rpubs.com/phamdinhkhanh/389752

############# library(randomForest) library(mlbench) library(caret) library(e1071)

Load Dataset

data(Sonar) dataset <- Sonar x <- dataset[,1:60] y <- dataset[,61]

customRF <- list(type = "Classification", library = "randomForest", loop = NULL)

customRF$parameters <- data.frame(parameter = c("mtry", "ntree"), class = rep("numeric", 2), label = c("mtry", "ntree"))

customRF$grid <- function(x, y, len = NULL, search = "grid") {}

customRF$fit <- function(x, y, wts, param, lev, last, weights, classProbs) { randomForest(x, y, mtry = param$mtry, ntree=param$ntree) }

Predict label

customRF$predict <- function(modelFit, newdata, preProc = NULL, submodels = NULL) predict(modelFit, newdata)

Predict prob

customRF$prob <- function(modelFit, newdata, preProc = NULL, submodels = NULL) predict(modelFit, newdata, type = "prob")

customRF$sort <- function(x) x[order(x[,1]),] customRF$levels <- function(x) x$classes

control <- trainControl(method="repeatedcv", number=10, repeats=3,

allowParallel = TRUE

                    )

tunegrid <- expand.grid(.mtry=c(1:2),.ntree=c(1000,1500))

set.seed(123)

custom <- train(Class~., data=dataset, method=customRF, metric=metric, tuneGrid=tunegrid, trControl=control)

summary(custom) ###########

But I get this error message and I'm struggling to find out the reason:

Something is wrong; all the Accuracy metric values are missing: Accuracy Kappa
Min. : NA Min. : NA
1st Qu.: NA 1st Qu.: NA
Median : NA Median : NA
Mean :NaN Mean :NaN
3rd Qu.: NA 3rd Qu.: NA
Max. : NA Max. : NA
NA's :4 NA's :4
Error: Stopping In addition: Warning message: In nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, : There were missing values in resampled performance measures.

Thank you!

Canderson156 commented 1 year ago

I'm currently struggling with the same issue as well. There are many stack overflow pages addressing this, but none of them seem to have any advice that fixes my issue, or gets to the root of why it is happening in the first place. I find that I can have two models that are almost identical and one will have this error and the other won't. I will post a reproducible example soon.

@Haitham-Hajjo, when I ran your code, I got this error message:

Error in metric %in% c("RMSE", "Rsquared") : object 'metric' not found

I tried deleting that part and it ran fine for me.

custom <- train(Class~., data=dataset, method=customRF, tuneGrid=tunegrid, trControl=control)

summary(custom) Length Class Mode
call 5 -none- call
type 1 -none- character predicted 208 factor numeric
err.rate 3000 -none- numeric
confusion 6 -none- numeric
votes 416 matrix numeric
oob.times 208 -none- numeric
classes 2 -none- character importance 60 -none- numeric
importanceSD 0 -none- NULL
localImportance 0 -none- NULL
proximity 0 -none- NULL
ntree 1 -none- numeric
mtry 1 -none- numeric
forest 14 -none- list
y 208 factor numeric
test 0 -none- NULL
inbag 0 -none- NULL
xNames 60 -none- character problemType 1 -none- character tuneValue 2 data.frame list
obsLevels 2 -none- character param 0 -none- list