What does glmnet on binary data model (P=1 or P=0)

456 Views Asked by At

I am running the following elastic net model on binary data (1=bad, 0 = Good). Does anyone know what type of model does glmnet fit by default: P(y=1) or P(y=0). Is there anyway to choose the former to fit the model.

cv.glmnet(x, y, family="binomial", type.measure="deviance", standardize=FALSE, nfolds=5, alpha=par)
1

There are 1 best solutions below

0
On

Buried within the glmnet documentation is your answer (type ?predict.glmnet within R):

Note that for ‘"binomial"’ models, results are returned only for the class corresponding to the second level of the factor response.

If the binary data you described was assigned to y as a simple numeric vector, then the model will produce a P(y=1) fit.

> x <- matrix(rnorm(100*20),100,20)  # create some sample input
> y <- as.factor(sample(0:1, 100, replace=TRUE))
> levels(y)
[1] "0" "1"
> py1_fit <- glmnet(x, y, family="binomial")

To invert and fit against P(y=0) instead, you just have to reorder the levels:

> y0 <- factor(y, levels=rev(levels(y)))
> levels(y0)
[1] "1" "0"
> py0_fit <- glmnet(x, y0, family="binomial")