-
Notifications
You must be signed in to change notification settings - Fork 1
/
sleepstudy.Rmd
616 lines (461 loc) · 10.9 KB
/
sleepstudy.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# 睡眠剥夺后的反应时间 {#sleepstudy}
```{r libraries, echo = FALSE}
library(tidyverse)
library(tidybayes)
library(bayesplot)
library(rstan)
library(loo)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
```
```{r}
data("sleepstudy", package = "lme4")
sleepstudy
```
```{r}
sleepstudy %>%
ggplot(aes(Days, Reaction)) +
geom_point()
```
```{r}
sleepstudy %>%
mutate(cond = paste0("Subject = ", Subject)) %>%
ggplot(aes(Days, Reaction)) +
geom_point() +
facet_wrap("cond", nrow = 3) +
theme_bw()
```
## Linear MLMs: Varying Intercepts and Varying Slopes
$$
\begin{aligned}
y_n &\sim \mathcal{N}(\mu_n, \sigma)\\
\mu_n &= b_{0j[n]} + b_{1j[n]} x_n \\
(b_{0j}, b_{1j}) &\sim \mathcal{MN}((b_0, b_1), \Sigma_{b}) \\
\end{aligned}
$$
$$
\Sigma_{b} = \left(
\begin{matrix}
\sigma_{b_0}^2 & \sigma_{b_0} \sigma_{b_1} \rho_{b_0 b_1} \\
\sigma_{b_0} \sigma_{b_1} \rho_{b_0 b_1} & \sigma_{b_1}^2
\end{matrix}
\right)
$$
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int N;
vector[N] y;
vector[N] x;
int J;
int<lower=1, upper=J> g[N];
}
parameters {
vector[J] alpha;
vector[J] beta;
real a;
real b;
real<lower=0> sigma;
corr_matrix[2] Rho;
vector<lower=0>[2] sigma_g;
}
transformed parameters {
vector[N] mu;
for (i in 1:N) {
mu[i] = alpha[g[i]] + beta[g[i]] * x[i];
}
}
model {
for(i in 1:N) {
target += normal_lpdf(y[i] | mu[i], sigma);
}
for(j in 1:J) {
[alpha[j], beta[j]]' ~ multi_normal([a, b]', quad_form_diag(Rho, sigma_g));
}
sigma ~ exponential(1);
a ~ normal(0, 1);
b ~ normal(0, 1);
Rho ~ lkj_corr(2);
sigma_g ~ exponential(1);
}
"
stan_data <- sleepstudy %>%
tidybayes::compose_data(
N = nrow(.),
x = Days,
y = Reaction,
J = n_distinct(Subject),
g = Subject
)
fit_mlm1 <- stan(model_code = stan_program, data = stan_data)
```
```{r}
fit_mlm1
```
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int N;
vector[N] y;
vector[N] x;
int J;
int<lower=1, upper=J> g[N];
}
parameters {
vector[J] alpha;
vector[J] beta;
real a;
real b;
real<lower=0> sigma;
corr_matrix[2] Rho;
vector<lower=0>[2] tau;
}
transformed parameters {
vector[2] YY[J];
vector[2] MU;
MU = [a, b]';
for (j in 1:J) {
YY[j] = [alpha[j], beta[j]]';
}
}
model {
vector[N] mu;
for (i in 1:N) {
mu[i] = alpha[g[i]] + beta[g[i]] * x[i];
}
for(i in 1:N) {
target += normal_lpdf(y[i] | mu[i], sigma);
}
for(j in 1:J) {
YY ~ multi_normal(MU, quad_form_diag(Rho, tau));
}
sigma ~ exponential(1);
a ~ normal(0, 1);
b ~ normal(0, 1);
Rho ~ lkj_corr(2);
tau ~ exponential(1);
}
"
stan_data <- sleepstudy %>%
tidybayes::compose_data(
N = nrow(.),
x = Days,
y = Reaction,
J = n_distinct(Subject),
g = Subject
)
fit_mlm2 <- stan(model_code = stan_program, data = stan_data)
```
```{r}
fit_mlm2
```
## 用stan-book的方法
系数设定为 array of vector
上面的方法是通过[alpha, beta]',拼凑成vector,目的是要构造成multi_normal()所需要的vector输入, 现在这一个是用 for(i in 1:n_group) 循环即可。
之所以能用 for 循环,是因为后者把系数定义成 array of vector 形式,一个vector的样子就像一根糖葫芦,一列一列的喂进去。
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int N;
int K;
matrix[N, K] X;
int J;
int<lower=1, upper=J> g[N];
vector[N] y;
}
parameters {
vector[K] beta[J]; // array of vector
vector[K] gamma; // fix effect
real<lower=0> sigma;
corr_matrix[K] Rho;
vector<lower=0>[K] tau;
}
transformed parameters {
vector[N] mu;
for (i in 1:N) {
mu[i] = X[i] * beta[g[i]];
}
}
model {
for(i in 1:N) {
target += normal_lpdf(y[i] | mu[i], sigma);
}
for(j in 1:J) {
beta[j] ~ multi_normal(gamma, quad_form_diag(Rho, tau));
}
sigma ~ exponential(1);
gamma ~ normal(0, 5);
Rho ~ lkj_corr(2);
tau ~ exponential(1);
}
"
stan_data <- sleepstudy %>%
tidybayes::compose_data(
N = nrow(.),
K = 2,
X = model.matrix(~ Days, .),
y = Reaction,
J = n_distinct(Subject),
g = Subject
)
fit_mlm3 <- stan(model_code = stan_program, data = stan_data)
```
```{r}
fit_mlm3
```
## 上面方法的**矢量化**优化
Optimization through Vectorization
```
for(i in 1:N) {
target += normal_lpdf(y[i] | mu[i], sigma);
} // for循环 log of simga 要循环N次
y ~ normal(mu, sigma); // 只计算一次
```
当然要平衡和兼顾**代码执行效率和代码可读性**
当前版本,个人感觉是最佳的
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int N;
int K;
matrix[N, K] X;
int J;
int<lower=1, upper=J> g[N];
vector[N] y;
}
parameters {
vector[K] beta[J]; // array of vector
vector[K] MU; // fix effect
real<lower=0> sigma;
corr_matrix[K] Rho;
vector<lower=0>[K] tau;
}
model {
vector[N] mu;
for (i in 1:N) {
mu[i] = X[i] * beta[g[i]];
}
y ~ normal(mu, sigma);
for(j in 1:J) {
beta[j] ~ multi_normal(MU, quad_form_diag(Rho, tau));
}
sigma ~ exponential(1);
MU ~ normal(0, 5);
Rho ~ lkj_corr(2);
tau ~ exponential(1);
}
generated quantities {
vector[N] y_rep;
for (n in 1:N) {
y_rep[n] = normal_rng(X[n] * beta[g[n]], sigma);
}
}
"
stan_data <- sleepstudy %>%
tidybayes::compose_data(
N = nrow(.),
K = 2,
J = n_distinct(Subject),
X = model.matrix(~ 1 + Days, .),
y = Reaction,
g = Subject
)
fit_mlm4 <- stan(model_code = stan_program, data = stan_data)
```
## Cholesky因子分解优化版(待理解)
这里**非中心化参数**,先给定一个 z (形式是矩阵,分布是标准正态), 通过z构建系数beta,(待理解)
- beta 是矩阵[J, K](注意与array of vector的结构不同),这里是一行一行的看,一行代表(intercept , beta_1, beta2, ...),因此得这样写`y ~ normal(rows_dot_product(beta[g], x), sigma);` beta[g]在前。
- `beta = gamma + (diag_pre_multiply(tau, L_Omega) * z)';` 矢量化的循环,是对结构的最外层开始的, 矩阵矢量化先分解beta[i],代表一行一行的。
- 疑问,matrix[J, K] gamma;是干什么用的,为何是矩阵?还不明白。
若这样写不能让代码效率不显著提升的,可以先不管。
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int N;
int K;
matrix[N, K] X;
int J;
int<lower=1, upper=J> g[N];
vector[N] y;
}
parameters {
matrix[K, J] z;
cholesky_factor_corr[K] L_Omega;
matrix[J, K] gamma;
real<lower=0> sigma;
vector<lower=0, upper=pi()/2>[K] tau_unif;
}
transformed parameters {
matrix[J, K] beta; //
vector<lower=0>[K] tau; //prior scale
for (k in 1:K) {
tau[k] = 2.5 * tan(tau_unif[k]);
}
beta = gamma + (diag_pre_multiply(tau, L_Omega) * z)';
}
model {
to_vector(z) ~ std_normal();
L_Omega ~ lkj_corr_cholesky(2);
to_vector(gamma) ~ normal(0, 5);
y ~ normal(rows_dot_product(beta[g], X), sigma);
}
"
stan_data <- sleepstudy %>%
tidybayes::compose_data(
N = nrow(.),
K = 2,
X = model.matrix(~ Days, .),
y = Reaction,
J = n_distinct(Subject),
g = Subject
)
fit_mlm5 <- stan(model_code = stan_program, data = stan_data)
```
```{r}
fit_mlm5
```
## 用 fit_mlm4 分析
```{r}
fit_mlm4 %>% write_rds(here::here("stan_save", "fit_mlm4.rds"))
fit_mlm4 <- read_rds(here::here("stan_save", "fit_mlm4.rds"))
```
```{r}
summary(fit_mlm4, c("MU"))$summary
```
```{r}
summary(fit_mlm4, c("y_rep"))$summary
```
```{r}
y_rep <- as.matrix(fit_mlm4, pars = "y_rep")
bayesplot::ppc_dens_overlay(y = sleepstudy$Reaction, yrep = y_rep[1:200, ])
```
```{r}
y_rep <- as.matrix(fit_mlm4, pars = "y_rep")
bayesplot::ppc_intervals(y = sleepstudy$Reaction,
yrep = y_rep,
x = sleepstudy$Days
)
```
```{r}
fit_mlm4 %>%
tidybayes::spread_draws(y_rep[i]) %>%
tidybayes::mean_qi() %>%
dplyr::bind_cols(sleepstudy)
```
```{r}
fit_mlm4 %>%
tidybayes::spread_draws(y_rep[i]) %>%
tidybayes::mean_qi() %>%
dplyr::bind_cols(sleepstudy) %>%
mutate(cond = paste0("Subject = ", Subject)) %>%
ggplot(aes(x = Days, y = y_rep), size = 2) +
geom_point(aes(x = Days, y = Reaction), size = 2) +
geom_line(color = "orange") +
geom_ribbon(aes(ymin = .lower, ymax = .upper),
alpha = 0.3,
fill = "gray50"
) +
facet_wrap(vars(cond), ncol = 6) +
theme_bw()
```
- 返回180 * 4000个样本,然后按照 i= 180 分组(18个人,每人10天),也就4000个抽样弄成一个数。
- 这里希望 180 * 4000样本,希望按照10天分组(希望横坐标为Days= c(0:9) 天)
```{r}
sleepstudy_i <- sleepstudy %>%
mutate(i = 1:n())
fit_mlm4_by_days <- fit_mlm4 %>%
tidybayes::spread_draws(y_rep[i]) %>%
ungroup() %>%
dplyr::left_join(
sleepstudy_i, by = "i"
) %>%
group_by(Days) %>%
tidybayes::mean_qi(y_rep, .width = c(.50))
fit_mlm4_by_days
```
```{r, fig.width=4, fig.height= 6}
p2 <- fit_mlm4_by_days %>%
ggplot(aes(x = Days, y = y_rep), size = 2) +
geom_line() +
geom_ribbon(aes(ymin = .lower, ymax = .upper),
alpha = 0.3,
fill = "gray50"
)
p2
```
重复的 marginal_effects()?和作者的一样?作者为了对比,没有分层的的对比,我也试试看
pauer用的 marginal_effects() 这个函数是怎么回事?
## 简单线性回归
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int N;
int K;
matrix[N, K] X;
vector[N] y;
}
parameters {
vector[K] beta;
real<lower=0> sigma;
}
model {
vector[N] mu;
for (i in 1:N) {
mu[i] = X[i] * beta;
}
y ~ normal(mu, sigma);
sigma ~ exponential(1);
}
generated quantities {
vector[N] y_rep;
for (n in 1:N) {
y_rep[n] = normal_rng(X[n] * beta, sigma);
}
}
"
stan_data <- sleepstudy %>%
tidybayes::compose_data(
N = nrow(.),
K = 2,
X = model.matrix(~ 1 + Days, .),
y = Reaction,
)
fit_lm <- stan(model_code = stan_program, data = stan_data)
```
```{r, fig.width=4, fig.height= 6}
sleepstudy_i <- sleepstudy %>%
mutate(i = 1:n())
fit_lm_by_days <- fit_lm %>%
tidybayes::spread_draws(y_rep[i]) %>%
ungroup() %>%
dplyr::left_join(
sleepstudy_i, by = "i"
) %>%
group_by(Days) %>%
tidybayes::mean_qi(y_rep, .width = c(.50))
p1 <- fit_lm_by_days %>%
ggplot(aes(x = Days, y = y_rep), size = 2) +
geom_line() +
geom_ribbon(aes(ymin = .lower, ymax = .upper),
alpha = 0.3,
fill = "gray50"
)
p1
```
```{r}
library(patchwork)
p1 + p2
```
与作者的图,还是很大差距,感觉我的方法是不对的
```
?brms::marginal_effects
```
## brms
```{r}
library(brms)
fit_brms <- brm(Reaction ~ Days + (Days | Subject),
data = sleepstudy)
```
```{r}
fit_brms
```