-
Notifications
You must be signed in to change notification settings - Fork 2
/
usage.Rmd
720 lines (629 loc) · 30.8 KB
/
usage.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
---
title: "Usage"
output:
rmarkdown::html_vignette:
toc: true
number_sections: true
vignette: >
%\VignetteIndexEntry{Usage}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
A mixed model of repeated measures (MMRM) analyzes longitudinal clinical trial data. In a longitudinal dataset, there are multiple patients, and each patient has multiple observations at a common set of discrete points in time.
# Data
To use the `brms.mmrm` package, begin with a longitudinal dataset with one row per patient observation and columns for the response variable, treatment group indicator, discrete time point indicator, patient ID variable, and optional baseline covariates such as age and region. If you do not have a real dataset of your own, you can simulate one from the package. The following dataset has the raw response variable, the essential factor variables, and continuous baseline covariates.^[Covariates can be categorical too.] In general, the outcome variable can either be the raw response or change from baseline.
```r
library(brms.mmrm)
library(dplyr)
library(magrittr)
set.seed(0L)
raw_data <- brm_simulate_simple(
n_group = 3,
n_patient = 100,
n_time = 4
) %>%
extract2("data") %>%
brm_simulate_continuous(c("biomarker1", "biomarker2", "biomarker3"))
raw_data
#> # A tibble: 1,200 × 7
#> response group time patient biomarker1 biomarker2 biomarker3
#> <dbl> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 1.11 group_1 time_1 patient_001 1.31 -0.361 1.52
#> 2 2.15 group_1 time_2 patient_001 1.31 -0.361 1.52
#> 3 2.54 group_1 time_3 patient_001 1.31 -0.361 1.52
#> 4 -1.73 group_1 time_4 patient_001 1.31 -0.361 1.52
#> 5 1.11 group_1 time_1 patient_002 0.107 -2.44 -0.139
#> 6 2.64 group_1 time_2 patient_002 0.107 -2.44 -0.139
#> 7 1.69 group_1 time_3 patient_002 0.107 -2.44 -0.139
#> 8 0.783 group_1 time_4 patient_002 0.107 -2.44 -0.139
#> 9 0.118 group_1 time_1 patient_003 1.44 -0.419 -1.54
#> 10 2.48 group_1 time_2 patient_003 1.44 -0.419 -1.54
#> # ℹ 1,190 more rows
```
Next, create a special classed dataset that the package will recognize. The classed data object contains a pre-processed version of the data, along with attributes to declare the outcome variable, whether the outcome is response or change from baseline, the treatment group variable, the discrete time point variable, control group, baseline time point, and the covariates selected for analysis.
```r
data <- brm_data(
data = raw_data,
outcome = "response",
role = "response",
group = "group",
patient = "patient",
time = "time",
covariates = c("biomarker1", "biomarker2"),
reference_group = "group_1",
reference_time = "time_1"
)
data
#> # A tibble: 1,200 × 6
#> response group time patient biomarker1 biomarker2
#> <dbl> <chr> <chr> <chr> <dbl> <dbl>
#> 1 1.11 group_1 time_1 patient_001 1.31 -0.361
#> 2 2.15 group_1 time_2 patient_001 1.31 -0.361
#> 3 2.54 group_1 time_3 patient_001 1.31 -0.361
#> 4 -1.73 group_1 time_4 patient_001 1.31 -0.361
#> 5 1.11 group_1 time_1 patient_002 0.107 -2.44
#> 6 2.64 group_1 time_2 patient_002 0.107 -2.44
#> 7 1.69 group_1 time_3 patient_002 0.107 -2.44
#> 8 0.783 group_1 time_4 patient_002 0.107 -2.44
#> 9 0.118 group_1 time_1 patient_003 1.44 -0.419
#> 10 2.48 group_1 time_2 patient_003 1.44 -0.419
#> # ℹ 1,190 more rows
class(data)
#> [1] "brms_mmrm_data" "tbl_df" "tbl" "data.frame"
roles <- attributes(data)
roles$row.names <- NULL
str(roles)
#> List of 14
#> $ names : chr [1:6] "response" "group" "time" "patient" ...
#> $ class : chr [1:4] "brms_mmrm_data" "tbl_df" "tbl" "data.frame"
#> $ brm_outcome : chr "response"
#> $ brm_role : chr "response"
#> $ brm_group : chr "group"
#> $ brm_time : chr "time"
#> $ brm_patient : chr "patient"
#> $ brm_covariates : chr [1:2] "biomarker1" "biomarker2"
#> $ brm_reference_group: chr "group_1"
#> $ brm_reference_time : chr "time_1"
#> $ brm_levels_group : chr [1:3] "group_1" "group_2" "group_3"
#> $ brm_labels_group : chr [1:3] "group_1" "group_2" "group_3"
#> $ brm_levels_time : chr [1:4] "time_1" "time_2" "time_3" "time_4"
#> $ brm_labels_time : chr [1:4] "time_1" "time_2" "time_3" "time_4"
```
# Formula
Next, choose a `brms` model formula for the fixed effect and variance parameters. The `brm_formula()` function from `brms.mmrm` makes this process easier. A cell means parameterization for this particular model can be expressed as follows. It specifies one fixed effect parameter for each combination of treatment group and time point, and it makes the specification of informative priors straightforward through the `prior` argument of `brm_model()`.
```r
brm_formula(
data = data,
intercept = FALSE,
baseline = FALSE,
group = FALSE,
time = FALSE,
baseline_time = FALSE,
group_time = TRUE
)
#> response ~ 0 + group:time + biomarker1 + biomarker2 + unstr(time = time, gr = patient)
#> sigma ~ 0 + time
```
For the purposes of our example, we choose a fully parameterized analysis of the raw response.
```r
formula <- brm_formula(
data = data,
intercept = TRUE,
baseline = FALSE,
group = TRUE,
time = TRUE,
baseline_time = FALSE,
group_time = TRUE
)
formula
#> response ~ group + group:time + time + biomarker1 + biomarker2 + unstr(time = time, gr = patient)
#> sigma ~ 0 + time
```
# Parameterization
The formula is not the only factor
that ultimately determines the fixed effect parameterization.
The ordering of the categorical variables in the data,
as well as the `contrast` option in R, affect the
construction of the model matrix. To see the model
matrix that will ultimately be used in `brm_model()`,
run `brms::make_standata()` and examine the `X` element
of the returned list.
The `contrast` option accepts a named vector of two character vectors which govern `model.matrix()` contrasts for unordered and ordered variables, respectively.
```r
options(contrasts = c(unordered = "contr.SAS", ordered = "contr.poly"))
```
The `make_standata()` function lets you see the data that `brms` will generate for Stan. This includes the fixed effects model matrix `X`. Note the differences in the `groupgroup_*` additive terms between the matrix below and the one above.
```r
head(brms::make_standata(formula = formula, data = data)$X)
#> Intercept groupgroup_1 groupgroup_2 timetime_1 timetime_2 timetime_3
#> 1 1 1 0 1 0 0
#> 2 1 1 0 0 1 0
#> 3 1 1 0 0 0 1
#> 4 1 1 0 0 0 0
#> 5 1 1 0 1 0 0
#> 6 1 1 0 0 1 0
#> biomarker1 biomarker2 groupgroup_1:timetime_1 groupgroup_2:timetime_1
#> 1 1.3126508 -0.3608809 1 0
#> 2 1.3126508 -0.3608809 0 0
#> 3 1.3126508 -0.3608809 0 0
#> 4 1.3126508 -0.3608809 0 0
#> 5 0.1068624 -2.4441488 1 0
#> 6 0.1068624 -2.4441488 0 0
#> groupgroup_1:timetime_2 groupgroup_2:timetime_2 groupgroup_1:timetime_3
#> 1 0 0 0
#> 2 1 0 0
#> 3 0 0 1
#> 4 0 0 0
#> 5 0 0 0
#> 6 1 0 0
#> groupgroup_2:timetime_3
#> 1 0
#> 2 0
#> 3 0
#> 4 0
#> 5 0
#> 6 0
```
If you choose a different contrast method, a different model matrix may result.
```r
options(
contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")
)
# different model matrix than before:
head(brms::make_standata(formula = formula, data = data)$X)
#> Intercept groupgroup_2 groupgroup_3 timetime_2 timetime_3 timetime_4
#> 1 1 0 0 0 0 0
#> 2 1 0 0 1 0 0
#> 3 1 0 0 0 1 0
#> 4 1 0 0 0 0 1
#> 5 1 0 0 0 0 0
#> 6 1 0 0 1 0 0
#> biomarker1 biomarker2 groupgroup_2:timetime_2 groupgroup_3:timetime_2
#> 1 1.3126508 -0.3608809 0 0
#> 2 1.3126508 -0.3608809 0 0
#> 3 1.3126508 -0.3608809 0 0
#> 4 1.3126508 -0.3608809 0 0
#> 5 0.1068624 -2.4441488 0 0
#> 6 0.1068624 -2.4441488 0 0
#> groupgroup_2:timetime_3 groupgroup_3:timetime_3 groupgroup_2:timetime_4
#> 1 0 0 0
#> 2 0 0 0
#> 3 0 0 0
#> 4 0 0 0
#> 5 0 0 0
#> 6 0 0 0
#> groupgroup_3:timetime_4
#> 1 0
#> 2 0
#> 3 0
#> 4 0
#> 5 0
#> 6 0
```
# Priors
Some analyses require informative priors, others require non-informative ones. Please use [`brms`](https://paul-buerkner.github.io/brms/) to construct a prior suitable for your analysis. The [`brms`](https://paul-buerkner.github.io/brms/) package has documentation on how its default priors are constructed and how to set your own priors. Once you have an R object that represents the joint prior distribution of your model, you can pass it to the `brm_model()` function described below. The `get_prior()` function shows the default priors for a given dataset and model formula.
```r
brms::get_prior(data = data, formula = formula)
#> prior class coef group resp dpar
#> (flat) b
#> (flat) b biomarker1
#> (flat) b biomarker2
#> (flat) b groupgroup_2
#> (flat) b groupgroup_2:timetime_2
#> (flat) b groupgroup_2:timetime_3
#> (flat) b groupgroup_2:timetime_4
#> (flat) b groupgroup_3
#> (flat) b groupgroup_3:timetime_2
#> (flat) b groupgroup_3:timetime_3
#> (flat) b groupgroup_3:timetime_4
#> (flat) b timetime_2
#> (flat) b timetime_3
#> (flat) b timetime_4
#> lkj(1) cortime
#> student_t(3, 0.9, 2.5) Intercept
#> (flat) b sigma
#> (flat) b timetime_1 sigma
#> (flat) b timetime_2 sigma
#> (flat) b timetime_3 sigma
#> (flat) b timetime_4 sigma
#> nlpar lb ub source
#> default
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> default
#> default
#> default
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
```
# Model
To run an MMRM, use the `brm_model()` function. This function calls `brms::brm()` behind the scenes, using the formula and prior you set in the `formula` and `prior` arguments.
```r
model <- brm_model(data = data, formula = formula, refresh = 0)
```
The result is a `brms` model object.
```r
model
#> Family: gaussian
#> Links: mu = identity; sigma = log
#> Formula: response ~ group + group:time + time + biomarker1 + biomarker2 + unstr(time = time, gr = patient)
#> sigma ~ 0 + time
#> Data: data[!is.na(data[[attr(data, "brm_outcome")]]), ] (Number of observations: 1200)
#> Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
#> total post-warmup draws = 4000
#>
#> Correlation Structures:
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
#> cortime(time_1,time_2) 0.42 0.05 0.33 0.51 1.00 3325
#> cortime(time_1,time_3) -0.80 0.02 -0.84 -0.76 1.00 2161
#> cortime(time_2,time_3) -0.54 0.04 -0.62 -0.46 1.00 3218
#> cortime(time_1,time_4) -0.29 0.05 -0.39 -0.18 1.00 4082
#> cortime(time_2,time_4) 0.03 0.06 -0.09 0.14 1.00 3227
#> cortime(time_3,time_4) -0.10 0.06 -0.21 0.01 1.00 3697
#> Tail_ESS
#> cortime(time_1,time_2) 2911
#> cortime(time_1,time_3) 2816
#> cortime(time_2,time_3) 3092
#> cortime(time_1,time_4) 2939
#> cortime(time_2,time_4) 2861
#> cortime(time_3,time_4) 2853
#>
#> Regression Coefficients:
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
#> Intercept 1.21 0.09 1.04 1.39 1.00 1319
#> groupgroup_2 -1.53 0.12 -1.77 -1.29 1.00 1394
#> groupgroup_3 0.19 0.13 -0.06 0.44 1.00 1386
#> timetime_2 1.32 0.10 1.11 1.51 1.00 1994
#> timetime_3 0.52 0.17 0.17 0.87 1.00 1479
#> timetime_4 -1.44 0.17 -1.79 -1.11 1.00 1484
#> biomarker1 -0.01 0.01 -0.03 0.01 1.00 6061
#> biomarker2 0.02 0.01 0.00 0.04 1.00 4965
#> groupgroup_2:timetime_2 0.04 0.14 -0.24 0.32 1.00 2310
#> groupgroup_3:timetime_2 0.02 0.14 -0.26 0.31 1.00 2186
#> groupgroup_2:timetime_3 -0.17 0.25 -0.65 0.31 1.00 1589
#> groupgroup_3:timetime_3 -0.21 0.25 -0.69 0.28 1.00 1596
#> groupgroup_2:timetime_4 -0.08 0.25 -0.55 0.42 1.00 1544
#> groupgroup_3:timetime_4 -0.27 0.25 -0.75 0.21 1.00 1609
#> sigma_timetime_1 -0.12 0.04 -0.20 -0.04 1.00 2469
#> sigma_timetime_2 0.01 0.04 -0.08 0.09 1.00 3347
#> sigma_timetime_3 -0.05 0.04 -0.13 0.03 1.00 2168
#> sigma_timetime_4 0.21 0.04 0.13 0.29 1.00 3483
#> Tail_ESS
#> Intercept 2071
#> groupgroup_2 1918
#> groupgroup_3 2158
#> timetime_2 2641
#> timetime_3 2113
#> timetime_4 1840
#> biomarker1 2949
#> biomarker2 2885
#> groupgroup_2:timetime_2 2727
#> groupgroup_3:timetime_2 2725
#> groupgroup_2:timetime_3 2149
#> groupgroup_3:timetime_3 2363
#> groupgroup_2:timetime_4 2053
#> groupgroup_3:timetime_4 2129
#> sigma_timetime_1 2791
#> sigma_timetime_2 3135
#> sigma_timetime_3 2622
#> sigma_timetime_4 2850
#>
#> Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
#> and Tail_ESS are effective sample size measures, and Rhat is the potential
#> scale reduction factor on split chains (at convergence, Rhat = 1).
```
# Marginals
Regardless of the choice of fixed effects formula, `brms.mmrm` performs inference on the marginal distributions at each treatment group and time point of the mean of the following quantities:
1. Response.
2. Change from baseline, if you set `role` to `"change"` in `brm_data()`.
3. Treatment difference, in terms of change from baseline.
4. Effect size: treatment difference divided by the residual standard deviation.
To derive posterior draws of these marginals, use the `brm_marginal_draws()` function.
```r
draws <- brm_marginal_draws(data = data, formula = formula, model = model)
draws
#> $response
#> # A draws_df: 1000 iterations, 4 chains, and 12 variables
#> group_1|time_1 group_1|time_2 group_1|time_3 group_1|time_4 group_2|time_1
#> 1 1.2 2.5 1.8 -0.386 -0.25
#> 2 1.2 2.6 1.7 -0.083 -0.23
#> 3 1.2 2.6 1.8 -0.278 -0.31
#> 4 1.2 2.6 1.7 -0.150 -0.32
#> 5 1.2 2.5 1.8 -0.160 -0.26
#> 6 1.1 2.5 1.8 -0.288 -0.19
#> 7 1.1 2.4 1.9 -0.181 -0.30
#> 8 1.2 2.6 1.8 -0.207 -0.33
#> 9 1.1 2.5 1.7 -0.070 -0.35
#> 10 1.2 2.6 1.6 -0.022 -0.44
#> group_2|time_2 group_2|time_3 group_2|time_4
#> 1 1.05 -0.0050 -1.9
#> 2 1.16 0.0115 -2.0
#> 3 0.99 0.0939 -2.0
#> 4 1.12 -0.0090 -1.9
#> 5 1.02 -0.0371 -1.8
#> 6 1.00 -0.0146 -2.0
#> 7 1.08 -0.1218 -1.7
#> 8 1.01 0.0043 -1.9
#> 9 1.16 -0.0098 -1.8
#> 10 0.91 0.1413 -1.9
#> # ... with 3990 more draws, and 4 more variables
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
#>
#> $difference_time
#> # A draws_df: 1000 iterations, 4 chains, and 9 variables
#> group_1|time_2 group_1|time_3 group_1|time_4 group_2|time_2 group_2|time_3
#> 1 1.3 0.55 -1.6 1.3 0.25
#> 2 1.3 0.47 -1.3 1.4 0.24
#> 3 1.4 0.59 -1.5 1.3 0.41
#> 4 1.4 0.52 -1.3 1.4 0.31
#> 5 1.4 0.60 -1.3 1.3 0.23
#> 6 1.4 0.68 -1.4 1.2 0.17
#> 7 1.3 0.77 -1.3 1.4 0.18
#> 8 1.4 0.57 -1.4 1.3 0.33
#> 9 1.3 0.61 -1.2 1.5 0.34
#> 10 1.4 0.44 -1.2 1.4 0.58
#> group_2|time_4 group_3|time_2 group_3|time_3
#> 1 -1.7 1.2 0.107
#> 2 -1.8 1.3 0.227
#> 3 -1.7 1.3 0.244
#> 4 -1.5 1.4 0.410
#> 5 -1.5 1.3 0.574
#> 6 -1.9 1.3 0.350
#> 7 -1.4 1.4 0.391
#> 8 -1.6 1.2 0.043
#> 9 -1.4 1.5 0.377
#> 10 -1.4 1.5 0.297
#> # ... with 3990 more draws, and 1 more variables
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
#>
#> $difference_group
#> # A draws_df: 1000 iterations, 4 chains, and 6 variables
#> group_2|time_2 group_2|time_3 group_2|time_4 group_3|time_2 group_3|time_3
#> 1 0.0069 -0.30 -0.028 -0.089 -0.447
#> 2 0.0631 -0.23 -0.498 0.016 -0.244
#> 3 -0.1212 -0.19 -0.173 -0.123 -0.347
#> 4 0.0026 -0.21 -0.211 -0.081 -0.115
#> 5 -0.1005 -0.37 -0.212 -0.037 -0.027
#> 6 -0.2115 -0.51 -0.460 -0.044 -0.329
#> 7 0.0383 -0.59 -0.153 0.064 -0.377
#> 8 -0.1074 -0.24 -0.215 -0.229 -0.529
#> 9 0.1657 -0.27 -0.214 0.148 -0.238
#> 10 -0.0431 0.14 -0.233 0.060 -0.147
#> group_3|time_4
#> 1 -0.19
#> 2 -0.40
#> 3 -0.27
#> 4 -0.53
#> 5 -0.37
#> 6 -0.19
#> 7 -0.42
#> 8 -0.47
#> 9 -0.38
#> 10 -0.40
#> # ... with 3990 more draws
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
#>
#> $effect
#> # A draws_df: 1000 iterations, 4 chains, and 6 variables
#> group_2|time_2 group_2|time_3 group_2|time_4 group_3|time_2 group_3|time_3
#> 1 0.0070 -0.32 -0.022 -0.090 -0.465
#> 2 0.0624 -0.25 -0.426 0.016 -0.258
#> 3 -0.1264 -0.20 -0.138 -0.128 -0.373
#> 4 0.0026 -0.22 -0.178 -0.082 -0.118
#> 5 -0.0994 -0.39 -0.183 -0.036 -0.028
#> 6 -0.2190 -0.58 -0.370 -0.046 -0.375
#> 7 0.0403 -0.59 -0.128 0.067 -0.379
#> 8 -0.1078 -0.25 -0.164 -0.230 -0.544
#> 9 0.1702 -0.26 -0.192 0.152 -0.226
#> 10 -0.0462 0.15 -0.195 0.064 -0.158
#> group_3|time_4
#> 1 -0.15
#> 2 -0.34
#> 3 -0.22
#> 4 -0.45
#> 5 -0.32
#> 6 -0.15
#> 7 -0.35
#> 8 -0.36
#> 9 -0.34
#> 10 -0.33
#> # ... with 3990 more draws
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
```
If you need samples from these marginals averaged across time points, e.g. an "overall effect size", `brm_marginal_draws_average()` can average the draws above across discrete time points (either all or a user-defined subset).
```r
brm_marginal_draws_average(draws = draws, data = data)
#> $response
#> # A draws_df: 1000 iterations, 4 chains, and 3 variables
#> group_1|average group_2|average group_3|average
#> 1 1.3 -0.28 1.4
#> 2 1.4 -0.27 1.4
#> 3 1.3 -0.30 1.4
#> 4 1.3 -0.27 1.4
#> 5 1.3 -0.27 1.3
#> 6 1.3 -0.31 1.4
#> 7 1.3 -0.27 1.4
#> 8 1.3 -0.31 1.4
#> 9 1.3 -0.24 1.4
#> 10 1.3 -0.32 1.4
#> # ... with 3990 more draws
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
#>
#> $difference_time
#> # A draws_df: 1000 iterations, 4 chains, and 3 variables
#> group_1|average group_2|average group_3|average
#> 1 0.076 -0.033 -0.165
#> 2 0.158 -0.065 -0.052
#> 3 0.175 0.015 -0.072
#> 4 0.213 0.073 -0.030
#> 5 0.216 -0.013 0.073
#> 6 0.225 -0.167 0.039
#> 7 0.279 0.044 0.033
#> 8 0.211 0.024 -0.199
#> 9 0.258 0.152 0.103
#> 10 0.210 0.163 0.048
#> # ... with 3990 more draws
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
#>
#> $difference_group
#> # A draws_df: 1000 iterations, 4 chains, and 2 variables
#> group_2|average group_3|average
#> 1 -0.109 -0.24
#> 2 -0.223 -0.21
#> 3 -0.160 -0.25
#> 4 -0.139 -0.24
#> 5 -0.229 -0.14
#> 6 -0.393 -0.19
#> 7 -0.235 -0.25
#> 8 -0.187 -0.41
#> 9 -0.106 -0.16
#> 10 -0.047 -0.16
#> # ... with 3990 more draws
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
#>
#> $effect
#> # A draws_df: 1000 iterations, 4 chains, and 2 variables
#> group_2|average group_3|average
#> 1 -0.110 -0.23
#> 2 -0.203 -0.20
#> 3 -0.154 -0.24
#> 4 -0.130 -0.22
#> 5 -0.223 -0.13
#> 6 -0.389 -0.19
#> 7 -0.227 -0.22
#> 8 -0.173 -0.38
#> 9 -0.093 -0.14
#> 10 -0.032 -0.14
#> # ... with 3990 more draws
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
```
The `brm_marginal_summaries()` function produces posterior summaries of these marginals, and it includes the Monte Carlo standard error (MCSE) of each estimate.
```r
summaries <- brm_marginal_summaries(draws, level = 0.95)
summaries
#> # A tibble: 165 × 6
#> marginal statistic group time value mcse
#> <chr> <chr> <chr> <chr> <dbl> <dbl>
#> 1 difference_group lower group_2 time_2 -0.240 0.00686
#> 2 difference_group lower group_2 time_3 -0.651 0.0115
#> 3 difference_group lower group_2 time_4 -0.550 0.00994
#> 4 difference_group lower group_3 time_2 -0.261 0.00608
#> 5 difference_group lower group_3 time_3 -0.691 0.0166
#> 6 difference_group lower group_3 time_4 -0.751 0.0114
#> 7 difference_group mean group_2 time_2 0.0372 0.00303
#> 8 difference_group mean group_2 time_3 -0.174 0.00625
#> 9 difference_group mean group_2 time_4 -0.0783 0.00634
#> 10 difference_group mean group_3 time_2 0.0213 0.00307
#> # ℹ 155 more rows
```
The `brm_marginal_probabilities()` function shows posterior probabilities of the form,
$$
\begin{aligned}
\text{Prob}(\text{treatment effect} > \text{threshold})
\end{aligned}
$$
or
$$
\begin{aligned}
\text{Prob}(\text{treatment effect} < \text{threshold})
\end{aligned}
$$
```r
brm_marginal_probabilities(
draws = draws,
threshold = c(-0.1, 0.1),
direction = c("greater", "less")
)
#> # A tibble: 12 × 5
#> direction threshold group time value
#> <chr> <dbl> <chr> <chr> <dbl>
#> 1 greater -0.1 group_2 time_2 0.828
#> 2 greater -0.1 group_2 time_3 0.375
#> 3 greater -0.1 group_2 time_4 0.532
#> 4 greater -0.1 group_3 time_2 0.814
#> 5 greater -0.1 group_3 time_3 0.324
#> 6 greater -0.1 group_3 time_4 0.242
#> 7 less 0.1 group_2 time_2 0.670
#> 8 less 0.1 group_2 time_3 0.866
#> 9 less 0.1 group_2 time_4 0.761
#> 10 less 0.1 group_3 time_2 0.712
#> 11 less 0.1 group_3 time_3 0.890
#> 12 less 0.1 group_3 time_4 0.936
```
Finally, `brm_marignal_data()` computes marginal means and confidence intervals on the response variable in the data, along with other summary statistics.
```r
summaries_data <- brm_marginal_data(data = data, level = 0.95)
summaries_data
#> # A tibble: 84 × 4
#> statistic group time value
#> <chr> <chr> <chr> <dbl>
#> 1 lower group_1 time_1 1.40
#> 2 lower group_1 time_2 2.71
#> 3 lower group_1 time_3 1.91
#> 4 lower group_1 time_4 0.0151
#> 5 lower group_2 time_1 -0.150
#> 6 lower group_2 time_2 1.23
#> 7 lower group_2 time_3 0.218
#> 8 lower group_2 time_4 -1.59
#> 9 lower group_3 time_1 1.57
#> 10 lower group_3 time_2 2.95
#> # ℹ 74 more rows
```
# Visualization
The `brm_plot_compare()` function compares means and intervals from many different models and data sources in the same plot. First, we need the marginals of the data.
```r
brm_plot_compare(
data = summaries_data,
model1 = summaries,
model2 = summaries
)
```
![plot of chunk response](usage/response-1.png)
If you omit the marginals of the data, you can show inference on change from baseline or the treatment effect.
```r
brm_plot_compare(
model1 = summaries,
model2 = summaries,
marginal = "difference_group" # treatment effect
)
```
![plot of chunk difference](usage/difference-1.png)
Additional arguments let you control the primary comparison of interest (the color aesthetic), the horizontal axis, and the faceting variable.
```r
brm_plot_compare(
model1 = summaries,
model2 = summaries,
marginal = "difference_group",
compare = "group",
axis = "time",
facet = "source" # model1 vs model2
)
```
![plot of chunk differencecustom](usage/differencecustom-1.png)
Finally, `brm_plot_draws()` can plot the posterior draws of the response, change from baseline, or treatment difference.
```r
brm_plot_draws(draws = draws$difference_group)
```
![plot of chunk draws](usage/draws-1.png)
The `axis` and `facet` arguments customize the horizontal axis and faceting variable, respectively.
```r
brm_plot_draws(
draws = draws$difference_group,
axis = "group",
facet = "time"
)
```
![plot of chunk drawscustom](usage/drawscustom-1.png)