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))