逻辑回归图中不同的结果量表(y 轴)

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

提问人:Mohammad 提问时间:10/30/2023 更新时间:10/31/2023 访问量:32

问:

为了绘制具有多个预测变量的逻辑回归,我在 R 中使用了一个库 (ggeffects)。为了制作所有变量的逻辑回归图,我编写了以下代码,它是函数的输出:glm.fitglm()

plts = lapply(names(coefficients(glm.fit))[-1],function(i){ return(plot(ggpredict(glm.fit,i))) })

最后,我使用了以下函数 wrap_plots(plts ,ncol = 2, nrow = 4)

我得到了我的 7 个预测变量的下图enter image description here

如您所见,WBC 的 Y 轴在 0 到 100% 之间,而 RBC 的 Y 轴在 0 到 60% 之间。如果有人能向我解释我所有预测变量的 Y 轴如何介于 0 到 100% 之间,我将不胜感激。

此致敬意

R 逻辑回归

评论

0赞 GuedesBF 10/31/2023
您是否想知道为什么 y 值始终介于 0 和 1(或 0 和 100%)之间?这些是概率,始终在 0-1 范围内
0赞 Gregor Thomas 10/31/2023
@GuedesBF我很确定 OP 希望每个子图的 y 限制为 0 和 1,而不是根据每个子图的范围缩小。
0赞 GuedesBF 10/31/2023
我怀疑,格里高尔,只是想确定一下

答:

2赞 gregor-fausto 10/31/2023 #1

以下是我所理解的包含在您的代码中的内容的摘要:

  • 你正在使用包中的函数来拟合模型。 是基本 R 包。glmstatsstats
  • 你正在使用包中的函数来构造预测。ggpredictggeffects
  • 调用 的输出意味着您正在使用包中的 plot-method。plotggpredictggeffects
  • 最后,您将使用包中的函数来组装最终图形。wrap_plotspatchwork
plts = lapply(names(coefficients(glm.fit))[-1],function(i){
   return(plot(ggpredict(glm.fit,i))) 
})

wrap_plots(plts ,ncol = 2, nrow = 4)

我对你的问题的理解是,你 (1) 想知道为什么你的图上的 y 轴会有所不同,以及 (2) 询问如何生成具有相同 y 轴比例 0-100% 的绘图。

  1. 绘图是由此功能 https://strengejacke.github.io/ggeffects/reference/plot.html 生成的。在您的代码中,它为每个系数生成一个图,这意味着每个图的 y 轴刻度都独立设置为该图中的数据范围。
  2. 要使用当前的绘图方法生成具有相同 y 轴比例的绘图,您需要添加到函数中。limits=c(0,1)plot
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)

下面是具有 2 个预测变量的 GLM 的可重现示例。首先,下面是压缩 y 轴刻度的示例。

# 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

现在,下面是新的 0-100% y 轴刻度的示例:

# 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