| Title: | Infrastructure for Mediation Model Fitting and Extraction |
|---|---|
| Description: | Provides S7-based infrastructure for fitting mediation models, extracting path coefficients, and performing bootstrap inference. Designed as a foundation package for the mediation analysis ecosystem, supporting 'probmed', 'RMediation', and 'medrobust' packages. Implements unified interfaces for model fitting across different engines (generalized linear models, linear mixed models for cluster-randomized designs, and a maximum-likelihood structural equation engine; Bayesian methods are planned), standardized extraction of mediation paths from various model types, and robust bootstrap inference methods. Mediation methods are described in MacKinnon, Lockwood and Williams (2004) <doi:10.1207/s15327906mbr3901_4>, Preacher and Hayes (2008) <doi:10.3758/brm.40.3.879>, Tofighi and MacKinnon (2011) <doi:10.3758/s13428-011-0076-x>, and VanderWeele (2014) <doi:10.1097/EDE.0000000000000121>. |
| Authors: | Davood Tofighi [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-8523-7776>) |
| Maintainer: | Davood Tofighi <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.22.2 |
| Built: | 2026-12-01 18:02:30 UTC |
| Source: | https://github.com/Data-Wise/medfit |
Conduct bootstrap inference to compute confidence intervals for mediation statistics. Supports parametric, nonparametric, and plugin methods.
bootstrap_mediation( statistic_fn, method = c("parametric", "nonparametric", "plugin"), mediation_data = NULL, data = NULL, n_boot = 1000L, ci_level = 0.95, parallel = FALSE, ncores = NULL, seed = NULL, cluster = NULL, ..., source_fit = NULL, effect = NULL )bootstrap_mediation( statistic_fn, method = c("parametric", "nonparametric", "plugin"), mediation_data = NULL, data = NULL, n_boot = 1000L, ci_level = 0.95, parallel = FALSE, ncores = NULL, seed = NULL, cluster = NULL, ..., source_fit = NULL, effect = NULL )
statistic_fn |
Function that computes the statistic of interest.
|
method |
Character string: bootstrap method. Options:
|
mediation_data |
A mediation data object (required for parametric/plugin):
MediationData, SerialMediationData, ParallelMediationData,
InteractionMediationData, JointMediationData, TwoLevelMediationData
or TwoLevelParallelMediationData (see |
data |
Data frame (required for nonparametric bootstrap) |
n_boot |
Integer: number of bootstrap samples (default: 1000) |
ci_level |
Numeric: confidence level between 0 and 1 (default: 0.95) |
parallel |
Logical: use parallel processing? (default: FALSE) |
ncores |
Integer: number of cores for parallel processing.
If NULL, uses |
seed |
Integer: random seed for reproducibility (optional but recommended) |
cluster |
Character string: name of a cluster variable in |
... |
Additional arguments (reserved for future use); must be empty with |
source_fit |
Optional accepted bounded FIML SEMFit, supplied by name.
Its original incomplete retained records, model and settings are authoritative.
Omit |
effect |
Scalar unstandardized effect, exactly |
Unavailable. While statistical admission is undecided, every non-NULL
source_fit is refused before any other check, random draw, refit or worker,
with a classed error (medfit_error_fiml_inference_unavailable). No option or
environment variable enables it. The text below records the implemented design
and is not a statement that the intervals are supported.
Supply source_fit and effect by name and choose method explicitly. Only
accepted three-variable Gaussian random-X simple mediation fits from fit_sem()
are supported, with the direct path free or fixed exactly at zero. This route
does not support callbacks, plugin estimates, clusters or additional arguments.
Fits with one or two covariates (including two parallel mediators with
covariates) are refused with medfit_error_fiml_covariate_source_fit, and fits
with two parallel mediators and no covariates with medfit_error_fiml_parallel_source_fit (both checks follow the
withheld-inference guard above).
Bounded FIML objects supplied as mediation_data or data remain refused.
Object-aware guards run before package-controlled RNG, workers or refits; opaque
user callbacks and external-first dispatch are outside that guarantee.
method = "nonparametric" gives a retained-row FIML percentile interval:
resample entire nonempty records at fixed retained N, preserving missingness and
duplicate rows, and refit the identical model with the accepted source protocol.
method = "parametric" gives a Gaussian parameter-draw percentile interval,
not a synthetic-data bootstrap: jointly draw the canonical free parameters using
the full observed/model covariance in original coordinates, without refitting.
The symmetric correlation-root factor permits only roundoff repairs; materially
asymmetric or indefinite covariance is refused. Fixed zeros stay exact. Negative
nuisance-variance draws are not filtered or truncated before evaluating the effect.
Both routes retain the original fitted effect as the point estimate and use
type-7 empirical quantiles. Every requested replicate is recorded in @inference
(schema fiml-bootstrap-1), including indices or draw IDs, reasons, source/RNG
provenance and retained/collected counts. Any failed, invalid or nonfinite required
replicate withholds the entire interval. Interruption returns an incomplete
length-n_boot result with interrupted/unattempted IDs and NA unresolved values.
Print, summary, tidy and changed-level confint() cannot report a survivor interval;
tidy distribution SD is also unavailable for withheld results. An all-success SD
is descriptive, not a robust standard error.
The target is conditional on retained nonempty records. Assumptions include IID jointly Gaussian data, ignorable MAR with distinct missingness/model parameters, positivity and identified information. Retention normalization must be independent of model parameters; independently empty MCAR records suffice. Arbitrary value-dependent empty-row deletion and MNAR are not covered. Numerical verification does not establish interval coverage; statistical admission remains deferred. See the Methods and Formulas article, Bounded FIML, for the mathematical definition. These source intervals are development code added after 0.16.0 and are withheld.
Parametric Bootstrap (method = "parametric"):
Samples parameter vectors from
Fast and efficient
Assumes asymptotic normality of parameters
Recommended for most applications with n > 50
Requires mediation_data argument
Nonparametric Bootstrap (method = "nonparametric"):
Resamples observations with replacement
Refits models for each bootstrap sample
More robust, no normality assumption
Computationally intensive
Use when normality is questionable or n is small
Requires data argument
Plugin Estimator (method = "plugin"):
Computes point estimate only
No confidence interval
Fastest method
Use for quick checks or when CI not needed
Requires mediation_data argument
The statistic_fn should be a function that:
For parametric/plugin: Takes a named numeric vector of parameters
For nonparametric: Takes a data frame
Returns a single numeric value
Common statistic functions for indirect effect:
# Using parameter names from MediationData
indirect_fn <- function(theta) {
theta["m_X"] * theta["y_M"]
}
Set parallel = TRUE to use multiple cores:
Uses parallel::mclapply() on Unix systems
Falls back to sequential on Windows
Automatically detects available cores
Always set a seed for reproducible results:
bootstrap_mediation(..., seed = 12345)
A fixed seed gives the same resamples for any ncores, and for sequential runs, because every
resample is drawn before the work is split across cores. This holds for the nonparametric and
cluster methods (earlier versions drew inside each worker). The statistic is still computed in the
workers, so a statistic_fn that uses random numbers itself is not covered (in a sequential run its
draws now follow all the resampling draws instead of interleaving with them, so its stream differs
from earlier versions), and floating-point results can differ in the last digits when BLAS threads
differ. The resamples are held in memory before the refits start: n_boot integer vectors of length
n (about 4 * n * n_boot bytes; 800 MB for n = 100000 and n_boot = 2000).
A BootstrapResult object containing:
Point estimate
Percentile confidence interval bounds (the and
quantiles of the bootstrap distribution)
Bootstrap distribution (for parametric and nonparametric)
Method used
BootstrapResult, MediationData, fit_mediation()
# Fit mediation model med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) # Define indirect effect function indirect_fn <- function(theta) theta["m_treatment"] * theta["y_mediator1"] # Plugin estimator (point estimate only, fastest) result_plugin <- bootstrap_mediation( statistic_fn = indirect_fn, method = "plugin", mediation_data = med_data ) print(result_plugin) # Parametric bootstrap (recommended for most applications) result <- bootstrap_mediation( statistic_fn = indirect_fn, method = "parametric", mediation_data = med_data, n_boot = 1000, ci_level = 0.95, seed = 12345 ) print(result) # Nonparametric bootstrap (slower but more robust) refit_fn <- function(boot_data) { fit_m <- lm(mediator1 ~ treatment + covariate1 + covariate2, data = boot_data) fit_y <- lm(outcome ~ treatment + mediator1 + covariate1 + covariate2, data = boot_data) unname(coef(fit_m)["treatment"] * coef(fit_y)["mediator1"]) } result_np <- bootstrap_mediation( statistic_fn = refit_fn, method = "nonparametric", data = mediation_demo, n_boot = 500, seed = 12345 ) print(result_np) if (requireNamespace("lme4", quietly = TRUE)) { # Treatment assigned to whole clusters (needs lme4); formulas hold fixed # effects only, the engine adds the random cluster intercept set.seed(1) J <- 30 id <- rep(seq_len(J), each = 6) cdat <- data.frame(school = factor(id), X = sample(rep(0:1, J / 2))[id]) cdat$M <- 0.5 * cdat$X + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) cdat$Y <- 0.2 * cdat$X + 0.4 * cdat$M + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) # Resample whole clusters. A refit that is singular or does not converge is # dropped, and the warning counts how many were nie_of <- function(d) { unname(nie(fit_mediation(Y ~ X + M, M ~ X, data = d, treatment = "X", mediator = "M", engine = "lmer", cluster = "school"))) } result_cluster <- bootstrap_mediation( nie_of, method = "nonparametric", data = cdat, cluster = "school", n_boot = 20, seed = 1 ) print(result_cluster) }# Fit mediation model med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) # Define indirect effect function indirect_fn <- function(theta) theta["m_treatment"] * theta["y_mediator1"] # Plugin estimator (point estimate only, fastest) result_plugin <- bootstrap_mediation( statistic_fn = indirect_fn, method = "plugin", mediation_data = med_data ) print(result_plugin) # Parametric bootstrap (recommended for most applications) result <- bootstrap_mediation( statistic_fn = indirect_fn, method = "parametric", mediation_data = med_data, n_boot = 1000, ci_level = 0.95, seed = 12345 ) print(result) # Nonparametric bootstrap (slower but more robust) refit_fn <- function(boot_data) { fit_m <- lm(mediator1 ~ treatment + covariate1 + covariate2, data = boot_data) fit_y <- lm(outcome ~ treatment + mediator1 + covariate1 + covariate2, data = boot_data) unname(coef(fit_m)["treatment"] * coef(fit_y)["mediator1"]) } result_np <- bootstrap_mediation( statistic_fn = refit_fn, method = "nonparametric", data = mediation_demo, n_boot = 500, seed = 12345 ) print(result_np) if (requireNamespace("lme4", quietly = TRUE)) { # Treatment assigned to whole clusters (needs lme4); formulas hold fixed # effects only, the engine adds the random cluster intercept set.seed(1) J <- 30 id <- rep(seq_len(J), each = 6) cdat <- data.frame(school = factor(id), X = sample(rep(0:1, J / 2))[id]) cdat$M <- 0.5 * cdat$X + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) cdat$Y <- 0.2 * cdat$X + 0.4 * cdat$M + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) # Resample whole clusters. A refit that is singular or does not converge is # dropped, and the warning counts how many were nie_of <- function(d) { unname(nie(fit_mediation(Y ~ X + M, M ~ X, data = d, treatment = "X", mediator = "M", engine = "lmer", cluster = "school"))) } result_cluster <- bootstrap_mediation( nie_of, method = "nonparametric", data = cdat, cluster = "school", n_boot = 20, seed = 1 ) print(result_cluster) }
S7 class containing results from bootstrap inference, including point estimates, confidence intervals, and bootstrap distribution.
BootstrapResult(estimate, ci_lower, ci_upper, ci_level, boot_estimates, n_boot, method, call, inference = list())BootstrapResult(estimate, ci_lower, ci_upper, ci_level, boot_estimates, n_boot, method, call, inference = list())
estimate |
Numeric scalar: point estimate of the statistic |
ci_lower |
Numeric scalar: lower bound of confidence interval |
ci_upper |
Numeric scalar: upper bound of confidence interval |
ci_level |
Numeric scalar: confidence level (e.g., 0.95 for 95% CI) |
boot_estimates |
Numeric vector: bootstrap distribution of estimates |
n_boot |
Integer scalar: number of bootstrap samples |
method |
Character scalar: bootstrap method ("parametric", "nonparametric", or "plugin") |
call |
Call object or NULL: original function call |
inference |
Optional list, empty by default for legacy results. Bounded FIML
source intervals use schema |
This class standardizes bootstrap inference results across different bootstrap methods (parametric, nonparametric, plugin).
The class includes validation to ensure consistency between
method type and required fields. See bootstrap_mediation() for the bounded
FIML source route. Its failed or incomplete results retain length-n_boot
distributions with unresolved NA values and withhold intervals through every
accessor. The optional metadata does not change legacy tidy/glance columns.
A BootstrapResult S7 object
# Parametric bootstrap result result <- BootstrapResult( estimate = 0.15, ci_lower = 0.10, ci_upper = 0.20, ci_level = 0.95, boot_estimates = rnorm(1000, 0.15, 0.02), n_boot = 1000L, method = "parametric", call = NULL )# Parametric bootstrap result result <- BootstrapResult( estimate = 0.15, ci_lower = 0.10, ci_upper = 0.20, ci_level = 0.95, boot_estimates = rnorm(1000, 0.15, 0.02), n_boot = 1000L, method = "parametric", call = NULL )
S7 class for mediation with a treatment assigned to whole clusters (such as
schools) and a mediator and outcome measured on the individuals in them. It
holds the paths of a two-level linear mixed model pair: the mediator model
M ~ X + ... + (1 | cluster) and the outcome model with the mediator split
into its within-cluster deviation and the observed cluster mean. medfit
computes the effects; causal interpretation is the user's responsibility.
ClusterMediationData(a_path, b_within, b_between, c_prime, estimates, vcov, kr_df, treatment, mediator, outcome, cluster, n_obs, n_clusters, cluster_sizes, parameterization, covariates_centered, se_type, reml, converged, sigma_m, sigma_y, tau_m, tau_y, data, source_package)ClusterMediationData(a_path, b_within, b_between, c_prime, estimates, vcov, kr_df, treatment, mediator, outcome, cluster, n_obs, n_clusters, cluster_sizes, parameterization, covariates_centered, se_type, reml, converged, sigma_m, sigma_y, tau_m, tau_y, data, source_package)
a_path |
Numeric scalar: effect of the treatment on the mediator. |
b_within |
Numeric scalar: within-cluster mediator effect on the outcome. |
b_between |
Numeric scalar: between-cluster (cluster-mean) mediator effect on the outcome. |
c_prime |
Numeric scalar: direct effect of the treatment. |
estimates |
Named numeric vector of parameter estimates: the four alias rows plus each model's fixed effects. |
vcov |
Square variance-covariance matrix of |
kr_df |
Named numeric vector of Kenward-Roger degrees of freedom, one
per path, when |
treatment, mediator, outcome
|
Character names of the variables. |
cluster |
Character name of the cluster variable. |
n_obs |
Integer number of observations. |
n_clusters |
Integer number of clusters. |
cluster_sizes |
Integer vector with one size per cluster. |
parameterization |
|
covariates_centered |
Logical: whether every level-1 covariate in the outcome model has a cluster-mean companion. |
se_type |
|
reml |
Logical: whether the models were fitted by REML. |
converged |
Logical convergence flag. |
sigma_m, sigma_y
|
Numeric scalars: residual standard deviations of the mediator and outcome models. |
tau_m, tau_y
|
Numeric scalars: random-intercept standard deviations of the mediator and outcome models. |
data |
Optional data frame, or NULL. |
source_package |
Character name of the originating package. |
With the within parameterization
and the effect of the treatment on the mediator, the natural
indirect effect is . It splits into an own-mediator part
and a spillover part .
The split is the large-cluster, cluster-average approximation. With the
observed cluster mean, moving only member 's mediator by
also moves the mean by , so the exact own-mediator effect is
and the exact
spillover is ;
they still sum to . decompose() warns when the
approximation error exceeds half the own effect's standard error.
The estimand assumes randomized treatment, intact clusters, no interference
between clusters, linear models without mediator-by-treatment or
mediator-by-covariate products, and interference through the observed
cluster mean only; all members of a cluster must be in the analysis rows.
The own effect also needs no unmeasured lower-level mediator-outcome
confounding. The spillover, NIE and NDE need no unmeasured upper-level
mediator-outcome confounding either; under it only the sum of the direct
effect and is identified. print() and
summary() print this block. The validator ties the alias rows a, c_prime, b_within and
b_between of estimates and vcov to the path properties, so an object
with inconsistent numbers cannot be built.
kr_df holds the Kenward-Roger degrees of freedom of each path when
se_type = "kr", and is empty otherwise.
A ClusterMediationData S7 object.
Talloen, W., Moerkerke, B., Loeys, T., De Naeghel, J., Van Keer, H., & Vansteelandt, S. (2016). Estimation of indirect effects in the presence of unmeasured confounding for the mediator-outcome relationship in a multilevel 2-1-1 mediation model. Journal of Educational and Behavioral Statistics, 41(4), 359-391. doi:10.3102/1076998616636855
VanderWeele, T. J. (2010). Direct and indirect effects for neighborhood-based clustered and longitudinal data. Sociological Methods & Research, 38(4), 515-544. doi:10.1177/0049124110366236
# Hand-built object (the paths and their alias rows must agree) est <- c(a = 0.5, c_prime = 0.2, b_within = 0.3, b_between = 0.6) vc <- diag(0.01, 4) dimnames(vc) <- list(names(est), names(est)) cmd <- ClusterMediationData( a_path = 0.5, b_within = 0.3, b_between = 0.6, c_prime = 0.2, estimates = est, vcov = vc, treatment = "X", mediator = "M", outcome = "Y", cluster = "school", n_obs = 400L, n_clusters = 40L, cluster_sizes = rep(10L, 40), parameterization = "within", covariates_centered = TRUE, se_type = "model", reml = TRUE, converged = TRUE, sigma_m = 1, sigma_y = 1, tau_m = 0.5, tau_y = 0.5, source_package = "medfit" ) cmd@a_path * cmd@b_between # the NIE, 0.5 * 0.6# Hand-built object (the paths and their alias rows must agree) est <- c(a = 0.5, c_prime = 0.2, b_within = 0.3, b_between = 0.6) vc <- diag(0.01, 4) dimnames(vc) <- list(names(est), names(est)) cmd <- ClusterMediationData( a_path = 0.5, b_within = 0.3, b_between = 0.6, c_prime = 0.2, estimates = est, vcov = vc, treatment = "X", mediator = "M", outcome = "Y", cluster = "school", n_obs = 400L, n_clusters = 40L, cluster_sizes = rep(10L, 40), parameterization = "within", covariates_centered = TRUE, se_type = "model", reml = TRUE, converged = TRUE, sigma_m = 1, sigma_y = 1, tau_m = 0.5, tau_y = 0.5, source_package = "medfit" ) cmd@a_path * cmd@b_between # the NIE, 0.5 * 0.6
Bounded FIML fits are refused before an external-engine comparison or refit;
this function does not extend their supported inference. See fit_sem().
Runs the likelihood-ratio test of a product null () in
an external SEM engine and compares it with medfit's own mbco_sem()
statistic. A general-purpose optimizer handed the nonlinear constraint
can drop it (OpenMx at a zero start) or stop at the
wrong branch (lavaan from either start, OpenMx from its default start),
which reports a statistic above the true minimum.
compare_lr() flags exactly those cases and nothing else.
A warning is raised only on observed evidence, never because an LR is zero:
medfit_warning_engine_constraint_dropped when the engine's returned fit
violates the product constraint, and medfit_warning_engine_statistic_above_max
when the engine's statistic exceeds medfit's. Both inherit from
medfit_warning_engine_comparison. If medfit has no accepted reference, the
engines' unrestricted fits differ from medfit's, or a run fails or cannot be
checked, the verdict is "unresolved" and neither side is blamed. medfit has
no accepted reference when one of its own refits cannot be fitted (see
mbco_sem()): the reference
LR is then NA and the result is a comparison with that verdict, not an
error.
Only observed-variable models are compared. With either engine, a
saturated mean structure (every intercept free) and fixed_x (all, a named
subset or a lenient subset) are supported: the fixed exogenous moments are
written to the engine as fixed parameters at their sample values (in OpenMx,
a fit with a mean structure is run on the raw data the fit used). Latent variables,
product terms, fixed or constrained intercepts, weights, factors, other fixed
or shared parameters, start/bound modifiers and constraints in the model are
refused by name.
Known limit (#320): an engine's statistic can land above medfit's reference
because the null is two lines, and
, each with its own minimum. A general-purpose optimizer
settles on one line; when it settles on the line with the larger minimum,
the engine's statistic is that line's LR, not the product-null LR. medfit
fits each line as a linear model and keeps the smaller (see mbco_sem()).
medfit_warning_engine_statistic_above_max says only that a feasible,
converged engine statistic exceeds the reference; it does not say which line
the engine reached or whether it reached that line's minimum. A run that
stopped short of the minimum on either line raises the same warning. Treat
the warning as undiagnosed until both line minima are computed: if the
engine's statistic equals the other line's minimum, the engine chose the
wrong line and medfit's reference stands; otherwise it may be an optimizer
failure in the engine. On the pinned design (15 seeds by 4 cells, 60 runs
per engine) the warning fires in 18 of 60 lavaan runs (either start) and 13
of 60 OpenMx default-start runs; in that design each of those runs was
checked and equals the other line's minimum, and the smaller of lavaan's two
runs equals medfit's reference in all 60. The counts are unchanged by the
0.15.0 flip of the fit_sem() defaults, and no run is mislabeled.
compare_lr(object, paths, engine = c("lavaan", "openmx"), tolerance = 1e-04)compare_lr(object, paths, engine = c("lavaan", "openmx"), tolerance = 1e-04)
object |
|
paths |
Character vector: the labels of the regression paths whose
product is the indirect effect, as in |
engine |
|
tolerance |
Relative tolerance on the LR difference, scaled by
|
An object of class medfit_lr_comparison: a list with verdict
("agree", "disagree" or "unresolved"), reason, reference (medfit's
LR, NA when it has none, and whether it was accepted), runs (one row per engine start with the
LR, convergence, constraint residual and status) and engine.
if (requireNamespace("lavaan", quietly = TRUE)) { fit <- fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + cp*treatment", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL ) compare_lr(fit, paths = c("a", "b"), engine = "lavaan") }if (requireNamespace("lavaan", quietly = TRUE)) { fit <- fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + cp*treatment", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL ) compare_lr(fit, paths = c("a", "b"), engine = "lavaan") }
Return the components of the total effect for an object whose outcome model has treatment-by-mediator products.
For an InteractionMediationData object this is VanderWeele's (2014) four-way decomposition: controlled direct effect (CDE), reference interaction (INTref), mediated interaction (INTmed), and pure indirect effect (PIE), together with the derived natural direct and indirect effects and the total effect. For a JointMediationData object it is the CDE and the joint natural direct and indirect effects (VanderWeele and Vansteelandt 2014).
decompose(x, ...)decompose(x, ...)
x |
An InteractionMediationData or JointMediationData object. |
... |
Additional arguments (ignored). |
For a 0/1 treatment, outcome model
,
mediator model ,
and reference mediator level :
with ,
, and
.
is the mediator model's prediction at no treatment and the covariate means.
A named numeric vector: c(cde, int_ref, int_med, pie, nde, nie, total) for InteractionMediationData, c(cde, nde, nie, total) for
JointMediationData.
VanderWeele, T. J. (2014). A unification of mediation and interaction: A 4-way decomposition. Epidemiology, 25(5), 749–761.
VanderWeele, T. J., & Vansteelandt, S. (2014). Mediation analysis with multiple mediators. Epidemiologic Methods, 2(1), 95–115. doi:10.1515/em-2012-0010
fit_m <- lm(mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo) fit_y <- lm(outcome ~ treatment * mediator1 + covariate1 + covariate2, data = mediation_demo) med_int <- extract_mediation(fit_m, model_y = fit_y, treatment = "treatment", mediator = "mediator1") decompose(med_int)fit_m <- lm(mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo) fit_y <- lm(outcome ~ treatment * mediator1 + covariate1 + covariate2, data = mediation_demo) med_int <- extract_mediation(fit_m, model_y = fit_y, treatment = "treatment", mediator = "mediator1") decompose(med_int)
Generic function to extract mediation structure (a, b, c' paths and variance-covariance matrices) from fitted models. This function provides a unified interface for extracting mediation information from various model types (lm, glm, lavaan, lmer, and a SEMFit from the sem engine).
extract_mediation(object, ...)extract_mediation(object, ...)
object |
Fitted model object (lm, glm, lavaan, lmerMod, or a SEMFit) |
... |
Additional arguments passed to methods. Common arguments include:
|
The extract_mediation() generic provides methods for different model types:
lm/glm: Extract from linear and generalized linear models
lavaan: Extract from structural equation models
lmerMod: Extract from two linear mixed models for a treatment assigned
to whole clusters, as a ClusterMediationData: the mediator model is
object, the outcome model is model_y, and cluster names the cluster
variable. Both models must be fitted to the same rows; the cluster means
must be computed on those rows, and a pair of fits that kept different
individuals is an error (best effort: it compares the data row names the
fits kept, so it cannot see a mismatch between frames whose row names were
both reset)
SEMFit: Extract from a model fitted by fit_sem() (the sem engine,
medfit's own maximum-likelihood SEM fitter), with the same arguments and structures as
the lavaan method. A treatment-by-mediator product written x:m (or m:x)
in the outcome equation gives the four-way decomposition, stored under the
canonical name m:x; without it decomposition = "four_way" is refused
by name. interaction = takes that product written with :. A fit from
fit_sem(missing = "fiml") extracts to a FIMLMediationData object for the
three-variable model and for the bounded one- and two-covariate models (see
the Bounded FIML section of fit_sem()). With covariates, treatment may
name any exogenous variable when no outcome path is fixed at zero; with
exactly one outcome path fixed at zero, that variable is the treatment. The
other exogenous variables are the covariates; three or more covariates, a
covariate on one equation only and every other structure are refused.
A fit with two parallel mediators extracts to a FIMLParallelMediationData
object when mediator names both mediators (structure = "parallel" or
"auto"); it carries the full covariance of the paths, and a refusal has
class medfit_error_fiml_extract_unsupported. With one or two covariates
the same rules as for the one-mediator covariate fits choose the treatment.
A two-mediator serial fit (the saturated graph or the pure chain, with no,
one or two covariates) extracts to a FIMLSerialMediationData object when mediator
names both mediators in the fitted order (a reversed order is refused with
the same class); it carries the full covariance of a, a2, d1, b1, b
and c_prime (saturated) or of a, d1, b and c_prime (chain).
brmsfit: Extract from Bayesian models (future)
Note: OpenMx extraction is planned for a future release.
The returned objects share one interface (nie(), nde(), te(),
confint(), bootstrap_mediation(), ...) for use by other medfit functions
and dependent packages (probmed, RMediation, medrobust).
The class returned depends on the structure. A single mediator gives a
MediationData object, or an InteractionMediationData object when the
outcome model has a treatment-by-mediator term. A mediator vector of
length two or more gives a SerialMediationData or ParallelMediationData
object; for lm/glm fits whose outcome model has a treatment-by-mediator term
written with : or *, it gives a JointMediationData object with the
joint natural effects of the mediators. Any other product term in a
multi-mediator fit errors.
With object the fitted mediator model (the first mediator's model for
several mediators), the lm/glm method takes:
model_y: the fitted outcome model. With several mediators include every
mediator, not only the last: omitting one that also affects the outcome
biases the others' coefficients.
treatment: name of the treatment variable.
mediator: name of the mediator, or an ordered character vector of two or
more mediator names.
mediator_models: list of the fitted models for mediators 2 to k, in
order; required whenever mediator has length two or more.
structure: "auto" (default), "serial", or "parallel"; "auto"
classifies the mediators from the mediator models.
decomposition: "auto" (default) uses the four-way decomposition when
a single mediator's outcome model has a treatment-by-mediator term;
"four_way" requires that term; "two_way" ignores it and returns a
MediationData object.
m_star: the reference mediator level for the controlled direct effect
(default 0; for JointMediationData a scalar or a vector named by the
interacting mediators).
vcov_fun: function returning each model's covariance matrix (default
stats::vcov(); for example sandwich::vcovHC). Not supported for
JointMediationData.
outcome, data: optional; detected from the models when omitted.
The lavaan method takes a fitted lavaan model and the variable names; see
?extract_mediation_lavaan for its arguments.
For lm/glm fits, each equation's block of @vcov is that model's own
covariance. The blocks for different equations are stored as zero. This is
exact for simple mediation, serial chains, and the four-way model, whose
later equations contain every regressor of the earlier ones, but for
parallel mediators it leaves out the covariance between their a paths.
JointMediationData stores the full stacked least-squares covariance, and a
lavaan fit estimates all equations jointly, so for the same data the
intervals can differ between engines.
The four-way effects that contain a covariate mean (int_ref, the NDE and
the total effect) treat that mean as a fixed constant, so their standard
errors and intervals are conditional on the observed covariates. They omit
the sampling variance of the estimated mean, theta3 * gamma times the
variance of the covariate mean, so they can understate the uncertainty of a
population-average effect. CDE, int_med, PIE and the NIE do not depend on
the mean. For population-average standard errors set
options(medfit.covariate_mean_variance = TRUE): tidy() and confint() then add the
variance of the estimated covariate mean,
(theta3 * gamma)^2 times the variance of the covariate mean (case-weighted
when the fit has weights), to int_ref, the NDE and the total effect; the
default stays conditional, as lavaan's := and the glm route are. Or
resample rows with bootstrap_mediation() (method = "nonparametric").
The simple single-mediator MediationData from lm/glm fits names three
coefficients m_<treatment>, y_<mediator> and y_<treatment>
(m_X, y_M and y_X for variables X and M), and code that looks
coefficients up by name, such as the parametric bootstrap in probmed, relies on
them. The lavaan method adds the same three names to @estimates and
@vcov for an observed, continuous treatment, mediator and outcome. Each is
a second name for the a, b or c_prime path, with the same estimate and
covariances, appended after the existing rows. A lavaan fit with a latent or
ordered treatment, mediator or outcome, and any extraction with
standardized = TRUE, gets none of the three and works with the plugin
estimators only. Serial, parallel and four-way objects are not
affected.
An S7 object whose class depends on the mediation structure (see Details): MediationData, InteractionMediationData, SerialMediationData, ParallelMediationData, JointMediationData, or ClusterMediationData. Each carries the path coefficients, the full parameter vector and its variance-covariance matrix, residual standard deviations (for Gaussian models), variable names, and the original data when available.
MediationData, fit_mediation(), bootstrap_mediation()
# Extract the mediation structure from fitted lm models, using the # simulated mediation_demo data bundled with medfit fit_m <- lm(mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo) fit_y <- lm(outcome ~ treatment + mediator1 + covariate1 + covariate2, data = mediation_demo) med_data <- extract_mediation(fit_m, model_y = fit_y, treatment = "treatment", mediator = "mediator1") if (requireNamespace("lme4", quietly = TRUE)) { # Treatment assigned to whole clusters (needs lme4); formulas hold fixed # effects only, the engine adds the random cluster intercept set.seed(1) J <- 30 id <- rep(seq_len(J), each = 6) cdat <- data.frame(school = factor(id), X = sample(rep(0:1, J / 2))[id]) cdat$M <- 0.5 * cdat$X + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) cdat$Y <- 0.2 * cdat$X + 0.4 * cdat$M + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) # The outcome model carries the mediator as its within-cluster deviation # plus the observed cluster mean cdat$M_bar <- ave(cdat$M, cdat$school) cdat$M_w <- cdat$M - cdat$M_bar fit_m <- lme4::lmer(M ~ X + (1 | school), data = cdat) fit_y <- lme4::lmer(Y ~ X + M_w + M_bar + (1 | school), data = cdat) cluster_med <- extract_mediation(fit_m, model_y = fit_y, treatment = "X", mediator = "M", cluster = "school") nie(cluster_med) }# Extract the mediation structure from fitted lm models, using the # simulated mediation_demo data bundled with medfit fit_m <- lm(mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo) fit_y <- lm(outcome ~ treatment + mediator1 + covariate1 + covariate2, data = mediation_demo) med_data <- extract_mediation(fit_m, model_y = fit_y, treatment = "treatment", mediator = "mediator1") if (requireNamespace("lme4", quietly = TRUE)) { # Treatment assigned to whole clusters (needs lme4); formulas hold fixed # effects only, the engine adds the random cluster intercept set.seed(1) J <- 30 id <- rep(seq_len(J), each = 6) cdat <- data.frame(school = factor(id), X = sample(rep(0:1, J / 2))[id]) cdat$M <- 0.5 * cdat$X + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) cdat$Y <- 0.2 * cdat$X + 0.4 * cdat$M + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) # The outcome model carries the mediator as its within-cluster deviation # plus the observed cluster mean cdat$M_bar <- ave(cdat$M, cdat$school) cdat$M_w <- cdat$M - cdat$M_bar fit_m <- lme4::lmer(M ~ X + (1 | school), data = cdat) fit_y <- lme4::lmer(Y ~ X + M_w + M_bar + (1 | school), data = cdat) cluster_med <- extract_mediation(fit_m, model_y = fit_y, treatment = "X", mediator = "M", cluster = "school") nie(cluster_med) }
Fit mediation models using a specified modeling engine. This function provides a convenient formula-based interface for fitting both the mediator and outcome models simultaneously.
fit_mediation( formula_y, formula_m, data, treatment, mediator, engine = "glm", family_y = stats::gaussian(), family_m = stats::gaussian(), weights = NULL, se_type = c("model", "sandwich", "kr"), engine_args = list(), m_star = 0, cluster = NULL, model = NULL, ... )fit_mediation( formula_y, formula_m, data, treatment, mediator, engine = "glm", family_y = stats::gaussian(), family_m = stats::gaussian(), weights = NULL, se_type = c("model", "sandwich", "kr"), engine_args = list(), m_star = 0, cluster = NULL, model = NULL, ... )
formula_y |
Formula for outcome model (e.g., |
formula_m |
Formula for mediator model (e.g., |
data |
Data frame containing all variables. With the |
treatment |
Character string: name of treatment variable |
mediator |
Character string: name of mediator variable |
engine |
Character string: modeling engine to use. Currently supports:
|
family_y |
Family object for outcome model (default: |
family_m |
Family object for mediator model (default: |
weights |
Optional numeric vector of case weights (length |
se_type |
Variance-covariance estimator for |
engine_args |
Named list of engine-specific overrides (default:
|
m_star |
Numeric scalar: reference mediator level |
cluster |
Character string: name of the cluster variable in |
model |
Character string: model syntax for |
... |
Additional arguments passed to the fitting function. With the
|
The function fits two models:
Mediator model: formula_m (e.g., M ~ X + C1 + C2)
Outcome model: formula_y (e.g., Y ~ X + M + C1 + C2)
The treatment variable must appear in both formulas. The mediator variable must appear in the outcome formula but NOT in the mediator formula (as it is the response).
When engine = "glm" (default):
Models are fit using stats::glm()
Supports all GLM families (gaussian, binomial, poisson, etc.)
With a non-identity link the path coefficients are on the link
scale, where the product is not a natural indirect
effect on the outcome scale; interpret it with care or use
engine = "regmedint" for closed-form effects of a logistic outcome
A treatment-by-mediator term in formula_y requires Gaussian
identity-link models (the four-way formulas are linear); other families
error
For Gaussian models, residual standard deviations are extracted
Non-Gaussian outcomes have sigma_y = NULL
gaussian(): Continuous outcomes (default)
binomial(): Binary outcomes
poisson(): Count outcomes
Gamma(): Positive continuous outcomes
When engine = "regmedint":
Delegates to regmedint::regmedint() (suggested package) for closed-form
natural (in)direct effects, with or without a treatment-mediator interaction
Returns MediationData or InteractionMediationData depending on whether
formula_y contains a treatment-by-mediator interaction term; use
engine_args to override the derived regmedint arguments
When the fit yields an InteractionMediationData, m_star fixes the level
at which the controlled direct effect is read off:
and
.
The two shift in compensating directions, so nde(), nie(), te(), and
pm() are invariant to m_star; only the CDE/INTref split moves.
The engines reach the same result by different routes. engine = "glm"
applies m_star at extraction time, after the coefficients are fit;
engine = "regmedint" passes it to regmedint::regmedint() as m_cde,
where it is consumed by that package's closed-form estimator at fitting
time. Supplying m_star for a fit with no treatment-by-mediator term is an
error, not a silent no-op.
A MediationData object containing the fitted mediation structure,
or an InteractionMediationData object when formula_y contains a
treatment-by-mediator interaction term.
VanderWeele, T. J. (2014). A unification of mediation and interaction: A 4-way decomposition. Epidemiology, 25(5), 749–761.
MediationData, extract_mediation(), bootstrap_mediation()
# mediation_demo is simulated data bundled with medfit; its covariates # confound the mediator-outcome relation, so both models adjust for them med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) print(med_data) # Binary outcome (takes longer to fit): dichotomize the outcome at its median demo <- mediation_demo demo$outcome_bin <- as.integer(demo$outcome > stats::median(demo$outcome)) med_data_bin <- fit_mediation( formula_y = outcome_bin ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = demo, treatment = "treatment", mediator = "mediator1", family_y = binomial() ) if (requireNamespace("lme4", quietly = TRUE)) { # Treatment assigned to whole clusters (needs lme4); formulas hold fixed # effects only, the engine adds the random cluster intercept set.seed(1) J <- 30 id <- rep(seq_len(J), each = 6) cdat <- data.frame(school = factor(id), X = sample(rep(0:1, J / 2))[id]) cdat$M <- 0.5 * cdat$X + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) cdat$Y <- 0.2 * cdat$X + 0.4 * cdat$M + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) cluster_fit <- fit_mediation( Y ~ X + M, M ~ X, data = cdat, treatment = "X", mediator = "M", engine = "lmer", cluster = "school" ) nie(cluster_fit) # a * b_between decompose(cluster_fit) # own-mediator and spillover parts }# mediation_demo is simulated data bundled with medfit; its covariates # confound the mediator-outcome relation, so both models adjust for them med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) print(med_data) # Binary outcome (takes longer to fit): dichotomize the outcome at its median demo <- mediation_demo demo$outcome_bin <- as.integer(demo$outcome > stats::median(demo$outcome)) med_data_bin <- fit_mediation( formula_y = outcome_bin ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = demo, treatment = "treatment", mediator = "mediator1", family_y = binomial() ) if (requireNamespace("lme4", quietly = TRUE)) { # Treatment assigned to whole clusters (needs lme4); formulas hold fixed # effects only, the engine adds the random cluster intercept set.seed(1) J <- 30 id <- rep(seq_len(J), each = 6) cdat <- data.frame(school = factor(id), X = sample(rep(0:1, J / 2))[id]) cdat$M <- 0.5 * cdat$X + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) cdat$Y <- 0.2 * cdat$X + 0.4 * cdat$M + rnorm(J, sd = 0.5)[id] + rnorm(J * 6) cluster_fit <- fit_mediation( Y ~ X + M, M ~ X, data = cdat, treatment = "X", mediator = "M", engine = "lmer", cluster = "school" ) nie(cluster_fit) # a * b_between decompose(cluster_fit) # own-mediator and spillover parts }
Fits a model written in lavaan-style syntax to raw data with the sem engine,
medfit's own maximum-likelihood SEM fitter, and returns a SEMFit object. The engine handles
observed and latent variables, user labels, fixed values, starts and bounds,
equality and inequality constraints (==, <, >, linear or nonlinear), and defined
parameters (:=) with delta-method standard errors.
fit_sem( model, data, information = c("observed", "expected"), n_starts = NULL, control = list(), weights = NULL, se_type = c("model", "sandwich"), meanstructure = TRUE, fixed_x = TRUE, sampling_weights = lifecycle::deprecated(), missing = c("listwise", "fiml"), cluster = NULL, estimator = c("ML", "MLR") )fit_sem( model, data, information = c("observed", "expected"), n_starts = NULL, control = list(), weights = NULL, se_type = c("model", "sandwich"), meanstructure = TRUE, fixed_x = TRUE, sampling_weights = lifecycle::deprecated(), missing = c("listwise", "fiml"), cluster = NULL, estimator = c("ML", "MLR") )
model |
Character: model syntax. Lines may use |
data |
A data frame with the model's observed variables. Numeric columns are used as is; a
factor, character or logical column named on a regression's right side is expanded to 0/1 dummies
( |
information |
Character: |
n_starts |
Positive integer or |
control |
A |
weights |
Optional numeric vector of non-negative case weights,
one per row of |
se_type |
Character: |
meanstructure |
Logical or |
fixed_x |
|
sampling_weights |
|
missing |
Exact missing-data method: |
cluster |
Experimental. |
estimator |
|
A SEMFit object. With a product term whose factor is a regression target (x:m with
m regressed on x), glance() reports the chi-square, df and p-value as NA with a note, and
anova() and mbco_sem() refuse; see SEMFit-methods.
diagnostics$ledger holds the versioned outcome ledger described below.
fixed_x is the exogenous observed variables (lavaan's fixed.x = TRUE) and meanstructure = TRUE, so a default
fit matches a default lavaan::sem() fit. Against 0.14.x the parameter count, logLik(), AIC(), BIC() and the
names in coef(), vcov() and tidy() change; the structural estimates and their standard errors did not change
on the cases measured. Which random start a multi-start fit tries can change too (the retry seed follows the
parameter names). To keep the 0.14.x results, pass fixed_x = FALSE, meanstructure = NULL.
The 0.14.x options(medfit.native_defaults) opt-in no longer has any effect.
Explicit missing = "fiml" fits a joint Gaussian random-X model under ignorable
missingness (for example, MAR with the usual ignorability conditions). It evaluates
observed components, without imputing values. It supports numeric continuous
observed variables in four shapes: X -> M -> Y (three variables), the
same model with one covariate Z, with two covariates Z1 and Z2, and two
parallel mediators (see below). The
X -> Y path is free or fixed exactly at zero, intercepts are free, and
disturbances are independent except the free mediator residual covariance of the
parallel form. Names and labels are arbitrary.
With covariates the regressions are M ~ X + Z and Y ~ M + X + Z (one
covariate), or M ~ X + Z1 + Z2 and Y ~ M + X + Z1 + Z2 (two). Every
exogenous variable needs a written path to Y, and every covariate path to M
is free. The exogenous variables have a free joint covariance: write every pair
(in either order) or none; a partial set is refused. Any variable may be
incomplete. With no Y path fixed at zero, the first exogenous variable is the
fit's treatment and extract_mediation() may name any exogenous variable as the
treatment; with exactly one Y path fixed at zero, that variable is the
treatment. Parameter counts are 8 or 9 (three variables), 13 or 14 (one
covariate) and 19 or 20 (two covariates), without or with the direct path.
Three or more covariates, a covariate on one equation only, residual
covariances (other than the parallel mediator covariance), extra predictors beyond these, latent variables, products,
factors, correlated disturbances, parameter ties, bounds, general constraints,
defined parameters and syntax-supplied starts are not supported. The covariate
fits are implemented and numerically verified against lavaan and an independent
likelihood; their coverage has not been calibrated or statistically admitted.
Input checks and local numerical gates do not establish global identification,
a global optimum or inference coverage.
Two parallel mediators (M1 ~ X, M2 ~ X, Y ~ M1 + M2, with or without
Y ~ X) are fitted in two forms. Independent residuals require
the statement M1 ~~ 0*M2 (either order): an omitted statement is refused
because lavaan's default would free it. A free, label-less M1 ~~ M2 fits the
mediator residual covariance. A labeled, fixed nonzero, bounded, tied or
otherwise constrained covariance is refused. The free-parameter count is 12 or
13 (independent residuals) and 13 or 14 (free covariance), without or with the
direct path. extract_mediation() needs both mediators named and returns a
FIMLParallelMediationData object with the full path covariance of a1, a2,
b1, b2 and c_prime, so the sum of the indirect effects keeps every
cross-path term. Extraction refusals for the parallel form have class
medfit_error_fiml_extract_unsupported.
Two parallel mediators also take one covariate Z or two covariates Z1,
Z2 (M1 ~ X + Z, M2 ~ X + Z, Y ~ M1 + M2 + X + Z, in either residual
form). The covariate rules are those above: every exogenous variable has a free
path to both mediators and a written path to Y, the exogenous covariance is
free (write every pair or none), and any variable may be incomplete. With no Y
path fixed at zero, extract_mediation() may name any exogenous variable as the
treatment; with one fixed at zero that variable is the treatment. The
free-parameter count is 18 to 20 (one covariate) and 25 to 27 (two), without or
with the direct path and the free residual covariance. Three or more mediators,
three or more covariates and a covariate on only some of the equations stay
refused.
Two serial mediators are fitted in two forms, each with no, one or two
covariates: the saturated forward graph M1 ~ X, M2 ~ M1 + X and
Y ~ M1 + M2 + X, and the pure chain M1 ~ X, M2 ~ M1 and Y ~ M2 (the direct
path Y ~ X is free, 0*X, or omitted in both without a covariate), with the independence of all three
disturbance pairs written as M1 ~~ 0*M2, M1 ~~ 0*Y and M2 ~~ 0*Y
(either order within a pair; an omitted or free statement is refused). The
saturated model has 14 free parameters (13 with the direct path fixed) and is just
identified with the direct path free, so a pair of variables that is never
observed together makes the fit fail closed (medfit_sem_fiml_error at the
numerical stage). The chain has 12 free parameters (11 with the direct path
fixed), is overidentified (2 or 3 degrees of freedom) and so is a restriction
the data can reject; it fails closed for fewer unobserved pairs. A graph with
exactly one of the two shortcuts, and a fixed or labeled shortcut, is refused.
With one covariate Z it predicts M1, M2 and Y (M1 ~ X + Z,
M2 ~ M1 + X + Z or M2 ~ M1 + Z, Y ~ ... + X + Z), X and Z have a free
covariance, and every exogenous variable needs a written Y path (free; at most
one fixed at exactly zero, which makes that variable the treatment). The
saturated model then has 20 free parameters (19) and the chain 18 (17). With two
covariates Z1 and Z2 both predict every equation, the three exogenous
covariances (X ~~ Z1, X ~~ Z2, Z1 ~~ Z2) are all written or none (a
partial set is refused), and the saturated model has 27 free parameters (26) and
the chain 25 (24). In the pure chain the treatment is structural, the variable
with no path to M2: leaving a variable out of the equation of M2 makes it the
treatment, and exactly one variable may be left out. In the saturated graph with
a fixed-zero direct path the variable with the fixed-zero path is the only valid
treatment.
extract_mediation() needs both mediators named in the fitted
order (the reversed order is refused) and returns a FIMLSerialMediationData
object with the full covariance of a, a2 (X to M2), d1 (M1 to M2), b1
(M1 to Y), b and c_prime; nie() is the chain effect a * d1 * b,
nie(type = "total") adds a2 * b + a * b1, and paths(), tidy(),
coef(type = "paths") and summary() list all six paths. For the chain the
object has the four paths a, d1, b and c_prime, and nie() and
nie(type = "total") are the same effect. The covariate's coefficients are never
paths: the aliases are the treatment's. In the saturated graph with a free direct
path a covariate may be named the treatment; in the pure chain naming a covariate
is refused (both covariates, with the classed medfit_error_fiml_extract_unsupported). Its normal intervals
are available; pm(), quick(), glance() and bootstrap intervals are not
(bootstrap_mediation(source_fit = ) stays withheld, and behind that guard a
serial fit raises medfit_error_fiml_serial_source_fit). Serial models with three
or more covariates and every other serial shape stay refused.
Supply fixed_x = FALSE explicitly; omission, TRUE and named subsets are errors.
Require meanstructure = TRUE, information = "observed", se_type = "model",
no weights, empty control, and n_starts = NULL or 3. These numerical-control
restrictions reproduce the initial validated protocol. Missing values never
select FIML automatically, and a failed fit never falls back to listwise deletion.
fit_mediation() with engine = "sem" forwards these settings through engine_args; its
se_type = "model" remains a top-level argument.
nobs() counts rows with any observed model variable; n_dropped counts only
all-empty rows. Singleton patterns and data with no complete rows are supported
when the local identification and information checks pass. Original positional
row IDs and pattern counts are retained. logLik() includes the full Gaussian
constants, with its df attribute equal to the free-parameter count (8 or 9 for three variables; see above for
covariates).
AIC and BIC use that likelihood and retained row count. The discrepancy f and
model-test df are NA: no saturated missing-data reference model is fitted.
Supported operations include SEMFit coefficient/covariance and likelihood
summaries, parameter tidy/normal confidence intervals, and extraction followed by
paths(), nie(), nde(), te(), coef(), vcov(), print/summary, tidy and
normal path/effect confidence intervals. Extracted aliases follow topology;
source labels/full covariance remain in the validated durable fit context.
At a = b = 0, the NIE point value remains available but its inferential fields
are unavailable; a known fixed direct path is a different case.
SEMFit glance() reports likelihood/IC fields with unavailable model-test fields;
extracted-object glance(), quick(), pm(), extracted-object bootstrap, profiles,
MBCO and fit tests remain unavailable. The bootstrap_mediation() source_fit
intervals are implemented but withheld (refused with a classed error) until
statistical admission is decided; a covariate fit is additionally refused with
medfit_error_fiml_covariate_source_fit (including a parallel fit with
covariates) and a parallel fit without covariates with
medfit_error_fiml_parallel_source_fit, so no source-fit interval exists for
either. Delta-method Wald intervals stay available for these extracted objects;
as for the three-variable model, the NIE standard error is NA only at the
exact origin (every a and b path exactly zero).
Object-aware package guards precede package-controlled refits/RNG/workers; opaque user callbacks and external-first
dispatch remain outside that guarantee. Adapter/topology/numerical failures retain
stage, reason, original parent and completed attempts; basic argument-type errors
can occur earlier. A dedicated fiml-1 ledger binds likelihood, rows, settings
and attempts; the version-1 ledger below continues to describe listwise fits.
See the Methods and Formulas article, Bounded FIML, for the likelihood and
the sem engine guide for the supported workflow. Bounded FIML was introduced
in 0.16.0; released 0.15.0 does not provide this option.
One equality c*a*b == q is supported when the constants and q/c are finite and nonzero, a and b
are distinct free scalar labels after expanding definitions, and no other constraint row involves either label.
The engine fits both signs of a independently with the existing optimizer and acceptance gates, respecting
user bounds and effective variance floors. It returns the lowest accepted discrepancy only when each required
sign branch is accepted or excluded by an explicit bounds contradiction. An unresolved branch stops the fit
with medfit_error_no_convergence; an all-empty search returns no fit. Zero-product fitting is unchanged.
Repeated factors, multiple product equalities, coupled rows and other unsupported shapes retain local search.
internals$search_evidence is a separate versioned internal record, not a change to the outcome-ledger schema.
Its coverage is charted for covered sign branches, local for unsupported shapes, or unresolved for an
incomplete search. It retains restrictions, context, branch states, candidates, objectives, gates and attempts;
validation checks their attribution to the model and sample. Charted coverage does not prove a global optimum
within a branch. Existing optimum status meanings are retained, and objects without this optional record remain
usable. Counts distinguish optimizer starts, accepted starts and synthetic reference attempts.
The stored raw constraint residual is diagnostic only. Its optional standardized value uses the gradient and
covariance at an already available matching reference fit without the tested restriction, never the projected
constrained covariance. No additional fit or covariance calculation is requested. The ordinary optimizer reference
lacks covariance, so the standard error and standardized residual are NA with reason
reference_covariance_unavailable. These diagnostics do not affect acceptance or inference.
Observed-information covariance includes the curvature of active nonlinear constraints; expected information retains its Fisher definition. Sandwich standard errors use the corresponding information as bread. Active inequalities, user bounds and solver variance floors give a local tangent covariance, with exactly zero variance for a pinned coordinate. This is not the full sampling distribution at a boundary. See the Methods and Formulas article, under Constraints, for the covariance formula.
diagnostics$ledger is a list with schema_version = 1L. Each node has id, parent_id, the exact parsed
constraints, and context (the estimation sample, the model syntax and parameters, the likelihood domain,
the settings and n_obs). enumeration records state and the number of required children; complete means
every required restriction was attempted, not that any was globally optimized. outcome is resolved or
unresolved. Separate fields record optimizer (acceptance state, original status, decrement, retries and
optimum diagnostics), likelihood (availability and f), feasibility (witness or unknown), covariance
(available, unavailable, not_retained or failed), improper, the retained notices (warnings and
messages), the original condition (class, message, call) and the recursive children. IDs are deterministic
positions within the call, not certificates of equivalent parameter spaces. No global optimum is asserted, and a
failed branch is unknown, never certified infeasible.
A node whose constraints hold zero-product rows also has branch_map: state, raw_count, the parser
factor_counts, unique_count and raw_to_canonical, a structural restriction-set ID for each raw row. The IDs
collapse duplicate or permuted constraint rows, not arbitrary affine equivalence, and every raw row is still fitted
in its original order (ties go to the first). More than 64 raw combinations, duplicates included, is refused before
the grid is allocated, even when the unique count would be smaller; that node is not_enumerated, with an unknown
unique count and no raw mapping, and nothing is fitted or certified. Its message prints the count without scientific
notation, labels a count above as approximate (a double need not be an exact cardinality there)
and names an overflow; raw_count is Inf on overflow. If the map cannot be built while an error is recorded, its
state is unavailable with that condition, and the original error is not replaced. The map is rebuilt from the
node's constraints during validation, so a forged map is refused.
The one proof of emptiness in this version-1 ledger is the literal certificate literal_zero_nonzero: an explicit
label == 0 equality and an explicit finite nonzero literal equality on the same parsed label (a == 0 with
a == 1). Its label, source
rows and values are kept in certificate and rebuilt from the node's own parsed parameters and constraints before
any inference uses it. A certified-empty branch is retained with outcome = "excluded",
feasibility = "certified_empty" and an unavailable likelihood: it is not fitted and is not a successful fit. No
tolerance, solver status, numerical rank, bound, general affine contradiction or fixed value in the parameter
table is a proof, and a literal too small to represent (1e-999) is unknown.
A zero-product fit returns only if at least one required branch resolves and every other branch resolves or is
certified empty. A direct fit whose own equalities contradict literally stops with medfit_sem_infeasible, and a
union whose every branch is certified empty with medfit_sem_empty_union; both are refinements of
medfit_error_no_convergence and carry the ledger. Otherwise it stops with a
medfit_error_no_convergence error, never a fit; the error carries the ledger of every required branch as
$ledger and, when a branch survived, $best_survivor (its id and discrepancy f, diagnostic only, never a
p-value). The union's own node does not aggregate its children's notices; inspect each child. The notices of a
failed call are in the ledger only; the error message keeps its text. A data or model error keeps its own class
and also carries $ledger. The estimation sample is stored once, on the root node; a descendant fitted on the
same rows carries only context$sample_hash. A fit made before the ledger existed must be refitted before
mbco_sem() can use it, which refuses it with a medfit_error_ledger error.
When the optimizer accepts a fit and the covariance stage explicitly diagnoses non-positive-definite information
or a negative variance, the call stops with
medfit_sem_covariance_error, never a fit. The class is a sibling of medfit_error_no_convergence: no optimizer
failed, so the handlers that report an unresolved result for an optimizer failure do not take it. Its fields are
schema_version = 1L, stage = "covariance", partial (the accepted optimizer result: f, theta, sample,
domain, dimensions and settings; no vcov or defined) and ledger (the same node as partial$ledger, with
covariance = "failed" and post_fit: versioned evidence with the original condition's class, message, call and
diagnostic kind, information_not_pd or negative_variance). Other covariance errors propagate unchanged,
without a retained likelihood. Singular information still warns and returns an NA covariance, recorded as
unavailable rather than failed.
The message is the original one. Resolved here means an accounted-for accepted likelihood, not a successful
covariance. A union root reports the first failing branch's post_fit, in branch order, even when another branch
supplies its minimum likelihood, and does not claim the winning branch failed: inspect the branch IDs. A branch that
did not converge still stops the call with medfit_error_no_convergence first. Definition values and gradients are
checked before the covariance, and model, data, optimizer, objective and other errors get no covariance certificate.
The condition is not accepted as an input to mbco_sem(), and a fit that carries post_fit is refused with
medfit_error_ledger.
fit <- fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + treatment\nab := a*b", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL ) fit@defined # A treatment-by-mediator product: the four-way decomposition fit4 <- fit_sem( "mediator1 ~ treatment\noutcome_int ~ treatment + mediator1 + treatment:mediator1", data = mediation_demo, se_type = "sandwich" ) decompose(extract_mediation(fit4, treatment = "treatment", mediator = "mediator1", outcome = "outcome_int"))fit <- fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + treatment\nab := a*b", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL ) fit@defined # A treatment-by-mediator product: the four-way decomposition fit4 <- fit_sem( "mediator1 ~ treatment\noutcome_int ~ treatment + mediator1 + treatment:mediator1", data = mediation_demo, se_type = "sandwich" ) decompose(extract_mediation(fit4, treatment = "treatment", mediator = "mediator1", outcome = "outcome_int"))
S7 class for simple mediation with a treatment-by-mediator interaction
( where the outcome model
contains an term). It carries VanderWeele's (2014)
four-way decomposition of the total effect into controlled direct effect
(CDE), reference interaction (INTref), mediated interaction (INTmed), and
pure indirect effect (PIE):
with and
. medfit computes the
decomposition; causal interpretation is the user's responsibility (it requires
the four no-unmeasured-confounding assumptions of VanderWeele 2014).
InteractionMediationData(a_path, b_path, c_prime, interaction, cde, int_ref, int_med, pie, nde, nie, total_effect, m_star, estimates, vcov, sigma_m, sigma_y, treatment, mediator, outcome, mediator_predictors, outcome_predictors, data, n_obs, converged, source_package)InteractionMediationData(a_path, b_path, c_prime, interaction, cde, int_ref, int_med, pie, nde, nie, total_effect, m_star, estimates, vcov, sigma_m, sigma_y, treatment, mediator, outcome, mediator_predictors, outcome_predictors, data, n_obs, converged, source_package)
a_path |
Numeric scalar: treatment -> mediator effect ( |
b_path |
Numeric scalar: mediator -> outcome main effect ( |
c_prime |
Numeric scalar: treatment -> outcome main effect ( |
interaction |
Numeric scalar: treatment x mediator coefficient ( |
cde, int_ref, int_med, pie
|
Numeric scalars: the four-way components (controlled direct, reference interaction, mediated interaction, pure indirect). |
nde, nie, total_effect
|
Numeric scalars: derived natural direct effect (CDE + INTref), natural indirect effect (INTmed + PIE), and total effect (the sum of all four components). |
m_star |
Numeric scalar: reference mediator level for the decomposition (default 0). |
estimates |
Numeric vector of all parameter estimates. |
vcov |
Square variance-covariance matrix of |
sigma_m |
Optional numeric scalar mediator residual SD, or NULL. |
sigma_y |
Optional numeric scalar outcome residual SD, or NULL. |
treatment, mediator, outcome
|
Single character strings naming the treatment / mediator / outcome. |
mediator_predictors, outcome_predictors
|
Character vectors of predictor names for the mediator and outcome models. |
data |
Optional data frame, or NULL. |
n_obs |
Integer number of observations. |
converged |
Logical convergence flag. |
source_package |
Character name of the originating package. |
Path coefficients follow the outcome model
and mediator model :
a_path = , b_path = ,
c_prime = , interaction = . With
reference level m_star () the components are
,
,
, and
. When the
decomposition collapses to standard simple mediation (CDE = NDE = ;
INTref = INTmed = 0; NIE = PIE = ).
is the mediator model's prediction at no treatment and the covariate means
. The effects are for the treatment contrast 0 to 1, and
the lm/glm extractor requires Gaussian identity-link models. From
fit_mediation(engine = "regmedint"), @estimates and @vcov also carry
regmedint's own delta-method covariance of the four components and the
derived effects.
An InteractionMediationData S7 object.
VanderWeele, T. J. (2014). A unification of mediation and interaction: A 4-way decomposition. Epidemiology, 25(5), 749–761.
# Hand-built object (theta3 = 0.2 interaction, m* = 0) imd <- InteractionMediationData( a_path = 0.5, b_path = 0.3, c_prime = 0.1, interaction = 0.2, cde = 0.1, int_ref = 0.04, int_med = 0.10, pie = 0.15, nde = 0.14, nie = 0.25, total_effect = 0.39, m_star = 0, estimates = c(a = 0.5, b = 0.3, c_prime = 0.1, theta3 = 0.2), vcov = diag(0.01, 4), treatment = "X", mediator = "M", outcome = "Y", mediator_predictors = "X", outcome_predictors = c("X", "M", "X:M"), n_obs = 200L, converged = TRUE, source_package = "medfit" ) nie(imd) # INTmed + PIE = 0.25 decompose(imd) # all four components + derived effects# Hand-built object (theta3 = 0.2 interaction, m* = 0) imd <- InteractionMediationData( a_path = 0.5, b_path = 0.3, c_prime = 0.1, interaction = 0.2, cde = 0.1, int_ref = 0.04, int_med = 0.10, pie = 0.15, nde = 0.14, nie = 0.25, total_effect = 0.39, m_star = 0, estimates = c(a = 0.5, b = 0.3, c_prime = 0.1, theta3 = 0.2), vcov = diag(0.01, 4), treatment = "X", mediator = "M", outcome = "Y", mediator_predictors = "X", outcome_predictors = c("X", "M", "X:M"), n_obs = 200L, converged = TRUE, source_package = "medfit" ) nie(imd) # INTmed + PIE = 0.25 decompose(imd) # all four components + derived effects
Recomputes the joint CDE, NDE, NIE and total effect of a
JointMediationData object from a named parameter vector, holding the
object's structure, reference levels (m_star) and sample covariate means
fixed. With the default estimates = object@estimates it returns the stored
effects. Its main use is as the statistic of a parametric bootstrap: the
draws bootstrap_mediation() passes to statistic_fn are named like
@estimates, and the NDE needs the prefixed intercept and covariate rows
(m1_, ..., y_) and the covariate means, not only the path aliases.
joint_effects(object, estimates = object@estimates)joint_effects(object, estimates = object@estimates)
object |
A JointMediationData object. |
estimates |
Named numeric vector containing every prefixed source row
of |
Named numeric vector: cde, nde, nie, te.
d <- mediation_demo fit <- extract_mediation( lm(mediator1 ~ treatment + covariate1 + covariate2, d), model_y = lm(outcome_int ~ treatment * mediator1 + mediator2 + covariate1 + covariate2, d), treatment = "treatment", mediator = c("mediator1", "mediator2"), mediator_models = list(lm(mediator2 ~ treatment + mediator1 + covariate1 + covariate2, d)) ) joint_effects(fit) # Parametric bootstrap of the joint NIE boot <- bootstrap_mediation( function(theta) joint_effects(fit, theta)[["nie"]], method = "parametric", mediation_data = fit, n_boot = 500, seed = 1 ) boot@ci_lower boot@ci_upperd <- mediation_demo fit <- extract_mediation( lm(mediator1 ~ treatment + covariate1 + covariate2, d), model_y = lm(outcome_int ~ treatment * mediator1 + mediator2 + covariate1 + covariate2, d), treatment = "treatment", mediator = c("mediator1", "mediator2"), mediator_models = list(lm(mediator2 ~ treatment + mediator1 + covariate1 + covariate2, d)) ) joint_effects(fit) # Parametric bootstrap of the joint NIE boot <- bootstrap_mediation( function(theta) joint_effects(fit, theta)[["nie"]], method = "parametric", mediation_data = fit, n_boot = 500, seed = 1 ) boot@ci_lower boot@ci_upper
S7 class for the joint natural effects of two or more mediators, serial or parallel, when the outcome model may contain treatment-by-mediator product terms (VanderWeele and Vansteelandt 2014). The mediators are treated as one block: the natural indirect effect (NIE) runs through every mediator and every path among them, and the natural direct effect (NDE) is the remainder. medfit computes the effects; causal interpretation is the user's responsibility.
JointMediationData(structure, mediators, treatment, outcome, interactions, a_total, b_paths, theta3, c_prime, cde, nde, nie, total_effect, m_star, estimates, vcov, data, n_obs, converged, source_package)JointMediationData(structure, mediators, treatment, outcome, interactions, a_total, b_paths, theta3, c_prime, cde, nde, nie, total_effect, m_star, estimates, vcov, data, n_obs, converged, source_package)
structure |
Single string, |
mediators |
Character vector of mediator names, in causal order. |
treatment, outcome
|
Single character strings. |
interactions |
Character vector naming the mediators that carry a treatment-by-mediator product in the outcome model (may be empty). |
a_total |
Numeric vector named by |
b_paths |
Numeric vector named by |
theta3 |
Numeric vector named by |
c_prime |
Numeric scalar: treatment main effect on the outcome
( |
cde, nde, nie, total_effect
|
Numeric scalars: controlled direct, natural direct, natural indirect and total effects. |
m_star |
Numeric vector named by |
estimates |
Named numeric vector of parameter estimates. |
vcov |
Square variance-covariance matrix of |
data |
Optional data frame, or NULL. |
n_obs |
Integer number of observations. |
converged |
Logical convergence flag. |
source_package |
Character name of the originating package. |
With outcome model
and the total (propagated) effect of the treatment on
mediator (for a serial chain this includes the paths through earlier
mediators), the unit-contrast effects are
and . is zero for a
mediator without a product term, and is the mean of
mediator under no treatment at the covariate means (propagated down a
serial chain like ). The validator enforces the NIE and CDE
identities, so an object with inconsistent numbers cannot be built.
The NIE is the effect through the mediators as a block: every path from
the treatment through any mediator, including paths among the mediators. It
is not split into per-mediator or per-path pieces, and none of its parts
should be reported as the effect "through M1". For a serial chain it
therefore differs from the default nie() of a SerialMediationData
(a * d * b, the effect through the full chain only): with no product term, the
joint NIE of M1 -> M2 is ,
not . The effects use the unit contrast of a 0/1
treatment (0 to 1).
The joint effects are identified under no-unmeasured-confounding assumptions stated for the whole mediator vector: none for the treatment and outcome, none for the mediators and the outcome, none for the treatment and the mediators, and no mediator-outcome confounder affected by the treatment outside the mediator vector (VanderWeele and Vansteelandt 2014). In a serial chain the earlier mediators are affected by the treatment and confound the later ones; that is allowed precisely because they are part of the vector. Mediator-outcome and treatment-mediator confounders must be controlled for every mediator.
Every model (each mediator model and the outcome model) must carry the same covariates. This is a medfit limitation that keeps the serial-chain algebra and the covariance exact, not a requirement of the method; differing sets error, naming the terms.
The models must be Gaussian with the identity link, unweighted, with an
intercept, fit to the same rows; every mediator must appear in the outcome
model; mediator lists the mediators in causal order.
A product must be written in the outcome formula with : or *. A product
precomputed as a data column (for example XM <- X * M1 added to the
data and then used as Y ~ X + M1 + M2 + XM) cannot be recognized from the
formula: medfit treats it as an ordinary covariate and the effects ignore
the interaction, with no error. Write X * M1 in the formula instead.
The NDE depends on the covariates; it is evaluated at their sample means.
The effects are linear in the covariates, so this equals the sample average
of the per-observation effects. Standard errors use the delta method with
analytic gradients over a stacked-OLS covariance that includes the
correlation between parallel mediator equations. They treat the covariate
means as fixed, so they are conditional on the observed covariates and
slightly understate the uncertainty of a population-average effect. For a
parametric bootstrap use joint_effects() as the statistic.
A JointMediationData S7 object.
VanderWeele, T. J., & Vansteelandt, S. (2014). Mediation analysis with multiple mediators. Epidemiologic Methods, 2(1), 95–115. doi:10.1515/em-2012-0010
# Hand-built parallel object with an X x M2 product (m2* = 0) jmd <- JointMediationData( structure = "parallel", mediators = c("M1", "M2"), treatment = "X", outcome = "Y", interactions = "M2", a_total = c(M1 = 0.5, M2 = 0.3), b_paths = c(M1 = 0.3, M2 = 0.4), theta3 = c(M2 = 0.2), c_prime = 0.1, cde = 0.1, nde = 0.12, nie = 0.33, total_effect = 0.45, m_star = c(M2 = 0), estimates = c(a1 = 0.5, a2 = 0.3, b1 = 0.3, b2 = 0.4, theta3_M2 = 0.2, c_prime = 0.1), vcov = diag(0.01, 6), n_obs = 200L, converged = TRUE, source_package = "medfit" ) jmd@nie # (0.3 + 0) * 0.5 + (0.4 + 0.2) * 0.3 = 0.33# Hand-built parallel object with an X x M2 product (m2* = 0) jmd <- JointMediationData( structure = "parallel", mediators = c("M1", "M2"), treatment = "X", outcome = "Y", interactions = "M2", a_total = c(M1 = 0.5, M2 = 0.3), b_paths = c(M1 = 0.3, M2 = 0.4), theta3 = c(M2 = 0.2), c_prime = 0.1, cde = 0.1, nde = 0.12, nie = 0.33, total_effect = 0.45, m_star = c(M2 = 0), estimates = c(a1 = 0.5, a2 = 0.3, b1 = 0.3, b2 = 0.4, theta3_M2 = 0.2, c_prime = 0.1), vcov = diag(0.01, 6), n_obs = 200L, converged = TRUE, source_package = "medfit" ) jmd@nie # (0.3 + 0) * 0.5 + (0.4 + 0.2) * 0.3 = 0.33
Tests the null hypothesis that an indirect effect, the product of two or more
labeled paths (, or a serial ), is zero in a
model fitted by fit_sem(). The product is zero exactly when one factor is,
so the model is refitted once per path with that path set to zero; each is a
linear equality constraint, so each refit is a single solve. The statistic is
the smallest of the likelihood-ratio statistics, referred to a chi-square
distribution with one degree of freedom (the minimum-of-k LRTs; see the
Methods and Formulas article). The nonlinear constraint
is never solved, because a nonlinear solver can stop at the branch with the
larger statistic.
mbco_sem( object, a, b, n_starts = NULL, control = NULL, fit = lifecycle::deprecated(), paths = NULL )mbco_sem( object, a, b, n_starts = NULL, control = NULL, fit = lifecycle::deprecated(), paths = NULL )
object |
|
a, b
|
Character strings: the labels of the two free regression paths
whose product is the indirect effect (for example |
n_starts, control
|
Passed to the optimizer for the two refits, as in
|
fit |
|
paths |
Character vector of two or more labels: the paths whose product
is the indirect effect ( |
Bounded FIML fits are refused before any constrained refit; their model tests
and MBCO inference are not supported. See fit_sem() for the bounded limits.
A fit made with weights is refused: the weighted discrepancy is
not a likelihood, so its difference is not chi-square. So is a fit that did
not converge, since its discrepancy is the reference both refits are
measured against.
A fit whose ledger is missing, malformed or does not describe the fit, and a refit that is not the null asked
for, are refused with a medfit_error_ledger error: the evidence is never inferred from converged.
A refit that fails the acceptance gate of the optimizer does not stop the
function: its row has converged = FALSE, f and diff_ll NA and the
failure text in note, a warning repeats the text, and statistic and
p.value are NA (never the minimum over the refits that worked, which
would hide the failure). A zero-product branch that cannot be fitted
stops fit_sem() itself (there is no partial fit to test against).
Inequality constraints in the model matter only when they put the null on
the boundary of the parameter space. If a bound or an inequality that
involves the tested parameter is active at its refit (a > 0 with the null
a = 0), the function warns that the statistic is a mixture of chi-squares,
not chi-square(1), and that the p-value is not valid. A bound the null sits
inside (a < 0.2), an inactive inequality and one on another parameter are
not mentioned. A null the constraints rule out (a > 0.3 with the null
a = 0) is an unresolved row whose note says so.
Only a product of labeled paths is tested; a sum of products (several
parallel indirect effects together) is not. If two of the paths are tied by
an equality (a == b), their refits impose the same restriction and their
diff_ll values coincide. The test is conservative: under a null where only
one factor is zero its size is at most the nominal level, and with more than
one zero factor it is smaller still.
A native two-level fit (experimental) is tested at one level: mbco_sem(fit, "a_w", "b_w") for the within
indirect effect and mbco_sem(fit, "a_b", "b_b") for the between one, with each null refit on both levels and the
same clusters. The statistic is the minimum likelihood ratio over the refits with one path set to 0, referred to
chi-square(1); it is conservative at the origin (both paths zero) and is not a test of the between-minus-within
contrast. Paths from different levels, or a label shared by both levels, are refused with class
medfit_error_twolevel_unsupported. A null refit whose solution is inadmissible (a negative level-2 variance)
gives the unresolved result above (statistic NA) with a warning of class medfit_warning_twolevel_inadmissible
that names the refit and the reason; one that does not converge gives the same result with a warning. The
statistic is never the other null's likelihood ratio alone. A warning says when a level-2 variance at a refit is
below 1% of that variable's implied between plus within variance, because the null then lies near the boundary
and chi-square(1) may not hold.
An object of class c("mbco_sem", "htest"): statistic (the
smallest diffLL), parameter (df = 1), p.value, method,
data.name, n_obs, unresolved (TRUE when any refit failed, see
Details), and tests, a data frame with one row per refit (constraint,
the minimized discrepancy f, diff_ll equal to n_obs * (f - fit@f),
converged, and note, the failure text of a refit that could not be
fitted, otherwise empty). It prints with the standard htest layout.
ledger is the version-1 outcome ledger documented in fit_sem(), with children mbco/full and
mbco/null:1, mbco/null:2, ..., in the requested path order. A null refit that is itself a zero-product
union keeps its branch children and notices. A null node's notices also record those raised at the MBCO
boundary, including the union winner's replay, so they can repeat a child's. Every null is attempted even if
another fails, and an unresolved null leaves the root unresolved. A nested union branch that is certified empty
by a literal zero/nonzero contradiction (see the outcome ledger in fit_sem()) is retained as excluded; a null
that is itself infeasible or an all-empty union stays an unresolved row, never a finite LR or a survivor-only
p-value. A null whose optimizer accepted but whose covariance stage explicitly diagnosed non-positive-definite
information or a negative variance (medfit_sem_covariance_error in
fit_sem()) keeps its likelihood after a check that it is the null asked for (same rows, domain and one more
restriction): the row stays resolved, the statistic and p-value are unchanged, a warning names the failure, and the
node has covariance = "failed" with post_fit and the notices. No covariance is invented; a fit that carries
post_fit is refused as object. Other covariance errors propagate unchanged; singular information keeps its
warning and NA covariance. For a native two-level fit (fit_sem(cluster = )) the result adds level
("within" or "between") and n_clusters; a single-level result does not have them.
fit <- fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + treatment", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL ) mbco_sem(fit, "a", "b") # a serial indirect effect, the product of three paths serial <- fit_sem( paste0("mediator1 ~ a*treatment\nmediator2 ~ d*mediator1 + treatment\n", "outcome ~ b*mediator2 + treatment"), data = mediation_demo ) mbco_sem(serial, paths = c("a", "d", "b"))fit <- fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + treatment", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL ) mbco_sem(fit, "a", "b") # a serial indirect effect, the product of three paths serial <- fit_sem( paste0("mediator1 ~ a*treatment\nmediator2 ~ d*mediator1 + treatment\n", "outcome ~ b*mediator2 + treatment"), data = mediation_demo ) mbco_sem(serial, paths = c("a", "d", "b"))
A simplified entry point for mediation analysis. Specify the data and variable names, and get results with minimal configuration.
This is the recommended starting point for most mediation analyses.
For more control over model specifications, use fit_mediation() directly.
med( data, treatment, mediator, outcome, covariates = NULL, boot = FALSE, n_boot = 1000L, seed = NULL, ... )med( data, treatment, mediator, outcome, covariates = NULL, boot = FALSE, n_boot = 1000L, seed = NULL, ... )
data |
A data frame containing all variables |
treatment |
Character: name of treatment (exposure) variable |
mediator |
Character: name of mediator variable |
outcome |
Character: name of outcome variable |
covariates |
Character vector: names of covariates to include (optional, default: none) |
boot |
Logical: compute a bootstrap confidence interval for the
indirect effect? (default: FALSE for speed). Uses a parametric bootstrap
of |
n_boot |
Integer: number of bootstrap samples (default: 1000) |
seed |
Integer: random seed for reproducibility (optional) |
... |
Additional arguments passed to |
med() is designed to be the simplest way to run a mediation analysis.
It constructs the model formulas automatically from variable names.
Fits Gaussian (continuous) mediator and outcome models
No covariates unless specified
No bootstrap unless requested (use boot = TRUE)
After running med(), use:
nie(result): Natural indirect effect
nde(result): Natural direct effect
te(result): Total effect
pm(result): Proportion mediated
quick(result): One-line summary
summary(result): Detailed summary
A MediationData object with mediation results
fit_mediation() for full control, quick() for instant summary,
nie(), nde(), te(), pm() for extracting effects
# mediation_demo is simulated data bundled with medfit; its covariates # confound the mediator-outcome relation, so adjust for them result <- med( data = mediation_demo, treatment = "treatment", mediator = "mediator1", outcome = "outcome", covariates = c("covariate1", "covariate2") ) print(result) # Quick summary quick(result) # With bootstrap CI (slower) result_boot <- med( data = mediation_demo, treatment = "treatment", mediator = "mediator1", outcome = "outcome", covariates = c("covariate1", "covariate2"), boot = TRUE, n_boot = 1000, seed = 42 )# mediation_demo is simulated data bundled with medfit; its covariates # confound the mediator-outcome relation, so adjust for them result <- med( data = mediation_demo, treatment = "treatment", mediator = "mediator1", outcome = "outcome", covariates = c("covariate1", "covariate2") ) print(result) # Quick summary quick(result) # With bootstrap CI (slower) result_boot <- med( data = mediation_demo, treatment = "treatment", mediator = "mediator1", outcome = "outcome", covariates = c("covariate1", "covariate2"), boot = TRUE, n_boot = 1000, seed = 42 )
The model syntax read by fit_sem() and by fit_mediation(engine = "sem") (the sem engine; "native" is the legacy spelling). It is a
lavaan-style core plus two extensions, each marked "medfit extension" below.
One statement per line (or separated by ;).
The general syntax below describes the default listwise path. The separate
bounded fit_sem(missing = "fiml", fixed_x = FALSE) option accepts only
three numeric observed variables in simple mediation with a free or exactly
zero direct path and free intercepts. General constraints, definitions, starts,
bounds, latent variables, products and factors are not supported for FIML.
See fit_sem() for all settings and downstream limits.
| Statement | Meaning |
y ~ x1 + x2 |
regression of y on x1 and x2 |
f =~ y1 + y2 + y3 |
latent variable f measured by the indicators |
y1 ~~ y2 |
covariance; y1 ~~ y1 a variance |
y ~ x + m + x:m |
product term: the observed variable x * m as a regressor (see Product terms) |
y ~ a*x |
label the path a; equal labels are one parameter |
y ~ 0.5*x |
fix the path at 0.5 |
y ~ start(0.3)*x, lower(0)*, upper(1)* |
starting value, lower and upper bound |
a == b, a + b == 1, cp == a*b |
equality constraint on labels; may be nonlinear |
a < b, a > 0 |
inequality constraint; label < number is a bound |
ab := a*b |
defined parameter, with a delta-method standard error |
Constraints may be nonlinear in the labels (cp == a*b, a*b == 0.1,
a*b > 0.1, ab > 0 with ab := a*b), solved with the Jacobian recomputed at
every point; a model with one tries every start and keeps the best accepted
solution, warning when the starts disagree. For such a model the residual
variances that appear in a nonlinear row get a lower bound of 1e-4 times their
natural unit (the variable's variance), because a step to a negative variance is
outside the row's domain; the fit warns when a bound is active, and a residual
variance below that bound (an R-squared above 0.9999) is not fitted exactly. The
model without the nonlinear rows is fitted first: its estimate, and copies of it
with the signs of the regression paths in the rows flipped (a product constraint
cannot change sign along its surface, so each sign pattern is its own branch),
are extra starts, and a feasible reference estimate is never replaced by a worse
accepted fit. Models without a nonlinear row are not affected.
One nonzero-product equality (a*b == 0.1, including a finite nonzero constant multiplier and definition aliases)
has a separate search for each sign of its two distinct free labels. Each required sign branch must be accepted
or excluded by its bounds; an unresolved branch stops the fit. Multiple product equalities, repeated factors
and another constraint involving either factor retain local search. The internal coverage record distinguishes
charted branches from local search, not global from local optima; see fit_sem(). A product of labels set to zero
(a*b == 0, b1*b2*b3 == 0) is instead solved exactly: one linear fit per
factor (a == 0, b == 0), keeping the smallest discrepancy; if a branch cannot be fitted, fit_sem()
stops with a medfit_error_no_convergence error instead of returning the best of the others; the error carries
the outcome ledger of every branch as $ledger (see fit_sem()). A sum of products
(a1*b1 + a2*b2 == 0) has no exact reduction, so it is best-effort: the fit is
the best of the starts tried, a disagreement warning means a better optimum may
exist, and n_starts raises the number of starts. Constrained standard errors use the active tangent space:
observed information includes nonlinear constraint
curvature, while expected information retains its Fisher definition. Active inequalities and bounds, including
solver variance floors, give a local tangent approximation rather than the full boundary sampling distribution.
See fit_sem() and the Methods and Formulas article for details. Labels used in a constraint
or a definition must be defined in the model. A constraint with no free
parameter is an error. Exogenous variables are given free
variances and covariances, as lavaan does with fixed.x = FALSE. An exogenous variable named in any ~~
statement is left out of that pairing: its variance and the covariances the model writes are kept, and its
covariances with the other exogenous variables are 0 unless written (lavaan does the same). fixed_x = TRUE
(or a vector of names) in fit_sem() fixes the moments of the selected ones at the
sample values instead, as fixed.x = TRUE does. Unlike lavaan, which keeps an
explicit x ~~ x or x ~ 1 free under fixed.x = TRUE and lets the degrees of
freedom move, medfit stops with an error when the model has a statement on a
selected variable's moments. Any
intercept statement (y ~ 1, or 1 as a term: y ~ 1 + x) turns the mean structure
on and frees every observed intercept the model does not write, as lavaan does;
y ~ 0*1 then restricts the mean of y to 0 (one degree of freedom), and
meanstructure = TRUE in fit_sem() turns it on without any statement, and a product term
(x:m, see Product terms) always turns it on. A
latent variable's mean is fixed at 0; a free latent ~1 is an error. A repeated 1 for one
variable is an error.
A variable cannot appear on both sides of ~ or =~ (y ~ y), and a path,
a (co)variance or an intercept takes one row: a repeated y ~ x is an error,
not a second row that competes for the label.
A factor, character or logical column named on a regression's right side is replaced by
one 0/1 dummy per non-reference level, named <column>_<level> (g with levels a, b,
c gives g_b and g_c). The reference level is the first factor level; a character
column sorts in the C locale, so the result does not depend on the session locale. A
logical column gives one dummy, <column>_TRUE. Dummies are built from the full column
before rows with missing values are dropped, and a missing value in the factor drops the
row once. tidy() and the parameter table show the dummies by name, and the fit's data
holds the dummies, not the factor.
| Limit | Message (starts with, or contains) |
| An ordered factor | "is an ordered factor" |
| A factor with one level | "has only one level" |
| A level that makes an invalid name | "makes the invalid name" |
| A dummy that is already a column | "is already a column of data" |
A label, fixed value, start or bound on a factor (b*g) |
"expands to" |
A constraint that names the factor (g == 0) |
"is a categorical column" |
| A product of two factors | "both sides are categorical columns" |
| A dummy with no variation in the rows used | "is constant in the rows the fit uses" |
| A factor as the outcome or a mediator | "must be numeric" |
A factor as treatment or mediator in extract_mediation() |
"is a factor in the model" |
A label goes on one dummy: b*g_b works, b*g stops, and a constraint names the label
(b == 0), never the factor. A bootstrap that refits from raw data re-expands the factor in each
resample, so a level missing from a character column's resample changes the reference level.
g:m expands to g_b:m and g_c:m, each a product term under the rules below. fixed_x = "g" fixes every dummy of
g, and a dummy name fixes one. A factor covariate in the mediator equation enters the
four-way covariate means through its dummies; a factor treatment or mediator is not
supported (recode a two-level factor as a 0/1 numeric column), and the four-way route
refuses a g:m product because its effects differ by level.
x:m is the product of two observed variables, written with no spaces, on the
right side of a regression (y ~ x + m + x:m). As in lavaan, the product is an
observed exogenous variable: the engine builds the column x * m from data
after listwise deletion, with a free variance, free covariances with the other
exogenous variables and (the mean structure is on) a free mean. The likelihood
does not change. x:m and m:x are one term, stored and printed under the
canonical name, the two names sorted in the C locale (m:x); the coefficient is
named y~m:x, or by its label (b*x:m labels the whole product).
extract_mediation() finds the treatment-by-mediator product through the same
name and returns the four-way decomposition; centering the mediator moves the
split between the controlled direct effect and the reference interaction, not
the natural effects.
A column in data named m:x or x:m is used only if it equals x * m on the
rows kept; otherwise the fit stops. A product term turns the mean structure on,
and meanstructure = FALSE is then an error. A coefficient equals the
equation-wise least-squares estimate inside the class below, and the sandwich
covariance (se_type = "sandwich") is the standard-error choice, because a
product is not normal; the engine warns once when it is not used. fixed_x
includes the product columns.
| Limit | Message (starts with, or contains) |
A space around : |
"a product ':' needs a variable name on each side, written without spaces (x:m)" |
x:x |
"of a variable with itself is not supported" |
x:m:w |
"a three-way or higher product" |
Product as a left side (x:m ~ y, x:m ~~ y) |
"cannot be a left side" |
Product in =~ |
"is only valid on the right side of a regression" |
Product inside a constraint or := |
"not inside the expression" |
| A latent variable in a product | "products with latent variables are not supported in this version" |
y ~ x:m + m:x (one product twice in an equation) |
"are the same product (canonical name" |
A factor that is the target or downstream of it (y ~ x + x:y) |
"(rule 8: no outcome-derived regressors)" |
| A loop of regressions | "The model is not recursive" |
A residual covariance between outcomes (m1 ~~ m2) |
"cannot have a residual covariance (rule 9)" |
| A label shared by two rows | "is used on more than one parameter" |
A fixed coefficient (0.3*x, 0*x) |
"has a fixed coefficient (rule 9)" |
A bound (lower(0)*x) |
"has a bound (rule 9" |
==, < or > naming a coefficient label |
"names the coefficient label" |
| A fixed, shared or constrained intercept | "(rule 9, intercept clause" |
meanstructure = FALSE with a product |
"meanstructure = FALSE, but the model has a product term" |
A column m:x in data that is not x * m |
"is not the product of" |
| A product input that is not numeric, factor, character or logical (a date) | "needs numeric variables" |
information = "expected", endogenous factor |
"with a product of an endogenous variable" |
anova() or mbco_sem() on such a fit |
"is not available for ... a model with a product term" |
Another product on the treatment or mediator, in extract_mediation() |
"does not support the product" |
AIC() or BIC() on fits with different observed variables |
"different observed variables" |
A product of two categorical columns (g:h) |
"both sides are categorical columns" |
fixed_x naming a column that appears only inside products |
"appears only inside products" |
A product in a modifier (y ~ a:b*x) |
"malformed modifier" |
| Four-way on a native fit without the treatment-by-mediator product | "needs the treatment-by-mediator product" |
extract_mediation(interaction = ) that is not a : product |
"must be the product written with :" |
extract_mediation(interaction = ) that is not treatment by mediator |
"is not the product of the treatment" |
Each error has one of these classes and also the class medfit_error_product; the message is the
reason, so a handler can match the class and a reader the message. The warning that the model test
is not valid (below) has the class medfit_warning_product_modeltest.
| Class | Raised for |
medfit_error_product_syntax |
spacing, x:x, three-way, left side, =~, constraint expression |
medfit_error_product_latent |
a latent variable in a product |
medfit_error_product_duplicate |
one product twice in an equation |
medfit_error_product_outcome_regressor |
rule 8 |
medfit_error_product_recursive |
a loop of regressions |
medfit_error_product_residual_covariance |
residual covariance between outcomes |
medfit_error_product_shared_label |
a label on two rows |
medfit_error_product_fixed_coefficient |
a fixed coefficient |
medfit_error_product_bound |
an inline bound |
medfit_error_product_constraint |
a constraint names a coefficient label |
medfit_error_product_intercept |
a fixed, shared or constrained intercept |
medfit_error_product_meanstructure |
meanstructure = FALSE |
medfit_error_product_column |
a data column that is not the product |
medfit_error_product_nonnumeric |
a non-numeric product input |
medfit_error_product_information |
information = "expected", endogenous factor |
medfit_error_product_modeltest |
anova() or mbco_sem() |
medfit_error_product_observed |
AIC() or BIC() across observed variables |
medfit_error_product_extract |
extract_mediation() refusals
|
Rules 8 and 9 are checked on the parsed table, so they need no data. Rule 8
rejects a regressor built from the outcome (it is correlated with the equation's
error); rule 9 names what is not yet supported with products, and the same
model works without the product. The covariance that completion would add
between two pure outcomes is fixed at 0 in a product model, with one note.
Products of observed variables only: a product with a latent variable, x:x and
three-way products are not supported.
The global chi-square is not valid when a factor of a product is itself a
regression target (the model treats the product as exogenous although it is a
function of that variable) or when the exogenous (co)variance block is
restricted. glance() then reports chi-square, df and p-value as NA with a
note, anova() and mbco_sem() refuse, and expected information is refused for
an endogenous product; logLik(), AIC and BIC stay defined, but those of a fit with a
product are not comparable with those of a fit without one, so AIC(fit_a, fit_b), BIC(), anova() and
mbco_sem() refuse such a pair. A product of
exogenous variables with the block free keeps the ordinary test.
A two-level model of observed variables is written in level: blocks and fitted with
fit_sem(model, data, cluster = "<cluster column>"). level: 1 and level: within open the within block;
level: 2, level: between and level: <cluster column> (as lavaan writes it) open the between block. Every
row belongs to a block, each level appears once, and level 1 comes first. A variable named in level: 1 rows
(a within variable) must also appear in level: 2 rows and must vary within clusters; a variable named only in
level: 2 rows is a cluster-level variable and must be constant within each cluster. The same label at both
levels is an equality constraint across levels. Paths have fixed slopes (no random slopes), and the between part of
each within variable is a latent cluster mean. Level-2 variances are fitted without a floor: an inadmissible
solution (a negative variance or a non-positive-definite implied covariance at either level) is the error
medfit_error_twolevel_inadmissible, which carries the fit (fit) and its outcome ledger (ledger). Unset
fixed_x means FALSE and unset meanstructure means TRUE on this route.
| Limit | Message (starts with, or contains) |
cluster = without level: blocks |
"is given but the model has no level: blocks" |
level: blocks without cluster = |
"the model uses level: blocks but cluster is not given" |
A cluster column that is not in data |
"is not a column of data" |
| Missing values in the cluster column | "has missing values; drop those rows first" |
| Fewer than two clusters | "has fewer than 2 clusters" |
| More than two levels | "the model has more than two levels" |
An unknown level name (level: school with another cluster column) |
"malformed level line" |
A row before the first level: line |
"has statements before the first level: line" |
| A level written twice | "appears more than once" |
level: 2 without level: 1 |
"rows but no level: 1 block" |
A variable named only in level: 1 rows |
"every level-1 variable also needs a level: 2 row" |
| A within variable constant within every cluster | "a cluster-level variable belongs only in level: 2 rows" |
A level: 2-only variable that varies within a cluster |
"must be constant within each cluster" |
Latent variables (=~) |
"latent variables (=~) are not supported in a two-level model" |
A product term (x:m) |
"a product term (x:m) is not supported in a two-level model" |
weights |
"weights are not supported in a two-level model" |
missing = "fiml" |
"missing = ... incomplete rows are dropped (listwise)" |
Nonlinear constraints (not a_w*b_w == a_b*b_b) |
"nonlinear constraints (including a product of labels" |
An explicit fixed_x = TRUE or a name vector |
"a two-level model takes fixed_x = FALSE only" |
meanstructure = FALSE |
"meanstructure = FALSE is refused" |
information = "expected" |
"information = ... is not supported in a two-level model; use" |
| An inadmissible solution | "the two-level solution is inadmissible" |
compare_lr() on the fit |
"is not available for a native two-level fit yet" |
mbco_sem() with paths from different levels |
"tests a product of paths at one level of a two-level fit" |
mbco_sem(fit, "a_w", "b_w") and mbco_sem(fit, "a_b", "b_b") test the indirect effect at one level: the minimum
likelihood ratio over the refits with each labeled path set to 0, referred to chi-square(1), as for a single-level
fit. The test is conservative when both paths are zero, and it is not a test of the between-minus-within
contrast. The result adds level and n_clusters; see mbco_sem().
extract_mediation() on the fit returns a TwoLevelMediationData, the same class as the lavaan two-level adapter
gives, with @source "native". glance() reports df, the total log likelihood, nobs, n_clusters and the
cluster-size range; chisq and p_value are NA with a note, because the engine fits no saturated two-level
model. The model page "Two-level mediation (1-1-1)" has a worked example.
Since medfit 0.15.0 an unset fixed_x means the exogenous observed variables (as lavaan's fixed.x = TRUE) and an
unset meanstructure means TRUE, so every observed intercept is free without a ~1 statement. Against 0.14.x the
parameter count, logLik(), AIC(), BIC() and the parameter names move; the structural estimates and their
standard errors did not move on the cases measured. A variable a ~~ or ~1 statement names is left free (message
class medfit_message_fixed_x_lenient) instead of the error an explicit fixed_x = TRUE gives. To keep the 0.14.x
results, pass fixed_x = FALSE, meanstructure = NULL. The 0.14.x options(medfit.native_defaults) opt-in no longer
has any effect. fit_sem() has the details.
Comma shorthand (medfit extension). A comma-separated left side
expands to one statement per name: y1, y2 ~ x means y1 ~ x and
y2 ~ x. Commas on the right side are not supported; use +.
lavaan reads y1, y2 ~ x differently: it silently keeps only y2.
A model copied between the two packages can therefore change meaning
without an error.
CONSTRAINT(...) (medfit extension). A constraint line may be
written CONSTRAINT(a == b), equivalent to the bare a == b. The keyword
is case-sensitive and starts the statement, one pair of parentheses
encloses the whole constraint, it holds exactly one comparison, and no
text follows the closing parenthesis.
Both sides of a constraint and the right side of a definition are
arithmetic expressions in the labels, with + - * / ^, parentheses, finite
numbers, and the functions exp, log, log10, sqrt, abs, min, max,
sin, cos, tan, pnorm and qnorm. Anything else is
rejected with the construct named.
# Labeled paths and a defined parameter fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + cp*treatment\nab := a*b", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL )@defined # Comma shorthand: one left-hand side per name coef(fit_sem( "mediator1, mediator2 ~ treatment\noutcome ~ mediator1 + mediator2 + treatment", data = mediation_demo ))[1:3] # An equality constraint, written with the CONSTRAINT() keyword coef(fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + treatment\nCONSTRAINT(a == b)", data = mediation_demo ))[c("a", "b")]# Labeled paths and a defined parameter fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + cp*treatment\nab := a*b", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL )@defined # Comma shorthand: one left-hand side per name coef(fit_sem( "mediator1, mediator2 ~ treatment\noutcome ~ mediator1 + mediator2 + treatment", data = mediation_demo ))[1:3] # An equality constraint, written with the CONSTRAINT() keyword coef(fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + treatment\nCONSTRAINT(a == b)", data = mediation_demo ))[c("a", "b")]
A simulated dataset for demonstrating medfit's simple, serial, parallel, and treatment-by-mediator interaction workflows with one running example. The data are simulated: they describe no real study, population, or finding.
mediation_demomediation_demo
A data frame with 400 rows and 8 variables:
Integer, 0/1. Randomized treatment assignment.
Numeric. First mediator; affected by treatment.
Numeric. Serial mediator; affected by treatment and
mediator1.
Numeric. Parallel mediator; affected by treatment only.
Numeric. Continuous confounder of the mediator-outcome relations.
Integer, 0/1. Binary confounder of the mediator-outcome relations.
Numeric. Outcome with no product terms; use it for the simple, serial, and parallel models.
Numeric. outcome plus a treatment by mediator1
interaction; use it only for models with that interaction: the four-way
decomposition with a single mediator, or joint effects with several
mediators (JointMediationData).
The variables follow a fictional workplace-training story, used only so the
examples read concretely: randomized assignment to a training program
(treatment) raises skill confidence (mediator1), which builds task
mastery (mediator2); the assignment also triggers supervisor check-ins
(mediator3); outcome is job performance.
All errors are independent standard normal. The generating equations are
and outcome_int adds .
Because the covariates confound the mediator-outcome relations, every model
should adjust for both covariate1 and covariate2. A model that omits a
downstream mediator estimates reduced-form coefficients rather than the
structural ones above: for example, the simple model outcome ~ treatment + mediator1 + covariates has limiting coefficients 0.55 for mediator1 and
0.41 for treatment. For a serial model, include mediator1 in the outcome
model as well; the serial indirect effect
is then the effect through the chain mediator1 to mediator2 only.
Simulated for package demonstration; not drawn from or representing
any real study. The generating script is data-raw/mediation_demo.R in
the package source repository.
data(mediation_demo) str(mediation_demo) # Simple mediation, adjusting for both covariates fit <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) nie(fit)data(mediation_demo) str(mediation_demo) # Simple mediation, adjusting for both covariates fit <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) nie(fit)
S7 class containing standardized mediation model structure, including path coefficients, parameter estimates, variance-covariance matrix, and metadata.
MediationData(a_path, b_path, c_prime, estimates, vcov, sigma_m, sigma_y, family_m, family_y, treatment, mediator, outcome, mediator_predictors, outcome_predictors, data, n_obs, converged, source_package)MediationData(a_path, b_path, c_prime, estimates, vcov, sigma_m, sigma_y, family_m, family_y, treatment, mediator, outcome, mediator_predictors, outcome_predictors, data, n_obs, converged, source_package)
a_path |
Numeric scalar: effect of treatment on mediator (a path) |
b_path |
Numeric scalar: effect of mediator on outcome (b path) |
c_prime |
Numeric scalar: direct effect of treatment on outcome (c' path) |
estimates |
Numeric vector: all parameter estimates |
vcov |
Numeric matrix: variance-covariance matrix of estimates |
sigma_m |
Numeric scalar or NULL: residual SD for mediator model |
sigma_y |
Numeric scalar or NULL: residual SD for outcome model |
family_m |
GLM |
family_y |
GLM |
treatment |
Character scalar: name of treatment variable |
mediator |
Character scalar: name of mediator variable |
outcome |
Character scalar: name of outcome variable |
mediator_predictors |
Character vector: predictor names in mediator model |
outcome_predictors |
Character vector: predictor names in outcome model |
data |
Data frame or NULL: original data |
n_obs |
Integer scalar: number of observations |
converged |
Logical scalar: whether models converged |
source_package |
Character scalar: package/engine used for fitting |
This class provides a unified container for mediation model information extracted from various model types (lm, glm, lavaan, etc.). It ensures consistency across the mediation analysis ecosystem.
The class includes comprehensive validation to ensure data integrity.
A MediationData S7 object
# Create a MediationData object med_data <- MediationData( a_path = 0.5, b_path = 0.3, c_prime = 0.2, estimates = c(0.5, 0.3, 0.2), vcov = diag(3) * 0.01, sigma_m = 1.0, sigma_y = 1.2, treatment = "X", mediator = "M", outcome = "Y", mediator_predictors = "X", outcome_predictors = c("X", "M"), data = NULL, n_obs = 100L, converged = TRUE, source_package = "stats" )# Create a MediationData object med_data <- MediationData( a_path = 0.5, b_path = 0.3, c_prime = 0.2, estimates = c(0.5, 0.3, 0.2), vcov = diag(3) * 0.01, sigma_m = 1.0, sigma_y = 1.2, treatment = "X", mediator = "M", outcome = "Y", mediator_predictors = "X", outcome_predictors = c("X", "M"), data = NULL, n_obs = 100L, converged = TRUE, source_package = "stats" )
Extract the natural direct effect from a mediation analysis result. The NDE represents the effect of treatment on outcome that does NOT operate through the mediator(s).
nde(x, ...)nde(x, ...)
x |
A MediationData, SerialMediationData, ParallelMediationData, InteractionMediationData, or JointMediationData object. |
... |
Additional arguments passed to methods. For a |
Without a treatment-by-mediator product (MediationData,
SerialMediationData, ParallelMediationData) the NDE is the direct-path
coefficient, . With a product it also depends on the
mediator's mean under no treatment:
InteractionMediationData: .
JointMediationData: , with
the mean of mediator under no treatment at the covariate means.
A numeric scalar of class mediation_effect carrying a type
attribute.
nie(), te(), pm(), paths(), decompose()
med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) nde(med_data)med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) nde(med_data)
Extract the natural indirect effect from a mediation analysis result. The NIE represents the effect of treatment on outcome that operates through the mediator(s).
nie(x, ...)nie(x, ...)
x |
A MediationData, SerialMediationData, ParallelMediationData,
InteractionMediationData, JointMediationData, or BootstrapResult
object. For a |
... |
Additional arguments passed to methods. For a
|
The effects are for a unit contrast of the treatment. By class:
MediationData: .
SerialMediationData: with type = "chain" (the default), the
effect through the full chain only,
;
with type = "total", the total indirect effect, the sum over every
treatment-to-outcome path through at least one mediator (including paths
that skip a mediator, such as X -> M2 -> Y), which equals
te(x) - nde(x).
ParallelMediationData: .
InteractionMediationData: .
JointMediationData: the joint NIE through all the mediators,
.
TwoLevelParallelMediationData (experimental): per level, the
specific effects and the total
; the contrast is within minus
between and descriptive, NA for a mediator whose a and b are both
equated across levels; the total contrast is NA only when every
mediator's contrast is.
The "Methods and Formulas" article on the package website gives the full formulas.
A numeric scalar of class mediation_effect carrying a type
attribute (a named vector, one value per mediator, for
TwoLevelParallelMediationData with type = "specific"). For intervals
use confint() or bootstrap_mediation().
nde(), te(), pm(), paths(), decompose()
med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) nie(med_data)med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) nie(med_data)
S7 class for parallel mediation, where a treatment affects an outcome
through two or more independent mediators operating in parallel
( for
). The total indirect effect is the sum
of the per-mediator products, .
This complements MediationData (simple) and SerialMediationData
(serial chains).
ParallelMediationData(a_paths, b_paths, c_prime, estimates, vcov, sigma_mediators, sigma_y, treatment, mediators, outcome, mediator_predictors, outcome_predictors, data, n_obs, converged, source_package)ParallelMediationData(a_paths, b_paths, c_prime, estimates, vcov, sigma_mediators, sigma_y, treatment, mediators, outcome, mediator_predictors, outcome_predictors, data, n_obs, converged, source_package)
a_paths |
Numeric vector: treatment -> mediator effects
|
b_paths |
Numeric vector: mediator -> outcome effects
|
c_prime |
Numeric scalar: direct effect |
estimates |
Numeric vector of all parameter estimates. |
vcov |
Square variance-covariance matrix of |
sigma_mediators |
Optional numeric vector of mediator residual SDs (length k), or NULL. |
sigma_y |
Optional numeric scalar outcome residual SD, or NULL. |
treatment, outcome
|
Single character strings naming the treatment / outcome. |
mediators |
Character vector of mediator names (length k, unique). |
mediator_predictors |
List of predictor-name vectors, one per mediator. |
outcome_predictors |
Character vector of outcome-model predictor names. |
data |
Optional data frame, or NULL. |
n_obs |
Integer number of observations. |
converged |
Logical convergence flag. |
source_package |
Character name of the originating package. |
A ParallelMediationData S7 object.
pmd <- ParallelMediationData( a_paths = c(0.5, 0.4), b_paths = c(0.6, 0.3), c_prime = 0.2, estimates = c(0.5, 0.4, 0.6, 0.3, 0.2), vcov = diag(0.01, 5), treatment = "X", mediators = c("M1", "M2"), outcome = "Y", mediator_predictors = list("X", "X"), outcome_predictors = c("X", "M1", "M2"), n_obs = 200L, converged = TRUE, source_package = "medfit" ) nie(pmd) # total indirect effect: sum(a_j * b_j) = 0.42 paths(pmd) # a1, b1, a2, b2, c_primepmd <- ParallelMediationData( a_paths = c(0.5, 0.4), b_paths = c(0.6, 0.3), c_prime = 0.2, estimates = c(0.5, 0.4, 0.6, 0.3, 0.2), vcov = diag(0.01, 5), treatment = "X", mediators = c("M1", "M2"), outcome = "Y", mediator_predictors = list("X", "X"), outcome_predictors = c("X", "M1", "M2"), n_obs = 200L, converged = TRUE, source_package = "medfit" ) nie(pmd) # total indirect effect: sum(a_j * b_j) = 0.42 paths(pmd) # a1, b1, a2, b2, c_prime
Extract all path coefficients from a mediation analysis result.
paths(x, ...)paths(x, ...)
x |
A MediationData, SerialMediationData, ParallelMediationData, InteractionMediationData, or JointMediationData object. |
... |
Additional arguments passed to methods |
For simple mediation (MediationData):
a: Treatment -> Mediator (X -> M)
b: Mediator -> Outcome (M -> Y | X)
c_prime: Direct effect (X -> Y | M)
For serial mediation (SerialMediationData):
a: Treatment -> First mediator
d (two mediators) or d21, d32, ...: Mediator-to-mediator paths
b: Last mediator -> Outcome
c_prime: Direct effect
For parallel mediation (ParallelMediationData): a1, b1, a2, b2,
..., c_prime.
For InteractionMediationData: a, b, c_prime, and theta3 (the
treatment-by-mediator coefficient).
For JointMediationData: the raw coefficients a1..aK, dij, b1..bK,
theta3_<mediator>, and c_prime. The a paths here are the raw
coefficients, not the propagated a* values used by the effects.
A named numeric vector of path coefficients
med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) paths(med_data)med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) paths(med_data)
Extract the proportion of the total effect that is mediated (operates through the mediator(s)).
pm(x, ...)pm(x, ...)
x |
A MediationData, SerialMediationData, ParallelMediationData, InteractionMediationData, or JointMediationData object. |
... |
Additional arguments passed to methods. For a |
For serial mediation (SerialMediationData) the numerator is the total
indirect effect, nie(x, type = "total"), and the denominator the full
total effect from te().
The proportion mediated can be:
Between 0 and 1: Normal mediation
Greater than 1: Suppression (direct and indirect effects have opposite signs)
Negative: Inconsistent mediation
The ratio has no delta-method standard error in medfit; bootstrap it with
bootstrap_mediation() for supported objects. pm() and bootstrap inference
refuse data extracted from bounded FIML fits; see fit_sem().
A numeric scalar of class mediation_effect, usually between 0 and
1 (negative or greater than 1 in cases of suppression effects), or
NA with a warning when the total effect is numerically zero.
med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) pm(med_data)med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) pm(med_data)
Print a one-line summary of mediation results, perfect for quick checks or a compact report line.
quick(x, digits = 3, ...)quick(x, digits = 3, ...)
x |
A MediationData or SerialMediationData object (or result from
|
digits |
Integer: number of significant digits (default: 3) |
... |
Additional arguments (ignored) |
quick() refuses data extracted from bounded FIML fits. Use print(),
summary() or tidy() for those objects; see fit_sem() for their limits.
Prints a compact one-line summary showing:
NIE (Natural Indirect Effect) with CI if available
NDE (Natural Direct Effect)
Proportion Mediated (PM)
If bootstrap results are available (from med(..., boot = TRUE)),
confidence intervals are shown for NIE.
Invisibly returns x
result <- med( data = mediation_demo, treatment = "treatment", mediator = "mediator1", outcome = "outcome", covariates = c("covariate1", "covariate2") ) # One-line summary quick(result)result <- med( data = mediation_demo, treatment = "treatment", mediator = "mediator1", outcome = "outcome", covariates = c("covariate1", "covariate2") ) # One-line summary quick(result)
Builds the control argument of fit_sem() and mbco_sem() (and of
fit_mediation() with engine = "sem", through
engine_args = list(control = )). A plain list is still accepted there and
keeps its earlier meaning: every entry is an nloptr option, exactly as
sem_control(nloptr = <list>).
sem_control(optimizer = "auto", n_starts = NULL, nloptr = list())sem_control(optimizer = "auto", n_starts = NULL, nloptr = list())
optimizer |
Character: |
n_starts |
Positive integer or |
nloptr |
Named list of options passed to |
The resolved search protocol of a fit (estimator, the algorithm that ran,
the number of starts and the start rule) is recorded in
fit@diagnostics$protocol.
A top-level n_starts that differs in value from sem_control(n_starts = ),
or optimizer = "slsqp" with a different nloptr algorithm, stops with
an error of class medfit_error_sem_control_conflict.
An object of class medfit_sem_control: a list with elements
optimizer, n_starts and nloptr.
ctl <- sem_control(nloptr = list(maxeval = 20000L)) ctl fit <- fit_sem("mediator1 ~ a*treatment\noutcome ~ b*mediator1 + cp*treatment", mediation_demo, control = ctl) fit@diagnostics$protocolctl <- sem_control(nloptr = list(maxeval = 20000L)) ctl fit <- fit_sem("mediator1 ~ a*treatment\noutcome ~ b*mediator1 + cp*treatment", mediation_demo, control = ctl) fit@diagnostics$protocol
S7 class holding a structural equation model fitted by the sem engine,
medfit's own maximum-likelihood SEM fitter (see fit_sem()): estimates, their covariance,
the parameter table, defined parameters (:=) with delta-method standard
errors, and the diagnostics of the optimizer.
SEMFit(theta, vcov, table, defined, f, n_obs, n_dropped, df, information, converged, data, model, diagnostics, internals, call)SEMFit(theta, vcov, table, defined, f, n_obs, n_dropped, df, information, converged, data, model, diagnostics, internals, call)
theta |
Named numeric vector: free-parameter estimates |
vcov |
Numeric matrix: covariance of |
table |
Data frame: one row per parameter-table row with |
defined |
Data frame of |
f |
Numeric scalar: minimized maximum-likelihood discrepancy; |
n_obs, n_dropped
|
Integer scalars: rows used and rows dropped for missing values (listwise deletion by default; only all-empty model rows for FIML) |
df |
Numeric scalar: model-test degrees of freedom; |
information |
Character scalar: |
converged |
Logical scalar: the acceptance gate passed. |
data |
Data frame: the rows used, on the model's observed variables |
model |
Character scalar: the model syntax |
diagnostics |
List: the versioned outcome |
internals |
List: the internal index objects the engine needs
( |
call |
Call object or NULL: the call that produced the fit |
A SEMFit S7 object
fit <- fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + treatment\nab := a*b", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL ) fit@table[fit@table$op == "~", c("lhs", "rhs", "est", "se")] fit@defined fit@convergedfit <- fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + treatment\nab := a*b", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL ) fit@table[fit@table$op == "~", c("lhs", "rhs", "est", "se")] fit@defined fit@converged
Accessors and display for a SEMFit returned by fit_sem().
These are S7 methods for the base generics, so there is no separate usage
section; each takes the SEMFit object as its first argument (object, or
x for print()) and ignores further arguments in ....
coef() returns the free-parameter estimates, named as lavaan names its
coefficients: the label when the row has one, else lhs op rhs without
spaces ("M~X", "X~~X", "eta=~m2"). vcov(), tidy() and the
accessors extract_mediation() reads use the same names.
vcov() returns their covariance, projected onto the active constraints
and bounds; rows for a pinned parameter are zero.
nobs() returns rows used after listwise deletion by default; for bounded
FIML it counts rows with any observed model variable.
For listwise fits, logLik() returns the log likelihood with the means free; its df
attribute counts the free parameters plus one mean per observed variable
that is not fixed. With fixed_x it is conditional on the fixed variables
(lavaan's value), so AIC() and BIC() of fits with different fixed sets
or data stop instead of ranking them; the same holds for anova() and
mbco_sem(). A fixed_x fit also lists its fixed moments after the free
parameters in tidy() (fixed_x column TRUE, no standard error) and in
summary(); coef() and vcov() leave them out. print() and summary()
say what the log likelihood is conditional on.
Bounded FIML. Raw total Gaussian logLik() includes constants; its df
attribute counts the free parameters (8 or 9; 13 or 14 with one covariate; 19 or 20 with two). AIC/BIC use
retained N.
f and model-test df are unavailable: no saturated missing-data reference
fit is performed. SEMFit glance() retains likelihood/IC fields with unavailable
model tests and a reason. Extracted-object glance() is instead deferred.
Normal parameter intervals, extraction and unstandardized path/effect
summaries are supported; bootstrap, profiles, MBCO and fit tests refuse.
See fit_sem() for settings, provenance and dispatch boundaries.
Model test with a product term. The global chi-square is not valid when
a factor of a product term (x:m) is itself a regression target, because
the model then treats the product as exogenous although it is a function of
m; nor when the variances and covariances of the exogenous variables
(every product included) are restricted (fixed, labeled, bounded, or left
out because a ~~ statement names one of them; a fixed_x moment is not a
restriction). fit_sem() then warns once, glance() reports chisq, df
and p_value as NA (with the reason in attr(, "note")), summary()
prints the reason, and anova() and mbco_sem() stop naming the product.
The coefficients equal equation-wise least squares and the sandwich
standard errors are unaffected, but only with observed information:
fit_sem(information = "expected") stops for a product with an endogenous
factor, because its standard errors (the sandwich built on them included)
are wrong there. fit_sem() also warns once for any product term fitted
without se_type = "sandwich". logLik(), AIC() and BIC() stay
defined, but the values of a fit with a product are not comparable with
those of a fit without one (the product column enters the likelihood), so
AIC(fit_a, fit_b), BIC(), anova() and mbco_sem() refuse such a
pair.
anova() tests nested fits by the likelihood-ratio statistic
on the difference in model degrees
of freedom: anova(fit_full, fit_restricted), or several fits, each tested
against the one with fewer degrees of freedom. It compares fits that were
already made and never refits. The fits must use the same observed
variables and rows, and neither may have sampling weights or have failed to
converge. Nesting is checked by parameter-table inclusion: the restricted
fit may free no parameter the other leaves out and must have more degrees
of freedom. That is necessary for nesting, not sufficient; whether two
models are nested in substance is the analyst's responsibility. A
restricted fit with a smaller discrepancy than the model it restricts is an
error (one fit is not at its global optimum). When a bound or an
inequality on a tested parameter is active at the restricted fit, the null
lies on the boundary of the parameter space, the statistic is a mixture of
chi-squares, and the function warns that the p-value is not valid, as
mbco_sem() does. The p-value is still returned; the logical attribute
attr(x, "p_valid") (named by row, NA for the first) is FALSE for each
such row, so code can read the verdict without parsing the warning. It
belongs to the object anova() returns: subsetting it with [ or binding
rows drops the attribute. Returns
an object of class "anova" with columns Df,
Chisq (n times the discrepancy), Chisq diff, Df diff and
Pr(>Chisq). Name rows by naming the arguments after the first.
confint() returns Wald (normal) intervals for the free parameters and
the defined parameters, confint(fit, parm, level = 0.95); parm takes
names or positions. A parameter pinned by a constraint or sitting on a
bound has a zero standard error and a point interval.
tidy() returns one row per free parameter, then the moments fixed_x fixed (a fixed_x
column, only when something is fixed), then per defined parameter:
term (the name coef() uses), op (the operator of its row in the
parameter table, := for a defined parameter), estimate, std_error,
statistic (the z value, NA when the standard error is zero),
p_value (two-sided), and with conf_int = TRUE the interval
confint() returns (conf_low, conf_high, at conf_level). type
is "all" (default), "parameters" or "defined".
glance() returns one row: nobs, npar (free parameters after the
equality constraints), df, chisq (n times the minimized discrepancy,
the test of the model against the saturated one), p_value (NA when
df is 0), logLik, AIC and BIC (with the means free, as logLik()
defines them) and fixed_x, the names the fit holds at their sample moments,
comma-joined ("" when none). A fit with estimator = "MLR" adds chisq_scaled, scaling_factor and
p_value_scaled after p_value: the Yuan-Bentler scaled chi-square (see fit_sem()).
A fit with sampling weights has no likelihood, so its
chisq, p_value, logLik, AIC and BIC are NA. There is no
converged column: fit_sem() returns a fit only for a solution that
passed the acceptance gate and errors otherwise, so @converged is TRUE
for every SEMFit and would carry no information.
summary() collects the estimates, standard errors and z statistics, the
defined parameters, and the diagnostics: rows dropped, information type,
degrees of freedom, retries, stationarity, optimum status, active bounds and constraints,
improper-solution flags and the multiplier sign check.
coef() a named numeric vector; vcov() a matrix; nobs() an
integer; logLik() an object of class "logLik"; summary() an object of
class "summary_SEMFit"; print() invisibly returns its argument.
fit <- fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + treatment\nab := a*b", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL ) coef(fit) vcov(fit)[c("a", "b"), c("a", "b")] nobs(fit) summary(fit) generics::tidy(fit, conf_int = TRUE) generics::glance(fit) # a likelihood-ratio test of b = 0 against the fitted model fit0 <- fit_sem(paste0(fit@model, "\nb == 0"), data = mediation_demo, fixed_x = FALSE, meanstructure = NULL) anova(fit, fit0)fit <- fit_sem( "mediator1 ~ a*treatment\noutcome ~ b*mediator1 + treatment\nab := a*b", data = mediation_demo, fixed_x = FALSE, meanstructure = NULL ) coef(fit) vcov(fit)[c("a", "b"), c("a", "b")] nobs(fit) summary(fit) generics::tidy(fit, conf_int = TRUE) generics::glance(fit) # a likelihood-ratio test of b = 0 against the fitted model fit0 <- fit_sem(paste0(fit@model, "\nb == 0"), data = mediation_demo, fixed_x = FALSE, meanstructure = NULL) anova(fit, fit0)
S7 class for serial mediation models where the effect flows through multiple mediators in sequence: X -> M1 -> M2 -> ... -> Mk -> Y.
This class supports serial mediation chains of any length, from simple two-mediator models (product-of-three: a * d * b) to complex chains with many mediators (product-of-k).
SerialMediationData(a_path, d_path, b_path, c_prime, estimates, vcov, sigma_mediators, sigma_y, treatment, mediators, outcome, mediator_predictors, outcome_predictors, data, n_obs, converged, source_package)SerialMediationData(a_path, d_path, b_path, c_prime, estimates, vcov, sigma_mediators, sigma_y, treatment, mediators, outcome, mediator_predictors, outcome_predictors, data, n_obs, converged, source_package)
a_path |
Numeric scalar: effect of treatment on first mediator (X -> M1) |
d_path |
Numeric vector: sequential mediator-to-mediator effects
|
b_path |
Numeric scalar: effect of last mediator on outcome (Mk -> Y) |
c_prime |
Numeric scalar: direct effect of treatment on outcome (X -> Y) |
estimates |
Numeric vector: all parameter estimates |
vcov |
Numeric matrix: variance-covariance matrix of estimates |
sigma_mediators |
Numeric vector or NULL: residual SDs for mediator models. Length should match number of mediators. First element is residual SD for M1 model, second element for M2 model, etc. |
sigma_y |
Numeric scalar or NULL: residual SD for outcome model |
treatment |
Character scalar: name of treatment variable |
mediators |
Character vector: names of mediators in sequential order. First element is M1, second element is M2, etc. |
outcome |
Character scalar: name of outcome variable |
mediator_predictors |
List of character vectors: predictor names for each mediator model. First list element contains predictors for M1 (typically just "X"), second element contains predictors for M2 (typically c("X", "M1")), etc. |
outcome_predictors |
Character vector: predictor names in outcome model |
data |
Data frame or NULL: original data |
n_obs |
Integer scalar: number of observations |
converged |
Logical scalar: whether all models converged |
source_package |
Character scalar: package/engine used for fitting |
Serial mediation models the indirect effect flowing through a sequence of mediators. The indirect effect through the full chain is the product of the chain's path coefficients:
2 mediators (product-of-three): Indirect = a * d * b
3 mediators (product-of-four): Indirect = a * d21 * d32 * b
k mediators (product-of-k+1): Indirect = a * d21 * d32 * ... * d(k,k-1) * b
This is the effect through the chain only, and what nie() returns by
default. Paths that skip a mediator (X -> M2, M1 -> Y, ...) are not part
of it; nie(x, type = "total") adds them, and te() is the full total
effect over every path, so pm() is the total indirect share of it. With
treatment-by-mediator products in the outcome model, extract_mediation()
returns a JointMediationData object with the effect through all the
mediators together.
a: Treatment -> First mediator (X -> M1)
d21: First -> Second mediator (M1 -> M2)
d32: Second -> Third mediator (M2 -> M3)
dji: Previous mediator -> Current mediator
b: Last mediator -> Outcome (Mk -> Y)
c': Direct effect (X -> Y, controlling for all mediators)
This class is designed to handle serial chains of any length:
Minimal case: 2 mediators (length(d_path) = 1)
No upper limit on chain length
Validator ensures consistency between mediators and paths
A SerialMediationData S7 object
# Two-mediator serial mediation (X -> M1 -> M2 -> Y) # Product-of-three: a * d * b serial_data <- SerialMediationData( a_path = 0.5, # X -> M1 d_path = 0.4, # M1 -> M2 (scalar for 2 mediators) b_path = 0.3, # M2 -> Y c_prime = 0.1, # X -> Y (direct) estimates = c(0.5, 0.4, 0.3, 0.1), vcov = diag(4) * 0.01, sigma_mediators = c(1.0, 1.1), # SD for M1, M2 models sigma_y = 1.2, treatment = "X", mediators = c("M1", "M2"), outcome = "Y", mediator_predictors = list( c("X"), # M1 ~ X c("X", "M1") # M2 ~ X + M1 ), outcome_predictors = c("X", "M1", "M2"), # Y ~ X + M1 + M2 data = NULL, n_obs = 100L, converged = TRUE, source_package = "lavaan" ) # Three-mediator serial mediation (X -> M1 -> M2 -> M3 -> Y) # Product-of-four: a * d21 * d32 * b serial_data_3 <- SerialMediationData( a_path = 0.5, # X -> M1 d_path = c(0.4, 0.35), # M1 -> M2, M2 -> M3 (vector for 3 mediators) b_path = 0.3, # M3 -> Y c_prime = 0.1, estimates = c(0.5, 0.4, 0.35, 0.3, 0.1), vcov = diag(5) * 0.01, sigma_mediators = c(1.0, 1.1, 1.05), # SD for M1, M2, M3 models sigma_y = 1.2, treatment = "X", mediators = c("M1", "M2", "M3"), outcome = "Y", mediator_predictors = list( c("X"), # M1 ~ X c("X", "M1"), # M2 ~ X + M1 c("X", "M1", "M2") # M3 ~ X + M1 + M2 ), outcome_predictors = c("X", "M1", "M2", "M3"), data = NULL, n_obs = 100L, converged = TRUE, source_package = "lavaan" )# Two-mediator serial mediation (X -> M1 -> M2 -> Y) # Product-of-three: a * d * b serial_data <- SerialMediationData( a_path = 0.5, # X -> M1 d_path = 0.4, # M1 -> M2 (scalar for 2 mediators) b_path = 0.3, # M2 -> Y c_prime = 0.1, # X -> Y (direct) estimates = c(0.5, 0.4, 0.3, 0.1), vcov = diag(4) * 0.01, sigma_mediators = c(1.0, 1.1), # SD for M1, M2 models sigma_y = 1.2, treatment = "X", mediators = c("M1", "M2"), outcome = "Y", mediator_predictors = list( c("X"), # M1 ~ X c("X", "M1") # M2 ~ X + M1 ), outcome_predictors = c("X", "M1", "M2"), # Y ~ X + M1 + M2 data = NULL, n_obs = 100L, converged = TRUE, source_package = "lavaan" ) # Three-mediator serial mediation (X -> M1 -> M2 -> M3 -> Y) # Product-of-four: a * d21 * d32 * b serial_data_3 <- SerialMediationData( a_path = 0.5, # X -> M1 d_path = c(0.4, 0.35), # M1 -> M2, M2 -> M3 (vector for 3 mediators) b_path = 0.3, # M3 -> Y c_prime = 0.1, estimates = c(0.5, 0.4, 0.35, 0.3, 0.1), vcov = diag(5) * 0.01, sigma_mediators = c(1.0, 1.1, 1.05), # SD for M1, M2, M3 models sigma_y = 1.2, treatment = "X", mediators = c("M1", "M2", "M3"), outcome = "Y", mediator_predictors = list( c("X"), # M1 ~ X c("X", "M1"), # M2 ~ X + M1 c("X", "M1", "M2") # M3 ~ X + M1 + M2 ), outcome_predictors = c("X", "M1", "M2", "M3"), data = NULL, n_obs = 100L, converged = TRUE, source_package = "lavaan" )
Extract the total effect from a mediation analysis result: the sum of the indirect and direct effects.
te(x, ...)te(x, ...)
x |
A MediationData, SerialMediationData, ParallelMediationData, InteractionMediationData, or JointMediationData object. |
... |
Additional arguments passed to methods. For a |
For a SerialMediationData object the total effect is the sum over
every directed X-to-Y path in the fitted models: the direct path, the full
chain, and every path that skips a mediator (for two mediators,
).
With the same covariates in every equation and linear models this equals
the treatment coefficient of the outcome regressed on the treatment and
covariates alone. A path missing from its model counts as zero; when a path
is in a model but its coefficient was not recorded (a hand-built object),
te() returns NA with a warning. For glm fits with a non-identity link
the sum of path products is on the linear-predictor scale, as for
MediationData.
A numeric scalar of class mediation_effect carrying a type
attribute.
nie(), nde(), pm(), paths(), decompose()
med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) te(med_data) # Verify: TE = NIE + NDE nie(med_data) + nde(med_data)med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) te(med_data) # Verify: TE = NIE + NDE nie(med_data) + nde(med_data)
tidy() converts a mediation data object or a BootstrapResult into a
tidy tibble, one row per path coefficient or effect. glance() returns a
one-row summary. The base generics stats::coef(), stats::vcov(),
stats::confint(), and stats::nobs() also have methods for every
mediation class; see Details.
tidy_s7_object(x, ...) glance_s7_object(x, ...)tidy_s7_object(x, ...) glance_s7_object(x, ...)
x |
A MediationData, SerialMediationData, ParallelMediationData,
InteractionMediationData, JointMediationData, ClusterMediationData,
TwoLevelMediationData, BootstrapResult, or SEMFit object (a |
... |
Passed to the class method: |
Path standard errors are the square roots of the diagonal of @vcov.
Effect standard errors (NIE, NDE, TE, and for interaction objects the
four-way components) use the delta method over the full @vcov, the same
computation as confint(parm = "effects"), so tidy(conf_int = TRUE)
reproduces confint() exactly. Intervals are normal approximations
(); the sampling
distribution of a product of coefficients is skewed, so for inference on
indirect effects prefer bootstrap_mediation(). tidy() raises no warning
about this; confint() does.
For a serial chain fitted as separate lm/glm regressions, @vcov has zero
covariances between equations, and the effect standard errors inherit that.
The proportion mediated (reported by glance()) has no standard error: it
is a ratio whose delta-method standard error is unstable when the total
effect is near zero, so bootstrap it instead.
For data extracted from a bounded FIML fit, tidy(), coefficients, covariance
and normal path/effect intervals are supported, but glance(), pm() and
bootstrap intervals refuse. The bootstrap recommendations above apply only
to other supported objects. glance() on the source SEMFit instead retains
likelihood and information-criterion fields, with model tests unavailable.
See fit_sem() for the bounded model, assumptions and inference limits.
coef(object, type = "paths"): the path coefficients; type = "effects"
gives the effects, "all" both, and for interaction objects
"components" gives the four-way components.
vcov(object): the stored @vcov, covering every entry of @estimates.
confint(object, parm = "paths", level = 0.95): normal intervals for the
paths, or with parm = "effects" for the effects using the delta-method
standard errors above (interaction objects also accept
parm = "components"). With parm = "effects" it warns that the normal
approximation may be inaccurate for an indirect effect.
nobs(object): the number of observations.
tidy(): a tibble (a data frame if tibble is not installed) with
columns term, estimate, std_error, and, when conf_int = TRUE,
conf_low and conf_high.
glance(): a one-row tibble with nie, nde, te, pm, nobs, and
converged; interaction objects add interaction and m_star, and
joint objects add cde, structure, n_mediators, interactions, and
m_star.
bootstrap_mediation(), MediationData
med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) # tidy() is the generic from the generics package (also re-exported by broom) generics::tidy(med_data) generics::tidy(med_data, type = "effects", conf_int = TRUE)med_data <- fit_mediation( formula_y = outcome ~ treatment + mediator1 + covariate1 + covariate2, formula_m = mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo, treatment = "treatment", mediator = "mediator1" ) # tidy() is the generic from the generics package (also re-exported by broom) generics::tidy(med_data) generics::tidy(med_data, type = "effects", conf_int = TRUE)
Likelihood-ratio test of the null that the within-level and the between-level indirect effects are equal,
, for a native two-level fit from fit_sem() with cluster =. The
statistic is the free fit's likelihood against the fit with that one nonlinear equality. It is a separate test: the
descriptive within-minus-between contrast of nie(level = "contrast") is never reported by nie() with a p-value,
and there is no Wald test of it here.
twolevel_equal_indirect( object, a_within = "a_w", b_within = "b_w", a_between = "a_b", b_between = "b_b", n_starts = NULL, control = NULL, n_boot = 0L, seed = NULL )twolevel_equal_indirect( object, a_within = "a_w", b_within = "b_w", a_between = "a_b", b_between = "b_b", n_starts = NULL, control = NULL, n_boot = 0L, seed = NULL )
object |
A SEMFit from |
a_within, b_within
|
The labels of the within-level (level 1) |
a_between, b_between
|
The labels of the between-level (level 2) |
n_starts, control
|
Optimizer settings for the constrained refit; |
n_boot |
Number of parametric bootstrap replications of the statistic (default 0, none). Each replication
draws clustered data of the same cluster sizes from the constrained fit, refits both models and keeps the
statistic; replications that do not resolve (an inadmissible, non-positive-definite or non-converged refit) are
dropped, so the bootstrap p-value is conditional on admissible refits, and the number dropped is reported
( |
seed |
Optional integer seed for the bootstrap; the caller's random number state is restored. |
The null a_w b_w = a_b b_b is a smooth surface except where all four paths are 0, where it is singular. Away
from that point the likelihood-ratio statistic is referred to chi-square(1), an asymptotic law in the number of
clusters J (the between-level paths rest on J clusters). Near it, and when a level-2 variance is near the
boundary, the chi-square(1) law may not hold; no closed-form law is claimed. The function warns when all four
free-fit paths are within two standard errors of 0 (class medfit_warning_twolevel_equal_singular) and when a
level-2 variance at the constrained fit is below 1% of its implied between plus within variance.
The constrained surface has sign twins: flipping the signs of the within pair , the
between pair or both leaves the equality satisfied, at a worse likelihood. The
optimizer's "the starts disagree" warning is not raised when every disagreeing accepted start is such a twin of the
kept fit; the lowest-discrepancy start is kept. Any other disagreement among the starts, and every other
optimizer warning, is signaled as usual.
After the refit the pair-sign twins of the kept fit are tried as well (a twin certificate): each flip of a pair
with no zero is fitted once from the kept estimates with that flip, and the lowest discrepancy is kept. It is a local
search of up to three single-start solves, not a proof of a global optimum: a twin refit that fails is skipped, and
the search runs at every n_starts, so a worse twin is not kept with n_starts = 1, where no second start is
compared. It changes the statistic only when a twin ends lower. A twin that ends at an inadmissible solution (a
level-2 variance on its boundary) or with a covariance matrix that is not positive definite, below the kept
discrepancy, makes the result unresolved, as it does with more starts. When the better optimum has a level-2
variance on its boundary the result is therefore unresolved or, with n_starts = 1 in very small samples (about 12
clusters), a non-global optimum, as before the certificate.
The row allows eight sign patterns; flipping pairs together reaches the four of the kept fit's orbit. The other
orbit does hold local optima: a per-elimination oracle found local optima in both orbits in 30 of 35 small cells
(closest gap 0.02 LR). The kept orbit still won every time that was tested (no win from the other orbit in 84 stress
cells, 114 QA cells with an all-orthant oracle and 715 constrained fits from signed starts), so the coverage of the
other orbit rests on the free-fit-pattern and all-positive default starts: evidence, not a proof. The pair flip is
a symmetry of the constraint, not of the likelihood, so the twins are separate local optima. Cost: a call takes
about 3 to 4 times as long at n_starts = 1, about twice at the default and about 1.3 times at 30 starts; a
bootstrap replicate costs about 2.5 times the engine fits.
The guard admits the equality of any two a-then-b chains with the same x, m and y at levels 1 and 2, so a serial
a*d or d*b chain of the same shape is admitted too; only the a*b indirect effect of a single mediator has an
oracle and a gate here.
The constrained fit is never trusted blindly: if it ends at an inadmissible solution (a negative variance at either
level, a covariance matrix that is not positive definite, class medfit_warning_twolevel_inadmissible), does not
converge, or has a lower discrepancy than the free fit, the result is unresolved (statistic and p.value
NA, unresolved = TRUE, the reason in note) with a warning, never a likelihood-ratio number. If the fit
already carries a nonlinear constraint, or the four paths are not the level-1 and level-2 regressions, the call is
refused with class medfit_error_twolevel_unsupported; a single-level fit is medfit_error_twolevel_required.
An object of class c("twolevel_equal_indirect", "htest"): statistic (LR, the scale of
mbco_sem()), parameter (df = 1), p.value (chi-square(1)), method, data.name, n_obs,
n_clusters, level (c("within", "between")), indirect (the two indirect effects of the free fit and of the
constrained fit), unresolved, note, near_singular, twin, and boot (NULL, or a list with statistic,
p.value, n_boot, n_valid and n_dropped when n_boot > 0). twin records what the twin certificate did,
for resolved and unresolved results: tried (a twin refit was attempted), replaced (a twin replaced the kept
fit), flipped (read from the signs of the adopted fit's four path estimates against the kept fit's, not from
the start that produced it: "within" or "between" when both paths of that pair changed sign, "other" once
for any other sign change, including a path that reaches or leaves zero, and empty when no sign changed, as when
the twin is another optimum in the kept fit's sign pattern), f_gain (the kept discrepancy minus the adopted
one, 0 if none; the statistic drops by n_obs * f_gain) and unresolved (a better inadmissible or
covariance-stage twin made the result unresolved). print() does not show it.
set.seed(1) id <- rep(1:40, each = 6) x <- stats::rnorm(40)[id] + stats::rnorm(240) m <- 0.5 * x + stats::rnorm(40)[id] + stats::rnorm(240) y <- 0.4 * m + 0.2 * x + stats::rnorm(40)[id] + stats::rnorm(240) d <- data.frame(cluster = id, X = x, M = m, Y = y) fit <- fit_sem( "level: 1\n M ~ a_w*X\n Y ~ b_w*M + X\nlevel: 2\n M ~ a_b*X\n Y ~ b_b*M + X", data = d, cluster = "cluster" ) twolevel_equal_indirect(fit)set.seed(1) id <- rep(1:40, each = 6) x <- stats::rnorm(40)[id] + stats::rnorm(240) m <- 0.5 * x + stats::rnorm(40)[id] + stats::rnorm(240) y <- 0.4 * m + 0.2 * x + stats::rnorm(40)[id] + stats::rnorm(240) d <- data.frame(cluster = id, X = x, M = m, Y = y) fit <- fit_sem( "level: 1\n M ~ a_w*X\n Y ~ b_w*M + X\nlevel: 2\n M ~ a_b*X\n Y ~ b_b*M + X", data = d, cluster = "cluster" ) twolevel_equal_indirect(fit)
Experimental. The interface may change without the usual deprecation period.
twolevel_statistic( x, effect = c("nie_within", "nie_between", "nie_contrast", "nde_within", "nde_between", "te_within", "te_between", "pm_within", "pm_between", "a_within", "b_within", "c_prime_within", "a_between", "b_between", "c_prime_between"), treatment = NULL, mediator = NULL, outcome = NULL, level = NULL, specific = NULL )twolevel_statistic( x, effect = c("nie_within", "nie_between", "nie_contrast", "nde_within", "nde_between", "te_within", "te_between", "pm_within", "pm_between", "a_within", "b_within", "c_prime_within", "a_between", "b_between", "c_prime_between"), treatment = NULL, mediator = NULL, outcome = NULL, level = NULL, specific = NULL )
x |
A TwoLevelMediationData or TwoLevelParallelMediationData object, or a two-level |
effect |
One of |
treatment, mediator, outcome
|
Variable names. Required for a |
level |
For several mediators only: |
specific |
For |
Builds the scalar statistic_fn that bootstrap_mediation() needs for one effect of a two-level
(1-1-1) mediation model, so the within and the between indirect effect and their contrast can be
bootstrapped without writing the callback by hand. The form of the callback depends on the first
argument:
x a TwoLevelMediationData: the callback for method = "parametric" (and "plugin"). It takes
the named parameter vector drawn from , so the
cross-level covariance is part of every draw.
x a two-level lavaan fit (with treatment, mediator and outcome): the callback for
method = "nonparametric" with cluster =. It refits the fit's own model on each resampled data
set (the call stored in the fit, with data replaced) and returns the effect. The cluster
bootstrap gives every drawn cluster a fresh id, so a cluster drawn twice is refitted as two
clusters.
x a TwoLevelParallelMediationData (several mediators), or a two-level lavaan fit with a mediator
vector of length two or more: the same two callbacks for the effects that nie(), nde() and te()
define for that class. effect is then one of "nie" (the total indirect effect, the default), "nie_j"
(the specific indirect effect of the mediator named in specific), "nde" or "te", and level is
"within" (default), "between" or "contrast" (within minus between, "nie" and "nie_j" only). The
callback evaluates the same effect code as those generics, so at the estimate it returns their value.
A refit that errors, does not converge, warns, or leaves the between level unidentified (a treatment
or any mediator ICC below getOption("medfit.between_icc_error", 0.01), for a between effect or the
contrast; the ICC is the unrestricted (h1) one that lavaan::lavInspect(refit, "icc") reports) is a
failed replicate: bootstrap_mediation() excludes it and reports the count in a
warning. An inadmissible refit (a negative variance) is kept, as the point estimate keeps it. The
covariance checks of the point-estimate extraction are not repeated per refit (no standard errors are
computed). Refits skip standard errors, the model test and lavaan's post-check (se = "none",
test = "none", check.post = FALSE), which do not change the maximum likelihood estimates;
everything else comes from the fit's own call.
Refit environment. The refit re-evaluates the call stored in the fit with data replaced by the
resampled data (and, for a model that is not a literal string, with the fit's parameter table). Names
that lavaan leaves unevaluated in that call (for example control = ctl or estimator = est) are
resolved in the environment that called twolevel_statistic(), captured at that call, not where the fit
was made and not where the callback later runs. Create the callback in the scope that holds those
objects. Calls written with a namespace, such as lavaan::lavaan() resolve regardless.
The contrast is NA by constraint when both a and b are equated across levels and a between effect is
NA when the between level is not identified, so asking for either raises a classed error here
instead of returning a distribution of NA.
Several mediators. An invalid level, or level = "contrast" with effect = "nde" or "te", stops
with class medfit_error_twolevel_level. An effect not defined for several mediators, effect = "nie_j"
without specific or with a name that is not one of the mediators, and specific with another effect stop
with class medfit_error_twolevel_unsupported, as do level and specific given for one mediator. A
between effect or the contrast of a within-only fit stops with class medfit_between_unidentified, and the
contrast of a mediator (for "nie_j") or of every mediator (for "nie") whose a and b are both equated
across levels stops with class medfit_error_twolevel_constrained.
A function of one argument returning a number: a named parameter vector for a
TwoLevelMediationData or TwoLevelParallelMediationData, a data frame for a lavaan fit.
bootstrap_mediation(), TwoLevelMediationData, TwoLevelParallelMediationData
if (requireNamespace("lavaan", quietly = TRUE) && requireNamespace("MASS", quietly = TRUE)) { set.seed(1) id <- rep(1:60, each = 5) x <- stats::rnorm(60)[id] + stats::rnorm(300) m <- 0.5 * x + stats::rnorm(60)[id] + stats::rnorm(300) y <- 0.4 * m + 0.2 * x + stats::rnorm(60)[id] + stats::rnorm(300) d <- data.frame(id = id, X = x, M = m, Y = y) model <- " level: 1 M ~ X Y ~ M + X level: 2 M ~ X Y ~ M + X " fit <- lavaan::sem(model, data = d, cluster = "id") td <- extract_mediation(fit, treatment = "X", mediator = "M", outcome = "Y") bootstrap_mediation(twolevel_statistic(td, "nie_between"), method = "parametric", mediation_data = td, n_boot = 200, seed = 1) bootstrap_mediation( twolevel_statistic(fit, "nie_between", treatment = "X", mediator = "M", outcome = "Y"), method = "nonparametric", data = d, cluster = "id", n_boot = 20, seed = 1 ) }if (requireNamespace("lavaan", quietly = TRUE) && requireNamespace("MASS", quietly = TRUE)) { set.seed(1) id <- rep(1:60, each = 5) x <- stats::rnorm(60)[id] + stats::rnorm(300) m <- 0.5 * x + stats::rnorm(60)[id] + stats::rnorm(300) y <- 0.4 * m + 0.2 * x + stats::rnorm(60)[id] + stats::rnorm(300) d <- data.frame(id = id, X = x, M = m, Y = y) model <- " level: 1 M ~ X Y ~ M + X level: 2 M ~ X Y ~ M + X " fit <- lavaan::sem(model, data = d, cluster = "id") td <- extract_mediation(fit, treatment = "X", mediator = "M", outcome = "Y") bootstrap_mediation(twolevel_statistic(td, "nie_between"), method = "parametric", mediation_data = td, n_boot = 200, seed = 1) bootstrap_mediation( twolevel_statistic(fit, "nie_between", treatment = "X", mediator = "M", outcome = "Y"), method = "nonparametric", data = d, cluster = "id", n_boot = 20, seed = 1 ) }
Experimental. This class, twolevel_statistic() and the level argument of the effect generics may
change without the usual deprecation period (a two-level fitter in the sem engine, a later milestone, could change
the object).
TwoLevelMediationData( a_within, b_within, c_prime_within, a_between, b_between, c_prime_between, estimates, vcov, parameter_table, treatment, mediator, outcome, cluster, n_obs, n_clusters, cluster_sizes, icc, between_identified, admissible, converged, cross_level_equal, fixed_paths, source, fixed_x, information, h1_information, se_type, estimator, missing, lavaan_version, call, between_parameterization, data, source_package, source_version )TwoLevelMediationData( a_within, b_within, c_prime_within, a_between, b_between, c_prime_between, estimates, vcov, parameter_table, treatment, mediator, outcome, cluster, n_obs, n_clusters, cluster_sizes, icc, between_identified, admissible, converged, cross_level_equal, fixed_paths, source, fixed_x, information, h1_information, se_type, estimator, missing, lavaan_version, call, between_parameterization, data, source_package, source_version )
a_within, b_within, c_prime_within
|
Numeric scalars: within-level paths (always finite). |
a_between, b_between, c_prime_between
|
Numeric scalars: between-level paths; |
estimates |
Named numeric vector of the identified path rows, in the order above. |
vcov |
Covariance matrix of |
parameter_table |
Data frame, one row per path row ( |
treatment, mediator, outcome, cluster
|
Character scalars: variable names. |
n_obs, n_clusters
|
Integer scalars: rows used and number of clusters. |
cluster_sizes |
Integer vector of cluster sizes (length |
icc |
Named numeric vector: intraclass correlation of treatment, mediator and outcome. It is the
unrestricted (h1) estimate, the between variance over the total in the saturated two-level model
( |
between_identified |
Logical scalar: whether the between level is identified. |
admissible |
Logical scalar: |
converged |
Logical scalar: whether the fit converged. |
cross_level_equal |
Character vector: paths ( |
fixed_paths |
Character vector: path rows with |
source |
Character scalar: |
fixed_x, information, h1_information, se_type, estimator, missing
|
How the fit was made, read from it. |
lavaan_version, call
|
Character scalars: provenance of the fit. |
between_parameterization |
Character scalar: |
data |
Data frame or |
source_package |
Character scalar: |
source_version |
Character: |
S7 class for two-level mediation in which treatment, mediator and outcome are all measured on level-1
units nested in clusters (1-1-1), with the paths estimated separately at the within and the between
level (latent cluster means, fixed slopes). Built by extract_mediation() from a lavaan two-level
fit; the same class is the target of a sem engine fitter later (source says which).
Per level the indirect effect is , the direct effect is
, and the contrast is descriptive, not a test. When the
between level cannot be identified (for example a treatment with no between-cluster variance) the object
is within-only: the between properties are NA_real_ and the between rows are absent from
estimates and vcov.
A TwoLevelMediationData S7 object.
if (requireNamespace("lavaan", quietly = TRUE)) { set.seed(1) id <- rep(1:60, each = 5) xb <- stats::rnorm(60)[id] x <- xb + stats::rnorm(300) m <- 0.5 * x + stats::rnorm(60)[id] + stats::rnorm(300) y <- 0.4 * m + 0.2 * x + stats::rnorm(60)[id] + stats::rnorm(300) d <- data.frame(id = id, X = x, M = m, Y = y) model <- " level: 1 M ~ X Y ~ M + X level: 2 M ~ X Y ~ M + X " fit <- lavaan::sem(model, data = d, cluster = "id") td <- extract_mediation(fit, treatment = "X", mediator = "M", outcome = "Y") td@a_within * td@b_within }if (requireNamespace("lavaan", quietly = TRUE)) { set.seed(1) id <- rep(1:60, each = 5) xb <- stats::rnorm(60)[id] x <- xb + stats::rnorm(300) m <- 0.5 * x + stats::rnorm(60)[id] + stats::rnorm(300) y <- 0.4 * m + 0.2 * x + stats::rnorm(60)[id] + stats::rnorm(300) d <- data.frame(id = id, X = x, M = m, Y = y) model <- " level: 1 M ~ X Y ~ M + X level: 2 M ~ X Y ~ M + X " fit <- lavaan::sem(model, data = d, cluster = "id") td <- extract_mediation(fit, treatment = "X", mediator = "M", outcome = "Y") td@a_within * td@b_within }
Experimental. Like TwoLevelMediationData, this class may change without the usual deprecation period.
TwoLevelParallelMediationData( a_within, b_within, c_prime_within, a_between, b_between, c_prime_between, estimates, vcov, parameter_table, treatment, mediators, outcome, cluster, n_obs, n_clusters, cluster_sizes, icc, between_identified, admissible, converged, cross_level_equal, fixed_paths, source, fixed_x, information, h1_information, se_type, estimator, missing, lavaan_version, call, between_parameterization, data, source_package, source_version )TwoLevelParallelMediationData( a_within, b_within, c_prime_within, a_between, b_between, c_prime_between, estimates, vcov, parameter_table, treatment, mediators, outcome, cluster, n_obs, n_clusters, cluster_sizes, icc, between_identified, admissible, converged, cross_level_equal, fixed_paths, source, fixed_x, information, h1_information, se_type, estimator, missing, lavaan_version, call, between_parameterization, data, source_package, source_version )
a_within, b_within
|
Named numeric vectors of length k (names |
c_prime_within |
Numeric scalar: within-level direct path (always finite). |
a_between, b_between
|
Named numeric vectors of length k: between-level paths; all |
c_prime_between |
Numeric scalar: between-level direct path; |
estimates |
Named numeric vector of the identified path rows (the alias rows above). |
vcov |
Covariance matrix of |
parameter_table |
Data frame, one row per path row ( |
treatment, outcome, cluster
|
Character scalars: variable names. |
mediators |
Character vector of length |
n_obs, n_clusters
|
Integer scalars: rows used and number of clusters. |
cluster_sizes |
Integer vector of cluster sizes (length |
icc |
Named numeric vector: intraclass correlation of the treatment, each mediator and the outcome. It is the unrestricted (h1) estimate, as for TwoLevelMediationData; the within-only rule reads it. |
between_identified |
Logical scalar: whether the between level is identified. |
admissible |
Logical scalar: |
converged |
Logical scalar: whether the fit converged. |
cross_level_equal |
Character vector: paths equated across levels. |
fixed_paths |
Character vector: path rows with |
source |
Character scalar: |
fixed_x, information, h1_information, se_type, estimator, missing
|
How the fit was made, read from it. |
lavaan_version, call
|
Character scalars: provenance of the fit. |
between_parameterization |
Character scalar: |
data |
Data frame or |
source_package |
Character scalar: |
source_version |
Character: |
S7 class for two-level mediation with parallel mediators, all measured on level-1 units
nested in clusters (1-1-1), with the paths estimated separately at the within and the between level (latent
cluster means, fixed slopes). It is the parallel counterpart of TwoLevelMediationData: a separate class, so
the one-mediator object keeps its scalar paths. Every property other than the paths and mediators has the
meaning it has there.
Per level the specific indirect effect through is
, the total indirect effect is
and the direct effect is . When the between
level cannot be identified the object is within-only: the between path vectors hold NA_real_ (still
named by mediators), c_prime_between is NA_real_, and the between rows are absent from estimates and
vcov.
The alias rows of estimates and vcov are, per level, a_<M>_within for each mediator, then
b_<M>_within, then c_prime_within, and the same set with _between.
A TwoLevelParallelMediationData S7 object.