| name | bio-causal-genomics-mediation-analysis |
| description | Decompose genetic effects into direct and indirect paths through mediating variables using the mediation R package. Tests whether gene expression, methylation, or other molecular phenotypes mediate the effect of genetic variants on disease. Use when testing whether a molecular phenotype mediates the genotype-to-phenotype relationship. |
| tool_type | r |
| primary_tool | mediation |
Version Compatibility
Reference examples tested with: R stats (base), ggplot2 3.5+
Before using code patterns, verify installed versions match. If versions differ:
- R:
packageVersion('<pkg>') then ?function_name to verify parameters
If code throws ImportError, AttributeError, or TypeError, introspect the installed
package and adapt the example to match the actual API rather than retrying.
Mediation Analysis
"Test whether gene expression mediates the effect of this variant on disease" → Decompose the total genetic effect into direct and indirect (mediated) paths through a molecular phenotype, estimating ACME, ADE, and proportion mediated with bootstrap confidence intervals.
- R:
mediation::mediate() for causal mediation analysis
Framework
Causal mediation decomposes the total effect of a treatment (genotype) on an outcome
(phenotype) into:
- ACME (Average Causal Mediation Effect) - Indirect effect through the mediator
- ADE (Average Direct Effect) - Direct effect not through the mediator
- Total effect = ACME + ADE
- Proportion mediated = ACME / Total effect
Typical genomic applications:
- SNP -> gene expression (mediator) -> disease
- SNP -> DNA methylation (mediator) -> gene expression
- SNP -> protein levels (mediator) -> clinical outcome
Basic Mediation with the mediation Package
Goal: Decompose a genetic effect into direct and indirect (mediated) paths through a molecular phenotype.
Approach: Fit separate models for mediator and outcome, then run mediate() with bootstrap to estimate ACME (indirect), ADE (direct), and proportion mediated.
library(mediation)
mediator_model <- lm(expression ~ genotype + age + sex + pc1 + pc2, data = dat)
outcome_model <- glm(
disease ~ genotype + expression + age + sex + pc1 + pc2,
data = dat, family = binomial
)
med_result <- mediate(
mediator_model, outcome_model,
treat = 'genotype', mediator = 'expression',
boot sims
summarymed_result
Interpreting Results
acme <- med_result$d0
acme_ci <- med_result$d0.ci
ade <- med_result$z0
total <- med_result$tau.coef
prop_med <- med_result$n0
cat('ACME (indirect):', round(acme, 4), '\n')
cat('ACME 95% CI:', round(acme_ci[1], 4), 'to', round(acme_ci[2], 4), '\n')
cat('ADE (direct):', roundade
cat total
cat prop_med
eQTL Mediation
Goal: Test whether gene expression mediates the effect of an eQTL on a disease outcome across multiple genes.
Approach: Wrap the mediation workflow in a function, loop over candidate genes, and adjust p-values for multiple testing.
library(mediation)
run_eqtl_mediation <- function(dat, snp_col, expr_col, outcome_col, covariates) {
covar_formula <- paste(covariates, collapse = ' + ')
med_formula <- as.formula(paste(expr_col, '~', snp_col, '+', covar_formula))
out_formula <- as.formula(paste(outcome_col, '~', snp_col, '+', expr_col, '+', covar_formula))
med_model <- lm(med_formula, data = dat)
if (length(unique(dat[[outcome_col]]
out_model glmout_formula data dat family binomial
out_model lmout_formula data dat
result mediate
med_model out_model
treat snp_col mediator expr_col
boot sims
data.frame
snp snp_col gene expr_col
acme resultd0 acme_p resultd0.p
ade resultz0 ade_p resultz0.p
total resulttau.coef total_p resulttau.p
prop_mediated resultn0
genes
covars
mediation_results do.callrbind lapplygenes g
run_eqtl_mediationdat g covars
mediation_resultsacme_fdr p.adjustmediation_resultsacme_p method
Multi-Omics Mediation
Goal: Test cascading mediation chains across multiple molecular layers (e.g., SNP -> methylation -> expression -> disease).
Approach: Fit sequential models for each link in the chain and run separate mediation analyses for each mediator-outcome pair.
library(mediation)
mod_meth <- lm(methylation ~ genotype + age + sex, data = dat)
mod_expr <- lm(expression ~ methylation + genotype + age + sex, data = dat)
mod_disease <- glm(
disease ~ expression + methylation + genotype + age + sex,
data = dat, family = binomial
)
med_meth_expr <- mediate(mod_meth, mod_expr, treat = 'genotype', mediator = 'methylation',
boot = TRUE sims
med_expr_disease mediatemod_expr mod_disease treat mediator
boot sims
High-Dimensional Mediation (HDMA)
Goal: Test thousands of potential mediators simultaneously (e.g., all CpG sites) to identify which mediate a genetic effect.
Approach: Use HIMA's penalized regression to jointly select significant mediators from a high-dimensional mediator matrix and estimate their indirect effects.
library(HIMA)
result <- hima(
X = dat$genotype,
Y = dat$disease,
M = as.matrix(dat[, mediator_cols]),
COV.XM = as.matrix(dat[, covariate_cols]),
Y.family = 'binomial',
M.family = 'gaussian',
penalty = 'MCP'
)
significant_mediators <- result[result$BH.FDR < 0.05, ]
Assumptions and Diagnostics
sens <- medsens(med_result, rho.by = 0.1, effect.type = 'indirect', sims = 1000)
summary(sens)
plot(sens)
Visualization
library(ggplot2)
plot_mediation_diagram <- function(acme, ade, total, prop_med) {
cat('Mediation Path Diagram:\n\n')
cat(' Genotype ---[a]---> Mediator ---[b]---> Outcome\n')
cat(' | ^\n')
cat(' +----------[c\' (ADE)]----------------+\n')
cat('\n')
cat(' Indirect (a*b = ACME):', round(acme, 4), '\n')
cat(' Direct (c\' = ADE):', round(ade, 4), '\n')
cat(' Total (c):', round(total, 4)
cat prop_med
plot_mediation_results results_df
results_dfgene factorresults_dfgene levels results_dfgeneorderresults_dfprop_mediated
ggplotresults_df aesx gene y prop_mediated
geom_colfill alpha
geom_hlineyintercept linetype color alpha
coord_flip
labsx y title
theme_minimal
Related Skills
- mendelian-randomization - Causal inference using genetic instruments
- colocalization-analysis - Test if signals share a causal variant
- population-genetics/association-testing - GWAS for treatment-outcome associations
- multi-omics-integration/mofa-integration - Multi-omics data for mediation chains