Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R glmnet family = binomial predict values outside of 0-1

I'm trying to find a package in R for regularized logistic regression that predicts values between 0 - 1. I haven't had much luck though, having tried the lars package and now the glmnet package.

Below is code from the example in the reference manual for the glmnet package and I don't understand the output.

library(glmnet)

set.seed(1010)
n=1000;p=100
nzc=trunc(p/10)
x=matrix(rnorm(n*p),n,p)
beta=rnorm(nzc)
fx= x[,seq(nzc)] %*% beta
eps=rnorm(n)*5
y=drop(fx+eps)
px=exp(fx)
px=px/(1+px)
ly=rbinom(n=length(px),prob=px,size=1)
set.seed(1011)
cvob2=cv.glmnet(x,ly,family="binomial")
plot(cvob2) # had to add this comment to allow edit
coef(cvob2)
predict(cvob2,newx=x[1:5,], s="lambda.min")

             1
[1,] -1.721438
[2,]  0.914219
[3,]  1.111685
[4,]  1.805725
[5,] -4.200433

I don't understand why the output is not all within the 0 - 1 range.

Am I misunderstanding something here?

Can anyone recommend an easy to use package for regularized logistic regression?

Thanks.

like image 755
screechOwl Avatar asked Jun 08 '12 15:06

screechOwl


1 Answers

Check predict.glmnet docs: by default, it is type="link", i.e. link-transformed.

predict(cvob2,newx=x[1:5,],type="response", s="lambda.min")
like image 89
Dieter Menne Avatar answered Sep 24 '22 13:09

Dieter Menne