-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvisualization_priors.Rmd
192 lines (136 loc) · 3.73 KB
/
visualization_priors.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
---
title: "Visualizing priors"
subtitle: "<br/>With great power comes great responsibility: Stan for modern ecological modelling"
author: "Andrew MacDonald"
date: "`r Sys.Date()`"
output:
xaringan::moon_reader:
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
```
# Bayesian models
$$
\begin{align}
F_i & \sim \text{Poisson}(\lambda_i) \\
\text{log}(\lambda_i) &= \alpha + \beta x \\
\alpha & \sim \text{Normal}(??, ??) \\
\beta & \sim \text{Normal}(??, ??)
\end{align}
$$
---
# Bayesian models
$$
\begin{align}
F_i & \sim \text{Poisson}(\lambda_i) \\
\text{log}(\lambda_i) &= \alpha + \beta x \\
\alpha & \sim \text{Normal}(0, 1000) \\
\beta & \sim \text{Normal}(0, 1000)
\end{align}
$$
.footnote[as seen in Kéry & Royle 2016 p 188]
---
class: inverse, center, middle
# So what _is_ a good prior?
---
![](img/gabry_et_al_fig.png)
.footnote[
Gabry, Jonah, Daniel Simpson, Aki Vehtari, Michael Betancourt, and Andrew Gelman. « Visualization in Bayesian Workflow ». Journal of the Royal Statistical Society: Series A (Statistics in Society) 182 (2): 389‑402. https://doi.org/10.1111/rssa.12378.
]
---
# what does this prior mean?
$$
\begin{align}
F_i & \sim \text{Poisson}(\lambda_i) \\
\text{log}(\lambda_i) &= \alpha + \beta x \\
\alpha & \sim \text{Normal}(0, 1000) \\
\beta & \sim \text{Normal}(0, 1000)
\end{align}
$$
---
# what does this prior mean?
lets do an example about my favourites: fly larvae!
.pull-left[
<img src="img/lil_larv.png" style="width: 70%" />
]
.pull-right[
$$
\begin{align}
F_i & \sim \text{Poisson}(\lambda_i) \\
\text{log}(\lambda_i) &= \alpha + \beta x \\
\alpha & \sim \text{Normal}(0, 1000) \\
\beta & \sim \text{Normal}(0, 1000)
\end{align}
$$
where $x$ is some kind of standardized environmental variable
]
---
# A quick look at the math for those curious
$$
\begin{align}
\text{log}(\lambda) &= \alpha + \beta x \\
\lambda &= e^{\alpha + \beta x} \\
\lambda &= e^{\alpha}e^{\beta x} \\
\end{align}
$$
And since $N(0,1000)$ implies that -1000 and +1000 are entirely reasonable..
.pull-left[
* $2.72^{-1000}\times 2.72^{-1000}$
* (practically zero)
]
.pull-right[
* $2.72^{1000}\times 2.72^{1000}$
* (..kind of a lot)
]
---
# either no flies at all.. or a huge planet of maggots
<img src="img/many_insects.png" style="width: 70%" />
for comparison, the planet Saturn weighs 5.7 * 10^29 grams
---
# syntax: brms
```r
# define formula
insects_bf <- bf(
abundance ~ 1 + env,
family = poisson()
)
```
---
# syntax: setting priors
```r
get_prior(insects_bf, data = insect_data)
insect_priors <- c(
prior(normal(0,100), class = "b", coef = "env"),
prior(normal(0,100), class = "Intercept")
)
```
---
# syntax: sampling the model
```r
insect_samples <- brm(insects_bf,
data = insect_data, # does nothing
prior = insect_priors,
sample_prior = "only")
```
---
# Exercise I -- Fly larvae
* see the file `insects.R` on [github](https://github.com/aammd/ISEC_stan_course/blob/996eb5b2f49c52b0290c63a6635f925104bb623b/insects.R)
* on your own or in a small group, experiment with setting priors for insect abundances.
* AND/OR consider an animal or plant which is more relevant to you
* or stay with insects even if that is not your specialty, and set "vague" priors!
---
# exercises -- les poissons
using data from [Kaggle, about fish](https://www.kaggle.com/aungpyaeap/fish-market)
```{r message=FALSE, tidy=TRUE, results='asis'}
library(readr)
fish <- read_csv("https://raw.githubusercontent.com/aammd/ISEC_stan_course/master/Fish.csv")
knitr::kable(head(fish), format = 'html')
```
---
![](img/fish.png)
---