Entering edit mode
5.8 years ago
demoraesdiogo2017
▴
110
Hello
My question is fairly simple
Here I have table X with my explanatory variables, table y with my response values and, lastly, the coefficients given by multiple linear regression with lasso regularization from the package glmnet.
https://imgbbb.com/image/zgLnt
I assume variable A should have a coefficient of 1 as it fits perfectly with the response variable, and E should be the exact same value with a negative coefficient instead.
My code is the following:
library(glmnet)
Y1 <- as.matrix(Y)
is.matrix(Y1)
X1 <- as.matrix(X)
is.matrix(X1)
X2 <- t(X1)
CV = cv.glmnet(x=X1, y=Y1, family= "gaussian", type.measure = "mae", alpha =1 )
##
plot(CV)
##
best_lambda = CV$lambda.1se
lasso_coef = CV$glmnet.fit$beta[, CV$glmnet.fit$lambda == best_lambda]
##
fit = (glmnet(x=X1, y=Y1, family= "gaussian", alpha=1, lambda=CV$lambda.1se))
##
fit$beta[,1]
plot(lasso_coef, xvar = "lambda", label = TRUE)
lasso_coef <- as.matrix(fit$beta)
write.table(lasso_coef, "C:/Users/Diogo/Documents/masters/LIHC/teste de regressao/regressionlasso.txt", sep="\t")
What have I done wrong?
you're using lasso. It will strive to both fit the data and keep the coefficients small. The balance of these two forces is influencing the coefficients that are fitted.