I want to compare lassoshooting
and glmnet
for lasso.
No standardization option in lassoshooting
; so i standardized data first, fit the model and re-standardized to original scale.
Results are different and it seems lassoshooting
beta's is closer to the original beta's.
Do i have a mistake?
The code:
library(lassoshooting)
library(glmnet)
set.seed(327)
n = 500
p = 9
x = matrix(rnorm(n*p), ncol=p)
n = nrow(x)
b = c(.5, -.5, .25, -.25, .125, -.125, rep(0, 3))
y = x %*% b + rnorm(n, sd=.05)
xs = scale(x)
ys = scale(y)
lam = 0.1
glmnet_res = coef(glmnet(x, y), s=lam)[-1]
lassoshooting_res = lassoshooting(X=xs, y=ys, thr=1e-7, lambda=n*lam)$coefficients
# n in n*lam stems from difference between objective functions of two packages
# standard deviations for original scale
sds = apply(x,2,sd)
sdy = sd(y)
lasso_shooting_o = sdy*lassoshooting_res/sds
# compare
cbind(glmnet=glmnet_res, lassoshooting=lasso_shooting_o)
glmnet lassoshooting
[1,] 0.40123563 0.42270220
[2,] -0.38733635 -0.41195555
[3,] 0.14463257 0.16727953
[4,] -0.15914094 -0.17799495
[5,] 0.02942027 0.04958667
[6,] -0.01465437 -0.03777288
[7,] 0.00000000 0.00000000
[8,] 0.00000000 0.00000000
[9,] 0.00000000 0.00000000
# Is lassoshooting closer to true parameters ?
abs(lasso_shooting_o-b) <= abs(glmnet_res-b)
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
EDIT: According to the comment below, the new code
library(lassoshooting)
library(glmnet)
set.seed(327)
n = 500
p = 9
x = matrix(rnorm(n*p), ncol=p)
n = nrow(x)
b = c(5, -5, 25, -25, 125, -125, rep(0, 3))
y = x %*% b + rnorm(n, sd=.05)
# 1/n type standardization
xc = sweep(x, 2, colMeans(x))
sdc = sqrt(apply(xc, 2, crossprod)/nrow(x))
xs = sweep(xc, 2, sdc, "/")
ys = scale(y)*sqrt(n/(n-1))
lam = 0.1
# BOTH ARE STANDARDIZED: RESULTS ARE THE SAME
glmnet_std = coef(glmnet(xs, ys,standardize=F), s=lam)[-1]
lassoshooting_std = lassoshooting(X=xs, y=ys, thr=1e-7, lambda=n*lam)$coefficients
# n in n*lam stems from difference between objective functions of two packages
# compare
cbind(glmnet=glmnet_std, lassoshooting=lassoshooting_std)
#########################################################
glmnet lassoshooting
[1,] 0.00000000 0.00000000
[2,] 0.00000000 0.00000000
[3,] 0.04224107 0.04224107
[4,] -0.04178765 -0.04178765
[5,] 0.59188462 0.59188462
[6,] -0.59781943 -0.59781943
[7,] 0.00000000 0.00000000
[8,] 0.00000000 0.00000000
[9,] 0.00000000 0.00000000
#########################################################
# glmnet on ORIGINAL DATA with its own standardization
# lassoshooting on SCALED DATA (THEN RE-SCALED)
# VERY DIFFERENT RESULTS (selected variables are different too)
glmnet_o = coef(glmnet(x, y), s=lam)[-1]
# original scale
sdy = sd(y)/sqrt(n/(n-1))
lasso_shooting_o = sdy*lassoshooting_std/sdc # sdc is defined above
# compare
cbind(glmnet=glmnet_o, lassoshooting=lasso_shooting_o)
#########################################################
glmnet lassoshooting
[1,] 2.742806 0.000000
[2,] -2.412466 0.000000
[3,] 22.618378 7.379036
[4,] -23.014604 -7.205713
[5,] 122.877714 106.617676
[6,] -122.566183 -105.931439
[7,] 0.000000 0.000000
[8,] 0.000000 0.000000
[9,] 0.000000 0.000000
#########################################################
# OBVIOUSLY glmnet is correct.