在 R 中的混合模型中添加模型预测的置信区间——ggplot2?
Adding confidence intervals from model predictions in mixed models in R -- ggplot2?
我根据我的数据对平均置信区间进行了模型预测,我想将其添加到图表中。我知道如何绘制数据,但如何添加模型拟合均值和置信区间?对于后者 geom_ribbon 似乎没有完成这项工作。
df <- data.frame(
fertilizer = c("N","N","N","N","N","N","N","N","N","N","N","N","P","P","P","P","P","P","P","P","P","P","P","P","N","N","N","N","N","N","N","N","N","N","N","N","P","P","P","P","P","P","P","P","P","P","P","P"),
level = c("low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","low"),
growth = c(0,0,1,2,90,5,2,5,8,55,1,90,2,4,66,80,1,90,2,33,56,70,99,100,66,80,1,90,2,33,0,0,1,2,90,5,2,2,5,8,55,1,90,2,4,66,0,0),
repro = c(1,90,2,4,66,80,1,90,2,33,56,70,99,100,66,80,1,90,2,33,0,0,1,2,90,5,2,2,5,8,55,1,90,2,4,66,0,0,0,0,1,2,90,5,2,5,8,55)
)
mod1 <- lm(growth~ fertilizer + level + fertilizer :level, df)
df$predict <- predict(mod1)
predci <- predict(mod1, interval = "confidence")
dflm = cbind(df, predci)
ggplot(dflm, aes(x=fertilizer, y=predict, color = fertilizer)) +
theme_bw() +
scale_color_manual(values=c("#E69F00", "#1B9E77")) +
geom_ribbon(aes(ymin = lwr, ymax = upr, fill = fertilizer, color = NULL), alpha = .15) +
stat_summary(aes(color = fertilizer),fun.y = mean, geom = "point", size = 4, position = position_dodge(0.1)) +
facet_grid(.~level)
这是一种方法。首先,我们使用 expand.grid
来制作我们想要预测的每个值的行,并且只包含这些值。这样可以避免重复。
plot_data <- expand.grid(level = c("low", "high"), fertilizer=c("N","P"))
plot_data <- cbind(plot_data, predict(mod1, plot_data, interval="confidence"))
不,我们将 geom_errorbar
与预测区间值一起使用。
ggplot(plot_data, aes(fertilizer, color=fertilizer)) +
geom_point(aes(y=fit)) +
geom_errorbar(aes(ymin=lwr, ymax=upr)) +
scale_color_manual(values=c("#E69F00", "#1B9E77")) +
facet_grid(cols=vars(level))
我根据我的数据对平均置信区间进行了模型预测,我想将其添加到图表中。我知道如何绘制数据,但如何添加模型拟合均值和置信区间?对于后者 geom_ribbon 似乎没有完成这项工作。
df <- data.frame(
fertilizer = c("N","N","N","N","N","N","N","N","N","N","N","N","P","P","P","P","P","P","P","P","P","P","P","P","N","N","N","N","N","N","N","N","N","N","N","N","P","P","P","P","P","P","P","P","P","P","P","P"),
level = c("low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","low"),
growth = c(0,0,1,2,90,5,2,5,8,55,1,90,2,4,66,80,1,90,2,33,56,70,99,100,66,80,1,90,2,33,0,0,1,2,90,5,2,2,5,8,55,1,90,2,4,66,0,0),
repro = c(1,90,2,4,66,80,1,90,2,33,56,70,99,100,66,80,1,90,2,33,0,0,1,2,90,5,2,2,5,8,55,1,90,2,4,66,0,0,0,0,1,2,90,5,2,5,8,55)
)
mod1 <- lm(growth~ fertilizer + level + fertilizer :level, df)
df$predict <- predict(mod1)
predci <- predict(mod1, interval = "confidence")
dflm = cbind(df, predci)
ggplot(dflm, aes(x=fertilizer, y=predict, color = fertilizer)) +
theme_bw() +
scale_color_manual(values=c("#E69F00", "#1B9E77")) +
geom_ribbon(aes(ymin = lwr, ymax = upr, fill = fertilizer, color = NULL), alpha = .15) +
stat_summary(aes(color = fertilizer),fun.y = mean, geom = "point", size = 4, position = position_dodge(0.1)) +
facet_grid(.~level)
这是一种方法。首先,我们使用 expand.grid
来制作我们想要预测的每个值的行,并且只包含这些值。这样可以避免重复。
plot_data <- expand.grid(level = c("low", "high"), fertilizer=c("N","P"))
plot_data <- cbind(plot_data, predict(mod1, plot_data, interval="confidence"))
不,我们将 geom_errorbar
与预测区间值一起使用。
ggplot(plot_data, aes(fertilizer, color=fertilizer)) +
geom_point(aes(y=fit)) +
geom_errorbar(aes(ymin=lwr, ymax=upr)) +
scale_color_manual(values=c("#E69F00", "#1B9E77")) +
facet_grid(cols=vars(level))