-
Notifications
You must be signed in to change notification settings - Fork 1
/
wait_coffe.Rmd
839 lines (646 loc) · 18.1 KB
/
wait_coffe.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# 咖啡等待时间 {#wait-coffe}
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(brms)
library(tidybayes)
library(bayesplot)
library(rstan)
library(patchwork)
options(mc.cores = 4)
```
# Section 14.1: Varying slopes by construction
```{r}
a <- 3.5 # average morning wait time
b <- -1 # average difference afternoon wait time
sigma_a <- 1 # std dev in intercepts
sigma_b <- 0.5 # std dev in slopes
rho <- -.7 # correlation between intercepts and slopes
# the next three lines of code simply combine the terms, above
mu <- c(a, b)
cov_ab <- sigma_a * sigma_b * rho
sigma <- matrix(c(sigma_a^2, cov_ab,
cov_ab, sigma_b^2), ncol = 2)
```
```{r, message = F, warning = F}
sigmas <- c(sigma_a, sigma_b) # standard deviations
rho <- matrix(c(1, rho, # correlation matrix
rho, 1), nrow = 2)
# now matrix multiply to get covariance matrix
sigma <- diag(sigmas) %*% rho %*% diag(sigmas)
# how many cafes would you like?
n_cafes <- 20
set.seed(5) # used to replicate example
vary_effects <-
MASS::mvrnorm(n_cafes, mu, sigma) %>%
data.frame() %>%
set_names("a_cafe", "b_cafe")
head(vary_effects)
```
```{r}
n_visits <- 10
sigma <- 0.5 # std dev within cafes
set.seed(22) # used to replicate example
d <-
vary_effects %>%
mutate(cafe = 1:n_cafes) %>%
expand(nesting(cafe, a_cafe, b_cafe), visit = 1:n_visits) %>%
mutate(afternoon = rep(0:1, times = n() / 2)) %>%
mutate(mu = a_cafe + b_cafe * afternoon) %>%
mutate(wait = rnorm(n = n(), mean = mu, sd = sigma))
```
We might peek at the data.
```{r}
d %>%
glimpse()
```
```{r}
d1 <- d %>% select(wait, afternoon, cafe)
d1
```
```{r}
d1 %>% write_rds("./data/cafe.rds")
```
```{r}
d <- readr::read_rds("./data/cafe.rds")
```
### The varying slopes model.
The statistical formula for our varying intercepts and slopes café model follows the form
$$
\begin{align*}
\text{wait}_i & \sim \operatorname{Normal}(\mu_i, \sigma) \\
\mu_i & = \alpha_{\text{café}[i]} + \beta_{\text{café}[i]} \text{afternoon}_i \\
\begin{bmatrix} \alpha_\text{café} \\ \beta_\text{café} \end{bmatrix} & \sim \operatorname{MVNormal} \begin{pmatrix} \begin{bmatrix} \alpha \\ \beta \end{bmatrix}, \mathbf{S} \end{pmatrix} \\
\mathbf S & = \begin{bmatrix} \sigma_\alpha & 0 \\ 0 & \sigma_\beta \end{bmatrix} \mathbf R \begin{bmatrix} \sigma_\alpha & 0 \\ 0 & \sigma_\beta \end{bmatrix} \\
\alpha & \sim \operatorname{Normal}(5, 2) \\
\beta & \sim \operatorname{Normal}(-1, 0.5) \\
\sigma & \sim \operatorname{Exponential}(1) \\
\sigma_\alpha & \sim \operatorname{Exponential}(1) \\
\sigma_\beta & \sim \operatorname{Exponential}(1) \\
\mathbf R & \sim \operatorname{LKJcorr}(2),
\end{align*}
$$
感觉这里没说清楚,比如$\alpha_{cafe[i]}$ 应该是两个,一个是固定效应,一个变化效应
各自的分布是什么呢?
stan2 很简洁
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int n;
int n_cafe;
int cafe[n];
vector[n] afternoon;
vector[n] wait;
}
parameters {
vector[n_cafe] a_cafe;
vector[n_cafe] b_cafe;
real a;
real b;
vector<lower=0>[2] sigma_cafe;
real<lower=0> sigma;
corr_matrix[2] Rho;
}
model {
vector[n] mu;
vector[2] YY[n_cafe];
vector[2] MU;
Rho ~ lkj_corr(2);
sigma ~ exponential(1);
sigma_cafe ~ exponential(1);
a ~ normal(5, 2);
b ~ normal(-1, .5);
MU = [a, b]';
for (j in 1:n_cafe) {
YY[j] = [a_cafe[j], b_cafe[j]]';
}
YY ~ multi_normal(MU, quad_form_diag(Rho, sigma_cafe));
mu = a_cafe[cafe] + b_cafe[cafe] .* afternoon;
wait ~ normal(mu, sigma);
}
"
stan_data <- compose_data(d,
n_cafe = n_distinct(cafe)
)
m14.1a <- stan(model_code = stan_program, data = stan_data)
```
```{r}
m14.1a
```
用stan1的代码, 结构比较清晰
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int n; //多少观察值
int n_cafe; //分了多少组
int cafe[n];
real wait[n];
int afternoon[n];
}
parameters {
real a;
real b;
vector[n_cafe] a_cafe;
vector[n_cafe] b_cafe;
vector<lower=0>[2] sigma_cafe;
real<lower=0> sigma;
corr_matrix[2] Rho;
}
transformed parameters {
vector[2] MU;
vector[2] v_a_cafe_b_cafe[n_cafe];
cov_matrix[2] SRS_sigma_cafeRho;
MU = [a, b]';
for (j in 1:n_cafe) v_a_cafe_b_cafe[j] = [a_cafe[j], b_cafe[j]]';
SRS_sigma_cafeRho = quad_form_diag(Rho, sigma_cafe);
}
model {
vector[n] mu;
// priors
target += normal_lpdf(a| 5, 2);
target += normal_lpdf(b| -1, 0.5);
target += exponential_lpdf(sigma| 1);
target += exponential_lpdf(sigma_cafe| 1);
target += lkj_corr_lpdf(Rho| 2);
// linear model
for(i in 1:n) {
mu[i] = a_cafe[cafe[i]] + b_cafe[cafe[i]] * afternoon[i];
}
target += normal_lpdf(wait |mu, sigma);
target += multi_normal_lpdf(v_a_cafe_b_cafe | MU, SRS_sigma_cafeRho);
}
"
stan_data <- d %>%
tidybayes::compose_data(
n_cafe = n_distinct(cafe)
)
m14.1b <- stan(model_code = stan_program, data = stan_data)
```
```{r}
m14.1b
```
我的目的是:语法简介,结构也清晰
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int n;
int n_cafe;
int cafe[n];
vector[n] afternoon;
vector[n] wait;
}
parameters {
vector[n_cafe] a_cafe;
vector[n_cafe] b_cafe;
real a;
real b;
vector<lower=0>[2] sigma_cafe;
real<lower=0> sigma;
corr_matrix[2] Rho;
}
transformed parameters {
vector[2] YY[n_cafe];
vector[2] MU;
MU = [a, b]';
for (j in 1:n_cafe) {
YY[j] = [a_cafe[j], b_cafe[j]]';
}
}
model {
vector[n] mu;
sigma ~ exponential(1);
sigma_cafe ~ exponential(1);
a ~ normal(5, 2);
b ~ normal(-1, .5);
Rho ~ lkj_corr(2);
mu = a_cafe[cafe] + b_cafe[cafe] .* afternoon;
//YY ~ multi_normal(MU, quad_form_diag(Rho, sigma_cafe));
target += multi_normal_lpdf(YY | MU, quad_form_diag(Rho, sigma_cafe));
wait ~ normal(mu, sigma);
}
"
stan_data <- d %>%
tidybayes::compose_data(
n_cafe = n_distinct(cafe)
)
m14.1c <- stan(model_code = stan_program, data = stan_data)
```
```{r}
m14.1c
summary(m14.1c, c("Rho"))$summary
```
```{r, warning=FALSE, message=FALSE, results=FALSE}
datplot <- m14.1c %>%
spread_draws(Rho[i, j]) %>%
filter(i == 1, j == 2)
ggplot(datplot, aes(Rho)) +
geom_density() +
xlim(-1, 1) +
xlab('Correlation')
```
这里我用系数矩阵的方法写一遍
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int n;
int n_cafe;
int cafe[n];
vector[n] afternoon;
vector[n] wait;
}
parameters {
matrix[n_cafe, 2] a_cafe;
real a;
real b;
vector<lower=0>[2] sigma_cafe;
real<lower=0> sigma;
corr_matrix[2] Rho;
}
transformed parameters {
vector[2] YY[n_cafe];
vector[2] MU;
MU = [a, b]';
}
model {
vector[n] mu;
sigma ~ exponential(1);
sigma_cafe ~ exponential(1);
a ~ normal(5, 2);
b ~ normal(-1, .5);
Rho ~ lkj_corr(2);
for (i in 1:n) {
mu[i] = a_cafe[cafe[i], 1] + a_cafe[cafe[i], 2] * afternoon[i];
}
for (j in 1:n_cafe) {
a_cafe[j, 1:2] ~ multi_normal(MU, quad_form_diag(Rho, sigma_cafe));
}
wait ~ normal(mu, sigma);
}
"
stan_data <- d %>%
tidybayes::compose_data(
n_cafe = n_distinct(cafe)
)
m14.1d <- stan(model_code = stan_program, data = stan_data)
```
```{r}
m14.1d
```
用模型矩阵+ 系数矩阵 both完成
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int n; //obs
int K; //num of coef including intercept
int n_cafe; //group deepth levels
int cafe[n]; //group index
vector[n] wait; //Y variables
matrix[n, K] X; //model matrix
}
parameters {
matrix[n_cafe, K] coef;
real a;
real b;
vector<lower=0>[K] sigma_cafe;
real<lower=0> sigma;
corr_matrix[K] Rho;
}
transformed parameters {
vector[2] MU;
MU = [a, b]';
}
model {
vector[n] mu;
matrix[n, n_cafe] temp;
temp = X * coef';
for (i in 1:n) {
mu[i] = temp[i, cafe[i]];
}
sigma ~ exponential(1);
sigma_cafe ~ exponential(1);
a ~ normal(5, 2);
b ~ normal(-1, .5);
Rho ~ lkj_corr(2);
for (j in 1:n_cafe) {
coef[j, 1:2] ~ multi_normal(MU, quad_form_diag(Rho, sigma_cafe));
}
wait ~ normal(mu, sigma);
}
"
stan_data <- d %>%
tidybayes::compose_data(
K = 2,
wait = wait,
n_cafe = n_distinct(cafe),
cafe = cafe,
X = model.matrix(~afternoon, .)
)
m14.1e <- stan(model_code = stan_program, data = stan_data)
```
```{r}
m14.1e
```
## 用stan-book中的方法
模型矩阵的数据 + `array of vector` 形式的系数
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int N; // number of obs
int K; // number of predictors
matrix[N, K] X; // model_matrix
vector[N] y; // y
int J; // number of grouping
int<lower=1, upper=J> g[N]; // index for grouping
}
parameters {
vector[K] beta[J];
vector[K] MU;
real<lower=0> sigma;
vector<lower=0>[K] tau;
corr_matrix[K] Rho;
}
model {
vector[N] mu;
sigma ~ exponential(1);
tau ~ exponential(1);
Rho ~ lkj_corr(2);
for(i in 1:N) {
mu[i] = X[i] * beta[g[i]];
}
y ~ normal(mu, sigma);
beta ~ multi_normal(MU, quad_form_diag(Rho, tau));
}
"
stan_data <- d %>%
tidybayes::compose_data(
N = n,
K = 2,
J = n_distinct(cafe),
g = cafe,
y = wait,
X = model.matrix(~ 1 + afternoon, data = .)
)
m14.1f <- stan(model_code = stan_program, data = stan_data)
```
```{r}
summary(m14.1c, c("a", "b"))$summary
summary(m14.1f, c("MU"))$summary
summary(m14.1c, c("a_cafe"))$summary
summary(m14.1f, c("beta"))$summary
```
## 用brms
```{r b14.1}
b14.1 <-
brm(data = d,
family = gaussian,
wait ~ 1 + afternoon + (1 + afternoon | cafe),
prior = c(prior(normal(5, 2), class = Intercept),
prior(normal(-1, 0.5), class = b),
prior(exponential(1), class = sd),
prior(exponential(1), class = sigma),
prior(lkj(2), class = cor)),
iter = 2000, warmup = 1000, chains = 4, cores = 4,
seed = 867530,
file = "fits/b14.01")
```
## Advanced varying slopes
In [Section 13.3][More than one type of cluster] we saw that data can be considered **cross-classified** if they have multiple grouping factors. We used the `chipanzees` data in that section and we only considered cross-cassification by single intercepts. Turns out cross-classified models can be extended further. Let's load and wrangle those data.
```{r}
df <- read_rds("data/chimpabzees.rds")
df
```
```{r, warning = F, message = F}
# treatment 弄成因子,事实上为了让tidybayes::compose_data()方便转化成数值,
# 最后用stan 读成 int
d <-
df %>%
mutate(treatment = factor(1 + prosoc_left + 2 * condition)) #
d
```
```{r}
d %>% count(treatment)
d %>% count(actor)
d %>% count(block)
```
If I'm following along correctly with the text, McElreath's `m14.2` uses the centered parameterization. Recall from the last chapter that **brms** only supports the non-centered parameterization. Happily, McElreath's `m14.3` appears to use the non-centered parameterization. Thus, we'll skip making a `b14/2` and jump directly into making a `b14.3`. I believe one could describe the statistical model as
$$
\begin{align*}
\text{left_pull}_i & \sim \operatorname{Binomial}(n_i = 1, p_i) \\
\operatorname{logit} (p_i) & = \gamma_{\text{treatment}[i]} + \alpha_{\text{actor}[i], \text{treatment}[i]} + \beta_{\text{block}[i], \text{treatment}[i]} \\
\gamma_j & \sim \operatorname{Normal}(0, 1), \;\;\; \text{for } j = 1..4 \\
\begin{bmatrix} \alpha_{j, 1} \\ \alpha_{j, 2} \\ \alpha_{j, 3} \\ \alpha_{j, 4} \end{bmatrix} & \sim \operatorname{MVNormal} \begin{pmatrix} \begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \end{bmatrix}, \mathbf \Sigma_\text{actor} \end{pmatrix} \\
\begin{bmatrix} \beta_{j, 1} \\ \beta_{j, 2} \\ \beta_{j, 3} \\ \beta_{j, 4} \end{bmatrix} & \sim \operatorname{MVNormal} \begin{pmatrix} \begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \end{bmatrix}, \mathbf \Sigma_\text{block} \end{pmatrix} \\
\mathbf \Sigma_\text{actor} & = \mathbf{S_\alpha R_\alpha S_\alpha} \\
\mathbf \Sigma_\text{block} & = \mathbf{S_\beta R_\beta S_\beta} \\
\sigma_{\alpha, [1]}, ..., \sigma_{\alpha, [4]} & \sim \operatorname{Exponential}(1) \\
\sigma_{\beta, [1]}, ..., \sigma_{\beta, [4]} & \sim \operatorname{Exponential}(1) \\
\mathbf R_\alpha & \sim \operatorname{LKJ}(2) \\
\mathbf R_\beta & \sim \operatorname{LKJ}(2).
\end{align*}
$$
```{r}
glimpse(d)
```
先自己写,好像不能用block 这个在stan有特殊含义,所以改为block_id
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int n;
int n_actors;
int n_blocks;
int n_treatment;
int actor[n];
int block_id[n];
int treatment[n];
int pulled_left[n];
}
parameters {
vector[n_treatment] gamma;
vector[n_treatment] alpha[n_actors];
vector[n_treatment] beta[n_blocks];
vector<lower=0>[4] sigma_actor;
vector<lower=0>[4] sigma_block;
corr_matrix[4] rho_actor;
corr_matrix[4] rho_block;
}
model {
vector[n] p;
gamma ~ normal(0, 1);
sigma_actor ~ exponential(1);
sigma_block ~ exponential(1);
rho_actor ~ lkj_corr(4);
rho_block ~ lkj_corr(4);
for (i in 1:n) {
p[i] = gamma[treatment[i]] +
alpha[actor[i], treatment[i]] +
beta[block_id[i], treatment[i]];
p[i] = inv_logit(p[i]);
}
//alpha ~ multi_normal(rep_vector(0, 4),
// quad_form_diag(rho_actor, sigma_actor));
//beta ~ multi_normal(rep_vector(0, 4),
// quad_form_diag(rho_block, sigma_block));
for(j in 1:n_actors) {
alpha[j] ~ multi_normal(rep_vector(0, 4), quad_form_diag(rho_actor, sigma_actor));
}
for(j in 1:n_blocks){
beta[j] ~ multi_normal(rep_vector(0, 4), quad_form_diag(rho_block, sigma_block));
}
pulled_left ~ binomial(1, p);
}
"
stan_data <- d %>%
rename(block_id = block) %>%
tidybayes::compose_data(
n_actors = n_distinct(actor),
n_blocks = n_distinct(block_id),
n_treatment = n_distinct(treatment)
)
m14.2 <- stan(model_code = stan_program, data = stan_data)
```
```{r}
m14.2
```
用矩阵matrix装系数也是一样的效果
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int n;
int n_actors;
int n_blocks;
int n_treatment;
int actor[n];
int block_id[n];
int treatment[n];
int pulled_left[n];
}
parameters {
vector[n_treatment] gamma;
matrix[n_actors, n_treatment] alpha;
matrix[n_blocks, n_treatment] beta;
vector<lower=0>[n_treatment] sigma_actor;
vector<lower=0>[n_treatment] sigma_block;
corr_matrix[n_treatment] rho_actor;
corr_matrix[n_treatment] rho_block;
}
model {
vector[n] p;
gamma ~ normal(0,1);
sigma_actor ~ exponential(1);
sigma_block ~ exponential(1);
rho_actor ~ lkj_corr(2);
rho_block ~ lkj_corr(2);
for (i in 1:n) {
p[i] = gamma[treatment[i]] +
alpha[actor[i], treatment[i]] +
beta[block_id[i], treatment[i]];
p[i] = inv_logit(p[i]);
}
for(j in 1:n_actors) {
alpha[j] ~ multi_normal(rep_vector(0, 4), quad_form_diag(rho_actor, sigma_actor));
}
for(j in 1:n_blocks){
beta[j] ~ multi_normal(rep_vector(0, 4), quad_form_diag(rho_block, sigma_block));
}
pulled_left ~ binomial(1, p);
}
"
stan_data <- d %>%
rename(block_id = block) %>%
tidybayes::compose_data(
n_actors = n_distinct(actor),
n_blocks = n_distinct(block_id),
n_treatment = n_distinct(treatment)
)
m14.2a <- stan(model_code = stan_program, data = stan_data)
```
```{r}
m14.2a
```
## 模型14.3
这个的数学公式在哪里?
```{r, warning=FALSE, message=FALSE, results=FALSE}
stan_program <- "
data {
int n;
int n_actors;
int n_blocks;
int n_treatment;
int actor[n];
int block_id[n];
int treatment[n];
int pulled_left[n];
}
parameters {
matrix[n_treatment, n_actors] z_actor;
matrix[n_treatment, n_blocks] z_block;
vector[n_treatment] g;
vector<lower=0>[n_treatment] sigma_actor;
vector<lower=0>[n_treatment] sigma_block;
cholesky_factor_corr[n_treatment] L_Rho_block;
cholesky_factor_corr[n_treatment] L_Rho_actor;
}
transformed parameters {
matrix[n_actors, n_treatment] alpha;
matrix[n_blocks, n_treatment] beta;
beta = (diag_pre_multiply(sigma_block, L_Rho_block) * z_block)';
alpha = (diag_pre_multiply(sigma_actor, L_Rho_actor) * z_actor)';
}
model{
vector[n] p;
L_Rho_block ~ lkj_corr_cholesky( 2 );
sigma_block ~ exponential( 1 );
L_Rho_actor ~ lkj_corr_cholesky( 2 );
sigma_actor ~ exponential( 1 );
g ~ normal( 0 , 1 );
to_vector( z_block ) ~ normal( 0 , 1 );
to_vector( z_actor ) ~ normal( 0 , 1 );
for ( i in 1:n ) {
p[i] = g[treatment[i]] + alpha[actor[i], treatment[i]] + beta[block_id[i], treatment[i]];
p[i] = inv_logit(p[i]);
}
pulled_left ~ binomial( 1 , p );
}
"
stan_data <- d %>%
rename(block_id = block) %>%
tidybayes::compose_data(
n_actors = n_distinct(actor),
n_blocks = n_distinct(block_id),
n_treatment = n_distinct(treatment)
)
m14.3 <- stan(model_code = stan_program, data = stan_data)
```
```{r}
m14.3
```
brms版本
```{r, warning = F, message = F}
# wrangle.
d1 <-
df %>%
mutate(actor = factor(actor),
block = factor(block),
treatment = factor(1 + prosoc_left + 2 * condition),
# this will come in handy, later
labels = factor(treatment,
levels = 1:4,
labels = c("r/n", "l/n", "r/p", "l/p")))
d1
```
```{r b14.3}
b14.3 <-
brm(data = d1,
family = binomial,
pulled_left | trials(1) ~ 0 + treatment + (0 + treatment | actor) + (0 + treatment | block),
prior = c(prior(normal(0, 1), class = b),
prior(exponential(1), class = sd, group = actor),
prior(exponential(1), class = sd, group = block),
prior(lkj(2), class = cor, group = actor),
prior(lkj(2), class = cor, group = block)),
iter = 2000, warmup = 1000, chains = 4, cores = 4,
seed = 4387510,
file = "fits/b14.03")
```
```{r}
b14.3
```