Testing mediation and moderation; can one variable function as both mediator and moderator? I am trying to understand the relationship between three variables, namely dependent, independent and a third variable I have some theoretical evidence that the third variable has mediation or moderation effect on the dependent variable. How can I test this relation? Also, is it possible that a variable functions as both mediator and moderator variable at the same time?
 A: From definitions, I feel that a variable can not simultaneously function as mediator and moderator. Let's try to investigate both effects:
Mediaiton
Mediation is a hypothesized causal chain in which one variable affects a second variable that, in turn, affects a third variable. The intervening variable, $M$, is the mediator. It mediates the relationship between a predictor, $X$, and an outcome, $Y$. Graphically, mediation can be depicted in the following way: $$X \longrightarrow M \longrightarrow Y$$
Testing mediation


*

*Inspect if $Y$ is influenced by $X$ with $\hat y = \beta_0 + \beta_1x$

*See if $M$ is influenced by $X$ with $\hat m = \beta_0 + \beta_1x$

*See if $Y$ is influenced by $M$ with $\hat y = \beta_0 + \beta_1m$ 
If one or more of these relationships are nonsignificant, researchers usually conclude that mediation is not possible or likely. Assuming the above steps yield significant results,


*Conduct a multiple regression to see the influence of $X$ and $M$ on $Y$ with $\hat y = \beta_0 + \beta_1x + \beta_1m$ 
If $X$ is no longer significant when $M$ is controlled, the finding supports full mediation. If X is significant, i.e., both $X$ and $M$ both significantly predict $Y$, the finding indicates partial mediation.
Testing moderation
Let's assume a student's GPA (outcome variable) is affected not only by study-time (independent variable), but also by gender (moderating variable). In order to test moderation effect of gender, add to regression equation the interaction term between study-time and gender. $$GPA = \beta_0 + \beta_1x_{studytime} + \beta_2x_{gender} + \beta_3x_{studytime}x_{gender}$$
If $\beta_3$ is significant, there exists moderation.
A: Here is an article giving an example of a moderating mediator.
https://www.sciencedirect.com/science/article/abs/pii/S0005789417301144
This explains how a mediator may later become a moderator, however I would speculate that under most circumstances (particularly in biopsychology) mediation tests are detecting only statistical mediation, and in such case “full mediation” can not be truly observed unless all other potential mediators are controlled for in analysis (as well as other necessary conditions, such as random assignment to experimental condition and a longitudinal design); that being said a mechanism that partially explains an association between the independent and dependent  variables may also moderate some path (a2 or b2) of a second mediator.
For example the relation between poverty and poor health may be mediated (partially) by substance use, and partially by social support network size; however it’s also possible that the effect of poverty on substance abuse (a1 path) may be moderated by social support (i.e., people in poverty may only be more likely to engage in substance abuse if they don’t have a quality social network).
For more current methodology for mediation analysis please see Andrew Hayes.
A: TLDR: Moderation and mediation are two different things, but nothing prevents to have them both simultaneously. This is because a mediator may interact with treatment, and interaction, if one variable is considered as the treatment and the other as an effect-modifier, means moderation. It seems to me, however, that, in a mediation model, you're interested in the actual portion of the total effect explained by the indirect path while, in an moderation model, the focus is on the interaction effect (regardless of how much this affects results in practice).
Long reply: In the model by Erikson et al., 2005 (pdf, more deeply explored (and extended) by Buis, 2010): https://www.stata-journal.com/article.html?article=st0182 , the total effect is the sum of direct and indirect effect (or, if we can consider odds ratios, their product). Let’s call

*

*our treatment (or, in general, the variable whose we want to explore the effects), $X$, and let’s assume it’s binary: $X=0$ for untreated/controls, and $X=1$ for treated;

*our mediator $Z$, and $Z(0)$ its potential value under $X=0$, and $Z(1)$ its potential value under $X=1$;

