Nao
Nao

Reputation: 35

Coefficient of LASSO with caret

I ran LASSO algorithm with caret glmnet. I want to know the coefficients especially so as to know which variable shrinks to 0.

For example, I ran following code.(Since this code may be worthless in terms of statistics, please see it just as sample code)

library(tidyverse)
iris = data("iris") %>% as.tibble() %>% select(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
Sformula = as.formula(Sepal.Length ~ (Sepal.Width + Petal.Length + Petal.Width)^2)
fitControl = trainControl( method = "cv", number = 10)
Fit = train(
     Sformula,
     data = iris,
     method = "glmnet",
     preProcess = c("center","scale"),
     weights = NULL,
     trControl = fitControl,
     tuneLength = 10
)
coef(Fit)

However, it just returned NULL.

How should I do?

complement

I ran coef(Fit$finalModel). It seemed coefficient, but 7×82 matrix. the number of feature variables and intercept is 7. What is the column of this matrix?

> coef(Fit$finalModel)
7 x 82 sparse Matrix of class "dgCMatrix"
   [[ suppressing 82 column names ‘s0’, ‘s1’, ‘s2’ ... ]]
                                                                                                                      
(Intercept)              5.843333 5.84333333 5.8433333 5.8433333 5.8433333 5.84333333 5.84333333 5.84333333 5.84333333
Sepal.Width              .        .          .         .         .         .          .          .          .         
Petal.Length             .        .          .         .         .         0.01198642 0.02620089 0.03826789 0.04878216
Petal.Width              .        .          .         .         .         .          .          .          .         
Sepal.Width:Petal.Length .        0.06075376 0.1169792 0.1689475 0.2169243 0.25032859 0.27810921 0.30448688 0.32918552
Sepal.Width:Petal.Width  .        .          .         .         .         .          .          .          .         
Petal.Length:Petal.Width .        .          .         .         .         .          .          .          .         
                                                                                                                         
(Intercept)              5.8433333 5.84333333 5.84333333 5.84333333 5.8433333 5.84333333 5.84333333 5.84333333 5.84333333
Sepal.Width              .         .          .          .          .         .          .          .          .         
Petal.Length             0.0577385 0.06528163 0.07155311 0.07668659 0.0806087 0.08401776 0.08647254 0.08822959 0.08939423
Petal.Width              .         .          .          .          .         .          .          .          .         
Sepal.Width:Petal.Length 0.3524559 0.37439483 0.39508484 0.41459979 0.4331915 0.45037744 0.46672510 0.48213864 0.49665175
Sepal.Width:Petal.Width  .         .          .          .          .         .          .          .          .         
Petal.Length:Petal.Width .         .          .          .          .         .          .          .          .        

Upvotes: 1

Views: 1669

Answers (1)

Colin H
Colin H

Reputation: 660

These are the coefficients returned along your regularization path (for each value of lambda, and alpha in this case too).

caret provides an optimal lambda, stored at Fit$bestTune$lambda. You can just use that to extract the optimal coefficients matching that lamba as below:

lambda_use <- min(Fit$finalModel$lambda[Fit$finalModel$lambda >= Fit$bestTune$lambda])
position <- which(Fit$finalModel$lambda == lambda_use)
data.frame(coef(Fit$finalModel)[, position])

And I'm not sure if this just your simplified example here, but this isn't lasso. You're running elastic net and tuning the alpha parameter in addition to lambda. You'll need to set the alpha parameter to 1 for lasso. Check out the question below for doing so in caret.

Question on glmnet lasso with caret

Upvotes: 2

Related Questions