The difference you are observing is due to the additional division by the number of observations, N, that GLMNET uses in their objective function and implicit standardization of Y by its sample standard deviation as shown below.
$$
\frac{1}{2N}\left\|\frac{y}{s_y}-X\beta\right\|^2_{2}+\lambda\|\beta\|^2_{2}/2
$$
where we use $1/n$ in place of $1/(n-1)$ for $s_y$,
$$
s_y=\frac{\sum_i(y_i-\bar{y})^2}{n}
$$
By differentiating with respect to beta, setting the equation to zero,
$$
X^TX\beta-\frac{X^Ty}{s_y}+N\lambda\beta =0
$$
And solving for beta, we obtain the estimate,
$$
\tilde{\beta}_{GLMNET}= (X^TX+N\lambda I_p)^{-1}\frac{X^Ty}{s_y}
$$
To recover the estimates (and their corresponding penalties) on the original metric of Y, GLMNET multiplies both the estimates and the lambdas by $s_y$ and returns these results to the user,
$$
\hat{\beta}_{GLMNET}=s_y\tilde{\beta}_{GLMNET}= (X^TX+N\lambda I_p)^{-1}X^Ty
$$
$$
\lambda_{unstd.}=s_y\lambda
$$
Compare this solution with the standard derivation of ridge regression.
$$
\hat{\beta}= (X^TX+\lambda I_p)^{-1}X^Ty
$$
Notice that $\lambda$ is scaled by an extra factor of N. Additionally, when we use the predict()
or coef()
function, the penalty is going to be implicitly scaled by $1/s_y$. That is to say, when we use these functions to obtain the coefficient estimates for some $\lambda^*$, we are effectively obtaining estimates for$\lambda=\lambda^*/s_y$.
Based on these observations, the penalty used in GLMNET needs to be scaled by a factor of $s_y/N$.
set.seed(123)
n <- 1000
p <- 100
X <- matrix(rnorm(n*p,0,1),n,p)
beta <- rnorm(p,0,1)
Y <- X%*%beta+rnorm(n,0,0.5)
sd_y <- sqrt(var(Y)*(n-1)/n)[1,1]
beta1 <- solve(t(X)%*%X+10*diag(p),t(X)%*%(Y))[,1]
fit_glmnet <- glmnet(X,Y, alpha=0, standardize = F, intercept = FALSE, thresh = 1e-20)
beta2 <- as.vector(coef(fit_glmnet, s = sd_y*10/n, exact = TRUE))[-1]
cbind(beta1[1:10], beta2[1:10])
[,1] [,2]
[1,] 0.23793862 0.23793862
[2,] 1.81859695 1.81859695
[3,] -0.06000195 -0.06000195
[4,] -0.04958695 -0.04958695
[5,] 0.41870613 0.41870613
[6,] 1.30244151 1.30244151
[7,] 0.06566168 0.06566168
[8,] 0.44634038 0.44634038
[9,] 0.86477108 0.86477108
[10,] -2.47535340 -2.47535340
The results generalize to the inclusion of an intercept and standardized X variables. We modify a standardized X matrix to include a column of ones and the diagonal matrix to have an additional zero entry in the [1,1] position (i.e. do not penalize the intercept). You can then unstandardize the estimates by their respective sample standard deviations (again ensure you are using 1/n when computing standard deviation).
$$
\hat\beta_{j}=\frac{\tilde{\beta_j}}{s_{x_j}}
$$
$$
\hat\beta_{0}=\tilde{\beta_0}-\bar{x}^T\hat{\beta}
$$
mean_x <- colMeans(X)
sd_x <- sqrt(apply(X,2,var)*(n-1)/n)
X_scaled <- matrix(NA, nrow = n, ncol = p)
for(i in 1:p){
X_scaled[,i] <- (X[,i] - mean_x[i])/sd_x[i]
}
X_scaled_ones <- cbind(rep(1,n), X_scaled)
beta3 <- solve(t(X_scaled_ones)%*%X_scaled_ones+1000*diag(x = c(0, rep(1,p))),t(X_scaled_ones)%*%(Y))[,1]
beta3 <- c(beta3[1] - crossprod(mean_x,beta3[-1]/sd_x), beta3[-1]/sd_x)
fit_glmnet2 <- glmnet(X,Y, alpha=0, thresh = 1e-20)
beta4 <- as.vector(coef(fit_glmnet2, s = sd_y*1000/n, exact = TRUE))
cbind(beta3[1:10], beta4[1:10])
[,1] [,2]
[1,] 0.24534485 0.24534485
[2,] 0.17661130 0.17661130
[3,] 0.86993230 0.86993230
[4,] -0.12449217 -0.12449217
[5,] -0.06410361 -0.06410361
[6,] 0.17568987 0.17568987
[7,] 0.59773230 0.59773230
[8,] 0.06594704 0.06594704
[9,] 0.22860655 0.22860655
[10,] 0.33254206 0.33254206
Added code to show standardized X with no intercept:
set.seed(123)
n <- 1000
p <- 100
X <- matrix(rnorm(n*p,0,1),n,p)
beta <- rnorm(p,0,1)
Y <- X%*%beta+rnorm(n,0,0.5)
sd_y <- sqrt(var(Y)*(n-1)/n)[1,1]
mean_x <- colMeans(X)
sd_x <- sqrt(apply(X,2,var)*(n-1)/n)
X_scaled <- matrix(NA, nrow = n, ncol = p)
for(i in 1:p){
X_scaled[,i] <- (X[,i] - mean_x[i])/sd_x[i]
}
beta1 <- solve(t(X_scaled)%*%X_scaled+10*diag(p),t(X_scaled)%*%(Y))[,1]
fit_glmnet <- glmnet(X_scaled,Y, alpha=0, standardize = F, intercept =
FALSE, thresh = 1e-20)
beta2 <- as.vector(coef(fit_glmnet, s = sd_y*10/n, exact = TRUE))[-1]
cbind(beta1[1:10], beta2[1:10])
[,1] [,2]
[1,] 0.23560948 0.23560948
[2,] 1.83469846 1.83469846
[3,] -0.05827086 -0.05827086
[4,] -0.04927314 -0.04927314
[5,] 0.41871870 0.41871870
[6,] 1.28969361 1.28969361
[7,] 0.06552927 0.06552927
[8,] 0.44576008 0.44576008
[9,] 0.90156795 0.90156795
[10,] -2.43163420 -2.43163420