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

Paul Smith phhs80 at gmail.com
Fri Aug 5 01:30:59 CEST 2011


On Fri, Aug 5, 2011 at 12:02 AM, Marc Schwartz <marc_schwartz at me.com> wrote:
>> 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?
>
> 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.

Again, Mark, thanks a lot for your so helpful answer -- I completely
ignored model.matrix().

Paul



More information about the R-help mailing list