0

I am training a glmnet regularized logistic regression model using Caret's trainControl and train functions as follows using metric= "ROC" and get the following error:

> ctrl_s10_2class <- trainControl(method = "repeatedcv", number = 10, repeats = 10 , savePredictions = TRUE, classProbs = TRUE)
> model_train_glmnet_s10_2class <- train(Class ~ ZCR + Energy + SpectralC + SpectralS + SpectralE + SpectralF + SpectralR + MFCC1 + MFCC2 + MFCC3 + MFCC4 + MFCC5 + MFCC6 + MFCC7 + MFCC8 + MFCC9 + MFCC10 + MFCC11 + MFCC12 + MFCC13, data = training_s10_2class, method="glmnet", trControl = ctrl_s10_2class, metric = "ROC")

Error in evalSummaryFunction(y, wts = weights, ctrl = trControl, lev =  classLevels,  : 

train()'s use of ROC codes requires class probabilities. See the classProbs option of trainControl()

In addition: Warning messages:
1: In train.default(x, y, weights = w, ...) :
You are trying to do regression and your outcome only has two possible values Are you trying to do classification? If so, use a 2 level factor as your outcome column.
2: In train.default(x, y, weights = w, ...) :
cannnot compute class probabilities for regression

But I have already turned classProbs = TRUE on in trainControl function. Also, to address the warning messages, I figured I have to relevel my 2 class data which I did to find this error:

> sensor6data_s10_2class <- within(sensor6data_s10_2class, Class <- as.factor(Class))
> sensor6data_s10_2class$Class2 <- relevel(sensor6data_s10_2class$Class,ref="1")
> model_train_glmnet_s10_2class <- train(Class2 ~ ZCR + Energy + SpectralC + SpectralS + SpectralE + SpectralF + SpectralR + MFCC1 + MFCC2 + MFCC3 + MFCC4 + MFCC5 + MFCC6 + MFCC7 + MFCC8 + MFCC9 + MFCC10 + MFCC11 + MFCC12 + MFCC13, data = training_s10_2class, method="glmnet", trControl = ctrl_s10_2class, metric = "ROC")

Error in train.default(x, y, weights = w, ...) : 
At least one of the class levels is not a valid R variable name; This will cause errors when class probabilities are generated because the variables names will be converted to  X1, X0 . Please use factor levels that can be used as valid R variable names  (see ?make.names for help).

Any help to fix this with or without releveling is greatly appreciated! Thanks.

milos.ai
  • 3,882
  • 7
  • 31
  • 33
trickymaverick
  • 199
  • 1
  • 3
  • 8

2 Answers2

0

1: In train.default(x, y, weights = w, ...) : You are trying to do regression and your outcome only has two possible values Are you trying to do classification? If so, use a 2 level factor as your outcome column.

Seems like an error with the form of data used. You could try converting it to a factor:

training_s10_2class$Class2 = as.factor(training_s10_2class$Class2)

With that, you no longer need

classProbs = TRUE

When you remove it, it should take care of your second warning

2: In train.default(x, y, weights = w, ...) : cannnot compute class probabilities for regression

C Khoo
  • 23
  • 4
0

Based on the following error you get:

Error: Metric Accuracy not applicable for regression models
In addition: Warning message:
In train.default(x, y, weights = w, ...) :

It appears you are trying to do estimate a regression model where your outcome only has two possible values. Are you trying to do classification? If so, use a 2 level factor as your outcome column.

C.Robin
  • 1,085
  • 1
  • 10
  • 23