Class Probabilities Using ‘glmnet’ in Caret Package – How to Compute

caretglmnetmodelingr

If i do a modelLookup('glmnet') it says TRUE for probModel (and in fact, I'd expect it to be usable as a model to predict probabilities in a binary outcome prediction problem as glmnet has a 'binomial' family argument).

However, following the instructions from the caret package I say:

trainControl = trainControl(classProbs=TRUE)

modelFit = train(X, y, method='glmnet', trControl=trainControl)

and I get:

cannot compute class probabilities for regression

Am I doing something wrong?

Best Answer

I suspect your y is of class numeric and is not an R factor. You can look at the documentation for glmnet directly,

   y: response variable. Quantitative for ‘family="gaussian"’ or
      ‘family="poisson"’ (non-negative counts). For
      ‘family="binomial"’ should be either a **factor with two
      levels, or a two-column matrix of counts or proportions**.

(emphasize is mine.)

or check it with the following toy example:

library(caret)
data(iris)
iris.sub <- subset(iris, Species %in% c("setosa", "versicolor"))
train(iris.sub[,1:4], factor(iris.sub$Species), method='glmnet', 
      trControl=trainControl(classProbs=TRUE))  # work
train(iris.sub[,1:4], as.numeric(iris.sub$Species), method='glmnet', 
      trControl=trainControl(classProbs=TRUE))  # 'cannnot compute class probabilities for regression'