php image 平滑曲线,r 使用ggplot2无法获得平滑曲线 - 糯米PHP

我正在尝试使用lme4包来拟合混合效果模型。不幸的是,我无法共享正在使用的数据。我也找不到与我的问题有关的玩具数据集。所以在这里,我展示了到目前为止我所遵循的步骤:

首先,我绘制了数据的总体趋势,如下所示:

p21

p21+ geom_point() + geom_smooth()

12gtz.png

基于此,数据中似乎存在一些非线性趋势。因此,我尝试拟合二次模型,如下所示:

sub_data$age_cent=sub_data$age-mean((sub_data)$age)

sub_data$age_centsqr=(sub_data$age-mean((sub_data)$age))^2

m1= lmer(y ~ 1 + age_cent + age_centsqr +(1 | id) , sub_data, REML = TRUE)

In the above model i only included a random intercept because i don't have enough data to include both random slope and intercept.Then i extracted the predictions of these model at population level as follows :

pred1=predict(m1,re.form=NA)

Next I plotted these predictions along with a smooth quadratic function like this

p21+ geom_point() + geom_smooth(method = "lm", formula = y ~ I(x) + I(x^2)

,col="red")+geom_line(aes(y=pred1,group = id) ,col="blue", lwd = 0.5)

ZKlA4.png

In the above plot , the curve corresponds to predictions are not smooth. Can any one helps me to figure out the reason for that ?

I am doing anything wrong here ?

Update :

As eipi10 pointed out , this may due to fitting different curves for different people.

But when i tried the same thing using a toy data set which is in the lme4 package , i got the same curve for each person as follows :

m1

pred1new1=predict(m1,re.form=NA)

p21

p21+ geom_point() + geom_smooth()

p21+ geom_point() + geom_smooth()+ geom_line(aes(y=pred1new1,group = Subject) ,col="red", lwd = 0.5)

1qaMT.png

结果不同的原因可能是什么?这是由于数据不平衡造成的吗?我使用的数据是在3个时间步中收集的,有些人在3个时间步中都没有。但是玩具数据集是平衡数据集。

谢谢

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值