Search code examples
rlogistic-regression

different scale for outcome(y-axis) in logistic regression plot


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 enter image description here

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,


Solution

  • Here's a summary of what I understand is wrapped up in your code:

    • You're using the function glm in the stats package to fit a model. stats is a base R package.
    • You're using the function ggpredict in the ggeffects package to construct predictions.
    • Calling plot on the output of ggpredict means you're using a plot-method from the ggeffects package.
    • Finally, you're using the function 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%.

    1. The plots are being produced by this function https://strengejacke.github.io/ggeffects/reference/plot.html. In your code, it's generating a single plot per coefficient which means that each plot's y-axis scale is set independently to the range of data in that plot.
    2. To produce plots with the same y-axis scale using your current plotting approach, you will want to add 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)
    

    enter image description here

    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)
    

    enter image description here