I put together a series of demos for a group of epidemiology students who are studying causal mediation analysis. Since mediation analysis is not always so clear or intuitive, I thought, of course, that going through some examples of simulating data for this process could clarify things a bit.
Quite often we are interested in understanding the relationship between an exposure or intervention on an outcome. Does exposure (A) (could be randomized or not) have an effect on outcome (Y)?
But sometimes we are interested in understanding more than whether or not (A) causes or influences (B); we might want to have some insight into the mechanisms underlying that influence. And this is where mediation analysis can be useful. (If you want to delve deeply into the topic, I recommend you check out this book by Tyler VanderWeele, or this nice website developed at Columbia University.)
In the example here, I am using the simplest possible scenario of an exposure (A), a mediator (M), and an outcome (Y), without any confounding:
A key challenge of understanding and conducting a mediation analysis is how we should quantify this concept of mediation. Sure, (A) has an effect on (M), which in turn has an effect on (Y), and (A) also may have an effect on (Y) through other pathways. But how can we make sense of all of this? One approach, which is a relatively recent development, is to use the potential outcome framework of causal inference to define the various estimands (or quantities) that arise in a mediation analysis. (I draw on a paper by Imai, Keele and Yamamoto for the terminology, as there is not complete agreement on what to call various quantities. The estimation methods and software used here are also described in the paper.)
Defining the potential outcomes
In an earlier post, I described the concept of potential outcomes. I extend that a bit here to define the quantities we are interested in. In this case, we have two effects of the possible exposure: (M) and (Y). Under this framework, each individual has a potential outcome for each level of (A) (I am assuming (A) is binary). So, for the mediator, (M_{i0}) and (M_{i1}) are the values of (M) we would observe for individual (i) without and with exposure, respectively. That is pretty straightforward. (From here on out, I will remove the subscript (i), because it gets a little unwieldy.)
The potential outcomes under (Y) are less intuitive, as there are four of them. First, there is (Y_{0M_0}), which is the potential outcome of (Y) without exposure for (A) and whatever the potential outcome for (M) is without exposure for (A). This is what we observe when (A=0) for an individual. (Y_{1M_1}) is the potential outcome of (Y) with exposure for (A) and whatever the potential outcome for (M) is with exposure for (A). This is what we observe when (A=1) for an individual. That’s all fine.
But we also have (Y_{0M_1}), which can never be observed unless we can intervene on the mediator (M) somehow. This is the potential outcome of (Y) without exposure for (A) and whatever the mediator would have been had the individual been exposed. This potential outcome is controversial, because it is defined across two different universes of exposure to (A). Finally, there is (Y_{1M_0}). It is analogously defined across two universes, but in reverse.
Defining the causal mediation effects and direct effects
The estimands or quantities that we are interested are defined in terms of the potential outcomes. The causal mediation effects for an individual are
[\begin{aligned}CME_0 &= Y_{0M_1} – Y_{0M_0} \CME_1 &= Y_{1M_1} – Y_{1M_0},\end{aligned}]
and the causal direct effects are
[\begin{aligned}CDE_0 &= Y_{1M_0} – Y_{0M_0} \CDE_1 &= Y_{1M_1} – Y_{0M_1}.\end{aligned}]
A few important points. (1) Since we are in the world of potential outcomes, we do not observe these quantities for everyone. In fact, we don’t observe these quantities for anyone, since some of the measures are across two universes. (2) The two causal mediation effects under do not need to be the same. The same goes for the two causal direct effects. (3) Under a set of pretty strong assumptions related to unmeasured confounding, independence, and consistency (see Imai et al for the details), the average causal mediation effects and average causal direct effects can be estimated using observed data only. Before I simulate some data to demonstrate all of this, here is the definition for the total causal effect (and its decomposition into mediation and direct effects):
[\begin{aligned}TCE &= Y_{1M_1} – Y_{0M_0} \&= CME_1 + CDE_0 \&= CME_0 + CDE_1\end{aligned}]
Generating the data
I’m using the simstudy
package to generate the data. I’ll start by generating the binary potential outcomes for the mediator, (M_0) and (M_1), which are correlated in this example. (P(M_1=1) > P(M_0=1)), implying that exposure to (A) does indeed have an effect on (M). Note that it is possible that for an individual (M_0 = 1) and (M_1 = 0), so that exposure to (A) has an effect contrary to what we see in the population generally. (We don’t need to make this assumption in the data generation process; we could force (M_1) to be 1 if (M_0) is 1.)
1 |
|
Observe treatment:
1 |
|
Initial data set:
1 |
|
1 |
|
(Y_{0M_0}) is a function of (M_0) and some noise (e_0), and (Y_{0M_1}) is a function of (M_1) and the same noise (this is not a requirement). However, if (M_0 = M_1) (i.e. the mediator is not affected by exposure status), then I am setting (Y_{0M_1} = Y_{0M_0}). In this case, (CME_0) for an individual is (2(M_1 – M_0)), so (CME_0 \in {-2, 0, 2}), and the population average (CME_0) will depend on the mixture of potential outcomes (M_0) and (M_1).
1 |
|
The same logic holds for (Y_{1M_0}) and (Y_{1M_1}), though at the individual level (CME_1 \in {-5, 0, 5}):
1 |
|
The observed mediator ((M)) and outcome ((Y)) are determined by the observed exposure ((A)).
1 |
|
Here is the entire data definitions table:
varnameformulavariancedistlink |—— |e0 |0 |1|normal |identity| |Y0M0|2 + M02 + e0 |0|nonrandom|identity| |Y0M1|2 + M12 + e0 |1|nonrandom|identity| |e1 |0 |1|normal |identity| |Y1M0|8 + M05 + e1 |0|nonrandom|identity| |Y1M1|8 + M15 + e1 |0|nonrandom|identity| |M |(A==0) * M0 + (A==1) * M1 |0|nonrandom|identity| |Y |(A==0) * Y0M0 + (A==1) * Y1M1|0|nonrandom|identity|
Based on the parameters used to generate the data, we can calculate the expected causal mediation effects:
[\begin{aligned}E[CME_0] &= E[2 + 2M_1 + e_0] – E[2+2M_0+e_0] \&= E[2(M_1 – M_0)] \&= 2(E[M_1] – E[M_0]) \&= 2(0.6 – 0.2) \&= 0.8\end{aligned}]
[\begin{aligned}E[CME_1] &= E[8 + 5M_1 + e_1] – E[8+5M_0+e_1] \&= E[5(M_1 – M_0)] \&= 5(E[M_1] – E[M_0]) \&= 5(0.6 – 0.2) \&= 2.0\end{aligned}]
Likewise, the expected values of the causal direct effects can be calculated:
[\begin{aligned}E[CDE_0] &= E[8 + 5M_0 + e_1] – E[2+2M_0+e_0] \&= E[6 + 5M_0 – 2M_0)] \&= 6 + 3M_0 \&= 6 + 3*0.2 \&= 6.6\end{aligned}]
[\begin{aligned}E[CDE_1] &= E[8 + 5M_1 + e_1] – E[2+2M_1+e_0] \&= E[6 + 5M_1 – 2M_1)] \&= 6 + 3M_1 \&= 6 + 3*0.6 \&= 7.8\end{aligned}]
Finally, the expected total causal effect is:
[\begin{aligned}ATCE &= E[CDE_0] + E[CME_1] = 6.6 + 2.0 \&= E[CDE_1] + E[CME_0] = 7.8 + 0.8 \&= 8.6\end{aligned}]And now, the complete data set can be generated.
1 |
|
1 |
|
Looking at the “observed” potential outcomes
The advantage of simulating data is that we can see what the average causal effects are based on the potential outcomes. Here are the average potential outcomes in the generated data set:
1 |
|
1 |
|
The four average causal effects based on the data are quite close to the expected values:
1 |
|
1 |
|
And the here is the average total causal effect from the data set:
1 |
|
1 |
|
All of these quantities can be visualized in this figure. The lengths of the solid vertical lines are the mediated effects. The lengths of the dotted vertical lines are the direct effects. And the sums of these vertical lines (by color) each represent the total effect:
Estimated causal mediation effect from observed data
Clearly, the real interest is in estimating the causal effects from data that we can actually observe. And that, of course, is where things start to get challenging. I will not go into the important details here (again, Imai et al provide these), but here are formulas that have been derived to estimate the effects (simplified since there are no confounders in this example) and the calculations using the observed data:
[\small\hat{CME_0} =\sum_{m\in0,1}E[Y | A=0, M=m][P(M=m | A=1)-P(M=m | A=0)]] |
1 |
|
1 |
|
[\small\hat{CME_1} =\sum_{m\in0,1}E[Y | A=1, M=m][P(M=m | A=1)-P(M=m | A=0)]] |
1 |
|
1 |
|
[\small\hat{CDE_0} =\sum_{m\in0,1}(E[Y | A=1, M=m] – E[Y | A=0, M=m])P(M=m | A=0)] |
1 |
|
1 |
|
[\small\hat{CDE_1} =\sum_{m\in0,1}(E[Y | A=1, M=m] – E[Y | A=0, M=m])P(M=m | A=1)] |
1 |
|
1 |
|
Estimation with mediation package
Fortunately, there is software available to provide these estimates (and more importantly measures of uncertainty). In R
, one such package is mediation
, which is available on CRAN. This package implements the formulas derived in the Imai et al paper.
Not surprisingly, the model estimates are in line with expected values, true underlying effects, and the previous estimates conducted by hand:
1 |
|
1 |
|
Related