-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch06_scm.qmd
410 lines (344 loc) · 11.7 KB
/
ch06_scm.qmd
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
# Structural Causal Models {#SCM}
```{r }
#| include: false
library(dplyr)
library(tidyr)
library(tidybayes)
library(rethinking)
library(brms)
library(skimr)
library(ggplot2)
library(dagitty)
library(ggdag)
library(ggdist)
library(patchwork)
library(paletteer)
```
Some options to facilitate the computations
```{r}
# For execution on a local, multicore CPU with excess RAM
options(mc.cores = parallel::detectCores())
# To avoid recompilation of unchanged Stan programs
rstan::rstan_options(auto_write = TRUE)
```
The default theme used by `ggplot2`
```{r}
# The default theme used by ggplot2
ggplot2::theme_set(ggthemes::theme_fivethirtyeight())
ggplot2::theme_update(title = element_text(color = "midnightblue"))
```
This is a ver y important point, in the intro to chapter 6.
> Regression will not sort it out. Regression is indeed an oracle, but a cruel one. It speaks in riddle and delights in punishing us for asking bad questions. The selection-distortion effect can happen inside of a multiple regression, becase the fact of adding a predictor induces statistical selection within the model, a phenomenon that goes by the unhelpful name of **collider bias**. This can mislead us into believing, for axample, that there is a negative associaiton between newswothiness and trustworthiness in general, ehen in fact it is just a consequence of conditioning on some variable.
## Multicollinearity
Multicollinearity means a very strong association between 2 or more predictor variables.
### Multicollinear legs
Create the data
```{r}
set.seed(6)
dataLegs <-
tibble(height = rnorm(100, mean = 10, sd = 2),
leg_prop = runif(100, min = 0.4, max = 0.5)) |>
mutate(leg_left = leg_prop * height + rnorm(100, mean = 0, sd = 0.02),
leg_right = leg_prop * height + rnorm(100, mean = 0, sd = 0.02))
```
which has the following correlations
```{r}
dataLegs |>
cor() |>
ggcorrplot::ggcorrplot(type = "lower", lab = TRUE, digits = 2,
show.legend = FALSE,
title = "Correlations between variables") +
scale_fill_paletteer_c("pals::warmcool", direction = -1)
```
```{r ch06_fit06_01}
tictoc::tic(msg = sprintf("run time of %s, use the cache.", "110 secs."))
fit06_01 <- xfun::cache_rds({brm(data = dataLegs,
family = gaussian,
height ~ 1 + leg_left + leg_right,
prior = c(prior(normal(10, 100), class = Intercept),
prior(normal(2, 10), class = b),
prior(exponential(1), class = sigma)),
iter = 2000, warmup = 1000, chains = 4, cores = detectCores(),
seed = 6)},
file = "ch06_fit06_01")
tictoc::toc()
```
```{r}
posterior_summary(fit06_01) |> round(digits = 3)
```
```{r}
tidybayes::get_variables(fit06_01)
```
```{r}
tidybayes::gather_draws(fit06_01, b_Intercept, b_leg_left, b_leg_right, sigma) |>
mean_hdi() |>
ggplot(aes(x = .value, xmin = .lower, xmax = .upper, y = .variable, color = .variable)) +
geom_pointinterval() +
ggrepel::geom_text_repel(aes(label = round(.value, 2))) +
scale_color_paletteer_d("Manu::Kereru") +
labs(title = "Leg model",
x = "value", y = NULL, color = NULL)
```
### Multicollinear milk
```{r}
data(milk)
dataMilk <- milk
dataMilk <- dataMilk |>
mutate(K = as.vector(scale(kcal.per.g)),
`F` = as.vector(scale(perc.fat)),
L = as.vector(scale(perc.lactose)))
```
```{r}
plotMilk <- list()
plotMilk <- within(plotMilk, {
fun_diag <- function(data, mapping, ...){
ggplot(data = data, mapping = mapping) +
geom_density(linewidth = 1, color = "pink")}
fun_upper <- function(data, mapping) {
ggplot(data = data, mapping = mapping) +
geom_text(size = 1, color = "blue")}
fun_lower <- function(data, mapping) {
ggplot(data = data, mapping = mapping) +
stat_density2d(linewidth = 1/3, color = "blue") +
geom_point(size = 1, color = "blue") +
geom_smooth(method = "loess")}
plot <- GGally::ggpairs(
data = dataMilk,
columns = c("K", "F", "L"),
title = "Milk example",
diag = list(continuous = fun_diag),
lower = list(continuous = fun_lower))
})
plotMilk$plot
```
```{r ch06_fit06_03}
tictoc::tic(msg = sprintf("run time of %s, use the cache.", "80 secs."))
fit06_03 <- xfun::cache_rds({
# k regressed on f
brm(data = dataMilk,
family = gaussian,
K ~ 1 + `F`,
prior = c(prior(normal(0, 0.2), class = Intercept),
prior(normal(0, 0.5), class = b),
prior(exponential(1), class = sigma)),
iter = 2000, warmup = 1000, chains = 4, cores = detectCores(),
seed = 6)},
file = "ch06_fit06_03")
tictoc::toc()
```
```{r ch06_fit06_04}
tictoc::tic(msg = sprintf("run time of %s, use the cache.", "90 secs."))
fit06_04 <- xfun::cache_rds({
# k regressed on f
brm(data = dataMilk,
family = gaussian,
K ~ 1 + L,
prior = c(prior(normal(0, 0.2), class = Intercept),
prior(normal(0, 0.5), class = b),
prior(exponential(1), class = sigma)),
iter = 2000, warmup = 1000, chains = 4, cores = detectCores(),
seed = 6)},
file = "ch06_fit06_04")
tictoc::toc()
```
and the coefficients are
```{r}
posterior_summary(fit06_03) |> round(digits = 3)
```
```{r}
posterior_summary(fit06_04) |> round(digits = 2)
```
and the multivariate which shows that each variable has now a much larger variance caused by the colinearity.
```{r ch06_fit06_05}
tictoc::tic(msg = sprintf("run time of %s, use the cache.", "60 secs."))
fit06_05 <- xfun::cache_rds({
brm(data = dataMilk,
family = gaussian,
K ~ 1 + `F` + L,
prior = c(prior(normal(0, 0.2), class = Intercept),
prior(normal(0, 0.5), class = b),
prior(exponential(1), class = sigma)),
iter = 2000, warmup = 1000, chains = 4, cores = detectCores(),
seed = 6)},
file = "ch06_fit06_05")
tictoc::toc()
```
## Post-treatment bias
```{r}
# how many plants would you like?
set.seed(7)
dataPlants <- tibble(
h0 = rnorm(100, mean = 10, sd = 2),
treatment = rep(0:1, each = 100 / 2),
fungus = rbinom(100, size = 1, prob = .5 - treatment * 0.4),
h1 = h0 + rnorm(100, mean = 5 - 3 * fungus, sd = 1))
skimr::skim(dataPlants)
```
### A prior is born
> If we center our prior for $p$ on 1, that implies an expectation of no change in height. That is less than we know. But we would allow $p$ to be less than 1, in case the experiment ges wrong. We also want to ensure $p>0$.
Therefore we use $p$ with log-normal distribution
$$
\begin{align*}
h_{1,i} &\sim \mathcal{N}(\mu_i, \sigma) \\
\mu_i &= h_{0,i} \times p \\
p &\sim \mathcal{LogNormal}(0, 0.25) \\
\sigma &\sim \mathcal{Exp}(1)
\end{align*}
$$
```{r ch06_fit06_06}
tictoc::tic(msg = sprintf("run time of %s, use the cache.", "90 secs."))
fit06_06 <- xfun::cache_rds({
out <- brm(
data = dataPlants,
family = gaussian,
h1 ~ 0 + h0,
prior = c(prior(lognormal(0, 0.25), class = b, lb = 0),
prior(exponential(1), class = sigma)),
iter = 2000, warmup = 1000, chains = 4, cores = detectCores(),
seed = 6)
out <- brms::add_criterion(out, criterion = c("waic", "loo"))
out},
file = "ch06_fit06_06")
tictoc::toc()
```
```{r}
brms::posterior_summary(fit06_06) |> round(digits = 2)
```
So the increase is 1.38 relative to $h_0$.
Now including the treatment and fungus we have
$$
\begin{align*}
h_{1,i} &\sim \mathcal{N}(\mu_i, \sigma) \\
\mu_i &= h_{0,i} \times p \\
p &\sim \alpha + \beta_1 treatment_i + \beta_2 fungus_i \\
\alpha &\sim \mathcal{LogNormal}(0, 0.25) \\
\beta_1 &\sim \mathcal{N}(0, 0.5) \\
\beta_2 &\sim \mathcal{N}(0, 0.5) \\
\sigma &\sim \mathcal{Exp}(1)
\end{align*}
$$
```{r ch06_fit06_07}
tictoc::tic(msg = sprintf("run time of %s, use the cache.", "80 secs."))
fit06_07 <- xfun::cache_rds({
out <- brm(
data = dataPlants,
family = gaussian,
bf(h1 ~ h0 * (a + t * treatment + f * fungus),
a + t + f ~ 1,
nl = TRUE),
prior = c(prior(lognormal(0, 0.2), nlpar = a, lb = 0),
prior(normal(0, 0.5), nlpar = t),
prior(normal(0, 0.5), nlpar = f),
prior(exponential(1), class = sigma)),
iter = 2000, warmup = 1000, chains = 4, cores = detectCores(),
seed = 6)
out <- brms::add_criterion(out, criterion = c("waic", "loo"))
out},
file = "ch06_fit06_07")
tictoc::toc()
```
```{r}
brms::posterior_summary(fit06_07) |> round(digits = 2)
```
Now the effect of the treatment is almost non existent.
### Blocked by consequence
The problem is that the fungus is part of a chain between the treatment and the growth.
```{r}
ggdag::dagify(h1 ~ h0, h1 ~ `F`, `F` ~ `T`) |>
ggdag::ggdag(layout = "sugiyama", node_size = 8, text_col = "yellow") +
ggdag::theme_dag_blank(
panel.background = element_rect(fill = "snow2", color = "snow2"))
```
so now we redo the model but without the fungus effect.
$$
\begin{align*}
h_{1,i} &\sim \mathcal{N}(\mu_i, \sigma) \\
\mu_i &= h_{0,i} \times p \\
p &\sim \alpha + \beta_1 treatment_i \\
\alpha &\sim \mathcal{LogNormal}(0, 0.25) \\
\beta_1 &\sim \mathcal{N}(0, 0.5) \\
\sigma &\sim \mathcal{Exp}(1)
\end{align*}
$$
```{r ch06_fit06_08}
tictoc::tic(msg = sprintf("run time of %s, use the cache.", "80 secs."))
fit06_08 <- xfun::cache_rds({
out <- brm(
data = dataPlants,
family = gaussian,
bf(h1 ~ h0 * (a + t * treatment),
a + t ~ 1, nl = TRUE),
prior = c(prior(lognormal(0, 0.2), nlpar = a, lb = 0),
prior(normal(0, 0.5), nlpar = t),
prior(exponential(1), class = sigma)),
iter = 2000, warmup = 1000, chains = 4, cores = detectCores(),
seed = 6)
out <- brms::add_criterion(out, criterion = c("waic", "loo"))},
file = "ch06_fit06_08")
tictoc::toc()
```
```{r}
posterior_summary(fit06_08) |> round(digits = 3)
```
and we now see more treatment effect.
## Collider bias
### Collider of false sorrow
```{r}
dataHappy <- rethinking::sim_happiness(seed = 1977, N_years = 1000)
# select age > 17 and rescale to [0, 1] and create indexed factor
# creating factor makes it easer with brms
dataHappy_gt17 <- dataHappy |>
filter(age > 17) |>
mutate(A = scales::rescale(age, to = c(0, 1)),
mid = factor(married + 1, labels = c("single", "married")))
# glimpse(dataHappy_gt17)
```
```{r ch06_fit06_09}
tictoc::tic(msg = sprintf("run time of %s, use the cache.", "80 secs."))
fit06_09 <- xfun::cache_rds({
out <- brm(
data = dataHappy_gt17,
family = gaussian,
happiness ~ 0 + mid + A,
prior = c(prior(normal(0, 1), class = b, coef = midmarried),
prior(normal(0, 1), class = b, coef = midsingle),
prior(normal(0, 2), class = b, coef = A),
prior(exponential(1), class = sigma)),
iter = 2000, warmup = 1000, chains = 4, cores = detectCores(),
seed = 6)
out <- brms::add_criterion(out, criterion = c("waic", "loo"))},
file = "ch06_fit06_09")
tictoc::toc()
```
```{r}
posterior_summary(fit06_09)
```
The fit finds that the effect of age on happiness is negative
now lets do it without the marriage factor
```{r ch06_fit06_10}
tictoc::tic(msg = sprintf("run time of %s, use the cache.", "90 secs."))
fit06_10 <- xfun::cache_rds({
out <- brm(
data = dataHappy_gt17,
family = gaussian,
happiness ~ 1 + A,
prior = c(prior(normal(0, 1), class = Intercept),
prior(normal(0, 2), class = b),
prior(exponential(1), class = sigma)),
iter = 2000, warmup = 1000, chains = 4, cores = detectCores(),
seed = 6)
out <- brms::add_criterion(out, criterion = c("waic", "loo"))},
file = "ch06_fit06_10")
tictoc::toc()
```
```{r}
posterior_summary(fit06_10)
```
Now the age has no effect on happiness! When we include marriage, we include a spurious association.
### The haunted DAG
## Confronting counfounding
See Overthinking box in section 6.4.3. Confounding occurs when
$$
Pr(Y \mid X) \neq Pr(Y \mid do(X))
$$
## Summary