AW27
AW27

Reputation: 505

Different Results with glmnet when intercept = F

I'm currently reviewing glmnet and noticed that the results of all coefficients are different when intercept = F. However, when reviewing the glmnet vignette, it looks like all this would do is set the coefficient of the intercept to 0.

I've been reviewing the literature and can't seem to find anything that would suggest that these results should be different.

library(glmnet)
x<-matrix(rnorm(100*20),100,20)
y<-rnorm(100)
# Build 2 models
With_Intercept<-glmnet(x,y)
Without_Intercept<-glmnet(x,y, intercept=F)
# Extract coefficients at a single value of lambda
cbind(coef(With_Intercept,s=0.01), coef(Without_Intercept,s=0.01))

Is there a valid explanation as to why these coefficients don't match?

Thanks in advance.

EDIT: Adding the results per request.

Initial Result:

21 x 2 sparse Matrix of class "dgCMatrix"
                        1            1
(Intercept) -0.0024897259  .          
V1          -0.0199487018 -0.019861709
V2           .             .          
V3           0.0345080811  0.034824143
V4          -0.0252635082 -0.025316191
V5           0.0709898950  0.071110913
V6          -0.0851168912 -0.085086680
V7          -0.0353605610 -0.035449329
V8          -0.0041396527 -0.004120999
V9           0.0149133910  0.015063492
V10         -0.0784355722 -0.078481974
V11          0.0007291132  0.000553981
V12         -0.0805830035 -0.080624026
V13         -0.1042633710 -0.104532519
V14          0.0773017549  0.077334566
V15          0.0197275150  0.019698681
V16          0.2098976451  0.209844040
V17         -0.2052982990 -0.205468553
V18         -0.1221542797 -0.121744419
V19          0.0203979166  0.020544481
V20         -0.0369057637 -0.037263778

Difference between results:

21 x 1 sparse Matrix of class "dgCMatrix"
                        1
(Intercept) -2.489726e-03
V1          -8.699233e-05
V2           .           
V3          -3.160618e-04
V4           5.268268e-05
V5          -1.210178e-04
V6          -3.021158e-05
V7           8.876837e-05
V8          -1.865364e-05
V9          -1.501005e-04
V10          4.640136e-05
V11          1.751322e-04
V12          4.102263e-05
V13          2.691482e-04
V14         -3.281074e-05
V15          2.883433e-05
V16          5.360506e-05
V17          1.702541e-04
V18         -4.098609e-04
V19         -1.465640e-04
V20          3.580142e-04

I've tried scale(y) and still get different results. So I'm not sure what exactly is going on with glmnet that causes such differences in coefficients. This is a practice dataset, but I've seen this issue with the QuickStartExample and other data as well.

Upvotes: 1

Views: 455

Answers (1)

DaveArmstrong
DaveArmstrong

Reputation: 21947

If the conditional mean of y when all of the x-variables equal zero is not zero, then the intercept will be estimated to be non-zero. By forcing this quantity to be zero, that changes all of the other coefficients, too. As you can see, if wee standardize the x and y variables, which sets all of their means to zero, the two results are the same (because the intercept in this case is, in fact, approximately zero).

With_Intercept<-glmnet(scale(x),c(scale(y)))
Without_Intercept<-glmnet(scale(x),c(scale(y)), intercept=FALSE)
# Extract coefficients at a single value of lambda
cbind(coef(With_Intercept,s=0.01), coef(Without_Intercept,s=0.01))[-1,]
# 20 x 2 sparse Matrix of class "dgCMatrix"
#               1           1
# V1  -0.07998034 -0.07998034
# V2  -0.04504846 -0.04504846
# V3   0.06832796  0.06832796
# V4   0.06775116  0.06775116
# V5  -0.08979266 -0.08979266
# V6  -0.05696729 -0.05696729
# V7   0.07466312  0.07466312
# V8   0.05139439  0.05139439
# V9  -0.02481709 -0.02481709
# V10 -0.07239820 -0.07239820
# V11 -0.07525438 -0.07525438
# V12 -0.14843772 -0.14843772
# V13 -0.16967153 -0.16967153
# V14 -0.05858588 -0.05858588
# V15  .           .         
# V16  .           .         
# V17  .           .         
# V18  0.10412824  0.10412824
# V19 -0.00632915 -0.00632915
# V20  0.11251063  0.11251063

Upvotes: 1

Related Questions