以下是我对您的代码中所理解内容的摘要:
- 您正在使用
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%的曲线图.
- 曲线图由该功能https://strengejacke.github.io/ggeffects/reference/plot.html产生.在您的代码中,它 for each 系数生成一个图,这意味着每个图的y轴比例是根据该图中的数据范围独立设置的.
- 要使用当前的绘图方法生成具有相同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)
下面是新的示例,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)