You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update Mediation analysis notebook to best practice (#280)
* create truncated regression example
* delete truncated regression example from main branch
* create truncated regression example
* delete truncated regression example from main branch
* create truncated regression example
* delete truncated regression example from main branch
* update to best practice
* get watermark header right
* remove silly "," from tags
* fix references, hopefully
* fix in text citations
* add 'path analysis' as a tag
* fix error in description of total effect
* combine some cells, for simplicity
* update estimate which is off by 1dp under v4 for some reason
* add dims="obs_id" to ConstantData
* remove reference to "now" in terms of current best practice
* improve DAG and fix typo
* add in a dims="obs_id"
* run pre commit hook
Co-authored-by: Benjamin T. Vincent <[email protected]>
Co-authored-by: Oriol (ZBook) <[email protected]>
This notebook covers Bayesian [mediation analysis](https://en.wikipedia.org/wiki/Mediation_(statistics)). This is useful when we want to explore possible mediating pathways between a predictor and an outcome variable.
19
24
20
-
It is important to note that the approach to mediation analysis has evolved over time. This notebook will attempt to use best practice as of now, and is heavily influenced by the approach of Hayes (2018) as set out in his textbook "Introduction to Mediation, Moderation and Conditional Process Analysis."
25
+
It is important to note that the approach to mediation analysis has evolved over time. This notebook was heavily influenced by the approach of {cite:t}`hayes2017introduction` as set out in his textbook "Introduction to Mediation, Moderation and Conditional Process Analysis."
21
26
22
27
```{code-cell} ipython3
23
28
import arviz as az
24
29
import matplotlib.pyplot as plt
25
30
import numpy as np
26
-
import pymc3 as pm
31
+
import pymc as pm
27
32
import seaborn as sns
28
33
29
34
from pandas import DataFrame
@@ -52,10 +57,10 @@ where $i$ indexes each observation (row in the dataset), and $i_M$ and $i_Y$ are
52
57
53
58

54
59
55
-
Using definitions from Hayes (2018), we can define a few effects of interest:
60
+
Using definitions from {cite:t}`hayes2017introduction`, we can define a few effects of interest:
56
61
-**Direct effect:** is given by $c'$. Two cases that differ by one unit on $x$ but are equal on $m$ are estimated to differ by $c'$ units on $y$.
57
62
-**Indirect effect:** is given by $a \cdot b$. Two cases which differ by one unit of $x$ are estimated to differ by $a \cdot b$ units on $y$ as a result of the effect of $x \rightarrow m$ and $m \rightarrow y$.
58
-
-**Total effect:** is $c = c' + a \cdot b$ which is simply the sum of the direct and indirect effects. This could be understood as: two cases that differ by one unit on $x$ are estimated to differ by $a \cdot b$ units on $y$ due to both the direct pathway $x \rightarrow y$ and the indirect pathway $c \rightarrow m \rightarrow m$. The total effect could also be estimated by evaluating the alternative model $y_i \sim \mathrm{Normal}(i_{Y*} + c \cdot x_i, \sigma_{Y*})$.
63
+
-**Total effect:** is $c = c' + a \cdot b$ which is simply the sum of the direct and indirect effects. This could be understood as: two cases that differ by one unit on $x$ are estimated to differ by $a \cdot b$ units on $y$ due to the indirect pathway $x \rightarrow m \rightarrow y$, and by $c'$ units due to the direct pathway $x \rightarrow y$. The total effect could also be estimated by evaluating the alternative model $y_i \sim \mathrm{Normal}(i_{Y*} + c \cdot x_i, \sigma_{Y*})$.
59
64
60
65
+++
61
66
@@ -85,9 +90,9 @@ sns.pairplot(DataFrame({"x": x, "m": m, "y": y}));
85
90
```{code-cell} ipython3
86
91
def mediation_model(x, m, y):
87
92
with pm.Model() as model:
88
-
x = pm.Data("x", x)
89
-
y = pm.Data("y", y)
90
-
m = pm.Data("m", m)
93
+
x = pm.ConstantData("x", x, dims="obs_id")
94
+
y = pm.ConstantData("y", y, dims="obs_id")
95
+
m = pm.ConstantData("m", m, dims="obs_id")
91
96
92
97
# intercept priors
93
98
im = pm.Normal("im", mu=0, sigma=10)
@@ -101,41 +106,30 @@ def mediation_model(x, m, y):
101
106
σy = pm.HalfCauchy("σy", 1)
102
107
103
108
# likelihood
104
-
pm.Normal("m_likehood", mu=im + a * x, sigma=σm, observed=m)
105
-
pm.Normal("y_likehood", mu=iy + b * m + cprime * x, sigma=σy, observed=y)
109
+
pm.Normal("m_likelihood", mu=im + a * x, sigma=σm, observed=m, dims="obs_id")
110
+
pm.Normal("y_likelihood", mu=iy + b * m + cprime * x, sigma=σy, observed=y, dims="obs_id")
106
111
107
112
# calculate quantities of interest
108
113
indirect_effect = pm.Deterministic("indirect effect", a * b)
109
114
total_effect = pm.Deterministic("total effect", a * b + cprime)
result = pm.sample(tune=4000, target_accept=0.9, random_seed=42)
133
126
```
134
127
135
128
Visualise the trace to check for convergence.
136
129
137
130
```{code-cell} ipython3
138
-
az.plot_trace(result);
131
+
az.plot_trace(result)
132
+
plt.tight_layout()
139
133
```
140
134
141
135
We have good chain mixing and the posteriors for each chain look very similar, so no problems in that regard.
@@ -171,7 +165,7 @@ ax = az.plot_posterior(
171
165
ax[0].set(title="direct effect");
172
166
```
173
167
174
-
- The posterior mean **direct effect** is 0.28, meaning that for every 1 unit of increase in $x$, $y$ increases by 0.28 due to the direct effect $x \rightarrow y$.
168
+
- The posterior mean **direct effect** is 0.29, meaning that for every 1 unit of increase in $x$, $y$ increases by 0.29 due to the direct effect $x \rightarrow y$.
175
169
- The posterior mean **indirect effect** is 0.49, meaning that for every 1 unit of increase in $x$, $y$ increases by 0.49 through the pathway $x \rightarrow m \rightarrow y$. The probability that the indirect effect is zero is infintesimal.
176
170
- The posterior mean **total effect** is 0.77, meaning that for every 1 unit of increase in $x$, $y$ increases by 0.77 through both the direct and indirect pathways.
177
171
@@ -182,25 +176,17 @@ Above, we stated that the total effect could also be estimated by evaluating the
@@ -218,25 +204,36 @@ As we can see, the posterior distributions over the direct effects are near-iden
218
204
+++
219
205
220
206
## Parameter estimation versus hypothesis testing
221
-
This notebook has focussed on the approach of Bayesian parameter estimation. For many situations this is entirely sufficient, and more information can be found in Yuan & MacKinnon (2009). It will tell us, amongst other things, what our posterior beliefs are in the direct effects, indirect effects, and total effects. And we can use those posterior beliefs to conduct posterior predictive checks to visually check how well the model accounts for the data.
207
+
This notebook has focussed on the approach of Bayesian parameter estimation. For many situations this is entirely sufficient, and more information can be found in {cite:t}`yuan2009bayesian`. It will tell us, amongst other things, what our posterior beliefs are in the direct effects, indirect effects, and total effects. And we can use those posterior beliefs to conduct posterior predictive checks to visually check how well the model accounts for the data.
222
208
223
-
However, depending upon the use case it may be preferable to test hypotheses about the presence or absence of an indirect effect ($x \rightarrow m \rightarrow y$) for example. In this case, it may be more appropriate to take a more explicit hypothesis testing approach to see examine the relative credibility of the mediation model as compared to a simple direct effect model (i.e. $y_i = \mathrm{Normal}(i_{Y*} + c \cdot x_i, \sigma_{Y*})$). Readers are referred to Nuijten et al (2014) for a hypothesis testing approach to Bayesian mediation models and to Kruschke (2011) for more information on parameter estimation versus hypothesis testing.
209
+
However, depending upon the use case it may be preferable to test hypotheses about the presence or absence of an indirect effect ($x \rightarrow m \rightarrow y$) for example. In this case, it may be more appropriate to take a more explicit hypothesis testing approach to see examine the relative credibility of the mediation model as compared to a simple direct effect model (i.e. $y_i = \mathrm{Normal}(i_{Y*} + c \cdot x_i, \sigma_{Y*})$). Readers are referred to {cite:t}`nuijten2015default`for a hypothesis testing approach to Bayesian mediation models and to {cite:t}`kruschke2011bayesian` for more information on parameter estimation versus hypothesis testing.
224
210
225
211
+++
226
212
227
213
## Summary
228
-
As stated at the outset, the procedures used in mediation analysis have evolved over time. So there are plenty of people who are not necessarily up to speed with modern best practice. The approach in this notebook sticks to that outlined by Hayes (2018), but it is relevant to be aware of some of this history to avoid confusion - which is particularly important if defending your approach in peer review.
214
+
As stated at the outset, the procedures used in mediation analysis have evolved over time. So there are plenty of people who are not necessarily up to speed with modern best practice. The approach in this notebook sticks to that outlined by {cite:t}`hayes2017introduction`, but it is relevant to be aware of some of this history to avoid confusion - which is particularly important if defending your approach in peer review.
229
215
230
216
+++
231
217
232
-
# References
218
+
## Authors
219
+
- Authored by Benjamin T. Vincent in August 2021
220
+
- Updated by Benjamin T. Vincent in February 2022
233
221
234
-
- Hayes, A. F. (2018). Introduction to Mediation, Moderation, and Conditional Process Analysis: A Regression‐Based Approach. New York: Guilford Press. Retrieved from https://doi.org/10.1111/jedm.12050
235
-
- Kruschke, J. (2011). Bayesian Assessment of Null Values Via Parameter Estimation and Model Comparison. Perspectives on Psychological Science, 6(3), 299–312. Retrieved from https://doi.org/10.1177/1745691611406925
236
-
- Nuijten, M. B., Wetzels, R., Matzke, D., Dolan, C. V., & Wagenmakers, E.-J. (2014). A default Bayesian hypothesis test for mediation. Behavior Research Methods, 47(1), 85–97. http://doi.org/10.3758/s13428-014-0470-2
237
-
- Yuan, Y., & MacKinnon, D. P. (2009). Bayesian mediation analysis. Psychological Methods, 14(4), 301–322. http://doi.org/10.1037/a0016972
0 commit comments