3
votes

I am trying to build simple multi-class logistic regression models using glmnet in R. However when I try to predict the test data and obtain contingency table I get an error. A sample session is reproduced below.

> mat = matrix(1:100,nrow=10)
> test = matrix(1:50,nrow=5)

> classes <- as.factor(11:20)

> model <- glmnet(mat, classes, family="multinomial", alpha=1)
> pred <- predict(model, test)
> table(pred, as.factor(11:15))
  Error in table(pred, as.factor(11:15)) : 
  all arguments must have the same length

Any help will be appreciated. R noob here.

Thanks.

1
You should spend more time with the documentation. Read ?predict.glmnet carefully, paying particular attention to the arguments type and s. - joran
@joran :I had tried type=class, response and link also. Had not worked still :-( - user721975
Once again, you simply need to read more carefully. The type argument expects a character, as in type = "class". Second, the s argument is very clearly documented, and necessary for what you are attempting. There's even an example illustrating its use. - joran
Yes, I was using the type argument correctly, but was not supplying s. Thanks for pointing it out. If you want to post your comment as an answer, I can accept it. - user721975
I'm glad you figured it out! :) - joran

1 Answers

6
votes

The predict method for a glmnet object requires that you specify a value for the argument s, which indicates which values of the regularization parameter for which you want predictions.

(glmnet fits the model for several values of this regularization parameter simultaneously.)

So if you don't specify a value for s, predict.glmnet returns predictions for all the values. If you want just a single set of predictions, you need to either set a value for s when you call predict, or you need to extract the relevant column after the fact.