-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualizing_bayesian_workflow_brms.Rmd
350 lines (268 loc) · 9.44 KB
/
visualizing_bayesian_workflow_brms.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
---
title: "Visualizing the Bayesian Workflow With BRMS"
author: "Anders Sundelin"
date: "2022-12-10"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(rstan)
library(brms)
library(bayesplot)
library(loo)
library(tidybayes)
```
## Visualizing the Bayesian Workflow
Monica Alexander has a great tutorial on prior predictive checks at https://www.monicaalexander.com/posts/2020-28-02-bayes_viz/.
```{r generate-sample-data, eval=FALSE}
# download better done through your browser, it will have a longer timeout than Rs default 60 seconds...
#download.file("https://data.nber.org/natality/2017/natl2017.csv.zip", destfile = "natl2017.csv.zip")
# read in and select
d <- read_csv("~/Downloads/natl2017.csv.zip")
head(d)
d <- d %>%
select(mager, mracehisp, meduc, bmi, sex, combgest, dbwt, ilive)
# only use a sample of the births
set.seed(853)
ds <- d[sample(1:nrow(d), nrow(d)*0.001),]
ds <- ds %>% mutate(dbwt= dbwt/1000)
write_rds(ds, path = "births_2017_sample.RDS")
```
```{r ingest}
ds <- read_rds("births_2017_sample.RDS")
head(ds)
```
## Simple Exploratory Data Analysis
```{r}
ds <- ds %>%
rename(birthweight = dbwt, gest = combgest) %>%
mutate(preterm = ifelse(gest<32, "Y", "N")) %>%
filter(ilive=="Y",gest< 99, birthweight<9.999)
ds %>%
ggplot(aes(log(gest), log(birthweight))) +
geom_point() + geom_smooth(method = "lm") +
scale_color_brewer(palette = "Set1") +
theme_bw(base_size = 14) +
ggtitle("birthweight v gestational age")
```
Separating out premature babies.
```{r}
ds %>%
ggplot(aes(log(gest), log(birthweight), color = preterm)) +
geom_point() + geom_smooth(method = "lm") +
scale_color_brewer(palette = "Set1") +
theme_bw(base_size = 14) +
ggtitle("birthweight v gestational age")
```
## Candidate Models
Alternative 1 - simple model, straight relationship between log birth weight and log gestational age:
$log(y_i) \sim N(\beta_0 + \beta_1 log(x_i), \sigma^2) $
Alternative 2 has an interaction term (both fixed effect and effect proportional to the original log gestational age)
$log(y_i) \sim N(\beta_0 + \beta_1 log(x_i) + \gamma_0 z_i + \gamma_1 log(x_i) z_i, \sigma^2) $
where
* $y_i$ is weight in kg
* $x_i$ is gestational age in weeks
* $z_i$ is preterm (0 or 1, if gestational age is less than 32 weeks)
## Prior Predictive Checks
We do simulations from priors and likelihood and plot the resulting distribution, to see whether we come up with reasonable values.
### Vague priors
```{r vague}
set.seed(182)
nsims <- 100
sigma <- 1 / sqrt(rgamma(nsims, 1, rate = 100))
beta0 <- rnorm(nsims, 0, 100)
beta1 <- rnorm(nsims, 0, 100)
# scale and center outcome (comes from the real data)
dsims <- tibble(log_gest_c = (log(ds$gest)-mean(log(ds$gest)))/sd(log(ds$gest)))
# build the parameter, based on the simulated priors (sigma, beta0/beta1 above)
for(i in 1:nsims){
# calculate new mean value
this_mu <- beta0[i] + beta1[i]*dsims$log_gest_c
# create a new column, with as many rows as dsims, and contents of new mean value, plus or minus a value corresponding to the stddev
dsims[paste0(i)] <- this_mu + rnorm(nrow(dsims), 0, sigma[i])
}
dsl <- dsims %>%
pivot_longer(`1`:`100`, names_to = "sim", values_to = "sim_weight")
dsl %>%
ggplot(aes(sim_weight)) + geom_histogram(aes(y = ..density..), bins = 20, fill = "turquoise", color = "black") +
xlim(c(-1000, 1000)) +
geom_vline(xintercept = log(104), color = "purple", lwd = 1.2, lty = 2) +
theme_bw(base_size = 16) +
annotate("text", x=300, y=0.0022, label= "Anders'\ncurrent weight",
color = "purple", size = 5)
```
Clearly, babies are not that heavy --- they should be in the 1.0-5.0 range (perhaps stretching up to 8-9 kg at the most, or in some cases perhaps lower than 1.0 kg)
### Weakly Informative Priors
```{r}
sigma <- abs(rnorm(nsims, 0, 1))
beta0 <- rnorm(nsims, 0, 1)
beta1 <- rnorm(nsims, 0, 1)
dsims <- tibble(log_gest_c = (log(ds$gest)-mean(log(ds$gest)))/sd(log(ds$gest)))
for(i in 1:nsims){
this_mu <- beta0[i] + beta1[i]*dsims$log_gest_c
dsims[paste0(i)] <- this_mu + rnorm(nrow(dsims), 0, sigma[i])
}
dsl <- dsims %>%
pivot_longer(`1`:`100`, names_to = "sim", values_to = "sim_weight")
dsl %>%
ggplot(aes(sim_weight)) + geom_histogram(aes(y = ..density..), bins = 20, fill = "turquoise", color = "black") +
geom_vline(xintercept = log(104), color = "purple", lwd = 1.2, lty = 2) +
theme_bw(base_size = 16) +
annotate("text", x=8, y=0.2, label= "Anders'\ncurrent weight", color = "purple", size = 5)
```
### Running the models from Stan
Skipped
Formatting the data for the models
```{r}
ds$log_weight <- log(ds$birthweight)
ds$log_gest_c <- (log(ds$gest) - mean(log(ds$gest)))/sd(log(ds$gest))
N <- nrow(ds)
log_weight <- ds$log_weight
log_gest_c <- ds$log_gest_c
preterm <- ifelse(ds$preterm=="Y", 1, 0)
# put into a list
stan_data <- list(N = N,
log_weight = log_weight,
log_gest = log_gest_c,
preterm = preterm)
```
```{r}
mod1 <- stan(data = stan_data,
file = "models/simple_weight.stan",
iter = 500,
seed = 243)
mod2 <- stan(data = stan_data,
file = "models/simple_weight_preterm_int.stan",
iter = 500,
seed = 263)
```
```{r}
summary(mod1)[["summary"]][c(paste0("beta[",1:2, "]"), "sigma"),]
```
```{r}
summary(mod2)[["summary"]][c(paste0("beta[",1:4, "]"), "sigma"),]
```
### Running the Models In BRMS
Why are we not using our priors? Instead, we are using BRMS's deafult priors.
```{r}
mod1b <- brm(log_weight~log_gest_c, data = ds)
mod2b <- brm(log_weight~log_gest_c*preterm, data = ds)
```
## Posterior Predictive Checks
```{r}
set.seed(1856)
y <- log_weight
yrep1 <- rstan::extract(mod1)[["log_weight_rep"]]
samp100 <- sample(nrow(yrep1), 100)
ppc_dens_overlay(y, yrep1[samp100, ])
```
```{r}
yrep2 <- rstan::extract(mod2)[["log_weight_rep"]]
ppc_dens_overlay(y, yrep2[samp100, ])
```
Manual handling of the density plots:
```{r}
# first, get into a tibble
rownames(yrep1) <- 1:nrow(yrep1)
dr <- as_tibble(t(yrep1))
dr <- dr %>% bind_cols(i = 1:N, log_weight_obs = log(ds$birthweight))
# turn into long format; easier to plot
dr <- dr %>%
pivot_longer(-(i:log_weight_obs), names_to = "sim", values_to ="y_rep")
# filter to just include 100 draws and plot!
dr %>%
filter(sim %in% samp100) %>%
ggplot(aes(y_rep, group = sim)) +
geom_density(alpha = 0.2, aes(color = "y_rep")) +
geom_density(data = ds %>% mutate(sim = 1),
aes(x = log(birthweight), col = "y")) +
scale_color_manual(name = "",
values = c("y" = "darkblue",
"y_rep" = "lightblue")) +
ggtitle("Distribution of observed and replicated birthweights") +
theme_bw(base_size = 16)
```
```{r}
ppc_stat(log_weight, yrep1, stat = 'median')
```
```{r}
ppc_stat(log_weight, yrep2, stat = 'median')
```
Replicating the proportion of births less than 2.5 kg.
```{r}
t_y <- mean(y<=log(2.5))
t_y_rep <- sapply(1:nrow(yrep1), function(i) mean(yrep1[i,]<=log(2.5)))
t_y_rep_2 <- sapply(1:nrow(yrep2), function(i) mean(yrep2[i,]<=log(2.5)))
ggplot(data = as_tibble(t_y_rep), aes(value)) +
geom_histogram(aes(fill = "replicated")) +
geom_vline(aes(xintercept = t_y, color = "observed"), lwd = 1.5) +
ggtitle("Model 1: proportion of births less than 2.5kg") +
theme_bw(base_size = 16) +
scale_color_manual(name = "",
values = c("observed" = "darkblue"))+
scale_fill_manual(name = "",
values = c("replicated" = "lightblue"))
```
```{r}
ggplot(data = as_tibble(t_y_rep_2), aes(value)) +
geom_histogram(aes(fill = "replicated")) +
geom_vline(aes(xintercept = t_y, color = "observed"), lwd = 1.5) +
ggtitle("Model 2: proportion of births less than 2.5kg") +
theme_bw(base_size = 16) +
scale_color_manual(name = "",
values = c("observed" = "darkblue"))+
scale_fill_manual(name = "",
values = c("replicated" = "lightblue"))
```
## Posterior Predictions with brms
```{r}
yrep1b <- posterior_predict(mod1b)
ds_yrep1 <- ds %>%
select(log_weight, log_gest_c) %>%
add_predicted_draws(mod1b)
head(ds_yrep1)
```
```{r}
pp_check(mod1b, type = "dens_overlay", nsamples = 100)
```
```{r}
pp_check(mod1b, type = "stat", stat = 'median', nsamples = 100)
```
```{r}
loglik1 <- rstan::extract(mod1)[["log_lik"]]
loglik2 <- rstan::extract(mod2)[["log_lik"]]
loo1 <- loo(loglik1, save_psis = TRUE)
loo2 <- loo(loglik2, save_psis = TRUE)
```
```{r}
loo_compare(loo1, loo2)
```
```{r}
plot(loo1)
```
Plotting the LOO Probability Integral Transform (LOO-PIT).
Looks to see where each point $y_i$ falls in its predictive distribution $p(y_i|y_{-i})$
If the model is well calibrated, these should look like uniform distributions (spread evenly between 0 and 1).
```{r}
ppc_loo_pit_overlay(yrep = yrep1, y = y, lw = weights(loo1$psis_object)) + ggtitle("LOO-PIT Model 1")
```
```{r}
ppc_loo_pit_overlay(yrep = yrep2, y = y, lw = weights(loo2$psis_object)) + ggtitle("LOO-PIT Model 2")
```
## LOO-CV with brms output
```{r}
loo1b <- loo(mod1b, save_psis = TRUE)
loo2b <- loo(mod2b, save_psis = TRUE)
loo_compare(loo1b, loo2b)
```
```{r}
plot(loo1b)
```
```{r}
ppc_loo_pit_overlay(yrep = yrep1b, y = y, lw = weights(loo1b$psis_object)) + ggtitle("LOO-PIT BRMS Model 1")
```
```{r}
yrep2b <- posterior_predict(mod2b)
ppc_loo_pit_overlay(yrep = yrep2b, y = y, lw = weights(loo2b$psis_object)) + ggtitle("LOO-PIT BRMS Model 2")
```