I fitted a LASSO logistic regression model using the caret package as follows,
require(ISLR)
require(caret)
mod_fitg <- train(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Volume,
data=Smarket, method = "glmnet",
tuneGrid=expand.grid(
.alpha=1,
.lambda=(0.002)),
family="binomial")
coef(mod_fitg$finalModel, mod_fitg$bestTune$lambda)
6 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) -0.088472239
Lag1 -0.065571845
Lag2 -0.035641733
Lag3 0.003564326
Lag4 0.001534829
Volume 0.110035397
The above coefficients are the standardized coefficients beacaue by defualt , the glmnet package standardize the coefficients. In this output, i want to know the meaning of the intercept term .
Because after the standardization , there should be no intercept term (As per the model coefficients using the same model using glmnet package)
y <- Smarket$Direction
x <- model.matrix(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Volume, Smarket)[, -1]
lasso.mod <- glmnet(x,y, alpha = 1, lambda = 0.002,family='binomial')
lasso.mod$beta
5 x 1 sparse Matrix of class "dgCMatrix"
s0
Lag1 -0.065571799
Lag2 -0.035641706
Lag3 0.003564320
Lag4 0.001534812
Volume 0.110035335
Your two models are the same, it is just that in lasso.mod
the intercept coefficient is stored in lasso.mod$a0
.
lasso.mod$a0
# s0
# -0.08847216
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.