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?
x
withcbind(1,x)
to add a column of ones and use the optionintercept=FALSE
. – Brooklet