我正在尝试使用lme4包来拟合混合效果模型。不幸的是,我无法共享正在使用的数据。我也找不到与我的问题有关的玩具数据集。所以在这里,我展示了到目前为止我所遵循的步骤:
首先,我绘制了数据的总体趋势,如下所示:
p21
p21+ geom_point() + geom_smooth()
基于此,数据中似乎存在一些非线性趋势。因此,我尝试拟合二次模型,如下所示:
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)
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)
结果不同的原因可能是什么?这是由于数据不平衡造成的吗?我使用的数据是在3个时间步中收集的,有些人在3个时间步中都没有。但是玩具数据集是平衡数据集。
谢谢