-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path12.Rmd
485 lines (366 loc) · 18.8 KB
/
12.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# Further Examples of Conditional Process Analysis
```{r, echo = FALSE, cachse = FALSE}
options(width = 110)
```
> In this chapter [Hayes built] on the foundation laid by stepping through an analysis of a more complicated conditional process model that include[d] moderation of both the indirect effects in a simple mediation model. [He did] so by first using a piecemeal approach that focuse[d] on each pathway in the model. With some understanding gained by this examination of the components in the process, [he brought] the pieces together into an integrated conditional process analysis...
>
> When an indirect effect of $X$ on $Y$ through $M$ is moderated, we call this phenomenon *moderated mediation*. In such a scenario, the mechanism represented by the $X \rightarrow M \rightarrow Y$ chain of events operates to varying degrees (or not at all) for certain people or in certain contexts. A similar-sounding phenomenon is *mediated moderation*, which refers to the scenario in which an interaction between X and some moderator $W$ on $Y$ is carried through a mediator $M$. [We'll see] in this chapter that a mediated moderation analysis is really nothing other than a mediation analysis with the product of two variables serving as the causal agent of focus. [@hayesIntroductionMediationModeration2018, p. 432]
## Revisiting the disaster framing study
Here we load a couple necessary packages, load the data, and take a `glimpse()`.
```{r, warning = F, message = F}
library(tidyverse)
disaster <- read_csv("data/disaster/disaster.csv")
glimpse(disaster)
```
Load **brms**.
```{r, message = F, warning = F}
library(brms)
```
At the top of page 433, Hayes fit a simple univariable model
$$Y = b_0 + b_1 X + e_Y,$$
where the $X$ is `frame` the $Y$ is `donate`. Here's the model.
```{r model12.1}
model12.1 <- brm(
data = disaster,
family = gaussian,
donate ~ 1 + frame,
chains = 4, cores = 4,
file = "fits/model12.01")
```
Check the summary.
```{r}
print(model12.1, digits = 3)
```
Hayes interpreted the coefficient for `frame` through the lens of a $t$ statistic and accompanying $p$-value. We'll just plot the posterior. For the figures in this chapter, we'll take theme cues from the vignettes from Matthew Kay's [**tidybayes** package](https://mjskay.github.io/tidybayes/).
```{r, fig.width = 4, fig.height = 2.5, warning = F, message = F}
library(tidybayes)
theme_set(theme_tidybayes() + cowplot::panel_border())
as_draws_df(model12.1) %>%
ggplot(aes(x = b_frame, y = 0)) +
stat_halfeye() +
geom_vline(xintercept = 0, linetype = 2) +
scale_y_continuous(NULL, breaks = NULL)
```
Now we fit the moderation model
$$Y = b_0 + b_1 X + b_2 W + b_3 XW + e_Y,$$
where `skeptic` is the $W$ variable.
```{r model12.2}
model12.2 <- brm(
data = disaster,
family = gaussian,
donate ~ 1 + frame + skeptic + frame:skeptic,
chains = 4, cores = 4,
file = "fits/model12.02")
```
Our `model12.2` summary matches nicely with the text.
```{r}
print(model12.2, digits = 3)
```
Here's our Figure 12.2.
```{r, fig.width = 6, fig.height = 5}
nd <- crossing(frame = 0:1,
skeptic = seq(from = 0, to = 7, length.out = 30))
fitted(model12.2, newdata = nd) %>%
data.frame() %>%
bind_cols(nd) %>%
mutate(frame = ifelse(frame == 0, str_c("Natural causes (X = ", frame, ")"),
str_c("Climate change (X = ", frame, ")"))) %>%
mutate(frame = factor(frame,
levels = c("Natural causes (X = 0)",
"Climate change (X = 1)"))) %>%
ggplot(aes(x = skeptic, y = Estimate)) +
geom_ribbon(aes(ymin = Q2.5, ymax = Q97.5, fill = frame),
alpha = 1/3) +
geom_line(aes(color = frame)) +
scale_fill_brewer(palette = "Set2") +
scale_color_brewer(palette = "Set2") +
coord_cartesian(xlim = c(1, 6),
ylim = c(3.5, 5.5)) +
labs(x = expression(paste("Climate Change Skepticism (", italic(W), ")")),
y = "Willingness to Donate to Victims") +
theme(legend.direction = "horizontal",
legend.position = "top",
legend.title = element_blank())
```
In Hayes's Figure 12.2, he emphasized the differences at the three levels of `skeptic`. If you want the full difference score distributions in a pick-a-point-approach sort of way, you might plot the densities with `tidybayes::stat_halfeye()`, which places coefficient plots at the base of the densities. In this case, we show the posterior medians with the dots, the 50% intervals with the thick horizontal lines, and the 95% intervals with the thinner horizontal lines.
```{r, fig.width = 6, fig.height = 2.5}
nd <- crossing(frame = 0:1,
skeptic = quantile(disaster$skeptic, probs = c(.16, .5, .86)))
fitted(model12.2,
summary = F,
newdata = nd) %>%
data.frame() %>%
set_names(mutate(nd, label = str_c(frame, "_", skeptic)) %>% pull(label)) %>%
mutate(iter = 1:n()) %>%
pivot_longer(-iter) %>%
separate(name, into = c("frame", "skeptic"), sep = "_", convert = T) %>%
pivot_wider(names_from = frame, values_from = value) %>%
mutate(difference = `1` - `0`) %>%
ggplot(aes(x = difference, y = skeptic, fill = skeptic %>% as.character())) +
stat_halfeye(point_interval = median_qi, .width = c(0.95, 0.5)) +
scale_fill_brewer() +
scale_y_continuous(breaks = quantile(disaster$skeptic, probs = c(.16, .5, .86)),
labels = quantile(disaster$skeptic, probs = c(.16, .5, .86)) %>% round(2)) +
theme(legend.position = "none")
```
Here's our simple mediation model, `model12.3`, using the multivariate syntax right in the `brm()` function.
```{r model12.3}
model12.3 <- brm(
data = disaster,
family = gaussian,
bf(justify ~ 1 + frame) +
bf(donate ~ 1 + frame + justify) +
set_rescor(FALSE),
chains = 4, cores = 4,
file = "fits/model12.03")
```
```{r}
print(model12.3, digits = 3)
```
Consider the Bayesian $R^2$ summaries.
```{r}
bayes_R2(model12.3) %>% round(digits = 3)
```
If you want the indirect effect with its intervals, you use `as_draws_df()` and data wrangle, as usual.
```{r}
as_draws_df(model12.3) %>%
mutate(ab = b_justify_frame * b_donate_justify) %>%
mean_qi(ab)
```
## Moderation of the direct and indirect effects in a conditional process model
Our conditional process model follows the form
\begin{align*}
M & = i_M + a_1 X + a_2 W + a_3 XW + e_M \\
Y & = i_Y + c_1' X + c_2' W + c_3' XW + b M + e_Y.
\end{align*}
We don't need to do anything particularly special to fit a model like this with **brms**. It just requires we do a careful job specifying the formulas in our `bf()` arguments. If you find this syntax a little too cumbersome, you can always specify the formulas outside of `brm()`, save them as one or multiple objects, and plug those objects into `brm()`.
```{r model12.4}
model12.4 <- brm(
data = disaster,
family = gaussian,
bf(justify ~ 1 + frame + skeptic + frame:skeptic) +
bf(donate ~ 1 + frame + justify + skeptic + frame:skeptic) +
set_rescor(FALSE),
chains = 4, cores = 4,
file = "fits/model12.04")
```
Check the model summary.
```{r}
print(model12.4, digits = 3)
```
Here are the Bayesian $R^2$ summaries.
```{r}
bayes_R2(model12.4)
```
### ~~Estimation using PROCESS~~.
We just fit that model. Next!
### Quantifying direct and indirect effects.
Here are summaries for $a_1$ through $a_3$.
```{r}
fixef(model12.4)[c(3:5), ] %>% round(digits = 3)
```
This is $b$.
```{r}
fixef(model12.4)["donate_justify", ] %>% round(digits = 3)
```
We'll need to employ `as_draws_df()` to compute $(a_1 + a_3 W)b$, as shown in Table 12.2.
```{r, warning = F}
draws <- as_draws_df(model12.4) %>%
mutate(`indirect effect when W is 1.592` = (b_justify_frame + `b_justify_frame:skeptic` * 1.592) * b_donate_justify,
`indirect effect when W is 2.800` = (b_justify_frame + `b_justify_frame:skeptic` * 2.800) * b_donate_justify,
`indirect effect when W is 5.200` = (b_justify_frame + `b_justify_frame:skeptic` * 5.200) * b_donate_justify)
draws %>%
pivot_longer(starts_with("indirect")) %>%
group_by(name) %>%
median_qi(value, .width = .95) %>%
mutate_if(is.double, round, digits = 3)
```
And if you really want that full-on Table 12.2 layout, try this.
```{r, warning = F, message = F}
draws %>%
transmute(a1 = b_justify_frame,
a3 = `b_justify_frame:skeptic`,
b = b_donate_justify,
c1 = b_donate_frame,
c3 = `b_donate_frame:skeptic`) %>%
expand_grid(w = c(1.592, 2.800, 5.200)) %>%
mutate(`a1 + a3w` = a1 + a3 * w,
`(a1 + a3w)b` = (a1 + a3 * w) * b,
`direct effect` = c1 + c3 * w) %>%
select(-(a1:a3), -(c1:c3)) %>%
pivot_longer(-w) %>%
group_by(w, name) %>%
summarise(mean = mean(value) %>% round(digits = 3)) %>%
pivot_wider(names_from = name,
values_from = mean) %>%
select(w, `a1 + a3w`, b, everything())
```
#### The conditional direct effect of $X$.
We already computed this in the last code block, above. But since we just focused on the posterior means, here's a summary of their medians and 95% intervals.
```{r, warning = F}
draws <- draws %>%
mutate(`direct effect when W is 1.592` = b_donate_frame + `b_donate_frame:skeptic` * 1.592,
`direct effect when W is 2.800` = b_donate_frame + `b_donate_frame:skeptic` * 2.800,
`direct effect when W is 5.200` = b_donate_frame + `b_donate_frame:skeptic` * 5.200)
draws %>%
pivot_longer(starts_with("direct")) %>%
group_by(name) %>%
median_qi(value, .width = .95) %>%
mutate_if(is.double, round, digits = 3) %>%
select(name:.upper)
```
We can always plot, too.
```{r, fig.width = 6, fig.height = 2.5}
w <- c(1.592, 2.800, 5.200)
draws %>%
expand_grid(w = w) %>%
mutate(`conditional direct effect` = b_donate_frame + `b_donate_frame:skeptic` * w) %>%
ggplot(aes(x = `conditional direct effect`, y = w, fill = w %>% as.character())) +
stat_halfeye(point_interval = median_qi, .width = c(0.95, 0.5)) +
scale_y_continuous(breaks = w) +
scale_fill_brewer() +
theme(legend.position = "none")
```
### Visualizing the direct and indirect effects.
We'll need to do some `draws` wrangling before we're ready to make our Figure 12.7. We'll save the results as `effects`.
```{r}
effects <- draws %>%
expand_grid(w = seq(from = 0, to = 6, length.out = 30)) %>%
mutate(`direct effect` = b_donate_frame + `b_donate_frame:skeptic` * w,
`indirect effect` = (b_justify_frame + `b_justify_frame:skeptic` * w) * b_donate_justify) %>%
pivot_longer(c(`direct effect`, `indirect effect`)) %>%
mutate(name = factor(name, levels = c("direct effect", "indirect effect"))) %>%
select(w:value)
head(effects)
```
Now we plot.
```{r, fig.width = 6, fig.height = 4}
# we'll need this for `geom_text()`
text <- tibble(
x = c(4.2, 4.7),
y = c(.28, -.28),
angle = c(3.6, 335),
name = factor(c("direct effect", "indirect effect"), levels = c("direct effect", "indirect effect")))
# plot!
effects %>%
ggplot(aes(x = w, color = name, fill = name)) +
stat_lineribbon(aes(y = value),
.width = .95, alpha = 1/3) +
geom_text(data = text,
aes(x = x, y = y,
angle = angle,
label = name),
size = 5) +
scale_fill_brewer(type = "qual") +
scale_color_brewer(type = "qual") +
coord_cartesian(xlim = c(1, 5.5),
ylim = c(-.6, .4)) +
labs(x = expression(Climate~Change~Skepticism~(italic(W))),
y = "Effects of Disaster Frame on Willingness to Donate") +
theme(legend.position = "none")
```
Note how wide those 95% intervals are relative to the scale of the $y$-axis, which I specifically kept within the same range as Figure 12.7 in the text. To me the message is clear: include credible-interval ribbons in your regression slope plots. They help depict how uncertain the posterior is in a way a simple line slopes just don't.
## Statistical inference
### Inference about the direct effect.
We've already computed the 95% intervals for the direct effect, $\theta_{X \rightarrow Y}$, conditional on the three levels of $W$. Here's a different look at those intervals, superimposed on the 80% and 50% intervals, using the `tidybayes::stat_interval()` function.
```{r, fig.width = 6, fig.height = 4, warning = F}
draws %>%
pivot_longer(starts_with("direct")) %>%
mutate(name = str_remove(name, "direct effect when W is ") %>% as.double()) %>%
ggplot(aes(x = name, y = value, group = name)) +
stat_interval(.width = c(.95, .80, .5)) +
scale_color_brewer("Interval") +
coord_cartesian(xlim = c(1, 5.5)) +
labs(x = expression(paste("Climate Change Skepticism (", italic(W), ")")),
y = "Conditional Direct Effect of Disaster Frame on\nWillingness to Donate")
```
### Inference about the indirect effect.
#### A statistical test of moderated mediation.
To get a sense of $a_3 b$, we just:
```{r, warning = F}
draws <- draws %>%
mutate(a3b = `b_justify_frame:skeptic` * b_donate_justify)
draws %>%
select(a3b) %>%
median_qi(a3b) %>%
mutate_if(is.double, round, digits = 3)
```
Here we'll combine `stat_intervalh()` and `stat_pointintervalh()` to visualize $a_3 b$ with a coefficient plot.
```{r, fig.width = 6, fig.height = 1}
draws %>%
ggplot(aes(x = a3b, y = 1)) +
stat_interval(.width = c(.95, .8, .5)) +
stat_pointinterval(point_interval = median_qi, .width = c(.95, .8, .5),
position = position_nudge(y = -.75)) +
scale_color_brewer("Interval") +
scale_y_discrete(NULL, breaks = NULL) +
coord_cartesian(xlim = c(-.5, 0)) +
labs(title = expression(paste("Coefficient plot for ", italic(a)[3], italic(b), " (i.e., the index of moderated mediation)")),
x = NULL) +
theme(legend.position = "none")
```
#### Probing moderation of mediation.
As we discussed in Chapter 11, our Bayesian version of the JN technique should be fine because HMC does not impose the normality assumption on the parameter posteriors. In this instance, I'll leave the JN technique plot as an exercise for the interested reader. Here we'll just follow along with the text and pick a few points.
We computed and inspected these 95% intervals, above. Here's another way we might `stat_halfeye()` to look at their entire densities.
```{r, fig.width = 5, fig.height = 3, warning = F}
draws %>%
pivot_longer(starts_with("indirect")) %>%
rename(`conditional indirect effect` = value) %>%
mutate(W = str_remove(name, "indirect effect when W is ") %>% as.double()) %>%
ggplot(aes(x = W, y = `conditional indirect effect`, fill = W %>% as.character())) +
geom_hline(yintercept = 0, linetype = 2) +
stat_halfeye(point_interval = median_qi, .width = 0.95) +
scale_fill_brewer() +
scale_x_continuous(breaks = c(1.592, 2.8, 5.2),
labels = c(1.6, 2.8, 5.2)) +
coord_cartesian(ylim = c(-1, 1)) +
theme(legend.position = "none",
panel.grid.minor.y = element_blank())
```
### Pruning the model.
Fitting the model without the interaction term is just a small change to one of our `formula` arguments.
```{r model12.5}
model12.5 <- brm(
data = disaster,
family = gaussian,
bf(justify ~ 1 + frame + skeptic + frame:skeptic) +
bf(donate ~ 1 + frame + justify + skeptic) +
set_rescor(FALSE),
chains = 4, cores = 4,
file = "fits/model12.05")
```
Here are the results.
```{r}
print(model12.5, digits = 3)
```
Since we're altering the model, we may as well use information criteria to compare the two versions.
```{r}
model12.4 <- add_criterion(model12.4, criterion = "loo")
model12.5 <- add_criterion(model12.5, criterion = "loo")
loo_compare(model12.4, model12.5) %>%
print(simplify = F)
```
The difference in LOO-CV values for the two models was modest. There's little predictive reason to choose one over the other. You could argue in favor of `model12.5` because it's simpler than `model12.4`. Since we've got a complex model either way, one might also consider which one was of primary theoretical interest.
## Mediated moderation
> Mediation is moderated if the indirect effect of $X$ on $Y$ through one or more mediators is contingent on a moderator. With evidence of moderated mediation, one can claim that the $X \rightarrow M \rightarrow Y$ chain of events functions differently or to varying degrees for different people, in different contexts or conditions, or whatever the moderator variable represents. Although similar in name and pronunciation to moderated mediation, the term *mediated moderation* refers to the phenomenon in which an interaction between $X$ and a moderator $W$ in a model of $Y$ is carried through a mediator. (p. 459, *emphasis* in the original)
Hayes later opined:
> Although there is an abundance of published examples of mediated moderation analysis, their frequency of occurrence in the literature should not be confused with meaningfulness of the procedure itself. I will argue toward the end of this section that rarely is the phenomenon of mediated moderation interesting when interpreted as such. It is almost always substantively more meaningful to conceptualize a mediated moderation process in terms of moderated mediation. But before doing this, I will describe how a mediated moderation analysis is undertaken. (p. 460)
### Mediated moderation as the indirect effect of a product.
Hayes explains this in the next subsection, but we've already fit this model presented in this subsection. We called it `model12.4`. Here's the summary.
```{r}
print(model12.4, digits = 3)
```
### Why mediated moderation is neither interesting nor meaningful.
Mediated moderation and moderated mediation are statistically the same.
> The only difference between them is how they are interpreted, and on what part of the model your attention is focused.
>
> Moderated mediation focuses on the conditional nature of an indirect effect--how an indirect effect is moderated. If you think of the terms "mediation" and "indirect effect" as essentially synonymous conceptually, then moderated mediation means a moderated indirect effect. Interpretive focus in a moderated mediation analysis is directed at estimating the indirect effect and how that effect varies as a function of a moderator. Mediated moderation, by contrast, asks about the mechanism through which an interaction between $X$ and a moderator $W$ operates, where the product of $X$ and $W$ is construed as the causal agent sending its effect to $Y$ through $M$. Focus in mediated moderation is the estimation of the indirect effect of the product of $X$ and $W$. (p. 465)
Hayes later concluded that we should avoid
> the articulation of hypotheses or research questions in terms of the mediation of the effect of a product, abandoning the term *mediated moderation* entirely, and instead [reframe] such hypotheses and research questions in terms of the contingencies of an indirect effect--moderated mediation. (p. 467, *emphasis* in the original)
## Session info {-}
```{r}
sessionInfo()
```
```{r, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```