Using Base R methods

To plot the logistic regression curve in base R, we first fit the variables in a logistic regression model by using the glm() function. The glm() function is used to fit generalized linear models, specified by giving a symbolic description of the linear predictor. Then we use that model to create a data frame where the y-axis variable is changed to its predicted value derived by using the predict() function with the above-created model. Then we plot a scatter plot of original points by using the plot() function and predicted values by using the lines() function.

Syntax:

logistic_model <- glm( formula, family, dataframe )

plot( original_dataframe )

lines( predicted_dataframe )

Parameter:

  • formula: determines the symbolic description of the model to be fitted.
  • family: determines the description of the error distribution and link function to be used in the model.
  • dataframe: determines the data frame to be used for fitting purpose

Example: Plot logistic regression

R




# load dataframe
df <- read.csv("Sample4.csv")
 
# create logistic regression model
logistic_model <- glm(var1 ~ var2, data=df, family=binomial)
 
#Data frame with hp in ascending order
Predicted_data <- data.frame(var2=seq(
  min(df$var2), max(df$var2),len=500))
 
# Fill predicted values using regression model
Predicted_data$var1 = predict(
  logistic_model, Predicted_data, type="response")
 
# Plot Predicted data and original data points
plot(var1 ~ var2, data=df)
lines(var1 ~ var2, Predicted_data, lwd=2, col="green")


Output:

How to Plot a Logistic Regression Curve in R?

In this article, we will learn how to plot a Logistic Regression Curve in the R programming Language.

Logistic regression is basically a supervised classification algorithm. That helps us in creating a differentiating curve that separates two classes of variables. To Plot the Logistic Regression curve in the R Language, we use the following methods.

Dataset used: Sample4

Similar Reads

Method 1:  Using Base R methods

To plot the logistic regression curve in base R, we first fit the variables in a logistic regression model by using the glm() function. The glm() function is used to fit generalized linear models, specified by giving a symbolic description of the linear predictor. Then we use that model to create a data frame where the y-axis variable is changed to its predicted value derived by using the predict() function with the above-created model. Then we plot a scatter plot of original points by using the plot() function and predicted values by using the lines() function....

Method 2: Using ggplot2 package

...