| Title: | Mediation Analysis with Multiple Imputation for Missing Data |
|---|---|
| Description: | Provides S4 classes and methods for conducting mediation analysis with multiply imputed datasets. Integrates with the 'mice' package for multiple imputation and supports structural equation modeling (SEM) using either 'lavaan' or 'OpenMx'. Implements Rubin's rules for pooling parameter estimates and standard errors across imputations. Designed to work seamlessly with 'RMediation' for computing confidence intervals of indirect effects in the presence of missing data. |
| Authors: | Davood Tofighi [aut, cre] (ORCID: <https://orcid.org/0000-0001-8523-7776>) |
| Maintainer: | Davood Tofighi <[email protected]> |
| License: | GPL-2 |
| Version: | 0.2.0 |
| Built: | 2026-06-12 11:45:59 UTC |
| Source: | https://github.com/Data-Wise/missingmed |
Fits a structural equation model to provided data using either the lavaan or OpenMx package.
fit_model(model, data)fit_model(model, data)
model |
A character string representing |
data |
A data frame containing the data to which the model will be fitted. |
A fitted model object from either lavaan or OpenMx, depending on the input.
## Not run: data("HolzingerSwineford1939", package = "lavaan") lav_model_syntax <- "visual =~ x1 + x2 + x3" fitted_model <- fit_model(lav_model_syntax, HolzingerSwineford1939) ## End(Not run)## Not run: data("HolzingerSwineford1939", package = "lavaan") lav_model_syntax <- "visual =~ x1 + x2 + x3" fitted_model <- fit_model(lav_model_syntax, HolzingerSwineford1939) ## End(Not run)
Computes inference for the indirect (mediated) effect from a fitted missingmed pipeline, dispatching to one of two engines:
infer(object, ...)infer(object, ...)
object |
An MDMediationFit (supports both |
... |
Method arguments: |
type = "mc" — Monte-Carlo / distribution-of-the-product confidence
interval via RMediation::ci_mediation_data() applied to the pooled
named medfit::MediationData.
type = "mbco" — D4-stacked MBCO likelihood-ratio test of
, computed from the per-imputation datasets (MBCO does not
commute with Rubin's rules; see per_imputation_list()).
For "mc", the list returned by RMediation::ci_mediation_data().
For "mbco", a named numeric vector c(D4, p, r4, nu, d_S).
run(), pool(), per_imputation_list()
This function checks whether a structural equation modeling (SEM) model,
represented by either a lavaan object or an MxModel object from the OpenMx
package, has been fitted. The function offers a convenient way to programmatically
verify if the model fitting step has been executed for a given model object.
Method for ANY object. This method returns FALSE for any object that is not
a lavaan or MxModel object.
Method for lavaan objects. This method checks the do.fit option in the
lavaan object to determine if the model has been fitted. If the do.fit
option is TRUE, the model has been fitted; otherwise, it has not been fitted.
Method for OpenMx objects. This method checks the wasRun slot in the
MxModel object to determine if the model has been fitted. If the wasRun
slot is TRUE, the model has been fitted; otherwise, it has not been fitted.
is_fit(model,...) ## S4 method for signature 'ANY' is_fit(model, ...) ## S4 method for signature 'lavaan' is_fit(model, ...) ## S4 method for signature 'MxModel' is_fit(model, ...)is_fit(model,...) ## S4 method for signature 'ANY' is_fit(model, ...) ## S4 method for signature 'lavaan' is_fit(model, ...) ## S4 method for signature 'MxModel' is_fit(model, ...)
model |
A lavaan object. |
... |
Additional arguments affecting the method dispatched (currently not used but available for future extensions). |
A logical value: TRUE if the model has been fitted, and FALSE otherwise.
lavaan::lavaan, OpenMx::MxModel
## Not run: # Assuming 'lav_model' is a lavaan model object lav_model_fit <- is_fit(lav_model) # Assuming 'mx_model' is an OpenMx model object mx_model_fit <- is_fit(mx_model) # Checking the output print(lav_model_fit) print(mx_model_fit) ## End(Not run)## Not run: # Assuming 'lav_model' is a lavaan model object lav_model_fit <- is_fit(lav_model) # Assuming 'mx_model' is an OpenMx model object mx_model_fit <- is_fit(mx_model) # Checking the output print(lav_model_fit) print(mx_model_fit) ## End(Not run)
This function checks if the lavaan model syntax is valid. It does so by attempting to parse and fit the model in a safe environment. If the model syntax is invalid, the function will return an error message.
is_lav_syntax(model, quiet = FALSE)is_lav_syntax(model, quiet = FALSE)
model |
A character string representing the lavaan model to be fitted. |
quiet |
A logical value indicating whether to suppress the error message. default is FALSE. |
A logical value indicating whether the model syntax is valid.
Davood Tofighi [email protected]
bad_model <- "y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9" is_lav_syntax(bad_model) good_model <- "visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 visual ~ speed textual ~ speed" is_lav_syntax(good_model)bad_model <- "y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9" is_lav_syntax(bad_model) good_model <- "visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 visual ~ speed textual ~ speed" is_lav_syntax(good_model)
Determines if a matrix is positive definite (all eigenvalues are strictly positive) by attempting Cholesky decomposition.
is_pd(x, quiet = FALSE) ## S4 method for signature 'matrix' is_pd(x, quiet = FALSE)is_pd(x, quiet = FALSE) ## S4 method for signature 'matrix' is_pd(x, quiet = FALSE)
x |
A numeric matrix. |
quiet |
Logical. If |
Returns TRUE if the matrix is positive definite, FALSE otherwise.
# Example of a positive definite matrix A <- matrix(c(1, 2, 2, 4), nrow = 2) is_pd(A) # Should return TRUE# Example of a positive definite matrix A <- matrix(c(1, 2, 2, 4), nrow = 2) is_pd(A) # Should return TRUE
This function checks if the lavaan model syntax is valid. It does so by attempting to parse and fit the model in a safe environment. If the model syntax is invalid, the function will return an error message.
is_valid_lav_syntax(model, data = NULL)is_valid_lav_syntax(model, data = NULL)
model |
A character string representing the lavaan model to be fitted. |
data |
A data frame containing the observed variables. |
A logical value indicating whether the model syntax is valid.
Davood Tofighi [email protected]
bad_model <- "y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9" data(HolzingerSwineford1939, package = "lavaan") is_valid_lav_syntax(bad_model, HolzingerSwineford1939) good_model <- "visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 visual ~ speed textual ~ speed" is_valid_lav_syntax(good_model, HolzingerSwineford1939)bad_model <- "y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9" data(HolzingerSwineford1939, package = "lavaan") is_valid_lav_syntax(bad_model, HolzingerSwineford1939) good_model <- "visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 visual ~ speed textual ~ speed" is_valid_lav_syntax(good_model, HolzingerSwineford1939)
Fits a SEM model to each dataset in a mids object without pooling the results.
This function is an extension for the lavaan::sem() function to handle mice::mids objects from the mice::mice package.
It allows for both a SEM model syntax as a character string or a pre-fitted lavaan::lavaan model object.
lav_mice(model, mids, ...)lav_mice(model, mids, ...)
model |
Either a character string representing the SEM model to be fitted or a pre-fitted lavaan::lavaan model object. |
mids |
A |
... |
Additional arguments to be passed to |
A list of lavaan::lavaan model fits, one for each imputed dataset.
Davood Tofighi [email protected]
## Not run: # library(mice) # library(lavaan) # Load Holzinger and Swineford (1939) dataset data("HolzingerSwineford1939", package = "lavaan") # Introduce missing data df_complete <- na.omit(HolzingerSwineford1939) amp <- mice::ampute(df_complete, prop = 0.2, mech = "MAR") data_with_missing <- amp$amp # Perform multiple imputation imputed_data <- mice::mice(data_with_missing, m = 3, maxit = 5, seed = 12345, printFlag = FALSE) # fit the Holzinger and Swineford (1939) example model HS_model <- " visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 " # Fit the SEM model without running fit_HS <- lavaan::sem(HS_model, data = data_with_missing, do.fit = FALSE) # Fit the SEM model without pooling to each imputed dataset fit_list1 <- lav_mice(HS_model, imputed_data) # 'fit_list1' now contains a list of lavaan objects, one for each imputed dataset # Fit the SEM model without pooling to each imputed dataset using a pre-fitted model object fit_list2 <- lav_mice(fit_HS, imputed_data) # 'fit_list2' now contains a list of lavaan objects, one for each imputed dataset ## End(Not run)## Not run: # library(mice) # library(lavaan) # Load Holzinger and Swineford (1939) dataset data("HolzingerSwineford1939", package = "lavaan") # Introduce missing data df_complete <- na.omit(HolzingerSwineford1939) amp <- mice::ampute(df_complete, prop = 0.2, mech = "MAR") data_with_missing <- amp$amp # Perform multiple imputation imputed_data <- mice::mice(data_with_missing, m = 3, maxit = 5, seed = 12345, printFlag = FALSE) # fit the Holzinger and Swineford (1939) example model HS_model <- " visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 " # Fit the SEM model without running fit_HS <- lavaan::sem(HS_model, data = data_with_missing, do.fit = FALSE) # Fit the SEM model without pooling to each imputed dataset fit_list1 <- lav_mice(HS_model, imputed_data) # 'fit_list1' now contains a list of lavaan objects, one for each imputed dataset # Fit the SEM model without pooling to each imputed dataset using a pre-fitted model object fit_list2 <- lav_mice(fit_HS, imputed_data) # 'fit_list2' now contains a list of lavaan objects, one for each imputed dataset ## End(Not run)
An S7 class holding multiply imputed data together with a medfit-style
mediation specification (outcome/mediator formulas + roles). It is the entry
point of the missingmed S7 pipeline
(set_md_mediation() -> run() -> pool() -> infer()) and the S7
successor of the S4 SemImputedData class.
MDMediationData( data = NULL, formula_y = NULL, formula_m = NULL, treatment = character(0), mediator = character(0), engine = "glm", family_y = NULL, family_m = NULL, method = "mi", mechanism = "mar", weight_formula = NULL, weight_stabilize = TRUE, weight_trim = 1, se_type = "sandwich", conf_int = FALSE, conf_level = 0.95, n_imputations = integer(0), original_data = data.frame() )MDMediationData( data = NULL, formula_y = NULL, formula_m = NULL, treatment = character(0), mediator = character(0), engine = "glm", family_y = NULL, family_m = NULL, method = "mi", mechanism = "mar", weight_formula = NULL, weight_stabilize = TRUE, weight_trim = 1, se_type = "sandwich", conf_int = FALSE, conf_level = 0.95, n_imputations = integer(0), original_data = data.frame() )
data |
For |
formula_y |
Outcome model formula (e.g. |
formula_m |
Mediator model formula (e.g. |
treatment |
Name of the treatment/exposure variable. |
mediator |
Name of the mediator variable. |
engine |
medfit fitting engine, e.g. |
family_y, family_m
|
|
method |
Estimator axis: |
mechanism |
Assumed missing-data mechanism: |
weight_formula |
(IPW) Missingness model specification: |
weight_stabilize |
(IPW) Logical; if |
weight_trim |
(IPW) Upper quantile at which to cap weights (e.g. |
se_type |
(IPW) Variance estimator passed to |
conf_int |
Logical; whether downstream output carries confidence
intervals. Defaults to |
conf_level |
Numeric in (0, 1); confidence level. Defaults to |
n_imputations |
Number of imputations (MI) or |
original_data |
The original data (pre-imputation for MI; the supplied frame for IPW). |
Fitting is delegated to medfit::fit_mediation() (one call per imputation),
so the per-imputation fits carry named path coefficients (a, b,
c_prime) ready for RMediation::RMediation inference. The estimator (method) and the
model are orthogonal axes: method selects the missing-data estimator
("mi"/"ipw"); the formulas/engine select the model.
An MDMediationData S7 object.
set_md_mediation(), medfit::fit_mediation(), SemImputedData
An S7 class holding the result of fitting a mediation model across all
imputations. Its defining feature is per_imputation: a list of named
medfit::MediationData objects, one per imputation. This list is what the
MBCO-MI path consumes, because MBCO does not commute with Rubin's rules
(D4-stacked MBCO needs the per-imputation fits, not the pooled estimate).
MDMediationFit( per_imputation = list(), fits = list(), m = integer(0), engine = "glm", conf_int = FALSE, conf_level = 0.95, weights = NULL, source = NULL )MDMediationFit( per_imputation = list(), fits = list(), m = integer(0), engine = "glm", conf_int = FALSE, conf_level = 0.95, weights = NULL, source = NULL )
per_imputation |
A list of named medfit::MediationData objects (length |
fits |
A list of the raw backend fits ( |
m |
Integer number of imputations. |
engine |
medfit fitting engine used (e.g. |
conf_int |
Logical; whether output carries confidence intervals. |
conf_level |
Numeric in (0, 1); confidence level. |
weights |
(IPW) Full-length numeric IPW weight vector ( |
source |
The originating MDMediationData (retained so MBCO can refit constrained/unconstrained models against the imputed data). |
It is the S7 successor of the S4 SemResults class.
An MDMediationFit S7 object.
run(), per_imputation_list(), SemResults
An S7 class holding the Rubin's-rules pooled mediation result. Its defining
feature is pooled: a single named medfit::MediationData built from the
pooled estimates and total variance-covariance, valid as input to
RMediation::ci_mediation_data() / RMediation::medci() (path coefficients
resolve by name). It is the S7 successor of the S4 PooledSEMResults class.
MDMediationResult( pooled = NULL, tidy_table = data.frame(), cov_total = NULL, cov_between = NULL, cov_within = NULL, m = integer(0), engine = "glm", conf_int = FALSE, conf_level = 0.95 )MDMediationResult( pooled = NULL, tidy_table = data.frame(), cov_total = NULL, cov_between = NULL, cov_within = NULL, m = integer(0), engine = "glm", conf_int = FALSE, conf_level = 0.95 )
pooled |
A named medfit::MediationData carrying the pooled estimates
and total vcov (path labels |
tidy_table |
A data frame of pooled estimates (term, estimate, std_error, p_value, var_w, var_b, var_tot). |
cov_total, cov_between, cov_within
|
The Rubin's-rules total, between-, and within-imputation covariance matrices. |
m |
Integer number of imputations pooled. |
engine |
medfit fitting engine used (e.g. |
conf_int |
Logical; whether the tidy table carries confidence intervals. |
conf_level |
Numeric in (0, 1); confidence level. |
An MDMediationResult S7 object.
pool(), infer(), PooledSEMResults
This function fits an OpenMx model to each imputed dataset in a 'mids' object from the 'mice' package. The function returns a list of OpenMx model fits.
mx_mice(model, mids, ...)mx_mice(model, mids, ...)
model |
An OpenMx model object. |
mids |
A 'mids' object from the 'mice' package. |
... |
Additional arguments to be passed to 'mxRun'. |
A list of OpenMx model fits.
Davood Tofighi [email protected]
## Not run: # library(OpenMx) # library(mice) # Fit a model to multiply imputed datasets data("HolzingerSwineford1939", package = "lavaan") # Introduce missing data df_complete <- na.omit(HolzingerSwineford1939) amp <- mice::ampute(df_complete, prop = 0.2, mech = "MAR") df_incomplete <- amp$amp # Perform multiple imputation imputed_data <- mice(df_incomplete, m = 3, method = "pmm", maxit = 5, seed = 12345) # Simple SEM model specification with OpenMx manifestVars <- paste0("x", 1:9) latVar <- c("visual", "textual", "speed") model <- mxModel("Simple SEM", type = "RAM", manifestVars = manifestVars, latentVars = latVar, mxPath(from = "visual", to = c("x1", "x2", "x3")), mxPath(from = "textual", to = c("x4", "x5", "x6")), mxPath(from = "speed", to = c("x7", "x8", "x9")), mxPath(from = manifestVars, arrows = 2), mxPath(from = latVar, arrows = 2, free = FALSE, values = 1.0), mxPath(from = "one", to = manifestVars, arrows = 1, free = TRUE, values = 1.0), mxPath(from = "one", to = latVar, arrows = 1, free = FALSE, values = 0), mxData(df_complete, type = "raw") ) # Assuming mx_mice is correctly defined in your environment fits <- mx_mice(model, imputed_data) summary(fits[[1]]) ## End(Not run)## Not run: # library(OpenMx) # library(mice) # Fit a model to multiply imputed datasets data("HolzingerSwineford1939", package = "lavaan") # Introduce missing data df_complete <- na.omit(HolzingerSwineford1939) amp <- mice::ampute(df_complete, prop = 0.2, mech = "MAR") df_incomplete <- amp$amp # Perform multiple imputation imputed_data <- mice(df_incomplete, m = 3, method = "pmm", maxit = 5, seed = 12345) # Simple SEM model specification with OpenMx manifestVars <- paste0("x", 1:9) latVar <- c("visual", "textual", "speed") model <- mxModel("Simple SEM", type = "RAM", manifestVars = manifestVars, latentVars = latVar, mxPath(from = "visual", to = c("x1", "x2", "x3")), mxPath(from = "textual", to = c("x4", "x5", "x6")), mxPath(from = "speed", to = c("x7", "x8", "x9")), mxPath(from = manifestVars, arrows = 2), mxPath(from = latVar, arrows = 2, free = FALSE, values = 1.0), mxPath(from = "one", to = manifestVars, arrows = 1, free = TRUE, values = 1.0), mxPath(from = "one", to = latVar, arrows = 1, free = FALSE, values = 0), mxData(df_complete, type = "raw") ) # Assuming mx_mice is correctly defined in your environment fits <- mx_mice(model, imputed_data) summary(fits[[1]]) ## End(Not run)
This function returns the number of imputations stored in a mids object
created by the mice package.
n_imp(x) ## S4 method for signature 'mids' n_imp(x)n_imp(x) ## S4 method for signature 'mids' n_imp(x)
x |
A |
An integer representing the number of imputations.
## Not run: # Assuming `imputed_data` is a mids object created by the mice package n_imp(imputed_data) ## End(Not run)## Not run: # Assuming `imputed_data` is a mids object created by the mice package n_imp(imputed_data) ## End(Not run)
Number of imputations
n_imputations(object, ...)n_imputations(object, ...)
object |
An MDMediationFit or MDMediationResult object. |
... |
Unused. |
Integer count of imputations.
Returns the list of per-imputation named medfit::MediationData objects
held in an MDMediationFit, together with the number of imputations m.
per_imputation_list(object, ...)per_imputation_list(object, ...)
object |
An MDMediationFit object. |
... |
Unused. |
This accessor exists because MBCO does not commute with Rubin's rules:
D4-stacked MBCO needs the per-imputation fits, not the pooled estimate. The
list it returns is the shape consumed by infer()(type = "mbco") and by an
external RMediation::mbco() MI entry point (missingmed issue #2).
A list with components per_imputation (a length-m list of named
medfit::MediationData) and m (the number of imputations).
Applies Rubin's (1987) rules to the list of per-imputation named
medfit::MediationData objects in an MDMediationFit, producing a single
pooled named medfit::MediationData (the pooled slot of the returned
MDMediationResult). Because the estimates and variance-covariance carry the
mediation path names (a, b, c_prime, ...), the pooled object is valid
input to RMediation::ci_mediation_data() / RMediation::medci().
pool(object, ...)pool(object, ...)
object |
An MDMediationFit object. |
... |
Unused. |
Pooling math (migrated from the S4 pool_sem / pool_tidy / pool_cov):
It is the S7 successor of the S4 pool_sem() method.
An MDMediationResult object.
Rubin, D. B. (1987). Multiple Imputation for Nonresponse in Surveys. Wiley.
pool_sem pools SEM analysis results, supporting lavaan and OpenMx models.
It calculates pooled estimates, standard errors, confidence intervals, and more.
This function pools the results of structural equation modeling (SEM)
analyses performed on multiple imputed datasets.
It supports pooling for models analyzed with either the lavaan
or OpenMx package. The function extracts and pools
relevant statistics (e.g., estimates, standard errors) across
all imputations, considering the specified confidence interval settings.
pool_sem(object) ## S4 method for signature 'SemResults' pool_sem(object)pool_sem(object) ## S4 method for signature 'SemResults' pool_sem(object)
object |
|
A generic function to pool SEM analysis results from multiple datasets or imputations.
Refer to method-specific documentation for details on pooling process and assumptions.
PooledSEMResults object containing pooled SEM analysis results.
A data.frame containing the pooled results of the SEM analyses. The column names adhere to tidy conventions and include the following columns:
term: The name of the parameter being estimated.
estimate: The pooled estimate of the parameter.
std_error: The pooled standard error of the estimate.
statistic: The pooled test statistic (e.g., z-value, t-value).
p_value: The pooled p-value for the test statistic.
conf_low: The lower bound of the confidence interval for the estimate.
conf_high: The upper bound of the confidence interval for the estimate.
Davood Tofighi [email protected]
lavaan::lavaan, OpenMx::OpenMx
## Not run: # Assuming `sem_results` is a SemResults object with lavaan model fits: library(RMediation) # Load Holzinger and Swineford (1939) dataset data("HolzingerSwineford1939", package = "lavaan") # Introduce missing data df_complete <- na.omit(HolzingerSwineford1939[paste0("x", 1:9)]) amp <- mice::ampute(df_complete, prop = 0.1, mech = "MAR") data_with_missing <- amp$amp # Perform multiple imputation imputed_data <- mice::mice(data_with_missing, m = 3, maxit = 3, seed = 12345, printFlag = FALSE) model <- " visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 " res_pooled <- imputed_data |> set_sem(model) |> run_sem() |> pool_sem() res_pooled@tidy_table # Print the pooled results ## End(Not run)## Not run: # Assuming `sem_results` is a SemResults object with lavaan model fits: library(RMediation) # Load Holzinger and Swineford (1939) dataset data("HolzingerSwineford1939", package = "lavaan") # Introduce missing data df_complete <- na.omit(HolzingerSwineford1939[paste0("x", 1:9)]) amp <- mice::ampute(df_complete, prop = 0.1, mech = "MAR") data_with_missing <- amp$amp # Perform multiple imputation imputed_data <- mice::mice(data_with_missing, m = 3, maxit = 3, seed = 12345, printFlag = FALSE) model <- " visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 " res_pooled <- imputed_data |> set_sem(model) |> run_sem() |> pool_sem() res_pooled@tidy_table # Print the pooled results ## End(Not run)
An S4 class to represent pooled results from SEM analysis across multiple imputations or datasets. It contains pooled estimates, standard errors, test statistics, p-values, and confidence intervals for each parameter estimated across multiple imputations.
tidy_tabledata.frame A data frame containing the pooled results of the SEM analyses. The column names adhere to tidy conventions and include the following columns:
term: The name of the parameter being estimated.
estimate: The pooled estimate of the parameter.
std_error: The pooled standard error of the estimate.
p_value: The pooled p-value for the test statistic.
conf_low: The lower bound of the confidence interval for the estimate.
conf_high: The upper bound of the confidence interval for the estimate.
cov_totalmatrix The pooled total covariance matrix of the parameter estimates.
cov_betweenmatrix The pooled between-imputation covariance matrix of the parameter estimates.
cov_withinmatrix The pooled within-imputation covariance matrix of the parameter estimates.
methodcharacter The method used for SEM analysis ('lavaan' or 'OpenMx').
conf_intlogical Whether to calculate confidence intervals for the pooled estimates. default is FALSE.
conf_levelnumeric The confidence level used in the interval calculation. default is 0.95.
Davood Tofighi [email protected]
Runs the mediation specification held in an MDMediationData object on every
imputed dataset, delegating each fit to medfit::fit_mediation(). The result
is an MDMediationFit whose per_imputation slot is a list of named
medfit::MediationData objects (one per imputation) — the shape consumed by
both Rubin's-rules pooling (pool()) and D4-stacked MBCO (infer()).
run(object, ...)run(object, ...)
object |
An MDMediationData object. |
... |
Additional arguments forwarded to |
It is the S7 successor of the S4 run_sem() method.
An MDMediationFit object.
set_md_mediation(), pool(), infer(), run_sem()
A generic function to run and analyze multiply imputed data sets.
This method facilitates running SEM analysis using either lavaan or OpenMx on multiply imputed datasets contained within a SemImputedData object.
run_sem(object, ...) ## S4 method for signature 'SemImputedData' run_sem(object, ...)run_sem(object, ...) ## S4 method for signature 'SemImputedData' run_sem(object, ...)
object |
A |
... |
Additional arguments passed to either lavaan::sem or OpenMx::MxModel. |
A SemResults object
Davood Tofighi [email protected]
## Not run: library(RMediation) # Load Holzinger and Swineford (1939) dataset data("HolzingerSwineford1939", package = "lavaan") # Introduce missing data df_complete <- na.omit(HolzingerSwineford1939[paste0("x", 1:9)]) amp <- mice::ampute(df_complete, prop = 0.1, mech = "MAR") data_with_missing <- amp$amp # Perform multiple imputation imputed_data <- mice::mice(data_with_missing, m = 3, maxit = 3, seed = 12345, printFlag = FALSE) model <- " visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 " res_sem <- imputed_data |> set_sem(model) |> run_sem() res_sem@estimate_df # long tidy table of estimates across imputed datasets ## End(Not run)## Not run: library(RMediation) # Load Holzinger and Swineford (1939) dataset data("HolzingerSwineford1939", package = "lavaan") # Introduce missing data df_complete <- na.omit(HolzingerSwineford1939[paste0("x", 1:9)]) amp <- mice::ampute(df_complete, prop = 0.1, mech = "MAR") data_with_missing <- amp$amp # Perform multiple imputation imputed_data <- mice::mice(data_with_missing, m = 3, maxit = 3, seed = 12345, printFlag = FALSE) model <- " visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 " res_sem <- imputed_data |> set_sem(model) |> run_sem() res_sem@estimate_df # long tidy table of estimates across imputed datasets ## End(Not run)
An S4 class to hold multiply imputed datasets for structural equation modeling (SEM) analysis.
It facilitates working with imputed data from the mice package and supports SEM analysis
using either the lavaan or OpenMx packages.
dataAn object of class mids from the mice package, representing multiply imputed datasets.
modelA lavaan or OpenMx model syntax to be used for SEM analysis. For lavaan models, the syntax should be a character string as described in lavaan::model.syntax. For OpenMx models, the syntax should be an OpenMx::mxModel object with or without OpenMx::mxData() specified; that is, mxModel syntax can be without data specified. In addition, both lavaan and OpenMx models can be a fitted model object in the respective package.
methodA character string indicating the SEM package to be used for analysis. It is a derived slot from the model slot, and it is set automatically based on the class of the model slot. The possible values are "lavaan" or "OpenMx".
conf_intA logical value indicating whether confidence intervals are
included in the SEM results. Defaults to FALSE.
conf_levelA numeric value specifying the confidence level for confidence intervals, which must be between 0 and 1. Defaults to 0.95.
original_dataA derived (from mids object) slot to store the original data used to create the imputed datasets.
n_imputationsA derived (from mids object) slot to store the number of imputations used to create the imputed datasets.
fit_modelSEM fitted to the original data with list wise deletion of missing data.
Davood Tofighi [email protected]
An S4 class for storing the results of SEM analysis
performed on multiply imputed datasets. Supports lavaan and OpenMx.
resultsA list of SEM model fits for each imputed dataset.
estimate_dfData frame of parameter estimates and standard errors.
coef_dfData frame of coefficient estimates for each imputed dataset.
cov_dfList of covariance matrices of coefficient estimates.
methodSEM package used for analysis: 'lavaan' or 'OpenMx'.
conf_intLogical; if confidence intervals are included.
conf_levelConfidence level for confidence intervals.
Davood Tofighi [email protected]
Constructs an MDMediationData object: the entry point of the missingmed S7
pipeline. It records a medfit-style mediation specification (outcome and
mediator formulas plus the treatment/mediator roles) together with the data.
Fitting is delegated to medfit::fit_mediation() downstream by run(). It is
the S7 successor of the S4 set_sem() constructor.
set_md_mediation( data, formula_y, formula_m, treatment, mediator, engine = "glm", family_y = stats::gaussian(), family_m = stats::gaussian(), method = c("mi", "ipw"), mechanism = c("mar", "mnar"), weight_formula = NULL, weight_stabilize = TRUE, weight_trim = 1, se_type = c("sandwich", "model"), conf_int = FALSE, conf_level = 0.95 )set_md_mediation( data, formula_y, formula_m, treatment, mediator, engine = "glm", family_y = stats::gaussian(), family_m = stats::gaussian(), method = c("mi", "ipw"), mechanism = c("mar", "mnar"), weight_formula = NULL, weight_stabilize = TRUE, weight_trim = 1, se_type = c("sandwich", "model"), conf_int = FALSE, conf_level = 0.95 )
data |
For |
formula_y |
Outcome model formula (e.g. |
formula_m |
Mediator model formula (e.g. |
treatment |
Name of the treatment/exposure variable. |
mediator |
Name of the mediator variable. |
engine |
medfit fitting engine. Defaults to |
family_y, family_m
|
|
method |
Estimator axis: |
mechanism |
Assumed missing-data mechanism: |
weight_formula |
(IPW) Missingness model: |
weight_stabilize |
(IPW) Use stabilized weights? Default |
weight_trim |
(IPW) Upper quantile to cap weights; |
se_type |
(IPW) |
conf_int |
Logical; whether downstream output carries confidence
intervals. Defaults to |
conf_level |
Numeric in (0, 1); confidence level. Defaults to |
Two estimators share the interface (method):
"mi" — data is a mice::mids object; run() fits every imputation.
"ipw" — data is a raw data.frame; run() reweights the complete cases
by inverse missingness probability and fits once.
An MDMediationData object.
MDMediationData, run(), pool(), infer(), medfit::fit_mediation()
## Not run: set.seed(1) d <- data.frame(X = rbinom(200, 1, .5), C = rnorm(200)) d$M <- .5 * d$X + .3 * d$C + rnorm(200) d$Y <- .2 * d$X + .4 * d$M + .3 * d$C + rnorm(200) d$M[sample(200, 30)] <- NA # MI imp <- mice::mice(d, m = 5, printFlag = FALSE) md_mi <- set_md_mediation(imp, Y ~ X + M + C, M ~ X + C, treatment = "X", mediator = "M") # IPW (raw data.frame) md_ipw <- set_md_mediation(d, Y ~ X + M + C, M ~ X + C, treatment = "X", mediator = "M", method = "ipw") ## End(Not run)## Not run: set.seed(1) d <- data.frame(X = rbinom(200, 1, .5), C = rnorm(200)) d$M <- .5 * d$X + .3 * d$C + rnorm(200) d$Y <- .2 * d$X + .4 * d$M + .3 * d$C + rnorm(200) d$M[sample(200, 30)] <- NA # MI imp <- mice::mice(d, m = 5, printFlag = FALSE) md_mi <- set_md_mediation(imp, Y ~ X + M + C, M ~ X + C, treatment = "X", mediator = "M") # IPW (raw data.frame) md_ipw <- set_md_mediation(d, Y ~ X + M + C, M ~ X + C, treatment = "X", mediator = "M", method = "ipw") ## End(Not run)
This function sets up an SEM model with multiply imputed data for analysis. The function accepts a mice::mids object and a model syntax for either lavaan::lavaan or OpenMx::OpenMx and returns a SemImputedData object for analysis. It returns an error if the provided data is not a mice::mids object or if the specified SEM analysis method is not supported. It returns an object of class SemImputedData.
set_sem(data, model, conf_int = FALSE, conf_level = 0.95) ## S4 method for signature 'mids' set_sem(data, model, conf_int = FALSE, conf_level = 0.95)set_sem(data, model, conf_int = FALSE, conf_level = 0.95) ## S4 method for signature 'mids' set_sem(data, model, conf_int = FALSE, conf_level = 0.95)
data |
A mice::mids object from the |
model |
A lavaan::lavaan or OpenMx::OpenMx model syntax to be used for SEM analysis. For |
conf_int |
A logical value indicating whether confidence intervals are
included in the SEM results. Defaults to |
conf_level |
A numeric value specifying the confidence level for
confidence intervals, which must be between 0 and 1. Defaults to 0.95.
If |
The function technically constructs a new SemImputedData object for structural equation modeling (SEM) analysis using either lavaan::lavaan or OpenMx::OpenMx on multiply imputed datasets. This function ensures that the provided data is a mice::mids object from the mice package and that the specified SEM analysis method is supported.
All the arguments data, method, conf_int, and conf_level are used to specify the SEM analysis. set_sem is a constructor function for SemImputedData class. These methods are used as constructors for the SemImputedData class.
An object of class SemImputedData. See SemImputedData for the details of the slots.
SemImputedData mice::mids lavaan::lavaan OpenMx::OpenMx
## Not run: data("HolzingerSwineford1939", package = "lavaan") df_complete <- na.omit(HolzingerSwineford1939) amp <- mice::ampute(df_complete, prop = 0.2, mech = "MAR") imputed_data <- mice::mice(amp$amp, m = 3, maxit = 3, seed = 12345, printFlag = FALSE) model <- " visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9" sem_data <- set_sem(imputed_data, model) str(sem_data) ## End(Not run)## Not run: data("HolzingerSwineford1939", package = "lavaan") df_complete <- na.omit(HolzingerSwineford1939) amp <- mice::ampute(df_complete, prop = 0.2, mech = "MAR") imputed_data <- mice::mice(amp$amp, m = 3, maxit = 3, seed = 12345, printFlag = FALSE) model <- " visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9" sem_data <- set_sem(imputed_data, model) str(sem_data) ## End(Not run)
Method to display a concise summary of a SemImputedData object. This method is automatically called when you print a SemImputedData object.
## S4 method for signature 'SemImputedData' show(object)## S4 method for signature 'SemImputedData' show(object)
object |
The SemImputedData object to be displayed. |
This method does not return a value but displays a summary of the SemImputedData object.
## Not run: data("HolzingerSwineford1939", package = "lavaan") hs_data <- HolzingerSwineford1939[paste0("x", 1:9)] |> mice::ampute() hs_data <- hs_data$amp imp_data <- mice::mice(HolzingerSwineford1939, m = 5) model_lav <- "visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9" imp_data <- mice::mice(hs_data, m = 5) sem_data <- SemImputedData(imp_data, model_lav) show(sem_data) ## End(Not run)## Not run: data("HolzingerSwineford1939", package = "lavaan") hs_data <- HolzingerSwineford1939[paste0("x", 1:9)] |> mice::ampute() hs_data <- hs_data$amp imp_data <- mice::mice(HolzingerSwineford1939, m = 5) model_lav <- "visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9" imp_data <- mice::mice(hs_data, m = 5) sem_data <- SemImputedData(imp_data, model_lav) show(sem_data) ## End(Not run)
Provides a comprehensive summary of an SemImputedData object, including the SEM method used, the number of imputations, basic information about the imputed data, and summaries of the original data and fitted model.
## S4 method for signature 'SemImputedData' summary(object, ...)## S4 method for signature 'SemImputedData' summary(object, ...)
object |
An object of class SemImputedData. |
... |
Further arguments passed to or from other methods. |
A textual summary of the SemImputedData object.
## Not run: # Assuming `sem_imputed_data` is an SemImputedData object summary(sem_imputed_data) ## End(Not run)## Not run: # Assuming `sem_imputed_data` is an SemImputedData object summary(sem_imputed_data) ## End(Not run)
Creates a data.frame for a log-likelihood object
## S3 method for class 'logLik' tidy(x, ...)## S3 method for class 'logLik' tidy(x, ...)
x |
A log-likelihood object, typically returned by logLik. |
... |
Additional arguments (not used) |
A data.frame with columns:
The term name
The log-likelihood value
The degrees of freedom
Davood Tofighi [email protected]
fit <- lm(mpg ~ wt, data = mtcars) logLik_fit <- logLik(fit) # Dispatch via the broom generic (registered against broom::tidy). tidy(logLik_fit)fit <- lm(mpg ~ wt, data = mtcars) logLik_fit <- logLik(fit) # Dispatch via the broom generic (registered against broom::tidy). tidy(logLik_fit)
Extracts parameter estimates from an OpenMx::MxModel from the OpenMx::OpenMx model and formats them into a tidy dataframe.
## S3 method for class 'MxModel' tidy(x, conf_int = FALSE, conf_level = 0.95, ...)## S3 method for class 'MxModel' tidy(x, conf_int = FALSE, conf_level = 0.95, ...)
x |
An object of class OpenMx::MxModel resulting from an SEM fit using OpenMx. |
conf_int |
Logical, whether to include confidence intervals in the output. |
conf_level |
The confidence level to use for the confidence intervals. |
... |
Additional arguments (currently not used). |
A tibble with one row per parameter and columns for parameter names, estimates, standard errors, and optionally confidence intervals.
OpenMx::OpenMx OpenMx::MxModel OpenMx::summary.MxModel
## Not run: # Load Holzinger and Swineford (1939) dataset data("HolzingerSwineford1939", package = "lavaan") # Simple SEM model specification with OpenMx manifestVars <- paste0("x", 1:9) latVar <- c("visual", "textual", "speed") model <- mxModel("Simple SEM", type = "RAM", manifestVars = manifestVars, latentVars = latVar, mxPath(from = "visual", to = c("x1", "x2", "x3")), mxPath(from = "textual", to = c("x4", "x5", "x6")), mxPath(from = "speed", to = c("x7", "x8", "x9")), mxPath(from = manifestVars, arrows = 2), mxPath(from = latVar, arrows = 2, free = FALSE, values = 1.0), mxPath(from = "one", to = manifestVars, arrows = 1, free = FALSE, values = 0), mxPath(from = "one", to = latVar, arrows = 1, free = FALSE, values = 0), mxData(HolzingerSwineford1939, type = "raw") ) # # Fit the model fit0 <- mxRun(model) tidy(fit0) ## End(Not run)## Not run: # Load Holzinger and Swineford (1939) dataset data("HolzingerSwineford1939", package = "lavaan") # Simple SEM model specification with OpenMx manifestVars <- paste0("x", 1:9) latVar <- c("visual", "textual", "speed") model <- mxModel("Simple SEM", type = "RAM", manifestVars = manifestVars, latentVars = latVar, mxPath(from = "visual", to = c("x1", "x2", "x3")), mxPath(from = "textual", to = c("x4", "x5", "x6")), mxPath(from = "speed", to = c("x7", "x8", "x9")), mxPath(from = manifestVars, arrows = 2), mxPath(from = latVar, arrows = 2, free = FALSE, values = 1.0), mxPath(from = "one", to = manifestVars, arrows = 1, free = FALSE, values = 0), mxPath(from = "one", to = latVar, arrows = 1, free = FALSE, values = 0), mxData(HolzingerSwineford1939, type = "raw") ) # # Fit the model fit0 <- mxRun(model) tidy(fit0) ## End(Not run)