I'm trying to fit an adaptive lasso for a multinomial logit regression with glmnet. My problem is the following: when I try to use the penalty matrix (a 2x3 matrix) penalty.factor in cv.glmnet I get the following error:
`Error in glmnet(x, y, weights = weights, offset = offset, lambda = lambda, : the length of penalty.factor does not match the number of variables``
The problem, however, is with the columns (categories) rather than the rows (variables) because if I use only one column (penalty[,1]
) of the penalty matrix, it works.
Rep code here:
y <- matrix(round(runif(100,1,3),0))
x <- matrix(rnorm(200),,2)
# Generate Penalties based on ridge regression
set.seed(4342)
ridge.cv <- cv.glmnet(x,y,alpha=0, family= "multinomial", type.measure = "deviance", nfolds = 10)
best_ridge <- do.call(cbind, coef(ridge.cv, s = ridge.cv$lambda.min))
penalty <- 1 / abs(as.matrix(best_ridge)[-1,])
# Cross-validation of Lambda
lasso.cv <- cv.glmnet(x,y,alpha=, family= "multinomial", type.measure = "deviance",
penalty.factor = penalty, nfolds = 10)
HOW COULD I USE THE FULL PENALTY MATRIX?? Thank you!