Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lasso r code - what is wrong with it?

I am attempting to carry out lasso regression using the lars package but can not seem to get the lars bit to work. I have inputted code:

diabetes<-read.table("diabetes.txt", header=TRUE)
diabetes
library(lars)
diabetes.lasso = lars(diabetes$x, diabetes$y, type = "lasso")

However, I get an error message of : Error in rep(1, n) : invalid 'times' argument.

I have tried entering it like this:

diabetes<-read.table("diabetes.txt", header=TRUE)
library(lars)
data(diabetes)
diabetes.lasso = lars(age+sex+bmi+map+td+ldl+hdl+tch+ltg+glu, y, type = "lasso")

But then I get the error message: 'Error in lars(age+sex + bmi + map + td + ldl + hdl + tch + ltg + glu, y, type = "lasso") : object 'age' not found'

Where am I going wrong?

EDIT: Data - as below but with another 5 columns.

             ldl          hdl          tch          ltg          glu
1   -0.034820763 -0.043400846 -0.002592262  0.019908421 -0.017646125
2   -0.019163340  0.074411564 -0.039493383 -0.068329744 -0.092204050
3   -0.034194466 -0.032355932 -0.002592262  0.002863771 -0.025930339
4    0.024990593 -0.036037570  0.034308859  0.022692023 -0.009361911
5    0.015596140  0.008142084 -0.002592262 -0.031991445 -0.046640874
like image 533
math11 Avatar asked Jan 07 '13 19:01

math11


2 Answers

I think some of the confusion may have to do with the fact that the diabetes data set that comes with the lars package has an unusual structure.

library(lars)
data(diabetes)
sapply(diabetes,class)
##        x         y        x2 
##   "AsIs" "numeric"    "AsIs" 

sapply(diabetes,dim)
## $x
## [1] 442  10
## 
## $y
## NULL
## 
## $x2
## [1] 442  64

In other words, diabetes is a data frame containing "columns" which are themselves matrices. In this case, with(diabetes,lars(x,y,type="lasso")) or lars(diabetes$x,diabetes$y,type="lasso") work fine. (But just lars(x,y,type="lasso") won't, because R doesn't know to look for the x and y variables within the diabetes data frame.)

However, if you are reading in your own data, you'll have to separate the response variable and the predictor matrix yourself, something like

X <- as.matrix(mydiabetes[names(mydiabetes)!="y",])
mydiabetes.lasso = lars(X, mydiabetes$y, type = "lasso")

Or you might be able to use

X <- model.matrix(y~.,data=mydiabetes)
like image 162
Ben Bolker Avatar answered Sep 22 '22 00:09

Ben Bolker


lars::lars does not appear to have a formula interface, which means you cannot use the formula specification for the column names (and furthermore it does not accept a "data=" argument). For more information on this and other "data mining" topics, you might want to get a copy of the classic text: "Elements of Statistical Learning". Try this:

# this obviously assumes require(lars) and data(diabetes) have been executed.
> diabetes.lasso = with( diabetes, lars(x, y, type = "lasso"))
> summary(diabetes.lasso)
LARS/LASSO
Call: lars(x = x, y = y, type = "lasso")
   Df     Rss       Cp
0   1 2621009 453.7263
1   2 2510465 418.0322
2   3 1700369 143.8012
3   4 1527165  86.7411
4   5 1365734  33.6957
5   6 1324118  21.5052
6   7 1308932  18.3270
7   8 1275355   8.8775
8   9 1270233   9.1311
9  10 1269390  10.8435
10 11 1264977  11.3390
11 10 1264765   9.2668
12 11 1263983  11.0000
like image 32
IRTFM Avatar answered Sep 21 '22 00:09

IRTFM