For Plotting logistic regression with multiple predictors I used a library(ggeffects) in R. To make logistic regression plots for all variables I wrote the below code which glm.fit
is an output of glm()
function:
plts = lapply(names(coefficients(glm.fit))[-1],function(i){ return(plot(ggpredict(glm.fit,i))) })
Finally, I used the below function wrap_plots(plts ,ncol = 2, nrow = 4)
and I get the below plot for my 7 predictor variables
as you see, the Y-axis for WBC is between 0 and 100% but for RBC, it is between 0 and 60%. I would appreciate it if anybody could explain to me how I Y-axis for all my predictors will be between 0 and 100%.
Best regards,
Here's a summary of what I understand is wrapped up in your code:
glm
in the stats
package to fit a model. stats
is a base R package.ggpredict
in the ggeffects
package to construct predictions.plot
on the output of ggpredict
means you're using a plot-method from the ggeffects
package.wrap_plots
from the patchwork
package to assemble your final figure.plts = lapply(names(coefficients(glm.fit))[-1],function(i){
return(plot(ggpredict(glm.fit,i)))
})
wrap_plots(plts ,ncol = 2, nrow = 4)
My understanding of your question is that you're (1) wondering why the y-axis on your plots varies, and (2) asking how to produce plots with the same y-axis scale of 0-100%.
limits=c(0,1)
to the plot
function.plts = lapply(names(coefficients(glm.fit))[-1],function(i){
return(plot(ggpredict(glm.fit,i),limits = c(0,1)))
})
wrap_plots(plts ,ncol = 2, nrow = 4)
Here's a reproducible example for a GLM with 2 predictors. First, here's the example with the compressed y-axis scale.
# load libraries
library(ggeffects)
library(patchwork)
# read data from
# https://environmentalcomputing.net/statistics/glms/glm-1/
crabs <- read.csv("https://environmentalcomputing.net/datasets/Crabs.csv")
# fit model
glm.fit <- glm(CrabPres ~ Time + Dist, family = binomial, data = crabs)
# predict the response for each coefficient
# then plot the predictions
plts = lapply(names(coefficients(glm.fit))[-1],function(i){
return(plot(ggpredict(glm.fit,i)))
})
# use patchwork to arrange plots
wrap_plots(plts ,ncol = 2, nrow = 1)
Now here's the example with the new, 0-100% y-axis scale:
# load libraries
library(ggeffects)
library(patchwork)
# read data from
# https://environmentalcomputing.net/statistics/glms/glm-1/
crabs <- read.csv("https://environmentalcomputing.net/datasets/Crabs.csv")
# fit model
glm.fit <- glm(CrabPres ~ Time + Dist, family = binomial, data = crabs)
# predict the response for each coefficient
# then plot the predictions
plts = lapply(names(coefficients(glm.fit))[-1],function(i){
return(plot(ggpredict(glm.fit,i),limits=c(0,1))
})
# use patchwork to arrange plots
wrap_plots(plts ,ncol = 2, nrow = 1)