使用ggplot绘制具有多个预测变量的模型的一个预测变量 [英] Plotting one predictor of a model that has several predictors with ggplot

查看:117
本文介绍了使用ggplot绘制具有多个预测变量的模型的一个预测变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是线性模型和ggplot的典型示例:

Here is an typical example of linear model and a ggplot:

require(ggplot2)

utils::data(anorexia, package = "MASS")

anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
                family = gaussian, data = anorexia)
coef(anorex.1)

   (Intercept)       Prewt   TreatCont     TreatFT 
     49.7711090  -0.5655388  -4.0970655   4.5630627 


ggplot(anorexia, aes(y=Postwt, x=Prewt)) + geom_point() + geom_smooth(method='lm', se=F)

我的问题是,由geom_smooth(...)进行的回归与anorex.1不是同一模型,而是:

My problem is that the regression that is made by geom_smooth(...) is not the same model than anorex.1 but is:

coef(lm(Postwt ~ Prewt, data=anorexia))

     (Intercept)       Prewt 
      42.7005802   0.5153804 

如何在ggplot上绘制模型anorexia1?

How can I plot the model anorexia1 on a ggplot?

我是否可以对Prewt进行截距(49.77)并估计(-0.5655)对anorexia1并使用geom_abline(..)进行绘制,对吗?有更简单的解决方案吗?

Could I just take the intercept (49.77) and estimate (-0.5655) of anorexia1 for Prewt and plot it with geom_abline(..), is it correct? Is there a simpler solution?

推荐答案

由于您的模型包含两个预测变量(水平的不同截距值)和偏移量变量,因此无法直接将其包含在geom_smooth()中.一种方法是制作新的数据帧dat.new,其中包含所有三个级别的Treat的值Prewt.然后使用这个新数据框,使用您的模型预测所有级别的Postwt值,并将预测值添加到新数据框

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

这篇关于使用ggplot绘制具有多个预测变量的模型的一个预测变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