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

1

6

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?

Turgescent answered 30/1, 2014 at 17:57 Comment(2)
Hi, you can replace x with cbind(1,x) to add a column of ones and use the option intercept=FALSE.Brooklet
... but it is not a good idea because lasso could set the intercept at 0Brooklet
O
7

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)

Overabundance answered 30/1, 2014 at 21:23 Comment(1)
thanks, I was looking for a way to access them from the data structure, but it doesn't seam to be possible.. Another way I have found, is using the fact that you have fit the model (y - ym) = b1*(x1 - x1m) + b2*(x2 - x2m), so that the intercept in terms of your un-centred variables is y = (y -b1*xm1 - b2*xm2) where the m denotes the mean of the variablesTurgescent

© 2022 - 2024 — McMap. All rights reserved.