Estimate direct, indirect, total, and proportion mediated effects from regression models.
Arguments
- data
A data frame.
- exposure
Exposure/treatment variable. Quoted and bare names are accepted.
- mediator
Mediator variable. Quoted and bare names are accepted.
- outcome
Outcome variable. Quoted and bare names are accepted.
- covariates
Optional character vector of covariate names. Quoted names are recommended in scripts, and bare names are also accepted.
- mediator_approach
Mediator model. Currently
"linear".- outcome_approach
Outcome model. One of
"linear"or"logit". Logistic mediation effects are reported on the predicted probability-difference scale.- exposure_value
Exposure value used as the treatment level. If
NULL, the second factor level, value 1 for 0/1 variables, or the 75th percentile for continuous exposures is used.- reference_value
Exposure value used as the reference level. If
NULL, the first factor level, value 0 for 0/1 variables, or the 25th percentile for continuous exposures is used.- sims
Number of non-parametric bootstrap replicates used for confidence intervals.
- conf_level
Confidence level for intervals.
- seed
Optional random seed for reproducible bootstrap intervals.
- format
One of
"flextable"(default) or"gt".- theme
Table theme.
Value
A list of class c("gtregression", "mediation_analysis", ...)
with elements:
- table
A formatted
flextableorgt_tbl.- table_body
Data frame of mediation effect estimates.
- table_display
Formatted data frame used to build the table.
- models
Fitted mediator and outcome models.
- boot
Bootstrap replicate estimates.
- values
Reference and exposure values used.
- variable_labels
Named character vector of display labels.
- call
Matched function call.
Details
This function is for planned mediation questions, not automatic causal discovery. Causal interpretation requires the usual mediation assumptions, including no unmeasured exposure-outcome, exposure-mediator, or mediator-outcome confounding, correct temporal order, and suitable model specification. Use a directed acyclic graph (DAG) and subject-matter knowledge before interpreting the estimates causally.
For outcome_approach = "linear", effects are mean differences. For
outcome_approach = "logit", effects are predicted probability
differences, not odds ratios.
Examples
med <- mediation_analysis(
data = data_diabetes_mediation,
exposure = obesity,
mediator = glucose,
outcome = diabetes,
covariates = c(age, blood_pressure, pregnancies, diabetes_pedigree),
outcome_approach = logit,
sims = 50,
seed = 123
)
med$table
Effect
Estimate
95% CI
p-value
Interpretation
Total effect
0.268
0.206 to 0.347
<0.001
Overall exposure-outcome association
Direct effect
0.200
0.136 to 0.271
<0.001
Association not through the mediator
Indirect effect
0.068
0.033 to 0.110
<0.001
Association through the mediator
Proportion mediated
0.255
0.126 to 0.395
<0.001
Share of total effect through the mediator
Effects are predicted probability differences from logistic outcome models.
Comparison: Obesity = Yes vs No; mediator = Plasma glucose; outcome = Diabetes. Bootstrap replicates = 50.
Adjusted for Age, Diastolic blood pressure, Number of pregnancies, Diabetes pedigree function.
Causal interpretation requires DAG-supported no-unmeasured-confounding and correct temporal-order assumptions.
med$table_body
#> effect Effect estimate conf.low conf.high
#> total total Total effect 0.26811037 0.20563604 0.3470686
#> direct direct Direct effect 0.19978127 0.13629337 0.2710736
#> indirect indirect Indirect effect 0.06832911 0.03280945 0.1100138
#> proportion proportion Proportion mediated 0.25485440 0.12640396 0.3954078
#> p.value Interpretation
#> total 0 Overall exposure-outcome association
#> direct 0 Association not through the mediator
#> indirect 0 Association through the mediator
#> proportion 0 Share of total effect through the mediator
# HTML-first output
mediation_analysis(
data = data_diabetes_mediation,
exposure = obesity,
mediator = glucose,
outcome = diabetes,
covariates = c(age, blood_pressure, pregnancies, diabetes_pedigree),
outcome_approach = logit,
format = gt,
sims = 50,
seed = 123
)$table
Effects are predicted probability differences from logistic outcome models.
Comparison: Obesity = Yes vs No; mediator = Plasma glucose; outcome = Diabetes. Bootstrap replicates = 50.
Adjusted for Age, Diastolic blood pressure, Number of pregnancies, Diabetes pedigree function.
Causal interpretation requires DAG-supported no-unmeasured-confounding and correct temporal-order assumptions.