Paul Smith
2011-Aug-04 22:41 UTC
[R] Can glmnet handle models with numeric and categorical data?
Dear All, Can the x matrix in the glmnet() function of glmnet package be a data.frame with numeric columns and factor columns? I am asking this because I have a model with both numeric and categorical predictors, which I would like to study with glmnet. I have already tried to use a data.frame, but with no success -- as far as I know, the matrix object can only have data of a single type. Is there some way of circumventing this problem? Thanks in advance, Paul
Marc Schwartz
2011-Aug-04 23:02 UTC
[R] Can glmnet handle models with numeric and categorical data?
On Aug 4, 2011, at 5:41 PM, Paul Smith wrote:> Dear All, > > Can the x matrix in the glmnet() function of glmnet package be a > data.frame with numeric columns and factor columns? I am asking this > because I have a model with both numeric and categorical predictors, > which I would like to study with glmnet. I have already tried to use a > data.frame, but with no success -- as far as I know, the matrix object > can only have data of a single type. Is there some way of > circumventing this problem? > > Thanks in advance, > > PaulHi Paul, My recollection is that you would use ?model.matrix on the data frame to create the requisite matrix input for glmnet(). The caution however, is that glmnet() standardizes the input covariates, which is not appropriate for factors. Thus, you would want to set 'standardize = FALSE' and use appropriate methods in pre-processing continuous variables. HTH, Marc Schwartz
Seemingly Similar Threads
- using glmnet for the dataset with numerical and categorical
- different L2 regularization behavior between lrm, glmnet, and penalized?
- A question on glmnet analysis
- glmnet with binary predictors
- Different results of coefficients by packages penalized and glmnet