R: coefficients from glmnet's ridge regression do not match

Viewed 117

In ridge regression, the coefficients have the analytical form:

enter image description here where lambda is a positive number.

Using glmnet, I performed a ridge regression with alpha =0, lambda = 1. However, why don't fit1$beta match with those of the analytical formula?

library(glmnet)
set.seed(3)
x = matrix(rnorm(100 * 20), 100, 20)
y = rnorm(100)
lambda <- 1
fit1 = glmnet(x, y, alpha = 0, lambda = lambda)
> head(fit1$beta)
6 x 1 sparse Matrix of class "dgCMatrix"
             s0
V1 -0.016685052
V2  0.024373749
V3 -0.008228917
V4  0.035215872
V5 -0.008518129
V6 -0.001925812
> head(solve(t(x) %*% x + lambda * diag(p)) %*% t(x) %*% y)
            [,1]
[1,] -0.05338655
[2,]  0.04285142
[3,] -0.03454318
[4,]  0.08704770
[5,] -0.02599216
[6,] -0.02031802
0 Answers
Related