R - Extract factor predictor names from caret and glmnet lasso model object

Viewed 1467

In the below example, I set up a df with 3 variables, predict, var1, and var2 (a factor).

When I run a model in caret or glmnet, the factor is converted into a dummy variable, such as var2b.

I'd like to extract the variable names programmatically and match the original variable names, not the dummy variable names -- is there a way to do this?

This is just an example, my real world problem has many variables with many different levels and therefore, I want to avoid doing this manually, like trying to substring out the "b".

Thanks!

library(caret)
library(glmnet)

df <- data.frame(predict = c('Y','Y','N','Y','N','Y','Y','N','Y','N'), var1 = c(1,2,5,1,6,7,3,4,5,6),
              var2 = c('a','a','b','b','a','a','a','b','b','a'))

str(df)

# 'data.frame': 10 obs. of  3 variables:
# $ predict: Factor w/ 2 levels "N","Y": 2 2 1 2 1 2 2 1 2 1
# $ var1   : num  1 2 5 1 6 7 3 4 5 6
# $ var2   : Factor w/ 2 levels "a","b": 1 1 2 2 1 1 1 2 2 1

test <- train(predict ~ .,
           data = df,
           method = 'glmnet',
           trControl = trainControl(classProbs = TRUE,
                                    summaryFunction = twoClassSummary,
                                    allowParallel = FALSE),
           metric = 'ROC',
           tuneGrid = expand.grid(alpha = 1,
                                  lambda = .005))

predictors(test)
# [1] "var1"  "var2b"
varImp(test)
# glmnet variable importance

# Overall
# var2b     100
# var1        0

coef(test)
# NULL
#################
x <- model.matrix(as.formula(predict~.),data=df)
x <-  x[,-1] ##remove intercept

df$predict <- ifelse(df$predict == 'Y', TRUE, FALSE)

glmnet1 <- glmnet::cv.glmnet(x = x,
                          y = df$predict,
                          type.measure='auc',
                          nfolds=3,
                          alpha=1,
                          parallel = FALSE)

rownames(coef(glmnet1))
# [1] "(Intercept)" "var1"        "var2b
2 Answers
Related