-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04-part3.Rmd
178 lines (144 loc) · 6.25 KB
/
04-part3.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
# Acknowledging Previous Findings: Informed Priors
## Video on Informed priors:
https://youtu.be/zBEiugiqbd4
After watching the videos we recommend you download the Markdown file and go through it in Rstudio:
```{r, echo=FALSE}
xfun::embed_file('04-part3.Rmd')
```
The data are available here (same data as the previous markdown):
```{r, echo=FALSE}
xfun::embed_file('vowel_space_area_data.csv')
```
The content of the markdown is reproduced below.
## Hands-on Exercises
### How to Encode Information in Priors
In this part, we'll have a closer look at how to encode information from previous studies within the prior specifications of our models. Let's start with the meta-analytic prior (i.e., Mean ES: 0.62, SE = 0.13) and add it to our prior specifications for the multi-level model. We do this by specifying the prior for the slope as normal(0.62, 0.13), as in the code block below:
```{r, results="hide", message=FALSE, warning=FALSE}
Articulation_f3 <- bf(ArticulationS ~ 1 + Register + (1+Register|Subject))
meta_analytic_prior <- c(
prior(normal(0, 1), class = Intercept),
prior(normal(0, 1), class = sd, coef = Intercept, group = Subject),
prior(normal(0.62, 0.13), class = b),
prior(normal(0, 1), class = sd, coef = RegisterIDS, group = Subject),
prior(normal(1, 0.5), class = sigma),
prior(lkj(2), class = cor))
Articulation_MAprior_m3 <-
brm(
Articulation_f3,
data = d,
save_pars = save_pars(all = TRUE),
family = gaussian,
prior = meta_analytic_prior,
file = "Articulation_MAprior_m3",
#refit = "on_change",
sample_prior = T,
iter = 10000,
warmup = 1000,
cores = 2,
chains = 2,
backend = "cmdstanr",
threads = threading(2),
control = list(
adapt_delta = 0.999,
max_treedepth = 20))
```
```{r, warning=FALSE}
pp_check(Articulation_MAprior_m3, ndraws = 50)
plot(conditional_effects(Articulation_MAprior_m3), points = T)
summary(Articulation_MAprior_m3)
```
Q8: How does the slope estimate for this model with the meta-analytic prior compare to that with the skeptical prior (i.e., Articulation_m3)?
__________________________________________________________________________________________________________________________________________
Let's try to do the same analysis for the Danish prior (i.e., Mean ES: -0.1, SE = 0.04):
```{r, results="hide", message=FALSE}
danish_prior <- c(
prior(normal(0, 1), class = Intercept),
prior(normal(0, 1), class = sd, coef = Intercept, group = Subject),
prior(normal(-0.1, 0.04), class = b),
prior(normal(0, 1), class = sd, coef = RegisterIDS, group = Subject),
prior(normal(1, 0.5), class = sigma),
prior(lkj(2), class = cor))
Articulation_Danishprior_m3 <-
brm(
Articulation_f3,
data = d,
save_pars = save_pars(all = TRUE),
family = gaussian,
prior = danish_prior,
file = "Articulation_Danishprior_m3",
#refit = "on_change",
sample_prior = T,
iter = 10000,
warmup = 1000,
cores = 2,
chains = 2,
backend = "cmdstanr",
threads = threading(2),
control = list(
adapt_delta = 0.999,
max_treedepth = 20))
```
```{r}
pp_check(Articulation_Danishprior_m3, ndraws = 50)
plot(conditional_effects(Articulation_Danishprior_m3), points = T)
summary(Articulation_Danishprior_m3)
```
Q9: How does the slope estimate for this model with the Danish prior compare to that with the skeptical prior (i.e., Articulation_m3)?
__________________________________________________________________________________________________________________________________________
### Prior-Posterior Updates Plot
Let's run the following code to extract and visualise the prior and posterior distributions from the different models:
```{r, warning=FALSE}
danish_prior_posterior <- as_draws_df(Articulation_Danishprior_m3) %>%
mutate(priors = "Danish") %>%
select(prior_b, b_RegisterIDS, priors)
ma_prior_posterior <- as_draws_df(Articulation_MAprior_m3) %>%
mutate(priors = "MA estimates") %>%
select(prior_b, b_RegisterIDS, priors)
skeptical_prior_posterior <- as_draws_df(Articulation_m3) %>%
mutate(priors = "Skeptical estimates") %>%
select(prior_b, b_RegisterIDS, priors)
Posterior <- rbind(danish_prior_posterior, ma_prior_posterior, skeptical_prior_posterior)
plot1 <- ggplot(Posterior) +
theme_classic() +
ggtitle("Priors") +
geom_density(aes(x = prior_b, fill = priors), alpha = 0.7) +
xlim(c(-1.5, 1.5)) +
geom_vline(xintercept = 0.0, linetype = 3) +
scale_fill_manual(name = "Prior",
labels = c('Meta-analytic', "Danish", "Skeptical"),
values=c("#FC4E07", "steelblue", "#228B22")) +
theme(plot.title = element_text(hjust = 0.5, size=15),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title.y=element_blank(),
axis.line.x = element_line(size=0.1),
axis.line.y = element_line(size=0.0),
axis.text.x = element_blank(),
axis.title.x=element_blank(),
legend.position = "none")
plot2 <- ggplot(Posterior) +
theme_classic() +
xlim(c(-1.5, 1.5)) +
geom_density(aes(x = b_RegisterIDS, fill = priors), alpha = 0.7) +
xlab('Effect Size') +
ggtitle(expression(paste("Posteriors"))) +
geom_vline(xintercept = 0.0, linetype = 3) +
scale_fill_manual(name = "Priors:",
labels = c('Danish', 'Meta-analytic', "Skeptical"),
values=c("#FC4E07", "steelblue", "#228B22")) +
scale_color_manual(values=c("#FC4E07","#228B22", "steelblue")) +
theme(plot.title = element_text(hjust = 0.5, size=15),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title.y=element_blank(),
axis.line = element_line(size=0),
legend.position = "bottom")
priors_posteriors_plot <- plot_grid(plot1, plot2, ncol=1)
priors_posteriors_plot
```
Q10: Why does the model with the Danish study prior not shift as much as those with the skeptical and meta-analytic priors?
Answer:
__________________________________________________________________________________________________________________________________________
Q11: What does this tell us about the nature of evidence accumulation in Bayesian models?
Answer:
__________________________________________________________________________________________________________________________________________