visualising linear mixed model in R

六月ゝ 毕业季﹏ 提交于 2021-01-29 13:15:48

问题


I am running a linear mixed model for a dataset Developmental trajectory of 2 groups- assessed at 3 timepoints on a number of different measures. Predictor variables are age and maternal education. I am using nlme and ggplot2 packages. Here is my final model (GM_RAW is the dependent variable)

Model_5<-lme(GM_RAW~timepoint*Group+age+Maternal_Education, data=dat, 
random=~timepoint|ID,method="ML", na.action=na.omit,control=list(opt="optim"))
summary(Model_5)

I have plotted individual trajectories based on the raw data but I want to add the information from the predicted lines from the model for my 2 groups. I have tried various suggestions posted on stack overflow but none seem to work

dat$gmpred<-predict(Model_5)

GrossMotor<-ggplot(dat,aes(x=as.numeric(timepoint),y=GM_RAW,colour=Group))+
geom_point()+ geom_line(aes(group=ID))

GrossMotor + geom_line(data=dat, aes(y=gmpred))

I want to show the intercept and slope for my 2 groups on the final graph

some sample data

"","ID","timepoint","Maternal_Education","age","Group","GM_RAW"
"1",3012,"5 months",NA,188,"Typical Group",10
"2",3089,"5 months",NA,182,"Typical Group",9
"3",3012,"10 months",NA,328,"Typical Group",13
"4",3004,"5 months","Tertiary postgraduate",163,"Typical Group",6
"5",3008,"5 months","Tertiary postgraduate",171,"Typical Group",4
"6",3023,"5 months","Tertiary postgraduate",170,"Typical Group",8
"7",3063,"5 months","Tertiary postgraduate",181,"Typical Group",10
"8",3071,"5 months","Tertiary postgraduate",151,"Typical Group",8
"9",3074,"5 months","Tertiary postgraduate",183,"Typical Group",9
"10",3075,"5 months","Tertiary postgraduate",165,"Typical Group",9
"11",3087,"5 months","Tertiary postgraduate",172,"Typical Group",6
"12",3104,"5 months","Tertiary postgraduate",180,"Typical Group",7
"13",3115,"5 months","Tertiary postgraduate",199,"Typical Group",8
"14",3142,"5 months","Tertiary postgraduate",201,"Typical Group",9
"15",3161,"5 months","Tertiary postgraduate",189,"Typical Group",7
"16",3162,"5 months","Tertiary postgraduate",201,"Typical Group",8
"17",4002,"5 months","Tertiary postgraduate",202,"NF1",8
"18",4024,"5 months","Tertiary postgraduate",167,"NF1",8
"19",3004,"10 months","Tertiary postgraduate",315,"Typical Group",9
"20",3008,"10 months","Tertiary postgraduate",341,"Typical Group",9
"21",3023,"10 months","Tertiary postgraduate",358,"Typical Group",14
"22",3063,"10 months","Tertiary postgraduate",293,"Typical Group",17
"23",3071,"10 months","Tertiary postgraduate",302,"Typical Group",12
"24",3074,"10 months","Tertiary postgraduate",333,"Typical Group",12
"25",3075,"10 months","Tertiary postgraduate",318,"Typical Group",11
"26",3078,"10 months","Tertiary postgraduate",304,"Typical Group",9
"27",3087,"10 months","Tertiary postgraduate",335,"Typical Group",13
"28",3104,"10 months","Tertiary postgraduate",294,"Typical Group",10
"29",3115,"10 months","Tertiary postgraduate",305,"Typical Group",11
"30",3142,"10 months","Tertiary postgraduate",327,"Typical Group",11
"31",3161,"10 months","Tertiary postgraduate",328,"Typical Group",12
"32",3162,"10 months","Tertiary postgraduate",333,"Typical Group",10
"33",4002,"10 months","Tertiary postgraduate",335,"NF1",11
"34",4009,"10 months","Tertiary postgraduate",320,"NF1",11
"35",4024,"10 months","Tertiary postgraduate",351,"NF1",13
"36",3004,"14 months","Tertiary postgraduate",438,"Typical Group",21
"37",3008,"14 months","Tertiary postgraduate",460,"Typical Group",9
"38",3023,"14 months","Tertiary postgraduate",471,"Typical Group",18
"39",3063,"14 months","Tertiary postgraduate",445,"Typical Group",20

回答1:


I think the problem is the way you are constructing your model. Based on your data, I had to take away some covariates in order to make the model work. Also, if you include a random effect for "ID", you will have different intercepts for each ID, and thus for each timepoint you will have several y values in your prediction column. ggplot does link them, but of course it looks totally crooked.

