Working example from https://glmnet.stanford.edu/articles/glmnet.html#multinomial-regression-family-multinomial-:
data(MultinomialExample)
x <- MultinomialExample$x
y <- MultinomialExample$y
cvfit <- cv.glmnet(x, y, family = "multinomial")
cvfit
then returns:
Call: cv.glmnet(x = x, y = y, family = "multinomial")
Measure: Multinomial Deviance
Lambda Index Measure SE Nonzero
min 0.009791 35 1.409 0.05611 14
1se 0.022619 26 1.455 0.04132 9
Now I would like to extract the 9 Nonzero coefficients. However:
sum(matrix(coef(cvfit)$'1') != 0)
sum(matrix(coef(cvfit)$'2') != 0)
sum(matrix(coef(cvfit)$'3') != 0)
returns 10, 12, 10. Moreover:
length(Reduce(intersect,
list(rownames(coef(cvfit)$'1')[matrix(coef(cvfit)$'1') != 0],
rownames(coef(cvfit)$'2')[matrix(coef(cvfit)$'2') != 0],
rownames(coef(cvfit)$'3')[matrix(coef(cvfit)$'3') != 0])))
returns 1.
What does Nonzero = 9 represent for the lambda.1se and how can I recover these variable names and corresponding coefficients when type.multinomial = "ungrouped"
(default cv.glmnet()
setting when family = "multinomial"
) ?
Thanks