Search code examples
rr-caretglmnetcoefficients

Trying to extract coefficients from glmnet model returns NULL or "type must be either "raw" or "prob"" error


I am running a glmnet model in caret on the built-in infert dataset, e.g.,

infert_y <- factor(infert$case) %>% plyr::revalue(c("0"="control", "1"="case"))
infert_x <- subset(infert, select=-case)
new.x <- model.matrix(~., infert_x)
    
# Create cross-validation folds:
myFolds <- createFolds(infert_y, k = 10)


# Create reusable trainControl object:
myControl_categorical <- trainControl(
  summaryFunction = twoClassSummary,
  classProbs = TRUE, # IMPORTANT!
  verboseIter = TRUE,
  savePredictions = TRUE,
  index = myFolds
)


model_glmnet_pca <- train(
  x = new.x, 
  y = infert_y,
  metric = "ROC",
  method = "glmnet",
  preProcess=c("zv", "nzv","medianImpute", "center", "scale", "pca"),
  trControl = myControl_categorical,
  tuneGrid= expand.grid(alpha= seq(0, 1, length = 20),
                        lambda = seq(0.0001, 1, length = 100))
)

But when I try to get the coefficients:

bestlambda <- model_glmnet_pca$results$lambda[model_glmnet_pca$results$ROC == max(model_glmnet_pca$results$ROC)]

coef(model_glmnet_pca, s=bestlambda)

returns:

NULL

I tried:

coef.glmnet(model_glmnet_pca, s=bestlambda)

which returns:

Error in predict.train(object, s = s, type = "coefficients", exact = exact,  : 
  type must be either "raw" or "prob"

But surely when I'm calling coef() my "type" argument is set to "coefficients"? If I try

coef.glmnet(model_glmnet_pca, s=bestlambda, type="prob")

it returns:

Error in predict.train(object, s = s, type = "coefficients", exact = exact,  : 
  formal argument "type" matched by multiple actual arguments

I am very confused, can anyone point out what I'm doing wrong?


Solution

  • To get the coefficients from the best model, you can use:

    coef(model_glmnet_pca$finalModel, model_glmnet_pca$finalModel$lambdaOpt)
    

    See e.g. this link on using regularised regression models with caret.