-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathBayes_intro.Rmd
467 lines (293 loc) · 9.11 KB
/
Bayes_intro.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
---
title: 'An introduction to Bayesian modelling with brms and Stan'
author: 'Francisco Rodríguez-Sánchez'
institute: 'https://frodriguezsanchez.net'
aspectratio: 43 # use 169 for wide format
fontsize: 10pt
output:
binb::metropolis:
keep_tex: no
incremental: yes
fig_caption: no
pandoc_args: ['--lua-filter=hideslide.lua']
urlcolor: blue
linkcolor: blue
header-includes:
- \definecolor{shadecolor}{RGB}{230,230,230}
- \setbeamercolor{frametitle}{bg=gray}
---
```{r knitr_setup, include=FALSE, cache=FALSE}
library('knitr')
### Chunk options ###
## Text results
opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, size = 'tiny')
## Code decoration
opts_chunk$set(tidy = FALSE, comment = NA, highlight = TRUE, prompt = FALSE, crop = TRUE)
# ## Cache
opts_chunk$set(cache = TRUE)
# ## Plots
# opts_chunk$set(fig.path = 'knitr_output/figures/')
opts_chunk$set(fig.align = 'center', out.width = '90%')
### Hooks ###
## Crop plot margins
knit_hooks$set(crop = hook_pdfcrop)
## Reduce font size
## use tinycode = TRUE as chunk option to reduce code font size
# see http://stackoverflow.com/a/39961605
knit_hooks$set(tinycode = function(before, options, envir) {
if (before) return(paste0('\n \\', options$size, '\n\n'))
else return('\n\n \\normalsize \n')
})
```
## Our dataset: tree heights and DBH
::: nonincremental :::
- One species
- 10 plots
- 1000 trees
- Number of trees per plot ranging from 4 to 392
```{r echo=1}
trees <- read.csv('data/trees.csv')
summary(trees[, 1:3])
```
:::
## What's the relationship between DBH and height?
```{r echo=FALSE}
plot(trees$dbh, trees$height, pch=20, las=1, cex.lab=1.4, xlab='DBH (cm)', ylab='Height (m)')
```
## First step: linear regression (lm)
\footnotesize
```{r lm, echo=1}
simple.lm <- lm(height ~ dbh, data = trees)
summary(simple.lm)
```
## Center continuous variables
```{r echo=TRUE}
summary(trees$dbh)
trees$dbh.c <- trees$dbh - 25
```
So, all parameters will be referred to a 25 cm DBH tree.
## Linear regression with centred DBH
:::::::::::::: {.columns align=center}
::: {.column width='40%'}
```{r echo=FALSE}
plot(trees$dbh.c, trees$height, pch=20, las=1, cex.lab=1.4, xlab='DBH (cm)', ylab='Height (m)')
abline(lm(height ~ dbh.c, data=trees), col='red', lwd=3)
```
:::
::: {.column width='60%' }
```{r echo=FALSE}
library(arm)
simple.lm <- lm(height ~ dbh.c, data = trees)
display(simple.lm)
```
:::
::::::::::::::
# Let's make it Bayesian
## Bayesian inference: prior, posterior, and likelihood
$P(Unknown|Data) \propto P(Data|Unknown) \times P(Unknown)$
$Posterior \propto Likelihood \times Prior$
```{r echo = FALSE, results='hide'}
set.seed(28)
# example with tree diameters
diam.sd <- 20
diam <- rnorm(8, 30, diam.sd)
prior.diam <- 50
prior.diam.var <- 100
library(blmeco)
blmeco::triplot.normal.knownvariance(theta.data = mean(diam),
n = length(diam),
variance.known = diam.sd*diam.sd,
prior.theta = prior.diam,
prior.variance = prior.diam.var)
title("Sample size = 8")
```
```{r echo = FALSE, results='hide', eval=FALSE}
# height <- runif(10, 170, 190)
#
# prior.height <- 160
# prior.height.var <- 20
set.seed(123)
# example with students' hours of sleep
sleephours.sd <- 2
sleephours <- rnorm(8, 9, sleephours.sd)
prior.sleep <- 7
prior.sleep.var <- 1
library(blmeco)
blmeco::triplot.normal.knownvariance(theta.data = mean(sleephours),
n = length(sleephours),
variance.known = sleephours.sd*sleephours.sd,
prior.theta = prior.sleep,
prior.variance = prior.sleep.var)
title("How many hours of daily sleep?")
```
## What is the likelihood?
$L(\theta|x) = P(x|\theta)$
```{r echo=FALSE}
include_graphics("images/likelihood.PNG")
```
\tiny https://seeing-theory.brown.edu/bayesian-inference/index.html
## Bayesian inference: prior and likelihood produce posterior
```{r echo = FALSE, results='hide'}
set.seed(28)
# example with tree diameters
diam.sd <- 20
diam <- rnorm(8, 30, diam.sd)
prior.diam <- 50
prior.diam.var <- 100
library(blmeco)
blmeco::triplot.normal.knownvariance(theta.data = mean(diam),
n = length(diam),
variance.known = diam.sd*diam.sd,
prior.theta = prior.diam,
prior.variance = prior.diam.var)
title("Sample size = 8")
```
## With increasing sample size, likelihood dominates prior
```{r echo = FALSE, results='hide'}
set.seed(28)
# example with tree diameters
diam.sd <- 20
diam <- rnorm(100, 30, diam.sd)
prior.diam <- 50
prior.diam.var <- 100
library(blmeco)
blmeco::triplot.normal.knownvariance(theta.data = mean(diam),
n = length(diam),
variance.known = diam.sd*diam.sd,
prior.theta = prior.diam,
prior.variance = prior.diam.var)
title("Sample size = 100")
```
## Bayesian statistics in practice
- Integrate information (prior)
- Prior regularises unlikely estimates from data
- Particularly important with limited sample sizes
- Large dataset -> prior effect diminishes
- Uncertainty / Propagate errors
## Remember our model structure
$$
\begin{aligned}
y_{i} \sim N(\mu_{i}, \sigma^2) \\
\mu_{i} = \alpha + \beta x_{i}
\end{aligned}
$$
In this case:
$$
\begin{aligned}
Height_{i} \sim N(\mu_{i}, \sigma^2) \\
\mu_{i} = \alpha + \beta DBH_{i}
\end{aligned}
$$
$\alpha$: expected height when DBH = 25 cm
$\beta$: how much height increases with every unit increase of DBH
## Defining model formula
```{r echo=T}
library('brms')
height.formu <- brmsformula(height ~ dbh.c)
```
---
\Large
We must define **prior distributions** for every parameter
## brms default priors
\scriptsize
```{r echo=1}
get_prior(height.formu, data = trees)
```
## Choosing priors
Avoid 'non-informative' priors
Use *weakly informative* (e.g. relatively wide Normal or t-student distributions)
or *strongly informative* priors based on previous knowledge and common sense.
Some tips for setting priors:
- https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations
- [Priors chapter](https://www.routledge.com/rsc/downloads/Bayesian_FreeBook_Final.pdf?srsltid=AfmBOor9tMa_Aq_uAUA9Ol8w4U_bQ9dc-EDmDARh17vVjmz66i1sdUq2&page=8) in The BUGS book
- https://doi.org/10.1111/oik.05985
Run **prior predictive checks** (just priors, no data)
## Probability distribution explorer
https://distribution-explorer.github.io/
## Example: estimating people height across countries
:::::::::::::: {.columns align=center}
::: {.column width='50%'}
Unreasonable prior
```{r echo=FALSE}
plot(density(rnorm(1000, 0, 1000)), main='', xlab='Height (m)', cex = 3)
```
:::
::: {.column width='50%' }
Reasonable prior
```{r echo=FALSE}
plot(density(rnorm(1000, 2, 0.5)), main='', xlab='Height (m)', cex = 3)
```
:::
::::::::::::::
## Defining priors for our trees example
```{r echo=T}
priors <- c(
set_prior('normal(30, 10)', class = 'Intercept'),
set_prior('normal(0.5, 0.4)', class = 'b'),
set_prior('normal(0, 5)', class = 'sigma')
)
```
## Prior for intercept (average height of 25-cm diameter tree)
```{r echo=T}
plot(density(rnorm(1000, 25, 10)))
```
## Prior for slope
```{r echo=T}
plot(density(rnorm(1000, 0.5, 0.5)))
```
## Prior for sigma (residual sd)
```{r echo=F}
sig <- rnorm(10000, 0, 5)
sigma <- na.omit(ifelse(sig < 0, NA, sig))
hist(sigma, breaks = 30, probability = T)
```
## Prior predictive check
```{r echo=T, results='hide'}
height.mod <- brm(height.formu,
data = trees,
prior = priors,
sample_prior = 'only')
```
## Prior predictive check
```{r echo=T}
pp_check(height.mod, ndraws = 100)
```
## Fit model (now with data)
```{r echo=T, results='hide'}
height.mod <- brm(height.formu,
data = trees,
prior = priors)
```
## Model summary
\scriptsize
```{r echo=T}
summary(height.mod)
```
## Model visualisation
```{r echo=T}
plot(height.mod)
```
## Posterior predictive checking
```{r echo=T}
pp_check(height.mod, ndraws = 100)
```
## Interactive model exploration
```{r echo=T, eval=FALSE}
library('shinystan')
launch_shinystan(height.mod)
```
## The Bayesian workflow
```{r out.width='70%'}
include_graphics("images/Bayesian_workflow.png")
```
\tiny https://arxiv.org/abs/2011.01808
## Exercise
\Large
height ~ sex
## To read more
[Regression and other stories](https://avehtari.github.io/ROS-Examples/)
[Statistical Rethinking](https://xcelab.net/rm/)
[Statistical rethinking with brms, ggplot2, and the tidyverse](https://bookdown.org/content/4857/)
[Bayesian Population Analysis using WinBugs](https://www.sciencedirect.com/book/9780123870209/bayesian-population-analysis-using-winbugs)
[Applied Hierarchical Modeling in Ecology](https://www.mbr-pwrc.usgs.gov/pubanalysis/keryroylebook/)
[Stan user guide](https://mc-stan.org/docs/stan-users-guide/)