为了绘制具有多个预测值的Logistic回归图,我在R中使用了一个库(GgEffects).为了绘制所有变量的Logistic回归图,我编写了以下代码,其中glm.fitglm()个函数的输出:

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

最后,我使用了下面的函数 WRAP_PLOTS(PLTS,NCOL=2,NROW=4)

and I get the below plot for my 7 predictor variables enter image description here

如你所见,WBC的Y轴在0到WBC%之间,而RBC的Y轴在0到60%之间.如果有人能给我解释一下,为什么我所有的预测值都在0到WBC%之间,我将不胜感激.

诚挚的问候,

推荐答案

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

  • 您正在使用stats包中的函数glm来适应一个模型.stats是BASE R封装.
  • 您正在使用ggeffects包中的函数ggpredict来构造预测.
  • ggpredict的输出调用plot意味着您使用的是ggeffects包中的Plot-方法.
  • 最后,您将使用patchwork包中的函数wrap_plots来组合最终的图形.
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产生.在您的代码中,它 for each 系数生成一个图,这意味着每个图的y轴比例是根据该图中的数据范围独立设置的.
  2. 要使用当前的绘图方法生成具有相同y轴比例的绘图,您需要在plot函数的基础上加上limits=c(0,1).
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)

下面是一个具有两个预测器的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

R相关问答推荐

在R中,如何在使用tibble::enFrame % % unlist转换后从收件箱中重组嵌套列表?

在处理因素时,Base R grep家族比stringr变体快得多

使用Shiny组合和显示复制和粘贴的数据

使用ggcorrplot在相关性矩阵上标注supertitle和index标签

在"gt"表中添加第二个"groupname_col",而不连接列值

R Sapply函数产生的值似乎与for循环方法略有不同

如何在观测缺失的地方添加零

如何通过ggplot2添加短轴和删除长轴?

如何删除仅在数据集顶部和底部包含零的行

线性模型斜率在减少原始数据时提供NA

在多页PDF中以特定布局排列的绘图列表不起作用

将标识符赋给事件序列,避免错误观察

在数据帧列表上绘制GGPUP

根据纬度和距离连接两个数据集

如何使用前缀作为匹配来连接数据帧?

手动指定从相同数据创建的叠加图的 colored颜色

将列表中的字符串粘贴到R中for循环内的dplyr筛选器中

带RStatix的Wilcoxon环内检验

如何根据其他列中的两个条件来计算数据帧中的行之间的差异?

按组使用dummy r获取高于标准的行的平均值