*our outcome $Y$, and $Y(0,0)$ its potential value under $X=0$, $Y(1,1)$ its potential value under $X=1$, $Y(1,0)$ its counterfactual value when $X=1$, but with $Z(0)$ and $Y(0,1)$ its counterfactual value when $X=0$, but with $Z(1)$. In practice, counterfactual values are those we can’t typically observe, because we have one treatment status and the mediator’s value in the case of the other treatment status.

The total effect is given by $Y(1,1)-Y(0,0)$ and can be calculated as the sum of one direct and one indirect effect in any case. This seems to rule out the existence of an interaction, but it’s not the case.
The problem is that there are two ways how we can make this calculation, depending on which counterfactual we use.
A)  If we use $Y(1,0)$, we have, as direct effect:  $Y(1,0)-Y(0,0)$, i.e., starting from a situation with no treatment, we see what would change if we gave the treatment, but without changing the mediator variable. As indirect effect we would have:  $Y(1,1)-Y(1,0)$, i.e.: starting from the situation where the person is treated, but with the value of the mediating variable as if they were untreated, we see what would change if we moved the mediating variable to the one in case of treatment (so, the potential value in case of treatment).
B)  If we use $Y(0,1)$, we have, as direct effect:  $Y(1,1)-Y(0,1)$, i.e., starting from a counterfactual situation with no treatment, but the mediating variable at its value in case of treatment, we see what would change if we gave the treatment (so, the potential value in case of treatment).
As indirect effect we would have:  $Y(0,1)-Y(0,0)$, i.e.: starting from the situation where the person is untreated, we see what would change if we moved the mediating variable to the one in case of treatment.
I see it as a matter of whether the direct or indirect effect moves first, in the path from no-treatment to treatment. As noticed by Buis (2010): “The logic behind these two methods is exactly the same, but they do not have to result in exactly the same estimates for the direct and indirect effects”. In my undersatanding, however, in case of no interaction effects, this is just an estimation issue, because his model is based on estimating counterfactual probabilities through simulations, then deriving log-odds ratios, that may be slightly different depending on the path.
However, by introducing interaction effects, thus expressing the model like that: $Y=\alpha+\beta_1*X+\beta_2*Z+\beta_3*XZ$, it wouldn't be just an issue of estimation methods, but of identification. In fact, we would have:

*

*$Y(1,1)= \alpha+\beta_1+\beta_2*Z(1)+\beta_3*Z(1)$;

*$Y(1,0)= \alpha+\beta_1+\beta_2*Z(0)+\beta_3*Z(0)$;

*$Y(0,1)= \alpha+\beta_2*Z(1)$;

*$Y(0,0)= \alpha+\beta_2*Z(0)$.

