mccreeasy
mccreeasy

Reputation: 11

Converting logistic regression output from log odds to probability

I initially made this model for a class. Looking back at it, I found that, when I tried to convert my logistic regression output to probability, I got values greater than 1. I am using the following dataset: https://stats.idre.ucla.edu/stat/data/binary.csv

My code to set up the model:

mydata <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
mydata$rank<- factor(mydata$rank) 
mylogit <- glm(admit ~ gre + gpa + rank, data=mydata, family="binomial") 
summary(mylogit)

Now, I exponentiate these coefficients to get my odds("odds"):

odds <- exp(coef(mylogit))

and convert the odds to probability:

odds/(1 + odds)
# (Intercept)         gre         gpa       rank2       rank3       rank4
#  0.01816406  0.50056611  0.69083749  0.33727915  0.20747653  0.17487497

This output does not make sense; probability must be less than 1, and if GRE is 300, GPA is 3, and rank2 is true (all reasonable possibilities), then probability would be much more than 1.

What is my mistake here? What would be the correct way to convert this to probability?

Upvotes: 1

Views: 3270

Answers (1)

user2974951
user2974951

Reputation: 10375

  1. Your formula p/(1+p) is for the odds ratio, you need the sigmoid function
  2. You need to sum all the variable terms before calculating the sigmoid function
  3. You need to multiply the model coefficients by some value, otherwise you are assuming all the x's are equal to 1

Here is an example using mtcars data set

mod <- glm(vs ~ mpg + cyl + disp, mtcars, family="binomial")
z <- coef(mod)[1] + sum(coef(mod)[-1]*mtcars[1, c("mpg", "cyl", "disp")])
1/(1 + exp(-z))
# 0.3810432

which we can verify using

predict(mod, mtcars[1, c("mpg", "cyl", "disp")], type="response")
# 0.3810432

Upvotes: 2

Related Questions