-
Notifications
You must be signed in to change notification settings - Fork 18
/
24-bayesian_data_analysis3.Rmd
639 lines (505 loc) · 18 KB
/
24-bayesian_data_analysis3.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
# Bayesian data analysis 3
## Learning goals
- Evidence for null results.
- Only positive predictors.
- Dealing with unequal variance.
- Modeling slider data: Zero-one inflated beta binomial model.
- Modeling Likert scale data: Ordinal logistic regression.
## Load packages and set plotting theme
```{r, message=FALSE}
library("knitr") # for knitting RMarkdown
library("kableExtra") # for making nice tables
library("janitor") # for cleaning column names
library("tidybayes") # tidying up results from Bayesian models
library("brms") # Bayesian regression models with Stan
library("patchwork") # for making figure panels
library("GGally") # for pairs plot
library("broom.mixed") # for tidy lmer results
library("bayesplot") # for visualization of Bayesian model fits
library("modelr") # for modeling functions
library("lme4") # for linear mixed effects models
library("afex") # for ANOVAs
library("car") # for ANOVAs
library("emmeans") # for linear contrasts
library("ggeffects") # for help with logistic regressions
library("titanic") # titanic dataset
library("gganimate") # for animations
library("parameters") # for getting parameters
library("transformr") # for gganimate
library("rstanarm") # for Bayesian models
library("ggrepel") # for labels in ggplots
library("scales") # for percent y-axis
library("tidyverse") # for wrangling, plotting, etc.
```
```{r}
theme_set(theme_classic() + #set the theme
theme(text = element_text(size = 20))) #set the default text size
# set rstan options
rstan::rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
opts_chunk$set(comment = "",
fig.show = "hold")
```
## Evidence for the null hypothesis
See [this tutorial](https://mvuorre.github.io/posts/2017-03-21-bayes-factors-with-brms/) and this paper [@wagenmakers2010bayesiana] for more information.
### Bayes factor
#### Fit the model
- Define a binomial model
- Give a uniform prior `beta(1, 1)`
- Get samples from the prior
```{r}
df.null = tibble(s = 6, k = 10)
fit.brm_bayes = brm(formula = s | trials(k) ~ 0 + Intercept,
family = binomial(link = "identity"),
prior = set_prior(prior = "beta(1, 1)",
class = "b",
lb = 0,
ub = 1),
data = df.null,
sample_prior = TRUE,
cores = 4,
file = "cache/brm_bayes")
```
#### Visualize the results
Visualize the prior and posterior samples:
```{r, warning=FALSE}
fit.brm_bayes %>%
as_draws_df(variable = "[b]",
regex = T) %>%
pivot_longer(cols = -contains(".")) %>%
ggplot(mapping = aes(x = value,
fill = name)) +
geom_density(alpha = 0.5) +
scale_fill_brewer(palette = "Set1")
```
```{r}
fit.brm_bayes %>%
as_draws_df(variable = "[b]",
regex = T)
```
We test the H0: $\theta = 0.5$ versus the H1: $\theta \neq 0.5$ using the Savage-Dickey Method, according to which we can compute the Bayes factor like so:
$BF_{01} = \frac{p(D|H_0)}{p(D|H_1)} = \frac{p(\theta = 0.5|D, H_1)}{p(\theta = 0.5|H_1)}$
```{r}
fit.brm_bayes %>%
hypothesis(hypothesis = "Intercept = 0.5")
```
The result shows that the evidence ratio is in favor of the H0 with $BF_{01} = 2.22$. This means that H0 is 2.2 more likely than H1 given the data.
### LOO
Another way to test different models is to compare them via approximate leave-one-out cross-validation.
```{r}
set.seed(1)
df.loo = tibble(x = rnorm(n = 50),
y = rnorm(n = 50))
# visualize
ggplot(data = df.loo,
mapping = aes(x = x,
y = y)) +
geom_point()
# fit the frequentist model
fit.lm_loo = lm(formula = y ~ 1 + x,
data = df.loo)
fit.lm_loo %>%
summary()
# fit and compare bayesian models
fit.brm_loo1 = brm(formula = y ~ 1,
data = df.loo,
seed = 1,
file = "cache/brm_loo1")
fit.brm_loo2 = brm(formula = y ~ 1 + x,
data = df.loo,
seed = 1,
file = "cache/brm_loo2")
fit.brm_loo1 = add_criterion(fit.brm_loo1,
criterion = "loo",
file = "cache/brm_loo1")
fit.brm_loo2 = add_criterion(fit.brm_loo2,
criterion = "loo",
file = "cache/brm_loo2")
loo_compare(fit.brm_loo1, fit.brm_loo2)
model_weights(fit.brm_loo1, fit.brm_loo2)
```
## Dealing with heteroscedasticity
Let's generate some fake developmental data where the variance in the data is greatest for young children, smaller for older children, and even smaller for adults:
```{r}
# make example reproducible
set.seed(1)
df.variance = tibble(group = rep(c("3yo", "5yo", "adults"), each = 20),
response = rnorm(n = 60,
mean = rep(c(0, 5, 8), each = 20),
sd = rep(c(3, 1.5, 0.3), each = 20)))
```
### Visualize the data
```{r}
df.variance %>%
ggplot(aes(x = group, y = response)) +
geom_jitter(height = 0,
width = 0.1,
alpha = 0.7)
```
### Frequentist analysis
#### Fit the model
```{r}
fit.lm_variance = lm(formula = response ~ 1 + group,
data = df.variance)
fit.lm_variance %>%
summary()
fit.lm_variance %>%
glance()
```
#### Visualize the model predictions
```{r}
set.seed(1)
fit.lm_variance %>%
simulate() %>%
bind_cols(df.variance) %>%
ggplot(aes(x = group, y = sim_1)) +
geom_jitter(height = 0,
width = 0.1,
alpha = 0.7)
```
Notice how the model predicts that the variance is equal for each group.
### Bayesian analysis
While frequentist models (such as a linear regression) assume equality of variance, Bayesian models afford us with the flexibility of inferring both the parameter estimates of the groups (i.e. the means and differences between the means), as well as the variances.
#### Fit the model
We define a multivariate model which tries to fit both the `response` as well as the variance `sigma`:
```{r}
fit.brm_variance = brm(formula = bf(response ~ group,
sigma ~ group),
data = df.variance,
file = "cache/brm_variance",
seed = 1)
summary(fit.brm_variance)
```
Notice that sigma is on the log scale. To get the standard deviations, we have to exponentiate the predictors, like so:
```{r}
fit.brm_variance %>%
tidy(parameters = "^b_") %>%
filter(str_detect(term, "sigma")) %>%
select(term, estimate) %>%
mutate(term = str_remove(term, "b_sigma_")) %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
clean_names() %>%
mutate(across(-intercept, ~ exp(. + intercept))) %>%
mutate(intercept = exp(intercept))
```
#### Visualize the model predictions
```{r}
df.variance %>%
expand(group) %>%
add_epred_draws(object = fit.brm_variance,
dpar = TRUE ) %>%
select(group,
.row,
.draw,
posterior = .epred,
mu,
sigma) %>%
pivot_longer(cols = c(mu, sigma),
names_to = "index",
values_to = "value") %>%
ggplot(aes(x = value, y = group)) +
stat_halfeye() +
geom_vline(xintercept = 0,
linetype = "dashed") +
facet_grid(cols = vars(index))
```
This plot shows what the posterior looks like for both mu (the inferred means), and for sigma (the inferred variances) for the different groups.
```{r}
set.seed(1)
df.variance %>%
add_predicted_draws(object = fit.brm_variance,
ndraws = 1) %>%
ggplot(aes(x = group, y = .prediction)) +
geom_jitter(height = 0,
width = 0.1,
alpha = 0.7)
```
## Zero-one inflated beta binomial model
See this [blog post](https://mvuorre.github.io/posts/2019-02-18-analyze-analog-scale-ratings-with-zero-one-inflated-beta-models/).
## Ordinal regression
Check out the following two papers:
- @liddell2018analyzin
- @burkner2019ordinal
Let's read in some movie ratings:
```{r, warning=F, message=F}
df.movies = read_csv(file = "data/MoviesData.csv")
df.movies = df.movies %>%
pivot_longer(cols = n1:n5,
names_to = "stars",
values_to = "rating") %>%
mutate(stars = str_remove(stars,"n"),
stars = as.numeric(stars))
df.movies = df.movies %>%
uncount(weights = rating) %>%
mutate(id = as.factor(ID)) %>%
filter(ID <= 6)
```
### Ordinal regression (assuming equal variance)
#### Fit the model
```{r}
fit.brm_ordinal = brm(formula = stars ~ 1 + id,
family = cumulative(link = "probit"),
data = df.movies,
file = "cache/brm_ordinal",
seed = 1)
summary(fit.brm_ordinal)
```
#### Visualizations
##### Model parameters
The model infers the thresholds and the means of the Gaussian distributions in latent space.
```{r, warning=FALSE, message=FALSE}
df.params = fit.brm_ordinal %>%
parameters(centrality = "mean") %>%
as_tibble() %>%
clean_names() %>%
select(term = parameter, estimate = mean)
ggplot(data = tibble(x = c(-3, 3)),
mapping = aes(x = x)) +
stat_function(fun = ~ dnorm(.),
size = 1,
color = "black") +
stat_function(fun = ~ dnorm(., mean = df.params %>%
filter(str_detect(term, "id2")) %>%
pull(estimate)),
size = 1,
color = "blue") +
geom_vline(xintercept = df.params %>%
filter(str_detect(term, "Intercept")) %>%
pull(estimate))
```
##### MCMC inference
```{r, fig.height=20, fig.width=8}
fit.brm_ordinal %>%
plot(N = 9,
variable = "^b_",
regex = T)
```
```{r}
fit.brm_ordinal %>%
pp_check(ndraws = 20)
```
##### Model predictions
```{r}
conditional_effects(fit.brm_ordinal,
effects = "id",
categorical = T)
```
```{r}
df.model = add_epred_draws(newdata = expand_grid(id = 1:6),
object = fit.brm_ordinal,
ndraws = 10)
df.plot = df.movies %>%
count(id, stars) %>%
group_by(id) %>%
mutate(p = n / sum(n)) %>%
mutate(stars = as.factor(stars))
ggplot(data = df.plot,
mapping = aes(x = stars,
y = p)) +
geom_col(color = "black",
fill = "lightblue") +
geom_point(data = df.model,
mapping = aes(x = .category,
y = .epred),
alpha = 0.3,
position = position_jitter(width = 0.3)) +
facet_wrap(~id, ncol = 6)
```
### Gaussian regression (assuming equal variance)
#### Fit the model
```{r}
fit.brm_metric = brm(formula = stars ~ 1 + id,
data = df.movies,
file = "cache/brm_metric",
seed = 1)
summary(fit.brm_metric)
```
#### Visualizations
##### Model predictions
```{r, message=FALSE}
# get the predictions for each value of the Likert scale
df.model = fit.brm_metric %>%
parameters(centrality = "mean") %>%
as_tibble() %>%
select(term = Parameter, estimate = Mean) %>%
mutate(term = str_remove(term, "b_")) %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
clean_names() %>%
mutate(across(.cols = id2:id6,
.fns = ~ . + intercept)) %>%
rename_with(.fn = ~ c(str_c("mu_", 1:6), "sigma")) %>%
pivot_longer(cols = contains("mu"),
names_to = c("parameter", "movie"),
names_sep = "_",
values_to = "value") %>%
pivot_wider(names_from = parameter,
values_from = value) %>%
mutate(data = map2(.x = mu,
.y = sigma,
.f = ~ tibble(x = 1:5,
y = dnorm(x,
mean = .x,
sd = .y)))) %>%
select(movie, data) %>%
unnest(c(data)) %>%
group_by(movie) %>%
mutate(y = y/sum(y)) %>%
ungroup() %>%
rename(id = movie)
# visualize the predictions
df.plot = df.movies %>%
count(id, stars) %>%
group_by(id) %>%
mutate(p = n / sum(n)) %>%
mutate(stars = as.factor(stars))
ggplot(data = df.plot,
mapping = aes(x = stars,
y = p)) +
geom_col(color = "black",
fill = "lightblue") +
geom_point(data = df.model,
mapping = aes(x = x,
y = y)) +
facet_wrap(~id, ncol = 6)
```
### Oridnal regression (unequal variance)
#### Fit the model
```{r}
fit.brm_ordinal_variance = brm(formula = bf(stars ~ 1 + id) +
lf(disc ~ 0 + id, cmc = FALSE),
family = cumulative(link = "probit"),
data = df.movies,
file = "cache/brm_ordinal_variance",
seed = 1)
summary(fit.brm_ordinal_variance)
```
#### Visualizations
##### Model parameters
```{r}
df.params = fit.brm_ordinal_variance %>%
tidy(parameters = "^b_") %>%
select(term, estimate) %>%
mutate(term = str_remove(term, "b_"))
ggplot(data = tibble(x = c(-3, 3)),
mapping = aes(x = x)) +
stat_function(fun = ~ dnorm(.),
size = 1,
color = "black") +
stat_function(fun = ~ dnorm(.,
mean = 1,
sd = 2),
size = 1,
color = "blue") +
geom_vline(xintercept = df.params %>%
filter(str_detect(term, "Intercept")) %>%
pull(estimate))
```
##### Model predictions
```{r}
df.model = add_epred_draws(newdata = expand_grid(id = 1:6),
object = fit.brm_ordinal_variance,
ndraws = 10)
df.plot = df.movies %>%
count(id, stars) %>%
group_by(id) %>%
mutate(p = n / sum(n)) %>%
mutate(stars = as.factor(stars))
ggplot(data = df.plot,
mapping = aes(x = stars,
y = p)) +
geom_col(color = "black",
fill = "lightblue") +
geom_point(data = df.model,
mapping = aes(x = .category,
y = .epred),
alpha = 0.3,
position = position_jitter(width = 0.3)) +
facet_wrap(~id, ncol = 6)
```
### Gaussian regression (unequal variance)
#### Fit the model
```{r}
fit.brm_metric_variance = brm(formula = bf(stars ~ 1 + id,
sigma ~ 1 + id),
data = df.movies,
file = "cache/brm_metric_variance",
seed = 1)
summary(fit.brm_metric_variance)
```
#### Visualizations
##### Model predictions
```{r}
df.model = fit.brm_metric_variance %>%
tidy(parameters = "^b_") %>%
select(term, estimate) %>%
mutate(term = str_remove(term, "b_")) %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
clean_names() %>%
mutate(across(.cols = c(id2:id6),
.fns = ~ . + intercept)) %>%
mutate(across(.cols = contains("sigma"),
.fns = ~ 1/exp(.))) %>%
mutate(across(.cols = c(sigma_id2:sigma_id5),
.fns = ~ . + sigma_intercept)) %>%
set_names(c("mu_1", "sigma_1", str_c("mu_", 2:6), str_c("sigma_", 2:6))) %>%
pivot_longer(cols = everything(),
names_to = c("parameter", "movie"),
names_sep = "_",
values_to = "value") %>%
pivot_wider(names_from = parameter,
values_from = value) %>%
mutate(data = map2(.x = mu,
.y = sigma,
.f = ~ tibble(x = 1:5,
y = dnorm(x,
mean = .x,
sd = .y)))) %>%
select(movie, data) %>%
unnest(c(data)) %>%
group_by(movie) %>%
mutate(y = y/sum(y)) %>%
ungroup() %>%
rename(id = movie)
df.plot = df.movies %>%
count(id, stars) %>%
group_by(id) %>%
mutate(p = n / sum(n)) %>%
mutate(stars = as.factor(stars))
ggplot(data = df.plot,
mapping = aes(x = stars,
y = p)) +
geom_col(color = "black",
fill = "lightblue") +
geom_point(data = df.model,
mapping = aes(x = x,
y = y)) +
facet_wrap(~id, ncol = 6)
```
### Model comparison
```{r, eval=FALSE}
# currently not working
# ordinal regression with equal variance
fit.brm_ordinal = add_criterion(fit.brm_ordinal,
criterion = "loo",
file = "cache/brm_ordinal")
# Gaussian regression with equal variance
fit.brm_ordinal_variance = add_criterion(fit.brm_ordinal_variance,
criterion = "loo",
file = "cache/brm_ordinal_variance")
loo_compare(fit.brm_ordinal, fit.brm_ordinal_variance)
```
## Additional resources
- [Tutorial on visualizing brms posteriors with tidybayes](https://mjskay.github.io/tidybayes/articles/tidy-brms.html)
- [Hypothetical outcome plots](https://mucollective.northwestern.edu/files/2018-HOPsTrends-InfoVis.pdf)
- [Visual MCMC diagnostics](https://cran.r-project.org/web/packages/bayesplot/vignettes/visual-mcmc-diagnostics.html#general-mcmc-diagnostics)
- [Visualiztion of different MCMC algorithms](https://chi-feng.github.io/mcmc-demo/)
- [Frequentist equivalence test](https://www.carlislerainey.com/blog/2023-08-18-equivalence-tests/?s=09)
For additional resources, I highly recommend the brms and tidyverse implementations of the Statistical rethinking book [@mcelreath2020statistical], as well as of the Doing Bayesian Data analysis book [@kruschke2014doing], by Solomon Kurz [@kurz2020statistical; @kurz2022doingbayesian].
## Session info
Information about this R session including which version of R was used, and what packages were loaded.
```{r}
sessionInfo()
```