首页 > 解决方案 > 预测线图

问题描述

我用 lmer 安装了以下模型:

mod1<-lmer(logpd~arm*lspline(pday,knots=2)+(1|pid),data=q1)

我想用 ggplot 重新创建以下图:

obsmeans<-aggregate(q1$logpd,by=data.frame(q1$pday,q1$arm),mean);obsmeans
fitted<-aggregate(fitted(mod1),by=data.frame(q1$pday,q1$arm),mean);fitted

plot(obsmeans$x~fitted$q1.pday)
lines(fitted$x[which(fitted$q1.arm=="SP")]~fitted$q1.pday[which(fitted$q1.arm=="SP")],lty=1)
lines(fitted$x[which(fitted$q1.arm=="SP/ART")]~fitted$q1.pday[which(fitted$q1.arm=="SP/ART")],lty=2)

预测线

我可以用 ggplot 绘制观察到的平均值和拟合平均值,但不知道如何添加预测线?

pred<-data.frame(obsmeans,fitted)
ggplot(pred,aes(x=x,y=q1.pday))+
  geom_point()

ggplot

编辑

以下是观察到的平均值和拟合值:

obsmeans
   q1.pday q1.arm      x
1        0     SP 3.5566
2        1     SP 2.7925
3        2     SP 0.7763
4        3     SP 0.5454
5        7     SP 0.0973
6        0 SP/ART 3.4309
7        1 SP/ART 0.9286
8        2 SP/ART 0.1898
9        3 SP/ART 0.0482
10       7 SP/ART 0.0000

fitted:
  q1.pday q1.arm       x
1        0     SP  3.7687
2        1     SP  2.3389
3        2     SP  0.8795
4        3     SP  0.7068
5        7     SP  0.0641
6        0 SP/ART  3.1345
7        1 SP/ART  1.5416
8        2 SP/ART -0.0562
9        3 SP/ART -0.0345
10       7 SP/ART  0.0172

标签: rggplot2predict

解决方案


推荐阅读