R:使用混合效应模型绘制实际与观察的对比图

时间:2015-04-15 13:56:03

标签: r effects mixed

我有一个数据集:

 date=c(56,54,112,230,250,134,114)
 species=c("pink","blue","pink","green","black","orange","purple")
 year=c(1901,2000,1958,1978,1992,1992,1994)
 loc=c("forest","river","river","cloud","cloud")
 peop=c(1.0,-6.2,1.55,0.45,-2.8,3.45,4.1)
 per=c(1,5,63,9,45,1,2)
 tem=c(12,65,14,35,26,24,22)
 high=c(2500,3400,2600,2800,2546,2148,3654)

从这个数据集中我创建了一个混合效果模型:

 model<-lmer(date~(1|species)+ high + year + tem*peop + per)

我需要一个图表,用于绘制日期与模型预测值的实际观测值。谢谢!

1 个答案:

答案 0 :(得分:1)

构建数据(将数据放在数据框中而不是放在全局工作区中更好):

dd <-
    data.frame(date=c(56,54,112,230,250,134,114),
               species=c("pink","blue","pink","green","black",
               "orange","purple"),
               year=c(1901,2000,1958,1978,1992,1992,1994),
               ## I added a couple of values here since the
               ##  length didn't match the other variables
               loc=c("forest","river","river","cloud","cloud","forest","forest"),
               peop=c(1.0,-6.2,1.55,0.45,-2.8,3.45,4.1),
               per=c(1,5,63,9,45,1,2),
               tem=c(12,65,14,35,26,24,22),
               high=c(2500,3400,2600,2800,2546,2148,3654))

这个模型实际上不适合这么短的数据集,所以我复制了它(仍然非常人为,但可以用于说明)

dd <- do.call(rbind,replicate(10,dd,simplify=FALSE))

library("lme4")
model<-lmer(date~(1|species)+ high + year + tem*peop + per,dd)

预期的绘图(x轴)与观察到的(y轴):

plot(fitted(model),dd$date)
abline(a=0,b=1)  ## 1-to-1 line