[R] Can glmnet handle models with numeric and categorical data?

Marc Schwartz marc_schwartz at me.com
Fri Aug 5 01:02:55 CEST 2011


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,
> 
> Paul

Hi 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



More information about the R-help mailing list