df <- data.frame (yaxis = rnorm(120,5,1),
xaxis = rep(c("A","B","C","D","E", "F"), times = 20),
factor = rep(c("1","2","3","4","5", "6"), each = 20))
df$xaxis <- as.factor(df$xaxis)
df$factor <- as.factor(df$factor )
library(lmerTest)
mod <- lmer(yaxis ~ xaxis + (1|factor), df)
mod.fit <- predictInterval(mod, df)
df.fit <- cbind(df, mod.fit)
ggplot(df.fit, aes(x = xaxis, y = yaxis, color = xaxis, group = xaxis)) +
geom_point() +
theme_classic() +
geom_errorbar(aes(min = lwr, max = upr), color = "black", width = 0.3) +
geom_point(aes(y = mean(fit), x = xaxis), position = position_dodge(width = 0.25), color = "black",size = 5)
我想用预测的95% CI和平均值绘制原始数据。我尝试了上面的方法,但我得到了拟合数据的多个置信区间。我该如何纠正它?
1条答案
按热度按时间erhoui1w1#
您可以使用
summary(mod)$coefficients
从模型中提取固定效应。这允许您获得每个x轴值的预测,并使用简单的算术计算该预测的95%置信区间。