Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the intercept from a linear model with lasso (lars R package)

I am having an hard time in getting the model estimated by the R package lars for my data.

For example I create a fake dataset x and corresponding values y like this:

x = cbind(runif(100),rnorm(100))
colnames(x) = c("a","b")
y = 0.5 + 3 * x[,1,drop = FALSE]

Next I train a model that uses lasso regularization using the lars function:

m = lars(x,y,type = "lasso", normalize = FALSE, intercept = TRUE)

Now I would like to know what is the estimated model (that I know to be: y = 0.5 + 3 * x[,1] + 0 * x[,2])

I am only interested in the coefficients obtained in the last step:

cf = predict(m, x, s=1, mode = "fraction", type = "coef")$coef
cf
a b 
3 0

These are the coefficients that I expect, but I can't find a way to get the intercept (0.5) from m.

I have tried to check the code of predict.lars, where the fit is done as such:

fit = drop(scale(newx, 
           object$meanx, FALSE) %*% t(newbetas)) + object$mu)

I can see that the variables are scaled, and that the mean of y (object$mu) is used, but I can't find an easy way to obtain the value of the intercept I am looking for. How can I get that?

like image 894
lucacerone Avatar asked Jan 30 '14 17:01

lucacerone


1 Answers

intercept=T in lars has the effect of centering the x variables and y variable. It doesn't include an explicit intercept term with a coefficient.

That being said, you could do predict(m,data.frame(a=0,b=0),s=2)$fit to get the predicted value of y when the covariates are 0 (the definition of a traditional intercept)

like image 139
Jeremy Coyle Avatar answered Nov 09 '22 18:11

Jeremy Coyle