4
$\begingroup$

I'm trying to compare the results from glmnet and ncvreg in logistic regression. The methods have similar coefficients estimates in the case of lasso (alpha=1). It seems they are very different for the other values of alpha. The difference increase as alpha value gets smaller. I have attached the estimates resulted for alpha=1,0.7,0.2,0.01.

I believe that both of the methods use the same loss function and penalty function. Both of the packages standardize the predictors first, obtain the coefficient estimates and scale the coefficients to the original scale.

What is the reason for the different coefficient esimates?

library(ncvreg)
library(glmnet)

# Binomial
data(Heart)
X <- Heart$X
y <- Heart$y
n <- nrow(X)
lambda <- 0.05

# Lasso
alpha <- 1

fitncv <- ncvreg(X,y,family='binomial',penalty="lasso",alpha=alpha,eps=1e-7)
bncvreg <- as.matrix(coef(fitncv, lambda=lambda))

fitgnet <- glmnet(X,y,"binomial",alpha=alpha,thresh=1e-7)
bgnet <- coef(fitgnet,s=lambda,exact=TRUE,x=X,y=y)
# 
comb <- round(cbind(bncvreg,bgnet,bncvreg-bgnet), 5)
colnames(comb) <- c("ncvreg","glmnet","difference")

#               ncvreg   glmnet difference
# (Intercept) -2.93145 -2.93089   -0.00056
# sbp          0.00000  0.00000    0.00000
# tobacco      0.04127  0.04128   -0.00001
# ldl          0.07530  0.07531   -0.00001
# adiposity    0.00000  0.00000    0.00000
# famhist      0.47198  0.47198    0.00000
# typea        0.00356  0.00355    0.00001
# obesity      0.00000  0.00000    0.00000
# alcohol      0.00000  0.00000    0.00000
# age          0.03093  0.03092    0.00001


# enet
alpha <- 0.7

fitncv <- ncvreg(X,y,family='binomial',penalty="lasso",alpha=alpha,eps=1e-10)
bncvreg <- as.matrix(coef(fitncv, lambda=lambda))

fitgnet <- glmnet(X,y,"binomial",alpha=alpha)
bgnet <- coef(fitgnet,s=lambda,exact=TRUE,x=X,y=y)
# 
comb <- round(cbind(bncvreg,bgnet,bncvreg-bgnet), 5)
colnames(comb) <- c("ncvreg","glmnet","difference")

#               ncvreg   glmnet difference
# (Intercept) -3.74974 -3.58243   -0.16731
# sbp          0.00000  0.00023   -0.00023
# tobacco      0.05106  0.05067    0.00039
# ldl          0.09722  0.09553    0.00169
# adiposity    0.00000  0.00000    0.00000
# famhist      0.57815  0.55639    0.02175
# typea        0.01169  0.01056    0.00113
# obesity      0.00000  0.00000    0.00000
# alcohol      0.00000  0.00000    0.00000
# age          0.03495  0.03239    0.00256

# enet2
alpha <- 0.2

fitncv <- ncvreg(X,y,family='binomial',penalty="lasso",alpha=alpha,eps=1e-10)
bncvreg <- as.matrix(coef(fitncv, lambda=lambda))

fitgnet <- glmnet(X,y,"binomial",alpha=alpha)
bgnet <- coef(fitgnet,s=lambda,exact=TRUE,x=X,y=y)
# 
comb <- round(cbind(bncvreg,bgnet,bncvreg-bgnet), 5)
colnames(comb) <- c("ncvreg","glmnet","difference")

#               ncvreg   glmnet difference
# (Intercept) -5.58114 -4.97611   -0.60503
# sbp          0.00425  0.00463   -0.00038
# tobacco      0.06902  0.06441    0.00461
# ldl          0.14270  0.12517    0.01753
# adiposity    0.00000  0.00440   -0.00440
# famhist      0.78153  0.68973    0.09180
# typea        0.02781  0.02175    0.00606
# obesity     -0.01339 -0.00894   -0.00444
# alcohol      0.00000  0.00000    0.00000
# age          0.04196  0.03301    0.00895

# enet3
alpha <- 0.01

fitncv <- ncvreg(X,y,family='binomial',penalty="lasso",alpha=alpha,eps=1e-10)
bncvreg <- as.matrix(coef(fitncv, lambda=lambda))

fitgnet <- glmnet(X,y,"binomial",alpha=alpha)
bgnet <- coef(fitgnet,s=lambda,exact=TRUE,x=X,y=y)
# 
comb <- round(cbind(bncvreg,bgnet,bncvreg-bgnet), 5)
colnames(comb) <- c("ncvreg","glmnet","difference")

#               ncvreg   glmnet difference
# (Intercept) -5.93259 -5.17973   -0.75286
# sbp          0.00629  0.00614    0.00015
# tobacco      0.07645  0.06855    0.00791
# ldl          0.16390  0.13664    0.02726
# adiposity    0.01619  0.01601    0.00018
# famhist      0.87514  0.73805    0.13709
# typea        0.03603  0.02629    0.00974
# obesity     -0.05263 -0.03314   -0.01949
# alcohol      0.00013  0.00035   -0.00022
# age          0.04278  0.03238    0.01039
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.