Open In App

How to Plot Odds Ratio of Prediction of Logistic Model in R?

Logistic regression is a commonly used statistical method for modeling the relationship between a binary response variable and one or more predictor variables. The odds ratio is the measure of the association between the predictor variables and the binary response variable in a logistic regression model. The odds ratio is a useful tool for understanding the strength and direction of association between the predictor variables and the binary response variable.

Logistic Regression Model

Logistic regression is a statistical method used to analyze the relationship between a binary dependent variable and one or more independent variables. It is commonly used in fields of social sciences, medical research, and business analytics, among others.



The dependent variable in the logistic regression model is binary, meaning it can take only two values, usually represented as 0 and 1. The independent variables can be continuous, categorical, or a combination of both.

Syntax :



glm(formula, data, family)

where:

  • formula is the symbolic representation of regression model, typically expressed as a formula of the form response ~ predictor1 + predictor2 + …, where response is dependent variable and predictor1, predictor2, etc. are the predictor variables.
  • The data is the name of the data frame containing the variables used in formula.
  • family specifies the type of GLM to fit. For logistic regression, the family argument should be set to the binomial.

 Odds Ratio

The Odds ratio is a commonly used measure in logistic regression, which quantifies the relationship between the predictor variable and the response variable. It is defined as the ratio of odds of an event occurring in one group compared to the odds of the same event occurring in another group. 




# Logistic regression model
model <- glm(Species == "versicolor" ~ Sepal.Length +
             Sepal.Width + Petal.Length,
             data = iris,
             family = "binomial")
 
# Extract the odds ratio
or <- exp(coef(model))
ci <- exp(confint(model))
 
# Data frame
data <- data.frame(predictor = names(or),
                   odds_ratio = or,
                   lower_ci = ci[,1],
                   upper_ci = ci[,2])
 
# Plot the odds ratio
library(ggplot2)
ggplot(data, aes(x = predictor, y = odds_ratio,
                 ymin = lower_ci, ymax = upper_ci)) +
  geom_pointrange() +
  xlab("Predictor Variable") +
  ylab("Odds Ratios") +
  ggtitle("Odds Ratios for the Logistic Regression Model")

Output:

 




# the dataset
data(mtcars)
 
# logistic regression model
model <- glm(vs ~ mpg, data = mtcars,
             family = "binomial")
 
or <- exp(coef(model))
ci <- exp(confint(model))
 
data <- data.frame(predictor = names(or)[-1],
                   odds_ratio = or[-1],
                   lower_ci = ci[-1,1],
                   upper_ci = ci[-1,2])
 
ggplot(data, aes(x = predictor, y = odds_ratio,
                 ymin = lower_ci, ymax = upper_ci)) +
  geom_pointrange() +
  xlab("Miles per Gallon") +
  ylab("Odds Ratio") +
  ggtitle("Odds Ratio for Engine Type vs. Miles per Gallon")

Output:

 


Article Tags :