Thus, I removed the ID as a random effect and you will see that the plot works.

library(tidyverse)


foo <- foo %>% mutate(timepoint = str_replace(timepoint, " months", ""))

Model_5 <- nlme::lme(GM_RAW ~ Group,
  data = foo,
  random = ~ 1 | timepoint, method = "ML", na.action = na.omit
)

foo$gmpred <- predict(Model_5)

ggplot(foo, aes(x = as.numeric(timepoint), y = GM_RAW, colour = Group)) +
  geom_point() +
  geom_line(aes(group = ID)) +
  geom_line(aes(y = gmpred, group = Group, linetype = Group), color = "black")

If you really need to add ID as a random effect, you will need to change the grouping variable for the geom_line, I use interaction()

Model_5 <- nlme::lme(GM_RAW ~ Group,
  data = foo,
  random = ~ timepoint | ID, method = "ML", na.action = na.omit
)

foo$gmpred <- predict(Model_5)

ggplot(foo, aes(x = as.numeric(timepoint), y = GM_RAW, colour = Group)) +
  geom_point() +
  geom_line(aes(group = ID)) +
  geom_line(aes(y = gmpred, group = interaction(Group,ID), linetype = Group), color = "black")

Created on 2020-04-18 by the reprex package (v0.3.0)

data

foo <- readr::read_csv(",ID,timepoint,Maternal_Education,age,Group,GM_RAW
1,3012,5 months,NA,188,Typical Group,10
2,3089,5 months,NA,182,Typical Group,9
3,3012,10 months,NA,328,Typical Group,13
4,3004,5 months,Tertiary postgraduate,163,Typical Group,6
5,3008,5 months,Tertiary postgraduate,171,Typical Group,4
6,3023,5 months,Tertiary postgraduate,170,Typical Group,8
7,3063,5 months,Tertiary postgraduate,181,Typical Group,10
8,3071,5 months,Tertiary postgraduate,151,Typical Group,8
9,3074,5 months,Tertiary postgraduate,183,Typical Group,9
10,3075,5 months,Tertiary postgraduate,165,Typical Group,9
11,3087,5 months,Tertiary postgraduate,172,Typical Group,6
12,3104,5 months,Tertiary postgraduate,180,Typical Group,7
13,3115,5 months,Tertiary postgraduate,199,Typical Group,8
14,3142,5 months,Tertiary postgraduate,201,Typical Group,9
15,3161,5 months,Tertiary postgraduate,189,Typical Group,7
16,3162,5 months,Tertiary postgraduate,201,Typical Group,8
17,4002,5 months,Tertiary postgraduate,202,NF1,8
18,4024,5 months,Tertiary postgraduate,167,NF1,8
19,3004,10 months,Tertiary postgraduate,315,Typical Group,9
20,3008,10 months,Tertiary postgraduate,341,Typical Group,9
21,3023,10 months,Tertiary postgraduate,358,Typical Group,14
22,3063,10 months,Tertiary postgraduate,293,Typical Group,17
23,3071,10 months,Tertiary postgraduate,302,Typical Group,12
24,3074,10 months,Tertiary postgraduate,333,Typical Group,12
25,3075,10 months,Tertiary postgraduate,318,Typical Group,11
26,3078,10 months,Tertiary postgraduate,304,Typical Group,9
27,3087,10 months,Tertiary postgraduate,335,Typical Group,13
28,3104,10 months,Tertiary postgraduate,294,Typical Group,10
29,3115,10 months,Tertiary postgraduate,305,Typical Group,11
30,3142,10 months,Tertiary postgraduate,327,Typical Group,11
31,3161,10 months,Tertiary postgraduate,328,Typical Group,12
32,3162,10 months,Tertiary postgraduate,333,Typical Group,10
33,4002,10 months,Tertiary postgraduate,335,NF1,11
34,4009,10 months,Tertiary postgraduate,320,NF1,11
35,4024,10 months,Tertiary postgraduate,351,NF1,13
36,3004,14 months,Tertiary postgraduate,438,Typical Group,21
37,3008,14 months,Tertiary postgraduate,460,Typical Group,9
38,3023,14 months,Tertiary postgraduate,471,Typical Group,18
39,3063,14 months,Tertiary postgraduate,445,Typical Group,20")
#> Warning: Missing column names filled in: 'X1' [1]


来源:https://stackoverflow.com/questions/61288690/visualising-linear-mixed-model-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!