预测gamm4中每个组的值

时间:2017-06-30 11:22:58

标签: r predict mixed-models gam

我有这样的数据集:

dat <- structure(list(Y = c(152.75, 167.7, 169.7, 173.2, 174.4, 177.1, 
196, 200.45, 206.1, 206.65, 203, 186.65, 208.9, 192.95, 201.05, 
203.45, 200.3, 197.55, 205.1, 198.1, 205.15, 189.35, 201.25, 
194.55, 204.15, 200.95, 166.6, 165.1, 175.2, 168.4, 153, 168.4, 
161, 170.1, 168.15, 167.3, 169.2, 169.25, 185.35, 185.9, 178.55, 
193.2, 210.25, 203.75, 203.25, 203.7, 200.15, 204, 204, 206.3, 
197.7, 190.5, 185.95, 199, 185.1, 194.35, 186.2, 190.95, 191.55, 
177.8, 182.95, 186.3, 177.25, 186.35, 177.1, 183.9, 188.55, 184.05, 
188.55, 187.25, 185.25, 174.8, 180.9, 171.4, 169.6, 176.7, 178.35, 
191.3, 180.45, 187.5, 183.85, 187.7, 176.45, 188.7, 179.15, 183.25, 
180.1, 184.35, 185.35, 184.25, 182.55, 185.15, 181.2, 184.6, 
183.05, 182.35, 177.55, 179.85, 176.1, 175.9, 173.7, 180.7, 194.55, 
190.3, 200.5, 193.05, 191.55, 190.65, 194.9, 192.8, 202.65, 200.35, 
181.95, 194.85, 198.3, 199.7, 185.7, 195.9, 195.15, 191.85, 198.65, 
188.9, 192.25, 197.8, 185.75, 193.5, 178.2, 170.15, 175.4, 176.25, 
176.6, 179.8, 182, 173.35, 181.75, 188.05, 198.05, 204.75, 190.75, 
196.15, 193.15, 195.4, 192.35, 165.55, 187.15, 191.35, 200.4, 
200.4, 204.85, 211.3, 206.45, 205.95, 201, 198.6, 202.45, 192.95, 
198.25, 190.85, 182.9, 184.5, 175.75, 174.95, 178.8, 173.2, 174, 
176.75, 167.2, 161.1, 155.6, 178.6, 187.8, 194.05), X1 = c(4L, 
6L, 7L, 8L, 9L, 10L, 4L, 6L, 7L, 8L, 9L, 10L, 11L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L,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, 10L, 
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
17L, 17L, 18L, 18L, 18L), .Label = c("bec", "bi", "ebk", "ele", 
"eli", "ian", "isy", "ith", "lda", "lli", "na", "nja", "ra", 
"rda", "ria", "rik", "tje", "tri"), class = "factor")), .Names = c("Y", 
"X1", "X2"), row.names = c(142L, 143L, 144L, 145L, 146L, 147L, 
87L, 88L, 89L, 90L, 91L, 92L, 93L, 160L, 161L, 162L, 163L, 164L, 
165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 82L, 83L, 84L, 85L, 86L, 23L, 24L, 
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 
51L, 52L, 53L, 54L, 55L, 56L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 
81L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 
112L, 113L, 114L, 115L, 116L, 117L, 118L, 133L, 134L, 135L, 136L, 
137L, 138L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 119L, 
120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 66L, 
67L, 68L, 69L, 70L, 71L, 72L, 73L, 94L, 95L, 96L, 97L, 98L, 99L, 
100L, 101L, 130L, 131L, 132L, 148L, 149L, 150L, 151L, 152L, 153L, 
154L, 155L, 156L, 157L, 158L, 159L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 139L, 140L, 141L), class = "data.frame")

我在gamm4-package上应用了gamm4-model:

library(gamm4)
gamm.1 <- gamm4(Y ~ s(X1),random = ~(1+X1|X2),data = dat)

我还预测并使用以下方法绘制平滑值:

newDat <- data.frame(X1 = min(dat$X1):max(dat$X1))
p0 <- predict(gamm.1$gam,newDat,se=T)
plot(dat$X1,dat$Y)
lines(newDat$X1,p0$fit,lwd=3)

我的问题是:如何预测每组(X2)的平滑线? 我知道我可以通过ranef(gamm.1$mer)获得随机效果,但我不知道如何正确使用它们。

0 个答案:

没有答案