Lasso Model Question

1,574 views
Skip to first unread message

Tiffany Jiang

unread,
Jan 29, 2018, 7:59:16 PM1/29/18
to davi...@googlegroups.com
Hello,

I’m trying to implement lasso and ridge on my logistics regression model and I’m coming across difficulties. I’m using Introduction to Statistical Learning with Applications in R as a reference. 

I’m trying to create a matrix in order to use glmnet() for a lasso regression. I've attached my dataframe below. I've tried two ways: 

1) 
x <- model.matrix(acquired~., data = together)[,-101]
y <- together$acquired 

library(glmnet)  
grid= 10^seq(10, -2, length=100)
lasso.mod=glmnet(x,y, alpha=1, lambda=grid)

X is then transformed the dataframe that I've attached, x.csv, and then the error says: 

Error in glmnet(x, y, alpha = 1, lambda = grid) : 
  number of observations in y (221) not equal to the number of rows of x (0)

2) 

x <- as.matrix(together)

library(glmnet)  
grid= 10^seq(10, -2, length=100)
lasso.mod=glmnet(x,y, alpha=1, lambda=grid)

Error in storage.mode(y) = "double" : 
  invalid to change the storage mode of a factor

My x is attached as x_secondway. 

Thank you so much! I hope you have a good day. 

Tiffany
x.csv
together.csv
x_secondway.csv

Jaime Ashander

unread,
Jan 29, 2018, 8:39:27 PM1/29/18
to davi...@googlegroups.com
Hi Tiffany, 

The issue is that there is missing data (NAs) in `together, which the regularized regression methods implemented in glmnet cannot handle. You can use na.omit(together) or complete.cases(together) to see that every row of together has some missing data.

You could choose a subset of the columns for which there is complete information and method 1 would work: 

x <- model.matrix(acquired ~ ., together[ , c(1, 10, 102, 100)])
y <- together$acquired
glmnet( x, y, alpha=1, lambda=0.1)

# Call:  glmnet(x = x, y = y, alpha = 1, lambda = 0.1) 
#      Df %Dev Lambda
# [1,]  0    0    0.1


--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages