Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting predicted classes from R glmnet object

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.

like image 833
user721975 Avatar asked Mar 27 '26 21:03

user721975


1 Answers

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.

like image 107
joran Avatar answered Mar 31 '26 08:03

joran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!