试图弄清楚如何在 ANCOVA 中直观地展示调整后的均值是如何工作的。主要文献中有一些很好的已发表示例,但我无法使用 ggplot2 复制它们的可视化。我试图复制的例子:
Packard 和 Boardman 1999(图 2)和Barrett 2011(图 1)
library(ggplot2)
library(grid)
library(emmeans)
library(HH)
library(multcomp)
data(litter)
gest.mean <- mean(litter$gesttime) #mean of the covariate
model1 <- lm(weight ~ gesttime * dose, data=litter)
pred1 <- predict(model1)
model2 <- lm(weight ~ gesttime + dose, data=litter)
pred2 <- predict(model2)
#plot different slopes
plot1 <- ggplot(data = cbind(litter, pred1),
aes(gesttime, weight, color=dose)) + geom_point() +
geom_line(aes(y=pred1))+ #plots the predicted values (fitted line)
geom_vline(xintercept = gest.mean, linetype="dashed")+
labs(title = "Model1: Separate Slopes ANCOVA", subtitle = "model1 <-
lm(weight ~ gesttime * dose, data=litter)")
#plot same slopes
plot2 <- ggplot(data = cbind(litter, pred2),
aes(gesttime, weight, color=dose)) + geom_point() +
geom_line(aes(y=pred2))+
geom_vline(xintercept = gest.mean, linetype="dashed")+
labs(title = "Model2: Equal Slopes ANCOVA", subtitle = "model2 <- lm(weight ~
gesttime + dose, data=litter)")
#dashed vertical line shows the mean of covariate
#emmeans are calculated by adjusting points to mean of covariate along group specific slope
grid.newpage()
grid.draw(rbind(ggplotGrob(plot1), ggplotGrob(plot2), size = "last"))
summary(model1)
aov(model1)
summary(model2)
aov(model2)
#compare fits of model with interaction (sep. slopes) vs. model without (eq. slopes)
anova(model1,model2)
#EMmean post hocs to compare differences among four treatments at the grand mean of the covariate
#same as comparing intercepts when slopes are equal
#calculate model1 estimated marginal means (using interaction)
model1.emm <- emmeans(model1, "dose") #note that is gives warning message because sep slopes (interaction)
pairs(model1.emm)
#compare model1 marginal means (LS means)
plot(model1.emm, comparisons = TRUE)
CLD(model1.emm)
#calculate model2 estimated marginal means
model2.emm <- emmeans(model2, "dose")
pairs(model2.emm)
#compare model2 marginal means (LS means)
plot(model2.emm, comparisons = TRUE)
CLD(model2.emm)
#Just to show how EM means are used (intersect grand mean of covariate)
plot3 <- ggplot(data = cbind(litter, pred2),
aes(gesttime, weight, color=dose)) + geom_point() +
geom_line(aes(y=pred2))+
geom_vline(xintercept = gest.mean)+
geom_hline(yintercept = 28.87, linetype="dashed", color=c(1))+
geom_hline(yintercept = 29.33, linetype="dashed")+
geom_hline(yintercept = 30.56, linetype="dashed")+
geom_hline(yintercept = 32.35, linetype="dashed")+
labs(title = "Model2: Equal Slopes ANCOVA")
plot3
plot4 <-plot3 +
geom_segment(mapping=aes(x=gesttime, xend=gesttime+0.5, y=weight,
yend=weight+0.5, colour = "dose"), arrow=arrow(), size=.25, color="blue")
plot4
#obviously not what I wanted; individuals are not connected to mean of covariate (gesttime=22.08) along group-specific slope (sep. slopes) or common slope (eq. slopes)