forked from perlatex/R_for_Data_Science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bayesian_GLMM.Rmd
326 lines (241 loc) · 6.28 KB
/
bayesian_GLMM.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# 广义线性混合模型 {#bayesian-glmm}
文章数据和模型来源于[Solomon Kurz的课件](https://osf.io/3g8vf/),我在他的基础上用Stan重写了代码
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(tidybayes)
library(rstan)
library(loo)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
```
## 可爱的小狗狗们学习新技能
```{r}
dogs <- read.delim("./demo_data/dogs.txt") %>%
rename(dog = Dog)
```
这里将数据**宽表格**转换成**长表格**的形式
```{r}
dogs <- dogs %>%
pivot_longer(-dog, values_to = "y") %>%
mutate(trial = str_remove(name, "T.") %>% as.double())
head(dogs)
```
## 数据探索
30只狗狗需要学习新技能,每只狗有25次机会学习(`trial` = 0:24),`y`变量(`0` = fail, `1` = success)
这里随机选取8只狗,看它们学习进展情况
```{r}
subset <- sample(1:30, size = 8)
dogs %>%
filter(dog %in% subset) %>%
ggplot(aes(x = trial, y = y)) +
geom_point() +
scale_y_continuous(breaks = 0:1) +
theme(panel.grid = element_blank()) +
facet_wrap(~ dog, ncol = 4, labeller = label_both)
```
## 模型
由于没有更多的数据,因此我们建立最简单的logistic回归模型,并考虑多层结构(Logistic multilevel growth model)
$$
\begin{align*}
\text{y}_{i} & \sim \operatorname{Binomial}(1, \; p_{i}) \\
\operatorname{logit} (p_{i}) & = a_{j[i]} + b_{j[i]} \text{trial}_{i} \\
a_j & = \alpha_0 + u_i \\
b_j & = \beta_0 + v_i \\
\begin{bmatrix} u_i \\ v_i \end{bmatrix} & \sim \operatorname{Normal} \left (
\begin{bmatrix} 0 \\ 0 \end{bmatrix},
\begin{bmatrix} \sigma_u^2 & \\ \sigma_{uv} & \sigma_v^2 \end{bmatrix}
\right ).
\end{align*}
$$
原文中使用的是`lmer4::glmer()`
```{r, eval=FALSE}
library(lme4)
fit1 <- glmer(
data = dogs,
family = binomial,
y ~ 1 + trial + (1 + trial | dog))
summary(fit1)
```
这里用Stan代码重写如下
```{r, warning=FALSE, message=FALSE, eval=FALSE}
stan_program <- "
data {
int N;
int K;
matrix[N, K] X;
int<lower=0, upper=1> y[N];
int J;
int<lower=0, upper=J> g[N];
}
parameters {
array[J] vector[K] beta;
vector[K] MU;
vector<lower=0>[K] tau;
corr_matrix[K] Rho;
}
model {
// for(i in 1:N) {
// p[i] = inv_logit(X[i] * beta[g[i]]);
// }
//
// for(i in 1:N) {
// y[i] ~ bernoulli(p[i]);
// }
for(i in 1:N) {
y[i] ~ bernoulli_logit(X[i] * beta[g[i]]);
}
beta ~ multi_normal(MU, quad_form_diag(Rho, tau));
tau ~ exponential(1);
Rho ~ lkj_corr(2);
}
generated quantities {
vector[N] y_fit;
for(i in 1:N) {
y_fit[i] = inv_logit(X[i] * beta[g[i]]);
}
}
"
stan_data <- dogs %>%
tidybayes::compose_data(
N = n,
K = 2,
J = n_distinct(dog),
g = dog,
y = y,
X = model.matrix(~ 1 + trial, data = .)
)
mod0 <- stan(model_code = stan_program, data = stan_data)
```
模型中加入预测后,更新为
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int N;
int K;
matrix[N, K] X;
int<lower=0, upper=1> y[N];
int J;
int<lower=0, upper=J> g[N];
int M;
matrix[M, K] X_new;
int<lower=0, upper=J> g_new[M];
}
parameters {
array[J] vector[K] beta;
vector[K] MU;
vector<lower=0>[K] tau;
corr_matrix[K] Rho;
}
model {
for(i in 1:N) {
y[i] ~ bernoulli_logit(X[i] * beta[g[i]]);
}
beta ~ multi_normal(MU, quad_form_diag(Rho, tau));
tau ~ exponential(1);
Rho ~ lkj_corr(2);
}
generated quantities {
vector[M] y_epred;
vector[M] y_fit;
vector[M] y_predict;
for(i in 1:M) {
y_epred[i] = inv_logit(X_new[i] * MU);
y_fit[i] = inv_logit(X_new[i] * beta[g_new[i]]);
y_predict[i] = bernoulli_logit_rng(X_new[i] * beta[g_new[i]]);
}
}
"
newdata <-
dogs %>%
tidyr::expand(
dog,
trial = seq(from = 0, to = 24, by = 0.25)
)
stan_data <- dogs %>%
tidybayes::compose_data(
N = n,
K = 2,
J = n_distinct(dog),
g = dog,
y = y,
X = model.matrix(~ 1 + trial, data = .),
M = nrow(newdata),
X_new = model.matrix(~ 1 + trial, data = newdata),
g_new = newdata$dog
)
mod <- stan(model_code = stan_program, data = stan_data)
```
- `y_epred[i]` : 固定效应对应的成功**概率**
- `y_fit[i]` : 固定效应和随机效应,给出的是每只小狗的成功**概率**
- `y_predict[i]` : 每只小狗的预测结果(0和1)
## 结果
### 看看每只小狗的成长曲线
```{r}
fit <- mod %>%
tidybayes::gather_draws(y_fit[i]) %>%
ggdist::mean_qi(.value) # 不知道为什么ggdist::mean_hdi() 会多出几行
fit %>%
bind_cols(newdata) %>%
ggplot(aes(x = trial, y = .value, group = dog)) +
geom_line() +
theme(legend.position = "none")
```
### 看看小狗们平均成长曲线
```{r}
epred <- mod %>%
tidybayes::gather_draws(y_epred[i]) %>%
ggdist::mean_qi(.value)
epred %>%
bind_cols(newdata) %>%
filter(dog == 1) %>%
ggplot(aes(x = trial, y = .value, ymin = .lower, ymax = .upper)) +
geom_lineribbon() +
theme(legend.position = "none")
```
### 两张图画在一起
```{r}
m <- epred %>%
bind_cols(newdata) %>%
filter(dog == 1)
fit %>%
bind_cols(newdata) %>%
ggplot(aes(x = trial, y = .value, group = dog)) +
geom_lineribbon(
data = m,
aes(ymin = .lower, ymax = .upper)
) +
geom_line()
```
### 每只狗狗的原始数据和成长曲线画在一起
```{r}
fit %>%
bind_cols(newdata) %>%
filter(dog %in% subset) %>%
ggplot(aes(x = trial, y = .value, group = dog)) +
geom_lineribbon(aes(ymin = .lower, ymax = .upper)) +
geom_vline(xintercept = 2:3 * 5, color = "white") +
geom_hline(yintercept = c(.5, .8), color = "white") +
geom_point(
data = dogs %>% filter(dog %in% subset) ,
aes(y = y)
) +
labs(
subtitle = "Learning curves for a random sample of individual dogs",
y = "success probability") +
scale_y_continuous(
breaks = c(0, .5, .8, 1), labels = c("0", ".5", ".8", "1")
) +
theme(
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none"
) +
facet_wrap(~ dog, ncol = 4, labeller = label_both)
```
## 参考
- <https://osf.io/3g8vf/>
- <https://bookdown.org/roback/bookdown-BeyondMLR/>
```{r, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```