ostrichgroomer
ostrichgroomer

Reputation: 354

Extracting coefficients of multinomial cv.glmnet

How do you extract the coefficients corresponding to a specific lambda of a cv.glmnet object built on a multinomial model? When I try to do it using the syntax one might use for a binomial model, the coef function returns a list of coefficient sparse matrices, rather than a particular sparse matrix.

An example:

tempcv <- cv.glmnet(x=as.matrix(iris[,-5]), y=iris[,5], family="multinomial", 
                    nfolds=20, alpha=0.5)
coefsMin <- coef(tempcv, s="lambda.min")

When I ran this, I got:

> coefsMin[[3]]
5 x 1 sparse Matrix of class "dgCMatrix"
                  1
(Intercept)  -19.091925
Sepal.Length   .       
Sepal.Width   -3.755938
Petal.Length   4.355219
Petal.Width    8.909600

> coefsMin[[2]]
5 x 1 sparse Matrix of class "dgCMatrix"
                 1
(Intercept)   4.616488
Sepal.Length  1.649614
Sepal.Width   .       
Petal.Length -1.088160
Petal.Width  -1.884997

> coefsMin[[1]]
5 x 1 sparse Matrix of class "dgCMatrix"
                 1
(Intercept)  14.475437
Sepal.Length -1.843070
Sepal.Width   5.312490
Petal.Length -2.698684
Petal.Width  -5.708280

So the entries of the coefsMin list can have different coefficient levels and different degrees of sparsity. Do all of the coefficient sets somehow correspond to the same lambda value? If so, is there a reason (other than sparsity) to pick one over the other?

Thank you!

Upvotes: 4

Views: 2974

Answers (1)

ostrichgroomer
ostrichgroomer

Reputation: 354

Okay, I think my issue was forgetting how multinomial regression works. The prediction formula for multinomial regression is

enter image description here

So you need a set of coefficients for every class.

Upvotes: 3

Related Questions