I'm trying to adapt code for a binomial glmnet to make it work for a multinomial problem, but for some reason I keep getting an error code.
Here's the original code for the binomial model that works perfectly:
traininglasso <- stratified(sp_lasso, group = "Cat",
select = list(Cat = c("A","B", "C")),
size = c(86), replace=FALSE)
traininglasso[,Cat:=factor(Cat, labels = c("B", "B", "C") )]
check_lasso <- anti_join(sp_lasso, traininglasso, by=c("Accepted Symbol"))
check_lasso[,Cat:=factor(Cat, labels = c("B", "B", "C") )]
use_for_lasso <- within(for_lasso, Cat <- relevel(Cat, ref="C"))
lassod <- model.matrix(Cat~., use_for_lasso)[,-1]
cv.lassod <- cv.glmnet(lassod, use_for_lasso$Cat, alpha =1, family= "binomial")
lambdad <- cv.lassod
lasso_modeld <- glmnet(lassod, use_for_lasso$Cat, alpha =1, family = "binomial",
lambda = lambdad$lambda.1se)
coefd <- coef(lasso_modeld)
check_lasso_matrix <- model.matrix(Cat~., check_lasso)[,-1]
probslasso4 <- as.data.frame(predict.glmnet(lasso_modeld, type="response", newx = check_lasso_matrix))
Sorry that it's so wordy, but basically my steps are this:
Again, all of this works perfectly fine. However, when I leave my data as three categories and change the family to multinomial (those are quite literally the only changes I've made in the code below, everything else including the data is the same) I get this error message:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'as.matrix': requires numeric/complex matrix/vector arguments
I've read about other people getting this error and simply needing to reformat their matrices, but I suspect that's not my issue since the binomial code works with the matrix I used for that.
Here's the code that I've tried for the multinomial version that isn't working. I ran the entire code chunk above again, but I'm only including here the 4 lines that I edited to go from binomial to multinomial:
traininglasso[,Cat:=factor(Cat, labels = c("A", "B", "C") )]
check_lasso[,Cat:=factor(Cat, labels = c("A", "B", "C") )]
cv.lassod<- cv.glmnet(lassod, use_for_lasso$Cat, alpha =1, family= "multinomial")
lasso_modeld <- glmnet(lassod, use_for_lasso$Cat, alpha =1, family = "multinomial",
lambda = lambdad$lambda.1se)
Figured it out. For whatever reason, when you create a multinomial model with glmnet, you need to use the regular predict()
function instead of the predict.glmnet()
function. Using the same model matrix for both multinomial and binomial models works fine - seems like the error actually has nothing to do with the matrix format.