1

I have run a logit model through glmnet. I am extracting the coefficients from the minimum lambda, and it gives me the results I expect. However I have a factor variable with nine unique values, and glmnet produces a single coefficient for this, which is expected for a binary variable but not factor...

library(glmnet)
coef(model.obj, s = 'lambda.min')

#output:
TraumaticInj  2.912419e-02
Toxin         .           
OthInj        4.065266e-03
CurrentSTDYN  7.601812e-01
GeoDiv        1.372628e-02 #this is a factor variable w/ 9 options...

so my questions:

1) how should I interpret a single coefficient from a factor variable in glmnet?

2) is there a method to extract the coefficients for the different factors of the variable?

NiuBiBang
  • 628
  • 1
  • 15
  • 30

2 Answers2

1

Glmnet doesn't handle factor variables. You have to convert them to dummies using eg model. Matrix. So the results you are seeing is glmnet treating your factor variable as a single real variable.

seanv507
  • 1,206
  • 1
  • 11
  • 23
0

Can't be done, b/c glmnet doesn't treat factor variables. This is pretty much answered here: How does glmnet's standardize argument handle dummy variables?

This comment by @R_User in the answer is particularly insightful:

@DTRM - In general, one does not standardize categorical variables to retain the interpretability of the estimated regressors. However, as pointed out by Tibshirani here: statweb.stanford.edu/~tibs/lasso/fulltext.pdf, "The lasso method requires initial standardization of the regressors, so that the penalization scheme is fair to all regressors. For categorical regressors, one codes the regressor with dummy variables and then standardizes the dummy variables" - so while this causes arbitrary scaling between continuous and categorical variables, it's done for equal penalization treatment. – R_User Dec 6 '13 at 1:20

Community
  • 1
  • 1
NiuBiBang
  • 628
  • 1
  • 15
  • 30
  • I liked very much your answer; would be great if you add some code, e.g how to treat the variable as a dummy variable; or say, making a model.matrix ... – Areza Oct 08 '15 at 21:32