How to identify the non-zero coefficients in final caret elastic net model -

Viewed 408

I have used caret to build a elastic net model using 10-fold cv and I want to see which coefficients are used in the final model (i.e the ones that aren't reduced to zero). I have used the following code to view the coefficients, however, this apears to create a dataframe of every permutation of coefficient values used, rather than the ones used in the final model:

tr_control = train_control(method="cv",number=10)
formula = response ~.

model1 = caret::train(formula,
                    data=training,
                    method="glmnet",
                    trControl=tr_control,
                    metric = "Accuracy",
                    family = "binomial")

Then to extract the coefficients from the final model and using the best lambda value, I have used the following:

data.frame(as.matrix(coef(model1$finalModel, model1$bestTune$.lambda)))

However, this just returns a dataframe of all the coefficients and I can see different instances of where the coefficients have been reduced to zero, however, I'm not sure which is the one the final model uses. Using some slightly different code, I get slightly different results, but in this instance, non of the coefficients are reduced to zero, which suggests to me that the the final model isn't reducing any coefficients to zero:

data.frame(as.matrix(coef(model1$finalModel, model1$bestTune$lambda))) #i have removed the full stop preceeding lambda

Basically, I want to know which features are in the final model to assess how the model has performed as a feature reduction process (alongside standard model evaluation metrics such as accuracy, sensitivity etc).

1 Answers

Since you do not provide any example data I post an example based on the iris built-in dataset, slightly modified to fit better your need (a binomial outcome).

First, modify the dataset

library(caret)
set.seed(5)#just for reproducibility
iris
irisn <- iris[iris$Species!="virginica",]
irisn$Species <- factor(irisn$Species,levels = c("versicolor","setosa"))
str(irisn)
summary(irisn)

fit the model (the caret function for setting controls parameters for train is trainControl, not train_control)

tr_control = trainControl(method="cv",number=10)
model1  <-  caret::train(Species~.,
                      data=irisn,
                      method="glmnet",
                      trControl=tr_control,
                      family = "binomial")

You can extract the coefficients of the final model as you already did:

data.frame(as.matrix(coef(model1$finalModel, model1$bestTune$lambda)))

Also here the model did not reduce any coefficients to 0, but what if we add a random variable that explains nothing about the outcome?

irisn$new1 <- runif(nrow(irisn))
model2  <-  caret::train(Species~.,
                         data=irisn,
                         method="glmnet",
                         trControl=tr_control,
                         family = "binomial")
var <- data.frame(as.matrix(coef(model2$finalModel, model2$bestTune$lambda)))

Here, as you can see, the coefficient of the new variable was turning to 0. You can extract the variable name retained by the model with:

rownames(var)[var$X1!=0]

Finally, the accuracy metrics from the test set can be obtained with

confusionMatrix(predict(model1,test),test$outcome)
Related