用混合模型的回归线绘制图

时间:2017-10-20 08:53:26

标签: r ggplot2 lme4 mixed-models

我有一个60棵树的数据框。我需要根据DOY找到EWMZ的一个斜率。我使用了混合模型并发现了一个负斜率,但在制作图时它给出了一个正回归线。我不知道我以前用ggplot制作情节的方式是否正确。

DOY EWMZ    TREE
247 13,01   1
262 11,01   1
274 23,07   1
288 23,09   1
310 20,77   1
247 28,47   2
262 22,55   2
274 15  2
288 13,93   2
310 13,73   2
240 21,56   3
247 18,48   3
262 22  3
274 22,29   3
288 19,69   3
310 19,46   3
233 24,12   4
240 23,16   4
247 20,01   4
262 17,5    4
274 20,05   4
288 19,76   4
310 20,92   4
240 9,82    5
247 15,96   5
262 12,44   5
274 9,35    5
288 11,07   5
310 8,69    5


library(lme4)
library(sjPlot)
library(sjmisc)
library(ggplot2)
Model.1 <- lmer(EWMZ ~ DOY + (1 | TREE), data = Data.Grad.EWMZ)
mas <- ggplot(Data.Grad.EWMZ, aes(x = DOY, y = EWMZ)) + labs(x="Day of 
year",y="Totall number of cells")+
geom_point(shape = 16, size=1.8, col="red") +
geom_smooth(method = "lm", fill = "dodgerblue", level = .95)+
theme(panel.background = element_rect(fill = 'White', colour = 'White'))+
theme( axis.line = element_line(colour = "darkgrey", 
size = 1, linetype = "solid"))+  
theme_classic() 

1 个答案:

答案 0 :(得分:0)

geom_smooth不计算混合效果模型。为了绘制固定斜率的线,我推荐这样的东西:

Data.Grad.EWMZ <- structure(list(DOY = c(247, 262, 274, 288, 310, 247, 262, 274, 
288, 310, 240, 247, 262, 274, 288, 310, 233, 240, 247, 262, 274, 
288, 310, 240, 247, 262, 274, 288, 310), EWMZ = c(13.01, 11.01, 
23.07, 23.09, 20.77, 28.47, 22.55, 15, 13.93, 13.73, 21.56, 18.48, 
22, 22.29, 19.69, 19.46, 24.12, 23.16, 20.01, 17.5, 20.05, 19.76, 
20.92, 9.82, 15.96, 12.44, 9.35, 11.07, 8.69), TREE = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("1", 
"2", "3", "4", "5"), class = "factor")), .Names = c("DOY", "EWMZ", 
"TREE"), row.names = c(NA, -29L), class = "data.frame")


library(lme4)
library(ggplot2)

Model.1 <- lmer(EWMZ ~ DOY + (1 | TREE), data = Data.Grad.EWMZ)
ggplot(Data.Grad.EWMZ, aes(x = DOY, y = EWMZ, colour=TREE)) +
  labs(x="Day of year",y="Totall number of cells")+
  geom_point(shape = 16, size=1.8) +
  geom_abline(aes(intercept=`(Intercept)`, slope=DOY), as.data.frame(t(fixef(Model.1))))