# How do I compute class probabilities in caret package using 'glmnet' method?

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?

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'

• Spot on! I was pretty sure my y was of factor type but i had the factor(y.train) part commented out so it was integer type. Thanks!! – Palace Chan Apr 8 '12 at 22:01