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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With