From here, we have:
A)  First method: the direct effect is equal to: $Y(1,0)-Y(0,0)=\beta_1+\beta_3*Z(0)$; the indirect effect to: $Y(1,1)-Y(1,0)= \beta_2*Z(1)+\beta_3*Z(1)- \beta_2*Z(0)-\beta_3*Z(0)=(\beta_2+\beta_3)*(Z(1)-Z(0))$;
B)  Second method: the direct effect is equal to: $Y(1,1)-Y(0,1)=\beta_1+\beta_3*Z(1)$; the indirect effect to: $Y(0,1)-Y(0,0)= \beta_2*Z(1)-\beta_2*Z(0) = \beta_2*(Z(1)-Z(0))$.
You can notice that the difference between the direct (and indirect) effects in the two methods depends on the interaction parameter $\beta_3$ (it is: $\beta_3*(Z(1)-Z(0)$), also corresponding to the difference between the total effect and the sum of the direct effect by keeping the mediator to the one in case of no-treatment ($Y(1,0)-Y(0,0)$) and the indirect effect for the case of no-treatment ($Y(0,1)-Y(0,0)$).
VanderWheele (2013): https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3563853/
actually offers a three-way decomposition that uses such direct effect, such indirect effect, and the interaction term (i.e. the difference between the total effect and the sum of direct and indirect effect). Such interaction term shows that the mediator can also act as an effect-modifier, thus can also be a moderator.
Finally, VanderWheele(2014): https://journals.lww.com/epidem/Fulltext/2014/09000/A_Unification_of_Mediation_and_Interaction__A.19.aspx#errata shows that, in presence of a mediator, interactions can be accounted for by separating among 4 effects. I think that paper highlights that also interactions, in mediation models, are considered in terms of proportion of the effect explained. The main reason to do so is expressed by the author by saying "the 4-way decomposition provides 4 components capturing all the subtleties: the portion of the total effect that is
attributable just to mediation, just to interaction, to both mediation
and interaction, or to neither mediation nor interaction" in “Relation to Mediation Decompositions”, where he also describes at least three other reasons to propose his approach. The first one is to discuss not in terms of counterfactual outcomes (like $Y(1,0)$), that he defines as "difficult to interpret", but of possible values of the mediator (like $X=1, Z=0$). The second one is to separate the "pure direct effect" (often called "natural direct effect" in the literature) between a "controlled direct effect" (in the case of null mediator) and a "reference interaction effect" (expressing the change in the pure direct effect due to the presence of the mediator). This is particularly relevant in the case of a binary mediator where, even if the mediator is present even with no treatment, its effect on treatment effect may be worth investigating. The author explains the concept of "portion eliminated" to clarify this point. The third one is that he shows that the third component (the "mediated interaction effect") "is sometimes combined with the pure indirect effect to obtain the total indirect effect and sometimes combined with the pure
direct effect to obtain the total direct effect".
He starts by the case where the mediator is binary, by defining the “controlled direct effect” as the one in the case the mediator is 0. In his words: "The intuition behind this decomposition is that if the exposure affects the outcome for a particular individual, then at least 1 of 4 things must be the case. One possibility is that the exposure might affect the outcome through pathways that do not require the mediator (ie, the exposure affects the outcome even when the mediator is absent); in other words, the first component is non-zero. A second possibility is that the exposure effect might operate only in the presence of the mediator (ie, there is an interaction), with the exposure itself not necessary for the mediator to be present (ie, the mediator itself would be present in the absence of the exposure, although the mediator is itself necessary for the exposure to have an effect on the outcome); in other words, the second component is non-zero. A third possibility is that the exposure effect might operate only in the presence of the mediator (ie, there is an interaction), with the exposure itself needed for the mediator to be present (ie, the exposure causes the mediator, and the presence of the mediator is itself necessary for the exposure to have an effect on the outcome); in other words, the third component in non-zero. The fourth possibility is that the mediator can cause the outcome in the absence of the exposure, but the exposure is necessary for the mediator itself to be present; in other words, the fourth component is non-zero".
In his notation, the mediator is called $M$, and the second subscript for $Y$ does not stand for potential values of the mediator under the given value of $X$, but directly for mediators value; so, for example, $Y_{1,0}$ means: “$Y$ in case the person is treated and the mediator is absent”.
He calls the term : $Y_{11} – Y_{10} – Y_{01} + Y_{00}$ as “additive interaction”, that can be seen as the difference between the global effect of moving both the treatment and the mediator from 0 to 1, and the sum of the two separate effects of moving the treatment to 1 while leaving the mediator to 0 and viceversa. Given, as said above, he uses $M=0$ as reference case, the interaction plays a role only in case $M(1)=1$ (otherwise, either both interaction effects are null, or they cancel out each other, a case that however the author doesn’t seem to explore, also because, when talking about the proportion of the total effect due to each component, he says: “reporting such proportion measures, however, generally makes sense only if all the components
are in the same direction (eg, all positive or all negative)"). In that case, either also $M(0)=1$ (in such situation, the interaction is ascribed to “reference interaction”, because it takes place without needing the intervention), or $M(0)=0$ (in that case, the interaction is ascribed to “mediated interaction”, because it takes place only thank to the intervention).
At the beginning of the Appendix, the general case (i.e.: not restricted to binary exposure and mediator) is presented. The point is that, in that case, the decomposition between the 4 effects is conditional on the mediator’s value (i.e., it depends on it).
