[英]Mixed model plotting with R - showing the data points
I have run a mixed effects binary model using the following code:我使用以下代码运行了混合效果二进制 model:
model = glmer(A ~ B + (1|C), data = data, family = "binomial")
summary(model)
I am now plotting the marginal fixed effects for a variable of interest (B).我现在正在绘制感兴趣变量 (B) 的边际固定效应。 I have taken the code from the nice page on: https://cran.r-project.org/web/packages/ggeffects/vignettes/practical_logisticmixedmodel.html
我从漂亮的页面上获取了代码: https://cran.r-project.org/web/packages/ggeffects/vignettes/practical_logisticmixedmodel.html
To produce the graph I have used:要生成我使用的图表:
ggpredict(model, "B")
plot(ggpredict(model, "B"))
The following is created which I like.以下是我喜欢的创建。 But I want also the data points from the variable B to show on the graph.
但我还希望变量 B 中的数据点显示在图表上。 How can I add these in?
我怎样才能添加这些? Thanks.
谢谢。
welcome to stackoverflow:)欢迎来到stackoverflow :)
Sadly, I dont know how to (/whether it is possible) to add points to your plot of the ggpredict-object, since I am no good with ggplots:/ But I can do a workaround with baseplot.可悲的是,我不知道如何(/是否有可能)向 ggpredict-object 的 plot 添加点,因为我不擅长 ggplots:/ 但我可以使用 baseplot 解决方法。 Only thing missing are the grey confidence intervals...which may bw crucial for good looks?
唯一缺少的是灰色置信区间......这可能对美观至关重要? :D
:D
Cheers干杯
#using the example data from the link you provided:
library(magrittr)
library(ggeffects)
library(sjmisc)
library(lme4)
library(splines)
set.seed(123)
#creating the data:
dat <- data.frame(
outcome = rbinom(n = 100, size = 1, prob = 0.35),
var_binom = as.factor(rbinom(n = 100, size = 1, prob = 0.2)),
var_cont = rnorm(n = 100, mean = 10, sd = 7),
group = sample(letters[1:4], size = 100, replace = TRUE)
)
dat$var_cont <- sjmisc::std(dat$var_cont)
#model creation:
m1 <- glmer( outcome ~ var_binom + var_cont + (1 | group),
data = dat,
family = binomial(link = "logit")
)
#save results:
m1_results <- ggpredict(m1, "var_cont")
#same plot you did:
plot(m1_results)
#workaround using baseplot:
#plotting the raw data:
plot(dat$outcome~dat$var_cont,
pch = 16,
ylab = "outcome",
xlab = "var_cont",
yaxt = "n")
#adding yaxis with percentages:
axis(2, at = pretty(dat$outcome), lab=paste0(pretty(dat$outcome) * 100," %"), las = TRUE)
#adding the model taken from ggpredict:
lines(m1_results$predicted~m1_results$x,
type = "l")
#upper and lower conf intervals:
lines(m1_results$conf.low~m1_results$x,
lty=2)
lines(m1_results$conf.high~m1_results$x,
lty=2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.