How can I create a logistic regression curve in R?

Logistic regression is a statistical method used to model the relationship between a categorical dependent variable and one or more independent variables. In order to create a logistic regression curve in R, you will need to first import your data into R and then use the built-in functions and packages to fit a logistic regression model. This involves specifying the appropriate variables and settings, such as the type of regression (binomial or multinomial) and the link function. Once the model is fitted, you can use the summary function to view the results and assess the significance of the variables. Additionally, you can use the predict function to generate predicted values and plot them against the original data to visualize the logistic regression curve. By following these steps, you can effectively create a logistic regression curve in R to analyze and interpret the relationship between your variables.

Plot a Logistic Regression Curve in R


Often you may be interested in plotting the curve of a fitted in R.

Fortunately this is fairly easy to do and this tutorial explains how to do so in both base R and ggplot2.

Example: Plot a Logistic Regression Curve in Base R

The following code shows how to fit a logistic regression model using variables from the built-in mtcars dataset in R and then how to plot the logistic regression curve:

#fit logistic regression model
model <- glm(vs ~ hp, data=mtcars, family=binomial)

#define new data frame that contains predictor variable
newdata <- data.frame(hp=seq(min(mtcars$hp), max(mtcars$hp),len=500))

#use fitted model to predict values of vs
newdata$vs = predict(model, newdata, type="response")

#plot logistic regression curve
plot(vs ~ hp, data=mtcars, col="steelblue")
lines(vs ~ hp, newdata, lwd=2)

Logistic regression curve in base R

The x-axis displays the values of the predictor variable hp and the y-axis displays the predicted probability of the response variable am.

We can clearly see that higher values of the predictor variable hp are associated with lower probabilities of the response variable vs being equal to 1.

Example: Plot a Logistic Regression Curve in ggplot2

The following code shows how to fit the same logistic regression model and how to plot the logistic regression curve using the data visualization library :

library(ggplot2)#plot logistic regression curve
ggplot(mtcars, aes(x=hp, y=vs)) + 
  geom_point(alpha=.5) +
  stat_smooth(method="glm", se=FALSE, method.args = list(family=binomial))

Logistic regression curve in ggplot2

Note that this is the exact same curve produced in the previous example using base R.

Feel free to modify the style of the curve as well. For example, we could turn the curve into a red dashed line:

library(ggplot2)#plot logistic regression curve
ggplot(mtcars, aes(x=hp, y=vs)) + 
  geom_point(alpha=.5) +
  stat_smooth(method="glm", se=FALSE, method.args = list(family=binomial),
              col="red", lty=2)

Additional Resources

x