Lasso r code - what is wrong with it?

math11 picture math11 · Jan 7, 2013 · Viewed 15.2k times · Source

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

Answer

IRTFM picture IRTFM · Jan 7, 2013

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