18 Mediation
Mediation analysis is used to study the effect of an exposure variable (X) on the outcome (Y) through a third factor, known as mediator (M):
\[ X \rightarrow M \rightarrow Y \]
In practice, two regression models are fitted: one for the mediator (\(M \sim X\)) and one for the outcome (\(Y \sim X + M\)).
The contribution of a mediator is typically quantified in terms of Average Causal Mediated Effect (ACME) (Imai et al. 2010), that is, the portion of the association between X and Y that is explained by M. In practice, this corresponds to the difference between the Total Effect (TE) and the Average Direct Effect (ADE):
\[ ACME = TE - ADE \]
The microbiome can mediate the effects of multiple environmental stimuli on human health. However, the importance of its role as a mediator depends on the nature of the stimulus. For example, the effect of dietary fiber intake on host behavior is largely mediated by the gut microbiome (Logan and Jacka 2014). In contrast, the indirect impact of antibiotic use on mental health through an altered microbiome represents a more subtle process (Dinan and Dinan 2022).
In general, the wide range of mediation effects can be divided into two classes (Figure 18.1):
- partial mediation, where the mediator assists the exposure variable in conveying only part of the effect on the outcome
- complete mediation, where the mediator conveys the full effect of the exposure variable on the outcome
Mediation analysis is based on the assumption that the exposure variable, the mediator and the outcome follow one another in this temporal sequence. Therefore, investigating mediation is a suitable analytical choice in longitudinal studies, whereas it is discouraged in cross-sectional ones (Fairchild and McDaniel 2017).
We demonstrate a standard mediation analysis with the hitchip1006 dataset (Lahti et al. 2014) (Section D.6), which contains 1151 genus-level profiles from 1006 Western adults of 6 different nationalities. Some subjects were sampled more than once. As the models assume independent observations, we keep only the baseline sample (time == 0) of each subject. Note also that the exposure, mediator and outcome were measured at the same time point. This cross-sectional example cannot thus confirm the temporal order that mediation assumes, and it illustrates the method rather than a causal finding.
In our analyses, nationality and BMI group will represent the exposure (X) and outcome (Y) variables, respectively. We make the broad assumption that nationality reflects differences in the living environment between subjects.
# Convert BMI variable to numeric
tse$bmi_group <- as.numeric(tse$bmi_group)
# Agglomerate features by phylum
tse <- agglomerateByRank(tse, rank = "Phylum")
# Apply clr transformation to counts assay
tse <- transformAssay(
tse,
method = "clr",
pseudocount = 1
)In the following examples, the effect of living environment on BMI mediated by the microbiome is investigated in three different steps:
- global contribution by alpha diversity
- individual contributions by assay features
- joint contributions by reduced dimensions
18.1 Alpha diversity
First, we ask whether alpha diversity mediates the effect of living environment on BMI. Using the getMediation function, the variables X, Y and M are specified with the arguments treatment, outcome and mediator, respectively. We control for sex and age and limit comparisons to two nationality groups, Central Europeans (control) vs. Scandinavians (treatment).
# Analyze mediated effect of nationality on BMI via alpha diversity
# 100 bootstrap simulations to speed up execution, but ~1000 are recommended
set.seed(123)
med_df <- getMediation(
tse,
treatment = "nationality",
outcome = "bmi_group",
mediator = "diversity",
covariates = c("sex", "age"),
treat.value = "Scandinavia",
control.value = "CentralEurope",
boot = TRUE, sims = 100
)
# Plot results as a forest plot
plotMediation(med_df, layout = "forest")
The forest plot in Figure 18.2 shows significance for both ACME and ADE, which suggests that alpha diversity is a partial mediator of living environment on BMI. In contrast, if ACME but not ADE were significant, complete mediation would be inferred. The negative ACME means that the path through alpha diversity is associated with a lower BMI in the treatment group (Scandinavians) compared to the control group (Central Europeans). The sign of ACME combines the effects of X on M and of M on Y, so it does not alone tell whether the mediator is higher or lower in the treatment group. Here, alpha diversity is slightly higher in Scandinavians, and higher diversity is associated with a lower BMI.
18.2 Features
If we suspect that only certain features of the microbiome act as mediators, we can estimate their individual contributions by fitting one model for each feature in a selected assay. As multiple tests are performed, it is good practice to correct the significance of the findings with a method of choice to adjust p-values.
# Analyze mediated effect of nationality on BMI via clr-transformed features
# 100 bootstrap simulations to speed up execution, but ~1000 are recommended
set.seed(123)
tse <- addMediation(
tse,
name = "assay_mediation",
treatment = "nationality",
outcome = "bmi_group",
assay.type = "clr",
covariates = c("sex", "age"),
treat.value = "Scandinavia",
control.value = "CentralEurope",
boot = TRUE, sims = 100,
p.adj.method = "fdr"
)
# View results
kable(metadata(tse)$assay_mediation)| mediator | acme | acme_pval | acme_lower | acme_upper | ade | ade_pval | ade_lower | ade_upper | total | total_lower | total_upper | total_pval | acme_padj | ade_padj | total_padj | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 | Bacteroidetes | -0.0931 | 0.00 | -0.1454 | -0.0408 | -0.2031 | 0.00 | -0.3524 | -0.0276 | -0.2963 | -0.4712 | -0.1320 | 0.00 | 0.0000 | 0.0000 | 0.00 |
| 8 | Verrucomicrobia | -0.0462 | 0.00 | -0.0972 | -0.0175 | -0.2501 | 0.00 | -0.4441 | -0.0859 | -0.2963 | -0.5143 | -0.1199 | 0.00 | 0.0000 | 0.0000 | 0.00 |
| 1 | Actinobacteria | -0.0567 | 0.14 | -0.1184 | 0.0115 | -0.2396 | 0.04 | -0.4262 | -0.0183 | -0.2963 | -0.4396 | -0.0686 | 0.02 | 0.3733 | 0.0400 | 0.02 |
| 3 | Cyanobacteria | -0.0053 | 0.72 | -0.0311 | 0.0267 | -0.2910 | 0.00 | -0.4891 | -0.1276 | -0.2963 | -0.4778 | -0.1279 | 0.00 | 0.9400 | 0.0000 | 0.00 |
| 4 | Firmicutes | -0.0003 | 0.94 | -0.0208 | 0.0177 | -0.2960 | 0.00 | -0.4545 | -0.1473 | -0.2963 | -0.4515 | -0.1469 | 0.00 | 0.9400 | 0.0000 | 0.00 |
| 5 | Fusobacteria | 0.0003 | 0.92 | -0.0097 | 0.0093 | -0.2966 | 0.00 | -0.5043 | -0.1516 | -0.2963 | -0.5085 | -0.1460 | 0.00 | 0.9400 | 0.0000 | 0.00 |
| 6 | Proteobacteria | -0.0050 | 0.68 | -0.0467 | 0.0180 | -0.2913 | 0.02 | -0.4724 | -0.1235 | -0.2963 | -0.4811 | -0.1275 | 0.02 | 0.9400 | 0.0229 | 0.02 |
| 7 | Spirochaetes | 0.0015 | 0.76 | -0.0100 | 0.0144 | -0.2978 | 0.02 | -0.4639 | -0.1396 | -0.2963 | -0.4628 | -0.1368 | 0.02 | 0.9400 | 0.0229 | 0.02 |
For convenience, results can be visualized with a heatmap, where rows represent features and columns correspond to the coefficients for TE, ADE and ACME. Significant findings can be marked with p-values or stars (Figure 18.3).
# Plot results as a heatmap
plotMediation(
tse, "assay_mediation",
layout = "heatmap",
add.significance = "symbol"
)
Results suggest that only two out of eight features (Bacteroidetes and Verrucomicrobia) partially mediate the effect of living environment on BMI. The negative ACME means that the paths through these phyla are associated with a lower BMI in Scandinavians compared to Central Europeans. As explained above, the sign does not tell the direction of the abundance difference; for example, Bacteroidetes is less abundant and Verrucomicrobia more abundant in Scandinavians.
While analyses were conducted at the phylum level to simplify results, using original assays without agglomeration also represents a valid option. However, the increase in phylogenetic resolution also implies a higher probability of spurious findings, which in turn necessitates a stronger correction for multiple comparisons. A solution to this issue is proposed in the following section.
18.3 Ordination
Performing mediation analysis for each feature provides insight into individual contributions. However, this approach greatly increases the number of multiple tests to correct for and thus it reduces statistical power. To overcome this issue, it is possible to assess the joint contributions of groups of features by means of dimensionality reduction.
# Reduce dimensions with PCA
tse <- addPCA(
tse,
name = "PCA",
assay.type = "clr",
ncomponents = 3
)
# Analyze mediated effect of nationality on BMI via principal components
# 100 bootstrap simulations to speed up execution, but ~1000 are recommended
set.seed(123)
tse <- addMediation(
tse,
name = "reddim_mediation",
treatment = "nationality",
outcome = "bmi_group",
dimred = "PCA",
covariates = c("sex", "age"),
treat.value = "Scandinavia",
control.value = "CentralEurope",
boot = TRUE, sims = 100,
p.adj.method = "fdr"
)
# View results
kable(metadata(tse)$reddim_mediation)| mediator | acme | acme_pval | acme_lower | acme_upper | ade | ade_pval | ade_lower | ade_upper | total | total_lower | total_upper | total_pval | acme_padj | ade_padj | total_padj | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | PCA1 | -0.119 | 0.00 | -0.1968 | -0.0355 | -0.1773 | 0.08 | -0.3825 | 0.0502 | -0.2963 | -0.4396 | -0.0686 | 0.02 | 0.00 | 0.08 | 0.02 |
| 3 | PCA3 | -0.017 | 0.06 | -0.0411 | 0.0004 | -0.2793 | 0.00 | -0.4558 | -0.1225 | -0.2963 | -0.4778 | -0.1279 | 0.00 | 0.09 | 0.00 | 0.00 |
| 2 | PCA2 | 0.017 | 0.12 | -0.0011 | 0.0409 | -0.3133 | 0.00 | -0.4878 | -0.1487 | -0.2963 | -0.4712 | -0.1320 | 0.00 | 0.12 | 0.00 | 0.00 |
Results can be displayed as one forest plot for each reduced dimension. When combined with a heatmap of the feature loadings by dimension, it helps deduce whether certain groups of features act as mediators (Figure 18.4).
# Plot results as multiple forest plots
p1 <- plotMediation(
tse, "reddim_mediation",
layout = "forest"
)
# Plot loadings by principal component
p2 <- plotLoadings(
tse, "PCA",
ncomponents = 3, n = 8,
layout = "heatmap"
)
# Combine plots
p1 / p2
The plot above suggests that only PC1 mediates the effect of living environment on BMI. Within this dimension, Bacteroidetes and Actinobacteria are the largest contributors in opposite directions. Interestingly, in the previous section the former but not the latter appeared significant individually, which implies that mediation might emerge from their joint contribution.
18.4 Final remarks
This chapter introduced the concept of mediation and demonstrated a standard analysis of the microbiome as mediator at three different levels (global, individual and joint contributions). Importantly, the provided method is based on the mediation package and is limited to univariate comparisons and binary conditions for the exposure variable (Tingley et al. 2014). Therefore, it is recommended to reduce the number of mediators under study by means of a knowledge-based strategy to preserve statistical power.
A few methods for multivariate mediation analysis of high-dimensional omic data also exist (Xia 2021; Jiang et al. 2025). However, no one solution has emerged yet to become the gold standard in microbiome data analysis, mainly because the available approaches can only partially accommodate the specific properties of microbiome data, such as compositionality, sparsity and its hierarchical structure. While this chapter proposed a standard approach to mediation analysis, in the future fine-tuned solutions for the microbiome may also become common.
18.5 Exercises
We use the hitchip1006 dataset, as in this chapter. Unlike in this chapter, we compare Southern Europeans (SouthEurope, treatment) to Central Europeans (CentralEurope, control).
Solve the exercises in order, as each exercise continues from the previous one. Try to solve each exercise yourself before opening the solution.



