0

I've been using the cv.glmnet function to fit a lasso logistic regression model. I'm using R

Here's my code. I'm using the iris dataset.

df = iris %>% 
  mutate(Species = as.character(Species)) %>% 
  filter(!(Species =="setosa")) %>% 
  mutate(Species = as.factor(Species))
  
X = data.matrix(df %>% select(-Species))
y = df$Species

Model = cv.glmnet(X, y, alpha = 1, family = "binomial")

How do I get the model accuracy from the cv.glmnet object (Model).

If I had been using caret on a normal logistic regression model, accuracy is already in the output.

train_control = trainControl(method = "cv", number = 10)
M2 = train(Species ~., data = df, trControl = train_control, 
           method = "glm", family = "binomial")
M2$results

but a cv.glmnet object doesn't seem to contain this information.

Mistakamikaze
  • 446
  • 3
  • 19

1 Answers1

2

You want to add type.measure='class' as in Model 2 below, otherwise the default for family='binomial' is 'deviance'.

df = iris %>% 
  mutate(Species = as.character(Species)) %>% 
  filter(!(Species =="setosa")) %>% 
  mutate(Species = as.factor(Species))

X = data.matrix(df %>% select(-Species))
y = df$Species

Model  = cv.glmnet(X, y, alpha = 1, family = "binomial")
Model2 = cv.glmnet(X, y, alpha = 1, family = "binomial", type.measure = 'class')

Then cvm gives the misclassification rate.

Model2$lambda ## lambdas used in CV
Model2$cvm    ## mean cross-validated error for each of those lambdas

If you want results for the best lambda, you can use lambda.min

Model2$lambda.min ## lambda with the lowest cvm
Model2$cvm[Model2$lambda==Model2$lambda.min] ## cvm for lambda.min
bmacGTPM
  • 577
  • 3
  • 12
  • Thank you very much. Just wondering, is this misclassification rate a good indicator for model assessment. Or should I be using a more training vs testing data approach. – Mistakamikaze Oct 25 '20 at 04:08
  • Do you know how to also extract the confusion matrix? – Mistakamikaze Oct 25 '20 at 04:44
  • 1
    train/testing would be best, especially if you're comparing with other models – bmacGTPM Oct 25 '20 at 15:16
  • 1
    for the confusion matrix, see `confusion.glmnet` here https://www.rdocumentation.org/packages/glmnet/versions/4.0-2/topics/assess.glmnet – bmacGTPM Oct 25 '20 at 15:17