lemonslice27
lemonslice27

Reputation: 31

predict function for binary outcome using glmnet in R

I am using the predict function on a cv.glmnet object in R. I have a binary outcome vector that I am trying to predict (e.g. 0 1 1 0 1). Does the predict function default to finding predicted probabilities for predicting a 1 or a 0 by default? I could not find documentation that had an answer to the question.

Code with the general idea below:

for(holdout in 1:nrow(data)){
    myglmnet = cv.glmnet(data[-holdout, ], matrix(outcome[-holdout], nrow=1), family = "binomial", type.measure="mae", grouped=FALSE, alpha=1, nfolds=nrow(data))

    glmnet_hat[holdout] <- as.numeric(predict(myglmnet, matrix(data[holdout, ], nrow=1), type="response", s="lambda.min"))
}

Is glmnet_hat predicting the probabilities for a 0 or 1 response? Any documentation sources I could be linked to would also be great. Thanks!

Upvotes: 2

Views: 1425

Answers (1)

wusixer
wusixer

Reputation: 31

In your code above, you are modeling the probabilistic outcome by specifying type= "response". According to https://github.com/cran/glmnet/blob/4e4faf87ba78d977a9c4a31e308d9fb1b403f62a/inst/doc/glmnet_beta.Rmd, for binary outcome, you can usetype ="response" to get the fitted probabilities or use type ="class" to get the class label corresponding to the maximum probability.

Upvotes: 3

Related Questions