# In R, does “glmnet” fit an intercept?

I am fitting a linear model in R using glmnet. The original (non-regularized) model was fitted using lm and did not have a constant term (i.e. it was in the form lm(y~0+x1+x2,data)).

glmnet takes a matrix of predictors and a vector of responses. I've been reading glmnet documentation, and can find no mention of the constant term.

So, is there a way to ask glmnet to force the linear fit through the origin?

-

Yes, an intercept is included in a glmnet model, but it is not regularized (cf. Regularization Paths for Generalized Linear Models via Coordinate Descent, p. 13). More details about the implementation could certainly be obtained by carefully looking at the code (for a gaussian family, it is the elnet() function that is called by glmnet()), but it is in Fortran.

You could try the penalized package, which allows to remove the intercept by passing unpenalized = ~0 to penalized().

> x <- matrix(rnorm(100*20),100,20)
> y <- rnorm(100)
> fit1 <- penalized(y, penalized=x, unpenalized=~0,
standardize=TRUE)
> fit2 <- lm(y ~ 0+x)
> plot((coef(fit1) + coef(fit2))/2, coef(fit2)-coef(fit1))


To get Lasso regularization, you might try something like

> fit1b <- penalized(y, penalized=x, unpenalized=~0,
standardize=TRUE, lambda1=1, steps=20)
> show(fit1b)
> plotpath(fit1b)


As can be seen in the next figure, there is little differences between the regression parameters computed with both methods (left), and you can plot the Lasso path solution very easily (right).

-
Thanks for pointing out the penalized package. –  NPE Nov 16 '10 at 7:02