0

I have dataset:

      SalesPrice SqFeet Beds Baths AirCond Garage Pool Year Quality Style  Lot     Highway
  1:      360.0  3.032    4     4       1      2    0 1972       2     1 22.221       0
  2:      340.0  2.058    4     2       1      2    0 1976       2     1 22.912       0
  3:      250.0  1.780    4     3       1      2    0 1980       2     1 21.345       0
  4:      205.5  1.638    4     2       1      2    0 1963       2     1 17.342       0
  5:      275.5  2.196    4     3       1      2    0 1968       2     7 21.786       0

I want to create a model:

model.lm2 <- glmnet(x = xvars,y = df[,SalesPrice],alpha = 1,family = 'gaussian',lambda = CV$lambda.1se)

But in my case I have factor variable "Style" and if I won't change it to dummy variable glmnet function will work with it as with numerical variable.

So how can I create create dummy variables for glmnet(LASSO) by model.matrix?

Mat_nekras
  • 81
  • 6

1 Answers1

0

You can use my glmnetUtils package, which will do all this for you. It's not on CRAN yet, but should be there shortly. In the meantime, use devtools to install it from the Github repo.

devtools::install.github("hong-revo/glmnetUtils")
library(glmnetUtils)

model.lm2 <- glmnet(SalesPrice ~ ., data=df)
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187