Extracting Coefficient Variable Names from Glmnet into a Data.Frame

How get coefs name from glmnet in R?

Based on your input above it seems that you specified a glmnet call with a single lambda value.

In this particular case you can extract the names of the coefficients like this: names(out$beta[, 1][out$beta[, 1] != 0]). Please note that only the names of the non-zero betas are extracted, which makes sense when applying Lasso here since Lasso performs feature reduction.

A minimum reproducible example would be this:
out <- glmnet(as.matrix(mtcars[-1]), mtcars[["mpg"]], lambda = 1)

Change coefficient of cv.glmnet fitted regression in R

The coefficients for glmnet are stored under $glmnet.fit$beta :

library(glmnet)
library(Matrix)

fit = cv.glmnet(x=as.matrix(mtcars[,-1]),y=mtcars[,1])

head(fit$glmnet.fit$beta)
6 x 79 sparse Matrix of class "dgCMatrix"
[[ suppressing 79 column names ‘s0’, ‘s1’, ‘s2’ ... ]]

cyl . -0.01192151 -0.1447790 -0.2658654 -0.376195 -0.4770942 -0.5686616
disp . . . . . . .
hp . . . . . . .
drat . . . . . . .
wt . -0.45776130 -0.7006176 -0.9218541 -1.123436 -1.3065806 -1.4739808
qsec . . . . . . .

It's one column for every lambda tested and when you call coefficients(), by default you pull out the column corresponding to fit$lambda.1se. Let's say we want to change that column:

coefficients(fit,s=fit$lambda.1se)

11 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) 33.940487806
cyl -0.843038418
disp .
hp -0.006965929
drat .
wt -2.365917424
qsec .
vs .
am .
gear .
carb .

wh = which(fit$lambda==fit$lambda.1se)
fit$glmnet.fit$beta[,wh] = runif(nrow(fit$glmnet.fit$beta))

coefficients(fit,s=fit$lambda.1se)

11 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) 33.94048781
cyl 0.45636267
disp 0.28286532
hp 0.04186184
drat 0.55084730
wt 0.35273817
qsec 0.96165338
vs 0.79227125
am 0.01036681
gear 0.47738589
carb 0.17170791

And other columns and the intercept remains unchanged:

11 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) 36.38102033
cyl -0.87610600
disp .
hp -0.01377978
drat .
wt -2.75934250
qsec .
vs .
am 0.16806977
gear .
carb -0.01384960

It is really weird to do it. If you want to change the coefficients for prediction purposes, you can always pull out coefficients, alter and multiply them with your data matrix again.

Extracting coeftest results into a data frame

I had the same problem and finally used a little workaround:

model_1_coef_df <- as.data.frame(model_1_coef[,])

or, using dplyr syntax:

model_1_coef_df <- model_1_coef[,] %>% 
as_tibble() %>%
mutate(variable = rownames(model_1_coef))

Coerce model coefficients to clean, 2-column dataframe

Another way, and no hacks through attributes() function, but extracting the rownames and matrix values. The attributes(class(coefs)) informs that dgCMatrix is a sparse matrix created using Matrix package.

data.frame( predict_names = rownames(coefs),
coef_vals = matrix(coefs))

# predict_names coef_vals
# 1 (Intercept) 21.117339411
# 2 (Intercept) 0.000000000
# 3 cyl -0.371338786
# 4 disp -0.005254534
# 5 hp -0.011613216
# 6 drat 1.054768651
# 7 wt -1.234201216
# 8 qsec 0.162451314
# 9 vs 0.771959823
# 10 am 1.623812912
# 11 gear 0.544171362
# 12 carb -0.547415029


Related Topics



Leave a reply



Submit