-
Notifications
You must be signed in to change notification settings - Fork 23
/
chapter05.Rmd
473 lines (381 loc) · 11.7 KB
/
chapter05.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
---
title: "Chapter 05"
author: "Scott Spencer"
date: "8/22/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE, message = FALSE, error = FALSE)
library(dplyr); library(tidyr); library(rstan); library(skimr); library(ggplot2); library(ggthemes)
theme_set(theme_tufte(base_family = 'sans'))
```
The code below is meant as a directly-in-Stan translation of the examples in Chapter 5 of McElreath's *Statistical Rethinking*.
# Chapter 5
```{r}
data('WaffleDivorce', package = 'rethinking')
d <- WaffleDivorce; rm(WaffleDivorce)
```
Figure 5.1
```{r}
ggplot(d) +
stat_smooth(aes(WaffleHouses/Population, Divorce), method = 'lm', level = .89,
fullrange = T, color = 'black', alpha = .1, lwd = .3) +
geom_point(aes(WaffleHouses/Population, Divorce),
shape = 21, color = 'dodgerblue') +
geom_text(data = filter(d, Loc %in% c('AL', 'AR', 'GA', 'OK', 'ME', 'NJ', 'SC')),
aes(x = WaffleHouses/Population + 1.2, y = Divorce, label = Loc), size = 3) +
scale_x_continuous(limits = c(0, 50)) +
labs(x = 'Waffle Houses per million', y = 'Divorce Rate')
```
## 5.1 Spurious association
Standardize the predictors
```{r}
d <- d %>%
mutate(MedianAgeMarriage_z = (MedianAgeMarriage - mean(MedianAgeMarriage)) /
sd(MedianAgeMarriage),
Marriage_z = (Marriage - mean(Marriage)) / sd(Marriage))
```
Write a Stan model
```{stan output.var="m05_1"}
data {
int<lower=0> N;
vector[N] divorce;
vector[N] median_age_z;
}
parameters {
real a;
real bA;
real<lower=0, upper=10> sigma;
}
model {
vector[N] mu = a + median_age_z * bA;
target += normal_lpdf(divorce | mu, sigma);
target += normal_lpdf(a | 10, 10);
target += normal_lpdf(bA | 0, 10);
}
```
Organize data for Stan model, and sample.
```{r}
dat <- list(
N = NROW(d),
divorce = d$Divorce,
median_age_z = d$MedianAgeMarriage_z
)
fit05_1 <- sampling(m05_1, data = dat, iter = 1000, chains = 2, cores = 2)
```
Summarise model
```{r}
print(fit05_1, probs = c(0.10, 0.5, 0.9))
```
Write another Stan model
```{stan output.var="m05_2"}
data {
int N;
vector[N] divorce;
vector[N] marriage_z;
}
parameters {
real a;
real bM;
real<lower=0> sigma;
}
model {
vector[N] mu = a + marriage_z * bM;
target += normal_lpdf(divorce | mu, sigma);
target += normal_lpdf(a | 10, 10);
target += normal_lpdf(bM | 0, 10);
}
```
Organize data for Stan and sample.
```{r}
dat <- list(
N = NROW(d),
divorce = d$Divorce,
marriage_z = d$Marriage_z
)
fit05_2 <- sampling(m05_2, data = dat, iter = 1000, chains = 2, cores = 2)
```
Summarise the model
```{r}
print(fit05_2, probs = c(.1, .5, .9))
```
Setup plot for first model,
```{r}
# draw from posterior samples
post <- as.data.frame(fit05_1)
# recreate mu and simulate it with new data
f_mu <- function(x) post$a + post$bA * x
A_z_new <- seq(-3, 3)
mu <-
sapply(A_z_new, f_mu) %>%
as_tibble() %>%
rename_all(function(x) A_z_new) %>%
mutate(Iter = row_number()) %>%
gather(A_z, divorce, -Iter) %>%
group_by(A_z) %>%
mutate(hpdi_l = HDInterval::hdi(divorce, credMass = 0.8)[1],
hpdi_h = HDInterval::hdi(divorce, credMass = 0.8)[2]) %>%
mutate(mu = mean(divorce)) %>%
ungroup() %>%
mutate(A_z = as.numeric(A_z))
# plot raw data and model estimate of mu
p <- ggplot()
p1 <- p +
geom_point(data = d,
aes(MedianAgeMarriage_z, Divorce),
shape = 1, color = 'dodgerblue') +
geom_ribbon(data = mu,
aes(x = A_z, ymin = hpdi_l, ymax = hpdi_h), alpha = .1) +
geom_line(data = mu,
aes(x = A_z, y = mu))
```
Setup plot for second model
```{r}
# draw from posterior samples
post <- as.data.frame(fit05_2)
# recreate mu and simulate it with new data
f_mu <- function(x) post$a + post$bM * x
M_z_new <- seq(-3, 3)
mu <-
sapply(M_z_new, f_mu) %>%
as_tibble() %>%
rename_all(function(x) M_z_new) %>%
mutate(Iter = row_number()) %>%
gather(M_z, divorce, -Iter) %>%
group_by(M_z) %>%
mutate(hpdi_l = HDInterval::hdi(divorce, credMass = 0.8)[1],
hpdi_h = HDInterval::hdi(divorce, credMass = 0.8)[2]) %>%
mutate(mu = mean(divorce)) %>%
ungroup() %>%
mutate(M_z = as.numeric(M_z))
# plot raw data and model estimate of mu
p2 <- p +
geom_point(data = d,
aes(Marriage_z, Divorce),
shape = 1, color = 'dodgerblue') +
geom_ribbon(data = mu,
aes(x = M_z, ymin = hpdi_l, ymax = hpdi_h), alpha = .1) +
geom_line(data = mu,
aes(x = M_z, y = mu))
```
Figure 5.2
```{r}
library(gridExtra)
grid.arrange(p2, p1, nrow = 1)
```
### 5.1.2. Fitting the model
Fit model using both predictors
```{stan output.var="m05_3"}
data {
int N;
vector[N] divorce;
vector[N] marriage_z;
vector[N] median_age_z;
}
parameters {
real a;
real bA;
real bM;
real<lower=0> sigma;
}
model {
vector[N] mu = a + median_age_z * bA + marriage_z * bM;
target += normal_lpdf(divorce | mu, sigma);
target += normal_lpdf(a | 10, 10);
target += normal_lpdf(bA | 0, 10);
target += normal_lpdf(bM | 0, 10);
target += exponential_lpdf(sigma | 1);
}
```
Organize data and sample, this time including new data with which we want to predict.
```{r}
dat = list(
N = NROW(d),
divorce = d$Divorce,
marriage_z = d$Marriage_z,
median_age_z = d$MedianAgeMarriage_z
)
fit05_3 <- sampling(m05_3, data = dat, iter = 1000, chains = 2, cores = 2)
```
Summarise model
```{r}
print(fit05_3, probs = c(0.1, 0.5, 0.9))
```
### 5.1.3. Plotting multivariate posteriors
```{stan output.var="m05_4"}
data {
int<lower=1> N;
vector[N] A_z;
vector[N] M_z;
}
parameters {
real a;
real b;
real<lower=0> sigma;
}
model {
vector[N] mu = a + A_z * b;
target += normal_lpdf(M_z | mu, sigma);
target += normal_lpdf(a | 0, 10);
target += normal_lpdf(b | 0, 10);
target += exponential_lpdf(sigma | 1);
}
```
```{r}
dat <- list(
N = NROW(d),
A_z = d$MedianAgeMarriage_z,
M_z = d$Marriage_z
)
fit05_4 <- sampling(m05_4, data = dat, iter = 1000, chains = 2, cores = 2)
```
```{r}
print(fit05_4, probs = c(0.1, 0.5, 0.9))
```
#### 5.1.3.1 Predictor residual plots
```{r}
post <- as.matrix(fit05_4)
mu <- post[,"a"] + d$MedianAgeMarriage_z %*% t(post[,"b"])
mu <- rowMeans(mu)
resid <- d$Marriage_z - mu
ggplot() +
geom_segment(aes(x = d$MedianAgeMarriage_z,
xend = d$MedianAgeMarriage_z,
y = mu, yend = d$Marriage_z)) +
geom_point(data = d,
aes(MedianAgeMarriage_z, Marriage_z),
shape = 21, color = 'dodgerblue', fill = 'white') +
geom_abline(aes(slope = mean(post[,"b"]), intercept = mean(post[,"a"])))
```
#### 5.1.3.2 counterfactual plots
```{r}
# get draws for parameters
post <- as.matrix(fit05_3)
# setup new data
nd <-
expand.grid(median_age_z = seq(-3, 3),
marriage_z = seq(-3, 3)) %>%
as.matrix
# estimate mu
mu <- post[,1] + post[,2:3] %*% t(nd)
# get stats on mu
avg <- colMeans(mu)
hdi <- apply(mu, 2, HDInterval::hdi)
# simulate divorce rate
iter <- 1e4
y_hat <- matrix(nrow = iter, ncol = NROW(nd))
for(i in 1:NROW(nd)) y_hat[,i] <- rnorm(iter, post[,1] + post[,2:3] %*% as.matrix(nd[i,]), post[,4])
# get stats on sim
y_hat_avg <- colMeans(y_hat)
y_hat_hdi <- apply(y_hat, 2, HDInterval::hdi)
nd <-
as_tibble(nd) %>%
bind_cols(avg = avg,
mu_hdi_l = hdi[1,],
mu_hdi_h = hdi[2,],
y_hdi_l = y_hat_hdi[1,],
y_hdi_h = y_hat_hdi[2,])
```
Setup Figure 5.5a
```{r}
p1 <- ggplot(nd, aes(x = median_age_z, y = avg, group = marriage_z)) +
geom_line(data = nd,
color = 'gray90') +
geom_ribbon(data = nd %>% filter(marriage_z == 0),
aes(x = median_age_z, ymin = mu_hdi_l, ymax = mu_hdi_h), alpha = .1) +
geom_ribbon(data = nd %>% filter(marriage_z == 0),
aes(x = median_age_z, ymin = y_hdi_l, ymax = y_hdi_h), alpha = .1) +
geom_line(data = nd %>% filter(marriage_z == 0),
aes(x = median_age_z, y = avg)) +
geom_text(data = nd %>% filter(median_age_z == min(median_age_z)),
aes(label = marriage_z, x = median_age_z - 0.1, y = avg), size = 2) +
geom_text(data = nd %>% filter(median_age_z == max(median_age_z)),
aes(label = marriage_z, x = median_age_z + 0.1, y = avg), size = 2) +
labs(x = 'Standardized Median Age of Marriage', y = 'Divorce rate')
```
Setup Figure 5.5b
```{r}
p2 <- ggplot(nd, aes(x = marriage_z, y = avg, group = median_age_z)) +
geom_line(data = nd,
color = 'gray90') +
geom_ribbon(data = nd %>% filter(median_age_z == 0),
aes(x = marriage_z, ymin = mu_hdi_l, ymax = mu_hdi_h), alpha = .1) +
geom_ribbon(data = nd %>% filter(median_age_z == 0),
aes(x = marriage_z, ymin = y_hdi_l, ymax = y_hdi_h), alpha = .1) +
geom_line(data = nd %>% filter(median_age_z == 0),
aes(x = marriage_z, y = avg)) +
geom_text(data = nd %>% filter(marriage_z == min(marriage_z)),
aes(label = median_age_z, x = marriage_z - 0.1, y = avg), size = 2) +
geom_text(data = nd %>% filter(marriage_z == max(marriage_z)),
aes(label = median_age_z, x = marriage_z + 0.1, y = avg), size = 2) +
labs(x = 'Standardized Rate of Marriage', y = 'Divorce rate')
```
Figure 5.5
```{r}
grid.arrange(p2, p1, nrow = 1)
```
#### 5.1.3.3 posterior prediction plots
```{r}
# estimate mu
mu <- post[,1] + post[,2:3] %*% t(d[,14:15])
# get stats on mu
avg <- colMeans(mu)
hdi <- apply(mu, 2, HDInterval::hdi)
# simulate divorce rate
iter <- 1e4
y_hat <- matrix(nrow = iter, ncol = NROW(d[,14:15]))
for(i in 1:NROW(d[,14:15])) y_hat[,i] <- rnorm(iter, post[,1] + post[,2:3] %*% t(d[i,14:15]), post[,4])
# get stats on sim
y_hat_avg <- colMeans(y_hat)
y_hat_hdi <- apply(y_hat, 2, HDInterval::hdi)
d <- d %>% mutate(mu = avg,
mu_hdi_l = hdi[1,],
mu_hdi_h = hdi[2,],
y_hdi_l = y_hat_hdi[1,],
y_hdi_h = y_hat_hdi[2,])
```
Figure 5.6a predicted versus observed
```{r}
ggplot() +
geom_abline(intercept = 0, slope = 1,
linetype = 'dashed', color = 'gray70') +
geom_segment(data = d,
aes(x = Divorce, xend = Divorce,
y = mu_hdi_l, yend = mu_hdi_h),
color = 'dodgerblue') +
geom_point(data = d,
aes(Divorce, mu),
shape = 1, color = 'dodgerblue', fill = 'white') +
labs(x = "Observed divorce rate", y = 'Estimated average divorce rate',
subtitle = 'Observed versus\nestimated for each state')
```
Figure 5.6b
```{r}
d1 <- d %>%
mutate(pred_err = Divorce - mu) %>%
mutate(Loc = factor(Loc, levels = Loc[order(Divorce - mu)] ))
ggplot(d1) +
geom_vline(xintercept = 0, color = 'gray80') +
geom_segment(aes(x = -6, xend = 6, y = Loc, yend = Loc),
linetype = 'dotted', color = 'gray90') +
geom_segment(aes(x = Divorce - mu_hdi_h, xend = Divorce - mu_hdi_l,
y = Loc, yend = Loc),
shape = 3, color = 'black') +
geom_point(aes(Divorce - mu, Loc), shape = 21, fill = 'white') +
geom_point(aes(Divorce - y_hdi_l, Loc), shape = 3, color = 'gray80') +
geom_point(aes(Divorce - y_hdi_h, Loc), shape = 3, color = 'gray80') +
scale_x_continuous(limits = c(-6, 6)) +
labs(x = '', y = '')
```
Figure 5.6c
```{r}
ggplot() +
stat_smooth(data = d,
aes(WaffleHouses/Population, Divorce - mu), method = 'lm', fullrange = T, color = 'black', alpha = .2, lwd = .4) +
geom_point(data = d,
aes(WaffleHouses/Population, Divorce - mu), shape = 21, color = 'dodgerblue') +
geom_text(data = filter(d, Loc %in% c('AL', 'AR', 'GA', 'OK', 'ME', 'NJ', 'SC')),
aes(x = WaffleHouses/Population + 1.2, y = Divorce - mu, label = Loc), size = 3) +
labs(x = 'Waffles per capita', y = 'Divorce error')
```