Я думаю, проблема в том, как вы строите свою модель. На основании ваших данных мне пришлось убрать несколько ковариат, чтобы модель работала. Кроме того, если вы включите случайный эффект для «ID», у вас будут разные перехваты для каждого ID, и, таким образом, для каждого момента времени у вас будет несколько значений y в столбце прогноза. ggplot связывает их, но, конечно, выглядит совершенно криво.
Таким образом, я удалил идентификатор как случайный эффект, и вы увидите, что сюжет работает.
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")
Если вам действительно нужно добавить ID как случайный эффект, вам нужно изменить переменную группировки для geom_line, я использую 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")
Создано в 2020-04-18 с помощью представительного пакета (v0.3.0)
данные
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]