Plotting one predictor of a model that has several predictors with ggplot

时间秒杀一切 提交于 2019-12-04 17:04:05

As you have model that contains two predictors (different intercept values for levels) and also offset variable it won't e possible to directly include it in geom_smooth(). One way would be to make new data frame dat.new that contains values of Prewt for all three levels of Treat. Then use this new data frame to predict Postwt values for all levels using your model and add predicted values to new data frame

new.dat<-data.frame(Treat=rep(levels(anorexia$Treat),each=100),
                    Prewt=rep(seq(70,95,length.out=100),times=3))
anorexia.2<-data.frame(new.dat,Pred=predict(anorex.1,new.dat))
head(anorexia.2)
  Treat    Prewt     Pred
1   CBT 70.00000 80.18339
2   CBT 70.25253 80.29310
3   CBT 70.50505 80.40281
4   CBT 70.75758 80.51253
5   CBT 71.01010 80.62224
6   CBT 71.26263 80.73195

Now plot original points from the original data frame and add lines using new data frame that contains predictions.

ggplot(anorexia,aes(x=Prewt,y=Postwt,color=Treat))+geom_point()+
  geom_line(data=anorexia.2,aes(x=Prewt,y=Pred,color=Treat))

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!