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: a preset (
"minimal","clinical","striped","shaded","jama") or theme primitives.
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. With a logit outcome the effects are evaluated
with the mediator set to its predicted mean under each exposure value (the
mean is plugged into the outcome model). Because the logistic function is
non-linear this is an approximation to the effects obtained by averaging
over the mediator distribution; the table footnote states this.
Bootstrap replicates in which a model cannot be fitted are excluded from
the percentile intervals and p-values; a warning and the table footnote
report how many failed (also stored as boot_failed). The interval
column is labelled with conf_level.
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.226
0.145 to 0.289
<0.001
Overall exposure-outcome association
Direct effect
0.176
0.115 to 0.230
<0.001
Association not through the mediator
Indirect effect
0.051
0.009 to 0.087
<0.001
Association through the mediator
Proportion mediated
0.224
0.045 to 0.361
<0.001
Share of total effect through the mediator
Effects are predicted probability differences from logistic outcome models, evaluated with the mediator set to its predicted mean under each exposure value (an approximation: the outcome model is not averaged over the mediator distribution).
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.22624186 0.145471598 0.28880141
#> direct direct Direct effect 0.17561583 0.115092219 0.23025888
#> indirect indirect Indirect effect 0.05062603 0.009170749 0.08703669
#> proportion proportion Proportion mediated 0.22376950 0.044876738 0.36111969
#> 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, evaluated with the mediator set to its predicted mean under each exposure value (an approximation: the outcome model is not averaged over the mediator distribution).
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.