Package {wsMed}


Title: Within-Subject Mediation Analysis Using Structural Equation Modeling
Version: 1.1.0
Description: Within-subject mediation analysis using structural equation modeling. Examine how changes in an outcome variable between two conditions are mediated through one or more variables. Supports within-subject mediation analysis using the 'lavaan' package by Rosseel (2012) <doi:10.18637/jss.v048.i02>, and extends Monte Carlo confidence interval estimation to missing data scenarios using the 'semmcci' package by Pesigan and Cheung (2023) <doi:10.3758/s13428-023-02114-4>.
License: GPL (≥ 3)
Depends: R (≥ 4.1.0)
Encoding: UTF-8
RoxygenNote: 7.3.3
Suggests: rmarkdown, devtools, testthat
Config/testthat/edition: 3
Imports: knitr, lavaan, semmcci, mice, semboottools, MASS, rlang, dplyr, ggplot2, methods, stats
VignetteBuilder: knitr
LazyData: true
URL: https://yangzhen1999.github.io/wsMed/
BugReports: https://github.com/Yangzhen1999/wsMed/issues
NeedsCompilation: no
Author: Wendie Yang ORCID iD [aut, cre], Shu Fai Cheung ORCID iD [aut]
Maintainer: Wendie Yang <1581075494q@gmail.com>
Packaged: 2026-09-18 03:25:38 UTC; Administrator
Repository: CRAN
Date/Publication: 2026-09-18 04:40:02 UTC

Clean all CI column names into the standard form

Description

Clean all CI column names into the standard form

Usage

.clean_ci_names(df, ci_level = 0.95)

Fit SEM and run Monte-Carlo draws

Description

Fit SEM and run Monte-Carlo draws

Usage

.fit_and_mc(
  sem_model,
  data,
  Na = c("DE", "FIML"),
  R = 20000,
  alpha = 0.05,
  fixed.x = FALSE,
  verbose = TRUE,
  run_mc = TRUE,
  seed = NULL
)

Make CI column names like "2.5%CI.Lo / 97.5%CI.Up"

Description

Make CI column names like "2.5%CI.Lo / 97.5%CI.Up"

Usage

.make_ci_names(ci)

Create moderation output for wsMed

Description

Create moderation output for wsMed

Usage

.make_moderation(
  mc_res,
  data,
  W = NULL,
  MP = NULL,
  W_type = c("categorical", "continuous", "none"),
  alpha = 0.05,
  verbose = FALSE,
  point_estimates = NULL
)

Verbose message wrapper (internal)

Description

Verbose message wrapper (internal)

Usage

.v(..., verbose = TRUE)

Average Relative Increase in Variance

Description

Average Relative Increase in Variance

Usage

ARIVwrapper(between, within, M, k)

Arguments

between

Numeric matrix. Covariance between imputations \mathbf{V}_{\mathrm{between}}.

within

Numeric matrix. Covariance within imputations \mathbf{V}_{\mathrm{within}}.

M

Positive integer. Number of imputations.

k

Positive integer. Number of parameters.

Details

The average relative increase in variance is given by

\mathrm{ARIV} = \left( 1 + M^{-1} \right) \mathrm{tr} \left( \mathbf{V}_{\mathrm{between}} \mathbf{V}_{\mathrm{within}}^{-1} \right)

Value

Returns a numeric vector of length one.

Author(s)

Ivan Jacob Agaloos Pesigan


Generate Chained Mediation Model

Description

Dynamically generates a structural equation modeling (SEM) syntax for chained mediation analysis based on the prepared dataset. The function computes regression equations for mediators and the outcome variable, indirect effects along multi-step mediation paths, total effects, contrasts between indirect effects, and coefficients in different conditions.

Usage

GenerateModelCN(prepared_data, MP = character(0))

Arguments

prepared_data

A data frame returned by PrepareData(), containing the processed within-subject mediator and outcome variables. The data frame must include columns for difference scores (Mdiff) and average scores (Mavg) of mediators, as well as the outcome difference score (Ydiff).

MP

A character vector specifying which paths are moderated by variable(s) W. Valid entries include: - "a2", "a3", ...: moderation on the a paths (W → Mdiff), for mediators beyond M1. - "b2", "b3", ...: moderation on the b paths (Mdiff × W → Ydiff). - "b_1_2", "b_2_3", ...: moderation on cross-paths from one mediator to the next (e.g., M1 → M2). - "d2", "d3", ...: moderation on the d paths (Mavg × W → Ydiff). - "d_1_2", "d_2_3", ...: moderation on cross-paths from one Mavg to the next Mdiff. - "cp": moderation on the direct effect from X to Y (i.e., W → Ydiff).

      The function detects and inserts the correct interaction terms (e.g., \code{int_M2diff_W1}) based on these labels.

Details

This function is used to construct SEM models for chained mediation analysis. It automatically parses variable names from the prepared dataset and dynamically creates the necessary model syntax, including:

This model is suitable for chained mediation designs where mediators influence each other in a sequential manner, forming multi-step mediation paths.

Value

A character string representing the SEM model syntax for the specified chained mediation analysis.

See Also

PrepareData(), wsMed(), GenerateModelP()

Examples

# Example prepared data
prepared_data <- data.frame(
  M1diff = rnorm(100),
  M2diff = rnorm(100),
  M3diff = rnorm(100),
  M1avg = rnorm(100),
  M2avg = rnorm(100),
  M3avg = rnorm(100),
  Ydiff = rnorm(100)
)

# Generate SEM model syntax
sem_model <- GenerateModelCN(prepared_data)
cat(sem_model)


Generate Combined Parallel and Chained Mediation Model

Description

Dynamically generates a structural equation modeling (SEM) syntax for combined parallel and chained mediation analysis based on the prepared dataset. The function computes regression equations for mediators and the outcome variable, indirect effects for both parallel and chained mediation paths, total effects, contrasts between indirect effects, and coefficients in different X conditions.

Usage

GenerateModelCP(prepared_data, MP = character(0))

Arguments

prepared_data

A data frame returned by PrepareData(), containing the processed within-subject mediator and outcome variables. The data frame must include columns for difference scores (Mdiff) and average scores (Mavg) of mediators, as well as the outcome difference score (Ydiff).

MP

A character vector specifying which paths are moderated by variable(s) W. Acceptable values include: - "a2", "a3", ...: moderation on the a paths of parallel mediators (W → Mdiff). - "b2", "b3", ...: moderation on the b paths of parallel mediators (Mdiff × W → Ydiff). - "b_1_2", "b_1_3", ...: moderation on the paths from the chain mediator to parallel mediators. - "d_1_2", "d_1_3", ...: moderation on the paths from the chain mediator’s Mavg to parallel mediators. - "cp": moderation on the direct effect from X to Y (i.e., W → Ydiff).

      Each entry triggers inclusion of W’s main effect or interaction terms (e.g., \code{int_Mdiff_W}).

Details

This function is used to construct SEM models that combine parallel and chained mediation analysis. It automatically parses variable names from the prepared dataset and dynamically creates the necessary model syntax, including:

This model is suitable for designs where mediators include both a sequential chain (chained mediation) and independent parallel mediators.

Value

A character string representing the SEM model syntax for the specified combined parallel and chained mediation analysis.

See Also

PrepareData(), wsMed(), GenerateModelP(), GenerateModelCN()

Examples

# Example prepared data
prepared_data <- data.frame(
  M1diff = rnorm(100),
  M2diff = rnorm(100),
  M3diff = rnorm(100),
  M1avg = rnorm(100),
  M2avg = rnorm(100),
  M3avg = rnorm(100),
  Ydiff = rnorm(100)
)

# Generate SEM model syntax
sem_model <- GenerateModelCP(prepared_data)
cat(sem_model)


Generate a User-Defined Within-Subject Mediation Model

Description

Generates lavaan model syntax for a user-defined within-subject mediation model. Users specify directed paths among mediators and from mediators to the outcome. The function automatically adds the corresponding difference and level components and identifies all indirect effects.

Usage

GenerateModelCustom(prepared_data, paths, MP = character(0))

Arguments

prepared_data

A data frame returned by [PrepareData()]. It must contain 'M1diff', 'M1avg', ..., and 'Ydiff'.

paths

A character vector defining directed paths among mediators and from mediators to the outcome. For example: 'c("M1 -> M2", "M2 -> Y")'.

MP

A character vector identifying moderated paths. Supported labels follow the existing wsMed convention: 'a1', 'b1', 'd1', 'b_1_2', 'd_1_2', and 'cp'.

Main effects of W are always labeled and used for conditional intercepts, including when a/cp is not explicitly listed in MP.

Value

A character string containing lavaan model syntax.

Examples

prepared_data <- data.frame(
  M1diff = rnorm(100),
  M1avg  = rnorm(100),
  M2diff = rnorm(100),
  M2avg  = rnorm(100),
  M3diff = rnorm(100),
  M3avg  = rnorm(100),
  Ydiff  = rnorm(100)
)

model <- GenerateModelCustom(
 prepared_data = prepared_data,
 paths = c(
   "M1 -> M3",
   "M1 -> Y",
   "M3 -> Y",
   "M2 -> Y"
 )
)

cat(model)


Generate Parallel Mediation Model

Description

Dynamically generates a structural equation modeling (SEM) syntax for parallel mediation analysis based on the prepared dataset. The function computes regression equations for mediators and the outcome variable, indirect effects, total effects, contrasts between indirect effect, and .

Usage

GenerateModelP(prepared_data, MP = character(0))

Arguments

prepared_data

A data frame returned by PrepareData(), containing the processed within-subject mediator and outcome variables. The data frame must include columns for difference scores (Mdiff) and average scores (Mavg) of mediators, as well as the outcome difference score (Ydiff).

MP

A character vector specifying which paths are moderated by variable(s) W. Valid values include: - "a1", "a2", ...: moderation on the a paths (W → Mdiff). - "b1", "b2", ...: moderation on the b paths (Mdiff × W → Ydiff). - "d1", "d2", ...: moderation on the d paths (Mavg × W → Ydiff). - "cp": moderation on the direct effect from X to Y (i.e., W → Ydiff).

      This argument controls which interaction terms (e.g., \code{int_Mdiff_W}, \code{int_Mavg_W}) are
      added to the corresponding regression equations.

Details

This function is used to construct SEM models for parallel mediation analysis. It automatically parses variable names from the prepared dataset and dynamically creates the necessary model syntax, including:

This model is suitable for parallel mediation designs where multiple mediators act independently.

Value

A character string representing the SEM model syntax for the specified parallel mediation analysis.

See Also

PrepareData(), wsMed(), GenerateModelCN()

Examples

# Example prepared data
prepared_data <- data.frame(
  M1diff = rnorm(100),
  M2diff = rnorm(100),
  M1avg = rnorm(100),
  M2avg = rnorm(100),
  Ydiff = rnorm(100)
)

# Generate SEM model syntax
sem_model <- GenerateModelP(prepared_data)
cat(sem_model)

Generate Parallel and Chained Mediation Model

Description

Dynamically generates a structural equation modeling (SEM) syntax for mediation analysis that integrates both parallel and chained mediators. Unlike the Combined Parallel and Chained mediation model (GenerateModelCP), this function assumes that the chained mediator receives inputs from the parallel mediators and directly influences the outcome variable, emphasizing a downstream role for the chained mediator.

Usage

GenerateModelPC(prepared_data, MP = character(0))

Arguments

prepared_data

A data frame returned by PrepareData(), containing the processed within-subject mediator and outcome variables. The data frame must include columns for difference scores (Mdiff) and average scores (Mavg) of mediators, as well as the outcome difference score (Ydiff).

MP

A character vector specifying which paths are moderated by variable(s) W. Acceptable values include: - "a2", "a3", ...: moderation on the a paths (W → Mdiff). - "b2", "b3", ...: moderation on the b paths (Mdiff × W → Ydiff). - "b_2_1", "b_3_1", ...: moderation on the cross-paths from parallel mediators to the chain mediator (e.g., M2 → M1). - "d_2_1", "d_3_1", ...: moderation on the paths from parallel mediators’ centered means to M1. - "cp": moderation on the direct effect of X on Y (i.e., W → Ydiff).

      This argument controls which interaction terms (e.g., \code{int_Mdiff_W}, \code{int_Mavg_W}) are included
      in the regression equations. Variable names are automatically matched using the naming convention
      \code{"int_<predictor>_W<index>"}.

Details

This function is designed to build SEM models that integrate parallel and chained mediation structures. It automatically identifies variable names from the prepared dataset and generates the necessary model syntax, including:

This model is suitable for designs where mediators include both independent parallel paths and sequential chained paths, providing a comprehensive mediation analysis framework.

Value

A character string representing the SEM model syntax for the specified parallel and chained mediation analysis.

See Also

PrepareData(), wsMed(), GenerateModelP(), GenerateModelCN()

Examples

# Example prepared data
prepared_data <- data.frame(
  M1diff = rnorm(100),
  M2diff = rnorm(100),
  M3diff = rnorm(100),
  M1avg = rnorm(100),
  M2avg = rnorm(100),
  M3avg = rnorm(100),
  Ydiff = rnorm(100)
)

# Generate SEM model syntax
sem_model <- GenerateModelPC(prepared_data)
cat(sem_model)


Impute Missing Data Using Multiple Imputation

Description

The ImputeData function performs multiple imputation on a data frame with missing values using the mice package. It handles missing data by creating multiple imputed datasets based on a specified imputation method and returns a list of completed data frames.

Usage

ImputeData(
  data_missing,
  m = 5,
  method = "pmm",
  seed = 123,
  predictorMatrix = NULL
)

Arguments

data_missing

A data frame containing missing values to be imputed. The function replaces values coded as -999 with NA before imputation.

m

An integer specifying the number of imputed datasets to generate.

method

A character string specifying the imputation method. Default is "pmm" (predictive mean matching).

seed

An integer for setting the random seed to ensure reproducibility. Default is 123.

predictorMatrix

An optional matrix specifying the predictor structure for the imputation model. Default is NULL, meaning that the function will use the default predictor matrix created by mice.

Details

This function replaces specified missing value placeholders (e.g., -999) with NA, and then applies the multiple imputation by chained equations (MICE) procedure to generate multiple imputed datasets. It supports flexible imputation methods and allows for specifying a custom predictor matrix.

Value

A list of m imputed data frames.

Author(s)

Wendie Yang, Shufai Cheung

Examples

# Example data with missing values
data <- data.frame(
  M1 = c(rnorm(99), rep(NA, 1)),
  M2 = c(rnorm(99), rep(NA, 1)),
  Y1 = rnorm(100),
  Y2 = rnorm(100)
)
# Perform multiple imputation
imputed_data_list <- ImputeData(data, m = 5)
# Display the first imputed dataset
head(imputed_data_list[[1]])


Convert Lavaan Model to RAM Matrices

Description

Converts a lavaan-style matrix list into RAM (Reticular Action Model) format, including the A (asymmetric paths), S (symmetric paths), F (filter matrix), and M (means/intercepts) matrices.

Usage

Lav2RAM2(lav_mod)

Arguments

lav_mod

A named list of lavaan matrices including lambda, beta, theta, psi, and alpha.

Details

This function reorganizes the lavaan-style matrices into the RAM representation, commonly used for model standardization and transformation.

Value

A list with components:

A

Asymmetric path matrix (including factor loadings and structural paths)

S

Symmetric path matrix (variances and covariances)

F

Filter matrix mapping latent and observed variables

M

Row vector of intercepts/means


Process Monte Carlo Samples for Defined Parameters in SEM

Description

A wrapper for the internal .MCDef() function from the semmcci package. This function processes Monte Carlo samples to compute defined parameters for structural equation modeling (SEM).

Usage

MCDefWrapper(object, thetahat, thetahatstar_orig)

Arguments

object

A fitted lavaan SEM model object.

thetahat

A numeric vector of parameter estimates.

thetahatstar_orig

A matrix of Monte Carlo samples, where rows represent samples and columns represent parameters.

Details

This function takes Monte Carlo samples of parameter estimates and a fitted SEM model object to compute the defined parameters (e.g., indirect effects or user-defined contrasts) based on the model syntax. It is particularly useful for examining derived quantities in SEM analyses using Monte Carlo methods.

Value

A matrix of computed defined parameters for each Monte Carlo sample.

See Also

MCMI2(), RunMCMIAnalysis()


Monte Carlo Confidence Intervals for Multiple Imputation SEM Models

Description

Computes Monte Carlo confidence intervals (MCCI) for structural equation models (SEM) fitted to multiple imputed datasets. This function integrates SEM fitting across imputed datasets, pools the results, and generates confidence intervals through Monte Carlo sampling.

Usage

MCMI2(
  sem_model,
  imputations,
  R = 20000L,
  alpha = c(0.001, 0.01, 0.05),
  decomposition = "eigen",
  pd = TRUE,
  tol = 1e-06,
  seed = NULL,
  estimator = "ML",
  se = "standard",
  missing = "listwise",
  fixed.x = FALSE
)

Arguments

sem_model

A character string specifying the SEM model syntax.

imputations

A list of data frames, where each data frame represents an imputed dataset.

R

An integer specifying the number of Monte Carlo samples. Default is '20000L'.

alpha

A numeric vector specifying significance levels for the confidence intervals. Default is 'c(0.001, 0.01, 0.05)'.

decomposition

A character string specifying the decomposition method for the covariance matrix. Default is '"eigen"'. Options include '"chol"', '"eigen"', or '"svd"'.

pd

A logical value indicating whether to ensure positive definiteness of the covariance matrix. Default is 'TRUE'.

tol

A numeric value specifying the tolerance for positive definiteness checks. Default is '1e-06'.

seed

An optional integer specifying the random seed for reproducibility. Default is 'NULL'.

estimator

A character string specifying the estimator for SEM fitting. Default is '"ML"' (Maximum Likelihood).

se

A character string specifying the type of standard errors to compute. Default is '"standard"'.

missing

A character string specifying the method for handling missing data in SEM fitting. Default is '"listwise"'.

fixed.x

Whether exogenous covariates are treated as fixed in every imputation. Defaults to FALSE, consistently with wsMed().

Details

This function is designed for SEM models that require multiple imputation to handle missing data. It performs the following steps:

- **SEM Fitting**: Fits the specified SEM model to each imputed dataset using [lavaan::sem()].

- **Pooling Results**: Combines parameter estimates and covariance matrices across imputations using Rubin's rules.

- **Monte Carlo Sampling**: Generates Monte Carlo samples based on the pooled estimates and covariance matrices, and calculates confidence intervals for model parameters.

This function supports custom estimators, handling of missing data, and precision adjustments for Monte Carlo sampling. It is particularly useful for mediation analysis or complex SEM models where missing data are addressed using multiple imputation.

Value

An object of class 'semmcci' containing: - 'call': The matched function call. - 'args': A list of input arguments. - 'thetahat': The pooled parameter estimates. - 'thetahatstar': Monte Carlo samples for parameter estimates. - 'fun': The name of the function ('"MCMI2"').

See Also

[lavaan::sem()], [semmcci::MC()], [semmcci::MCStd()]

Examples

# Example SEM model
sem_model <- "
  Ydiff ~ b1 * M1diff + cp * 1
  M1diff ~ a1 * 1
  indirect := a1 * b1
  total := cp + indirect
"

# Example imputed datasets
imputations <- list(
  data.frame(M1diff = rnorm(100), Ydiff = rnorm(100)),
  data.frame(M1diff = rnorm(100), Ydiff = rnorm(100))
)

# Compute Monte Carlo confidence intervals
result <- MCMI2(
  sem_model = sem_model,
  imputations = imputations,
  R = 1000,
  alpha = c(0.05, 0.01),
  seed = 123
)

Monte Carlo Summary for Standardized Estimates

Description

Computes standardized estimates, standard errors, and confidence intervals based on Monte Carlo samples from a 'semmcci' object. This function fully standardizes both point estimates and sampling distributions (including intercepts).

Usage

MCStd2(mc, alpha = c(0.001, 0.01, 0.05))

Arguments

mc

A Monte Carlo result object of class 'semmcci', typically from 'MC()' or 'MCMI()'.

alpha

A numeric vector of significance levels (default: 'c(0.001, 0.01, 0.05)').

Details

The function standardizes the sampling distribution using 'StdLav2()' on each Monte Carlo draw, then summarizes the distribution into SEs and quantile-based confidence intervals. Invalid draws are excluded jointly and reported in the standardization_diagnostics attribute (requested, valid, invalid, indices and reasons).

Value

A data frame containing:

Parameter

Parameter name

Estimate

Standardized point estimate

SE

Standard deviation of standardized samples

R

Number of valid joint Monte Carlo replications

CI columns

Multiple confidence intervals based on 'alpha'


Wrapper for Internal Multiple Imputation Combining Function

Description

A wrapper function for the internal .MICombine() function from the semmcci package. This function pools parameter estimates and covariance matrices from multiple imputed datasets using Rubin's rules.

Usage

MICombineWrapper(coefs, vcovs, M, k, adj = FALSE)

Arguments

coefs

A list of numeric vectors, where each vector contains the parameter estimates from one imputed dataset.

vcovs

A list of numeric matrices, where each matrix represents the covariance matrix of the parameter estimates for one imputed dataset.

M

An integer indicating the number of imputations.

k

An integer specifying the number of parameters in the SEM model.

adj

A logical value indicating whether to apply an adjustment for finite samples. Default is TRUE.

Details

This wrapper provides an abstraction over the internal .MICombine() function from the semmcci package, allowing it to be used within your package without directly exposing the internal function. It is primarily designed for use within workflows that involve multiple imputation and structural equation modeling (SEM). Rubin's rules are applied to compute pooled parameter estimates and their covariance matrices.

Value

A list containing the pooled estimates and covariance matrix:

See Also

RunMCMIAnalysis()


Prepare Data for Two-Condition Within-Subject Mediation (WsMed)

Description

PrepareData() transforms raw pre/post data into the set of variables required by the WsMed workflow. It handles mediators, outcome, within-subject controls, between-subject controls, moderators, and all necessary interaction terms, while automatically centering / dummy-coding variables as needed.

Usage

PrepareData(
  data,
  M_C1,
  M_C2,
  Y_C1,
  Y_C2,
  C_C1 = NULL,
  C_C2 = NULL,
  C = NULL,
  C_type = NULL,
  W = NULL,
  W_type = NULL,
  center_W = TRUE,
  keep_W_raw = TRUE,
  keep_C_raw = TRUE
)

Arguments

data

A data frame with the raw pre/post measures.

M_C1, M_C2

Character vectors: mediator names at occasion 1 and 2 (equal length).

Y_C1, Y_C2

Character scalars: outcome names at occasion 1 and 2.

C_C1, C_C2

Optional character vectors: within-subject control names.

C

Optional character vector: between-subject control names.

C_type

Optional vector of the same length as C. Each element is one of "continuous", "categorical", or "auto" (default). Ignored when C = NULL.

W

Optional character vector: moderator names (one or more).

W_type

Optional vector of the same length as W. Same coding as C_type. Ignored when W = NULL.

center_W

Logical. Whether to center the moderator variable W.

keep_W_raw, keep_C_raw

Logical. If TRUE, keep the original W / C columns in the returned data.

Details

The function performs the following steps:

  1. Outcome difference: Ydiff = Y_C2 - Y_C1.

  2. Mediator variables for each pair (M_C1[i], M_C2[i]):

    • Mi_diff = M_C2 - M_C1

    • Mi_avg is the mean-centered average of the two occasions.

  3. Between-subject controls C:

    • Continuous variables are grand-mean centered (Cb1, Cb2, ...).

    • Categorical variables (binary or multi-level) are expanded into k - 1 dummy variables (Cb1_1, Cb2_1, Cb2_2, ...), using the first level as the reference.

  4. Within-subject controls Cw: difference and centered-average versions (Cw1diff, Cw1avg, ...).

  5. Moderators W (one or more):

    • Continuous variables are grand-mean centered (W1, W2, ...).

    • Categorical variables are dummy-coded in the same way as C.

  6. Interaction terms between each moderator column and each mediator column:

    • int_<Mi_diff>_<Wj>, int_<Mi_avg>_<Wj>.

  7. Two attributes are added to the returned data:

    • "W_info": raw names, dummy names, level mapping

    • "C_info": same structure for between-subject controls.

Row counts are preserved even if input factors contain NA values (model.matrix is called with na.action = na.pass).

Value

A data frame containing at minimum:

plus the attributes "W_info" and "C_info" described above.

See Also

PrepareMissingData, GenerateModelP, wsMed

Examples

set.seed(1)
raw <- data.frame(
  A1 = rnorm(50), A2 = rnorm(50),   # mediator 1
  B1 = rnorm(50), B2 = rnorm(50),   # mediator 2
  C1 = rnorm(50), C2 = rnorm(50),   # outcome
  D1 = rnorm(50), D2 = rnorm(50),   # within-subject control
  W_bin  = sample(0:1, 50, TRUE),   # between-subject binary C
  W_fac3 = factor(sample(c("Low","Med","High"), 50, TRUE)) # moderator W
)

prep <- PrepareData(
  data  = raw,
  M_C1  = c("A1","B1"), M_C2 = c("A2","B2"),
  Y_C1  = "C1",         Y_C2 = "C2",
  C_C1  = "D1",         C_C2 = "D2",
  C     = "W_bin",      C_type = "categorical",
  W     = "W_fac3",     W_type = "categorical"
)
head(prep)


Prepare Data with Missing Values for Mediation Analysis

Description

Handles missing values in the dataset through multiple imputation and prepares the imputed datasets for within-subject mediation analysis. The function imputes missing data, processes each imputed dataset, and provides diagnostics for the imputation process.

Usage

PrepareMissingData(
  data_missing,
  m = 5,
  method_num = "pmm",
  seed = 123,
  M_C1,
  M_C2,
  Y_C1,
  Y_C2,
  C_C1 = NULL,
  C_C2 = NULL,
  C = NULL,
  C_type = NULL,
  W = NULL,
  W_type = NULL,
  center_W = TRUE,
  keep_W_raw = TRUE,
  keep_C_raw = TRUE
)

Arguments

data_missing

A data frame containing the raw dataset with missing values.

m

An integer specifying the number of imputations to perform. Default is 5.

method_num

Character; imputation method for numeric variables (for example, "pmm", "norm"). Default is "pmm".

seed

An integer specifying the random seed for reproducibility. Default is 123.

M_C1

A character vector of column names representing mediators at condition 1.

M_C2

A character vector of column names representing mediators at condition 2. Must match the length of M_C1.

Y_C1

A character string representing the column name of the outcome variable at condition 1.

Y_C2

A character string representing the column name of the outcome variable at condition 2.

C_C1

Character vector of within-subject control variable names (condition 1).

C_C2

Character vector of within-subject control variable names (condition 2).

C

Character vector of between-subject control variable names.

C_type

Optional vector of the same length as C. Each element is "continuous", "categorical", or "auto" (default). Ignored when C = NULL.

W

Optional character vector: moderator names (at most J).

W_type

Optional vector of the same length as W. Same coding as C_type. Ignored when W = NULL.

center_W

Logical. Whether to center the moderator variable W.

keep_W_raw, keep_C_raw

Logical; keep the original W / C columns in the returned data?

Details

This function is designed to preprocess datasets with missing values for mediation analysis. It performs the following steps:

This function integrates imputation and data preparation, ensuring that the resulting datasets are ready for subsequent mediation analysis.

Value

A list containing:

processed_data_list

A list of m data frames, each representing an imputed and processed dataset ready for within-subject mediation analysis.

imputation_summary

A summary of the imputation process, including diagnostics and convergence information.

See Also

PrepareData, ImputeData, wsMed

Examples

# Example dataset with missing values
data("example_data", package = "wsMed")
set.seed(123)
example_dataN <- mice::ampute(
  data = example_data,
  prop = 0.1
)$amp

# Prepare the dataset with multiple imputations
prepared_missing_data <- PrepareMissingData(
  data_missing = example_dataN,
  m = 5,
  M_C1 = c("A2", "B2"),
  M_C2 = c("A1", "B1"),
  Y_C1 = "C2",
  Y_C2 = "C1"
)

# Access processed datasets
processed_data_list <- prepared_missing_data$processed_data_list
imputation_summary  <- prepared_missing_data$imputation_summary


Convert Standardized RAM Back to Lavaan Matrices

Description

Converts a standardized RAM object back to lavaan-style matrix structure. Preserves residual covariances on their transformed scales.

Usage

RAM2Lav2(ram, lav_mod, standardized = FALSE)

Arguments

ram

A RAM list containing standardized matrices ('A', 'S', 'F', and 'M').

lav_mod

A lavaan-style matrix list (e.g., GLIST) to be updated.

standardized

Retained for compatibility; RAM covariances are already rescaled.

Details

This function restores the internal lavaan model matrix structure from a RAM representation.

Value

A modified lavaan-style matrix list with updated 'lambda', 'beta', 'theta', 'psi', and 'alpha'.


Generate Random Variates from the Gaussian Distribution (Singular Value Decomposition)

Description

Generate Random Variates from the Gaussian Distribution (Singular Value Decomposition)

Usage

RandomGaussianSVDwrapper(Z, svd)

Arguments

Z

Numeric matrix. n by k matrix of independent random variates from the standard univariate normal distribution \mathbf{Z}.

svd

Object. Result of svd().

Value

Numeric matrix.

Author(s)

Ivan Jacob Agaloos Pesigan


Monte Carlo SEM with Multiple Imputation (WsMed Workflow)

Description

'RunMCMIAnalysis()' is a helper that:

  1. imputes missing data via PrepareMissingData;

  2. generates all WsMed variables in each completed data set;

  3. fits the user-supplied SEM model to each replicate; and

  4. pools the results via MCMI2(), producing Monte Carlo confidence intervals (MCCI) for all model parameters.

Usage

RunMCMIAnalysis(
  data_missing,
  m = 5,
  method_num = "pmm",
  seed = 123,
  M_C1,
  M_C2,
  Y_C1,
  Y_C2,
  C_C1 = NULL,
  C_C2 = NULL,
  C = NULL,
  C_type = NULL,
  W = NULL,
  W_type = NULL,
  keep_W_raw = TRUE,
  keep_C_raw = TRUE,
  sem_model,
  Na = "MI",
  R = 20000L,
  alpha = c(0.001, 0.01, 0.05),
  decomposition = "eigen",
  pd = TRUE,
  tol = 1e-06,
  fixed.x = FALSE
)

Arguments

data_missing

Data frame with missing values.

m

Integer, number of imputations. Default 5.

method_num

Character, imputation method for numeric variables (e.g., "pmm", "norm"). Default "pmm".

seed

Integer random seed (passed to mice and MCMI2).

M_C1, M_C2

Character vectors: mediator names at condition 1 & 2 (same length).

Y_C1, Y_C2

Character scalars: outcome names at condition 1 & 2.

C_C1, C_C2

Optional character vectors: within-subject controls.

C

Optional character vector: between-subject controls.

C_type

Optional vector (length = C); each element "continuous", "categorical", or "auto" (default).

W

Optional character vector: moderator name(s).

W_type

Optional vector (length = W); same coding as C_type.

keep_W_raw, keep_C_raw

Logical; keep raw W / C columns in the processed data? Defaults TRUE.

sem_model

Character string, lavaan syntax of the SEM to be fitted.

Na

Character, missing-data strategy. Currently only "MI" is implemented.

R

Integer, number of Monte Carlo samples (default 20000L).

alpha

Numeric vector, significance levels for two-sided CIs (default c(0.001, 0.01, 0.05)).

decomposition

Decomposition used by MCMI2() ("eigen" | "chol" | "svd"). Default "eigen".

pd

Logical, enforce positive-definite covariance (default TRUE).

tol

Numeric tolerance for PD checks. Default 1e-6.

fixed.x

Whether to treat exogenous covariates as fixed in every imputation.

Details

Internally the function calls:

Only the missing-data strategy Na = "MI" is supported.

Value

A list with three elements:

mc_result

A semmcci object returned by MCMI2().

first_imputed_data

The first processed data frame (useful for inspection or plotting).

imputation_summary

Diagnostics from PrepareMissingData().

See Also

PrepareMissingData, PrepareData, MCMI2, wsMed


Standardize Parameter Estimates in a Lavaan Model

Description

Applies full standardization (including intercepts) to a fitted lavaan model by converting to RAM form, performing standardization, and converting back to lavaan matrix structure.

Usage

StdLav2(est, object)

Arguments

est

A numeric vector of parameter estimates (free parameters).

object

A fitted lavaan model object (used to extract model structure).

Details

The function extracts the model's RAM representation via 'Lav2RAM2', applies 'StdRAM2' standardization, restores the standardized GLIST via 'RAM2Lav2', and retrieves standardized user-defined parameter estimates with 'lav_model_get_parameters()'.

Value

A numeric vector of fully standardized parameter estimates (including intercepts and defined parameters).


Standardize RAM Matrices

Description

Performs standardization of RAM matrices by rescaling path and variance structures using the implied covariance matrix. Intercepts are also standardized.

Usage

StdRAM2(ram_est, roles = NULL)

Arguments

ram_est

A RAM object list with matrices 'A', 'S', 'F', and 'M' as returned by 'Lav2RAM2()'.

roles

Internal list of dummy variable names and interaction components.

Details

The function computes the implied covariance matrix \Sigma = (I - A)^{-1} S (I - A)^{-T}, extracts standard deviations, and performs standardization via D^{-1} scaling.

Value

A list of standardized RAM matrices:

A

Standardized asymmetric path matrix

S

Standardized symmetric path matrix

F

Unchanged filter matrix

M

Standardized intercept vector


Test for a Positive Definite Matrix

Description

Returns TRUE if input is a positive definite matrix, and FALSE otherwise.

Usage

TestPositiveDefinitewrapper(eigen, tol = 1e-06)

Arguments

eigen

output of the eigen() function.

tol

Numeric. Tolerance.

Details

A k \times k symmetric matrix \mathbf{A} is positive definite if all of its eigenvalues are positive.

Value

Logical.

References

Wikipedia: Definite matrix


Monte Carlo Sampling for Parameter Estimates

Description

Generates Monte Carlo samples for parameter estimates using a covariance matrix and a location vector. This function is a wrapper for the internal .ThetaHatStar() function from the semmcci package.

Usage

ThetaHatStarWrapper(
  R = 20000L,
  scale,
  location,
  decomposition = "eigen",
  pd = TRUE,
  tol = 1e-06
)

Arguments

R

Integer. Number of Monte Carlo samples to generate.

scale

Numeric matrix. The covariance matrix of the parameter estimates.

location

Numeric vector. The mean (or location) of the parameter estimates.

decomposition

Character. Decomposition method for the covariance matrix. Options: "chol" (Cholesky), "eigen" (eigenvalue decomposition), "svd" (singular value decomposition). Default is "eigen".

pd

Logical. Ensure positive definiteness of the covariance matrix. Default is TRUE.

tol

Numeric. Tolerance for positive definiteness checks. Default is 1e-06.

Value

A list containing:

See Also

MCMI2(), RunMCMIAnalysis()


Compute Updated Parameter Estimates for SEM Models

Description

A wrapper for the internal .ThetaHat() function from the semmcci package. This function computes updated parameter estimates for structural equation models (SEM) using pooled estimates from multiple imputations or Monte Carlo simulations.

Usage

ThetaHatWrapper(object, est = NULL)

Arguments

object

A fitted lavaan SEM model object.

est

A numeric vector of pooled parameter estimates, typically obtained from multiple imputations or Monte Carlo simulations.

Details

The function takes a fitted SEM model object and pooled parameter estimates to calculate the updated parameter values. It is particularly useful for combining results from multiple imputations or Monte Carlo samples to refine parameter estimates.

Value

A numeric vector of updated parameter estimates.

See Also

MCMI2(), RunMCMIAnalysis()

Examples

NULL


Adjusted Total Sampling Covariance Matrix

Description

Adjusted Total Sampling Covariance Matrix

Usage

TotalAdjwrapper(ariv, within)

Arguments

ariv

Numeric. Average relative increase in variance.

within

Numeric matrix. Covariance within imputations \mathbf{V}_{\mathrm{within}}.

Details

The adjusted total sampling covariance matrix is given by

\tilde{\mathbf{V}}_{\mathrm{total}} = \left( 1 + \mathrm{ARIV} \right) \mathbf{V}_{\mathrm{within}}

Author(s)

Ivan Jacob Agaloos Pesigan


Apply PrepareData to Imputed Datasets and Return New MIDS Object

Description

This function applies the PrepareData() preprocessing step to each imputed dataset in a mids object and returns a new mids object with the transformed data. It is designed for use in within-subject mediation analysis pipelines when working with multiply imputed data.

Usage

TransformMidsWithPrepareData(mids_obj, M_C1, M_C2, Y_C1, Y_C2)

Arguments

mids_obj

A mids object created by the mice package, containing multiply imputed datasets.

M_C1

A character vector specifying the names of mediator variables under Condition 1 (e.g., pre-test).

M_C2

A character vector specifying the names of mediator variables under Condition 2 (e.g., post-test).

Y_C1

A character string specifying the outcome variable under Condition 1.

Y_C2

A character string specifying the outcome variable under Condition 2.

Details

The function performs the following steps:

This enables subsequent analyses to operate on a version of the multiply imputed data where difference scores and averages have already been computed.

Value

A new mids object where each imputed dataset has been processed using PrepareData().


Append significance stars based on CI

Description

Append significance stars based on CI

Usage

add_sig(df)

Summarise Effects with a Continuous Moderator

Description

'analyze_mm_continuous()' summarises Monte Carlo draws from a 'semmcci' object when the moderator 'W' is continuous. It reports moderated path coefficients, conditional indirect effects, conditional path coefficients, and effect curves across values of the moderator.

Usage

analyze_mm_continuous(
  mc_result,
  data,
  MP,
  W_raw_name = "W",
  ci_level = 0.95,
  W_values = NULL,
  n_curve = 120,
  digits = 8,
  point_estimates = NULL
)

Arguments

mc_result

A 'semmcci' object returned by 'MCMI2()'.

data

A processed data frame containing the original moderator variable. This is typically the first component returned by 'PrepareData()'.

MP

Requested focal paths; all fitted W main effects enter conditional effects.

W_raw_name

A character string giving the name of the moderator variable in 'data'. The default is '"W"'.

ci_level

A numeric value between zero and one specifying the two-sided confidence level. The default is '0.95'.

W_values

An optional numeric vector containing at least two distinct raw moderator values at which to evaluate the conditional effects. If 'NULL', the moderator mean and values one standard deviation below and above the mean are used.

n_curve

A positive integer specifying the number of moderator values used to construct each effect curve. The default is '120'.

digits

A non-negative integer specifying the number of decimal places used to round the reported results. The default is '8'.

point_estimates

Named plug-in parameter estimates. If omitted, the means of primitive coefficient draws are used.

Details

The function first summarises the coefficients associated with moderated paths, including interaction terms such as 'aw', 'bw', 'dw', and 'cpw', together with their corresponding base coefficients and confidence intervals.

It then evaluates conditional indirect effects and moderated path coefficients at three reference values of the moderator. By default, these values are the moderator mean and one standard deviation below and above the mean. Alternative reference values can be supplied through 'W_values'.

Finally, the function evaluates the conditional indirect effects and moderated path coefficients over a moderator grid. These results can be used to plot effect curves or identify regions in which the confidence interval excludes zero.

An asterisk is added to an effect when its confidence interval excludes zero.

Value

A named list with the following components:

mod_coeff

A data frame summarising the moderated path coefficients, their corresponding base coefficients, and confidence intervals.

beta_coef

A data frame containing the conditional indirect effects at the three reference values of the moderator.

path_HML

A data frame containing the conditional moderated path coefficients at the three reference values of the moderator.

theta_curve

A data frame containing the conditional indirect effects evaluated over the moderator grid.

path_curve

A data frame containing the conditional path coefficients evaluated over the moderator grid.


Apply Standardization to Parameter Definitions

Description

Replaces parameters in expressions with their standardized versions.

Usage

apply_standardization(definitions, std_map, path_std_map = list())

Arguments

definitions

A list of user-defined parameter expressions.

std_map

A named list mapping intercept parameter names to their observed variables.

path_std_map

A named list mapping slope parameters to predictor/outcome variables.

Value

A list of standardized parameter expressions.


Assert a scalar (whole-number) integer with optional bounds

Description

Assert a scalar (whole-number) integer with optional bounds

Usage

assert_scalar_int(
  x,
  name = deparse(substitute(x)),
  lower = NULL,
  upper = NULL,
  allow_null = FALSE
)

Bootstrap Standard Deviations for Standardization

Description

Performs bootstrap sampling to estimate SDs of variables used in standardization.

Usage

bootstrap_sd_list(data, var_names, nboot = 20000, seed = NULL)

Arguments

data

A data frame.

var_names

A character vector of variable names.

nboot

Integer. Number of bootstrap samples.

seed

Integer. Random seed.

Value

A named list of numeric vectors (bootstrapped SD samples).


Build Unstandardized Parameter Definitions

Description

Extracts parameter definition expressions from a fitted model.

Usage

build_definitions(fit)

Arguments

fit

A lavaan model object.

Value

A named list of parameter definitions.


Build Standardization Maps PATH

Description

Constructs mapping of parameter labels to variables for standardization.

Usage

build_path_std_map(fit)

Arguments

fit

A fitted lavaan model.

Value

A named list: slopes (path_std_map).


Build Standardization Maps

Description

Constructs mapping of parameter labels to variables for standardization.

Usage

build_std_map(fit)

Arguments

fit

A fitted lavaan model.

Value

A named list: intercepts (std_map).


Basic Contrasts for Indirect Effects and Pre/Post Path Coefficients

Description

'calc_basic_contrasts()' extracts two convenient sets of contrasts from a Monte-Carlo SEM result ('semmcci' object):

  1. All pairwise differences between indirect effects ('indirect_*' columns);

  2. Pre-test (X_0) and post-test (X_1) coefficients for every primary b path, obtained with X_1 = (2b + d)/2, X_0 = X_1 - d.

Usage

calc_basic_contrasts(
  mc_result,
  ci_level = 0.95,
  digits = 3,
  point_estimates = NULL
)

Arguments

mc_result

A Monte-Carlo result of class '"semmcci"' (returned by [MCMI2()]).

ci_level

Confidence level for the CI (default '0.95').

digits

Decimal places to keep (default '3').

point_estimates

Named fitted parameter estimates, including defined effects.

Details

* Indirect-effect columns are detected by the regular expression '^indirect_'. * A primary b path is any coefficient named 'b1', 'b_1_2', … Its matching d path ('d1', 'd_1_2', …) is paired automatically.

Each contrast is summarised with its plug-in estimate, sampling SD, and a symmetric 100(1-\alpha) 'mc_summary_pct()' and 'fix_pct_names()' ensure that the final CI columns are named, for example, '2.5

Value

A list with up to two data frames:

IE_contrasts

Pairwise contrasts of indirect effects, or 'NULL' if fewer than two are present.

Xcoef

Rows 'X1_b*' and 'X0_b*' for every detected b path, or 'NULL' if no b path is found.


Debug printer with indentation (internal)

Description

Debug printer with indentation (internal)

Usage

dbg(..., .lvl = 0, verbose = TRUE)

Evaluate Unstandardized Monte Carlo Definitions

Description

Evaluates user-defined parameter expressions from Monte Carlo samples without any standardization.

Usage

evaluate_definitions_unstd_v2(theta_star, definitions)

Arguments

theta_star

A matrix of Monte Carlo samples.

definitions

A named list of algebraic definitions (as strings).

Value

A data frame with R rows (simulations) and p + d columns (p = number of free parameters, d = number of defined parameters).


Evaluate Standardized Monte Carlo Expressions

Description

Evaluates user-defined parameters (defined via expressions) and standardized free parameters from Monte Carlo simulated samples. This version supports both intercept-based standardization and path-based standardization.

Usage

evaluate_definitions_v3(
  theta_star,
  definitions,
  std_map,
  sd_boot_list,
  sd_var_boot,
  path_std_map
)

Arguments

theta_star

A matrix of Monte Carlo samples.

definitions

A named list of parameter definitions (e.g., list(indirect := "a * b")).

std_map

A named character vector mapping intercept labels (e.g., a1) to variable names (e.g., M1diff).

sd_boot_list

A list of bootstrap SD samples for intercept variables.

sd_var_boot

A list of bootstrap SD samples for variables involved in slope paths.

path_std_map

A named list mapping path labels to a vector of (predictor, outcome) variable names.

Value

A data frame of R rows (simulations) * all parameters (standardized free + defined).


Example Data for within subject mediation

Description

A simulated dataset containing variables for within-subject mediation analysis. The dataset includes four within-subject variables (A, B, C, D), each measured at three levels:

Usage

example_data

Format

A tibble (data frame) with 100 rows and 12 variables:

A1

Numeric variable

A2

Numeric variable

A3

Numeric variable

B1

Numeric variable

B2

Numeric variable

B3

Numeric variable

C1

Numeric variable

C2

Numeric variable

C3

Numeric variable

D1

Numeric variable

D2

Numeric variable

D3

Numeric variable

Examples

data(example_data)
head(example_data)

Extract All Parameters and Definitions

Description

Extracts free and defined parameters from a fitted lavaan model, and builds a dependency map of user-defined parameters.

Usage

extract_all_parameters(fit)

Arguments

fit

A lavaan model object.

Value

A list with:


Fix legacy CI column names in a data.frame

Description

Converts columns like CI[LL] / CI[UL] or CI.LL / CI.UL to the standard produced by .make_ci_names().

Usage

fix_ci_names(df, ci = 0.95)

Fix % characters mangled by make.names()

Description

Fix % characters mangled by make.names()

Usage

fix_pct_names(df)

Generate Monte Carlo Samples

Description

Generates a Monte Carlo sample of model parameters from a multivariate normal distribution.

Usage

generate_mc_samples(fit, resolved_map, R = 20000, seed = NULL)

Arguments

fit

A fitted lavaan model.

resolved_map

A dependency map from resolve_all_dependencies().

R

Integer. Number of Monte Carlo samples to generate.

seed

Integer. Random seed.

Value

A numeric matrix of Monte Carlo samples.


Extract All Variables Needed for Standardization

Description

Extracts predictor and outcome variable names from a path standardization map.

Usage

get_all_variables_from_path_map(path_std_map)

Arguments

path_std_map

A named list returned by build_path_std_map().

Value

A character vector of variable names.


Parse All Possible Indirect Paths from Column Names

Description

Infers every unique mediation chain that can be constructed from a set of coefficient names following the WsMed naming convention (e.g., a1, b_1_3, d_2_1, b3). The function returns a list; each element describes one indirect effect:

Usage

get_indirect_paths(col_names)

Arguments

col_names

Character vector of coefficient names (typically colnames(mc_result$thetahatstar)).

Details

Internally the function:

  1. identifies all a, b, and d coefficients available in col_names;

  2. constructs a directed graph from X → Mi, Mi → Y, and Mi → Mj edges;

  3. runs a depth-first search from the exposure (X) to the outcome (Y);

  4. converts every node sequence (X → … → Y) into the corresponding coefficient sequence.

Duplicate paths (identical mediator ordering) are removed.

Value

A list of path descriptors; each element is itself a list with components path_name, coefs, and mediators. If no valid path exists, an empty list is returned.


Get safe number of CPUs for parallel processing

Description

Automatically returns 1 when on CI to avoid errors.

Usage

get_safe_ncpus()

Extract Target Variables for Standardization

Description

Extracts variable names from a fitted SEM model and a definition map that are required for computing standardized estimates in Monte Carlo simulations. Specifically, it identifies intercept terms (e.g., ~1) involved in user-defined parameters such as indirect effects (e.g., indirect := a * b) that require standard deviations for standardization.

Usage

get_sd_target_variables(fit, definition_map, data)

Arguments

fit

A lavaan model object. Must be fitted with labels for defined parameters.

definition_map

A named list returned from resolve_all_dependencies(), mapping defined parameters (e.g., "indirect1") to their algebraic components (e.g., c("a1", "b1")).

data

A data frame used to fit the model, typically the original observed dataset. Used to validate the existence of variables.

Value

A character vector of variable names whose standard deviations are needed to standardize the intercept estimates in Monte Carlo confidence interval analysis.


make_contrasts

Description

make_contrasts

Usage

make_contrasts(df, value_col = "Estimate", contrast_col = "Contrast")

Compute Monte Carlo Estimates, Standard Errors, and CIs (with Percent Labels)

Description

mc_summary_pct() summarizes a numeric vector of Monte Carlo samples x by computing its mean, standard deviation, and percentile-based confidence interval. The result is returned as a data.frame with column names that include percentage signs. This function is typically used to generate tables of mediation effects or path coefficient estimates with path labels.

Returned columns include:

Usage

mc_summary_pct(x, label, ci_level = 0.95, digits = 3)

Arguments

x

Numeric vector of Monte Carlo samples.

label

Character string for the value in the Path column of the output.

ci_level

Confidence level for the CI (default 0.95).

digits

Number of decimal places to retain (default 3).

Value

A data.frame containing the path label, estimate, standard error, and CI. Column names are formatted like "2.5%CI.Lo" and "97.5%CI.Up".


Null-coalescing operator

Description

Returns x unless it is NULL, otherwise returns y.

Usage

x %||% y

Plot conditional effects at moderator levels

Description

Draw point estimates and confidence intervals by categorical group or by the stored low, mean and high levels of a continuous moderator. Each effect is shown in its own panel. Categories are not joined by interpolating lines.

Usage

plot_conditional_effects(
  result,
  paths = NULL,
  type = c("indirect", "paths", "overall"),
  levels = NULL,
  standardized = FALSE,
  engine = c("mc", "boot"),
  labels = NULL,
  title = NULL,
  x_label = NULL,
  y_label = NULL,
  base_size = 12,
  ncol = NULL
)

Arguments

result

A result returned by wsMed().

paths

Optional character vector of effect identifiers, in display order. NULL selects all available effects. Both indirect_1 and indirect_effect_1 are accepted when they identify an available effect.

type

"indirect" for specific indirect effects, "paths" for available conditional path coefficients, or "overall" for total indirect and total effects.

levels

Optional group/level labels to select, in display order. NULL uses their stored order. For continuous W the labels are "-1 SD", "0 SD", "+1 SD"; the axis also displays stored raw W probe values.

standardized

Use standardized results, requested when fitting with standardized = TRUE. No scaling is recomputed from interval endpoints.

engine

For ci_method = "both", select "mc" or "boot". For a single engine its stored results are used.

labels

Optional named character vector mapping effect identifiers to display labels. Partial mappings are allowed.

title

Optional plot title.

x_label, y_label

Optional axis labels.

base_size

Base font size.

ncol

Optional number of facet columns.

Details

Intervals are the stored percentile conditional intervals, including jointly transformed scales for standardized results. They are individual intervals, not simultaneous confidence intervals.

Value

A ggplot object with plotted data and a wsmed_plot attribute.

See Also

plot_effects(), plot_contrasts()


Plot differences between mediation effects

Description

Draw contrast estimates and their confidence intervals relative to zero. With a moderator, contrasts compare groups or stored moderator levels within each effect. Without a moderator, specific indirect paths are compared with one another. The displayed subtraction order is explicit.

Usage

plot_contrasts(
  result,
  paths = NULL,
  type = c("indirect", "paths", "overall"),
  contrasts = NULL,
  standardized = FALSE,
  engine = c("mc", "boot"),
  labels = NULL,
  title = NULL,
  x_label = NULL,
  y_label = NULL,
  base_size = 12,
  ncol = NULL
)

Arguments

result

A result returned by wsMed().

paths

Optional character vector of effect identifiers, in display order. NULL selects all available effects. Both indirect_1 and indirect_effect_1 are accepted when they identify an available effect.

type

"indirect" for specific indirect effects, "paths" for available conditional path coefficients, or "overall" for total indirect and total effects.

contrasts

Optional exact contrast labels to select, in display order. NULL selects all. Inspect the corresponding result table or the returned plot's data$Contrast for available labels.

standardized

Use standardized results, requested when fitting with standardized = TRUE. No scaling is recomputed from interval endpoints.

engine

For ci_method = "both", select "mc" or "boot". For a single engine its stored results are used.

labels

Optional named character vector mapping effect identifiers to display labels. Partial mappings are allowed.

title

Optional plot title.

x_label, y_label

Optional axis labels.

base_size

Base font size.

ncol

Optional number of facet columns.

Details

Moderator contrasts use the existing joint-draw contrast tables. Without a moderator, only type = "indirect" is available: contrasts are computed from the stored joint draws, with point estimates evaluated at fitted/pooled parameters. Their intervals are percentile intervals. Standardized draws use the same draw-specific marginal endpoint scales as other standardized effects. No model is refitted. Interval endpoints for two separate effects are never subtracted to construct a contrast CI. paths and labels apply to effect panels when a moderator is present; without a moderator, paths selects the indirect effects to compare and labels maps the full contrast labels. Continuous overall contrasts are unavailable when the fitted result does not contain that contrast table.

Value

A ggplot object with plotted data and a wsmed_plot attribute.

See Also

plot_effects(), plot_conditional_effects()


Forest plot of mediation effects

Description

Plot fitted indirect effects, the total indirect effect, the direct effect (cp) and the total effect with their stored confidence intervals. With a moderator these are effects at the model's reference value (centered continuous W = 0, or the reference category), not averages across W. Use plot_conditional_effects() to show selected moderator levels instead.

Usage

plot_effects(
  result,
  paths = NULL,
  standardized = FALSE,
  engine = c("mc", "boot"),
  labels = NULL,
  title = NULL,
  x_label = NULL,
  y_label = NULL,
  base_size = 12
)

Arguments

result

A result returned by wsMed().

paths

Optional character vector of effect identifiers, in display order. NULL selects all available effects. Both indirect_1 and indirect_effect_1 are accepted when they identify an available effect.

standardized

Use standardized results, requested when fitting with standardized = TRUE. No scaling is recomputed from interval endpoints.

engine

For ci_method = "both", select "mc" or "boot". For a single engine its stored results are used.

labels

Optional named character vector mapping effect identifiers to display labels. Partial mappings are allowed.

title

Optional plot title.

x_label, y_label

Optional axis labels.

base_size

Base font size.

Details

MC intervals come from the parameter draws (or the stored standardized parameter table). Bootstrap plots use the bootstrap limits, not the normal-theory limits that may also be present in the table.

Value

A ggplot object. Its data contains the plotted estimates and CI.LL/CI.UL limits. The wsmed_plot attribute records the selected engine, scale and confidence level. Use ggplot2::ggsave() to export it.

See Also

plot_conditional_effects(), plot_contrasts(), plot_moderation_curve()


Plot conditional effects across a continuous moderator

Description

Plot a conditional indirect effect, total effect or path coefficient with its pointwise confidence band. Highlight stored grid values whose interval excludes zero. Boundary labels use raw moderator units, not sample percentiles.

Usage

plot_moderation_curve(
  result,
  path_name,
  title = NULL,
  x_label = "Moderator (W, raw units)",
  y_label = "Estimate",
  ns_fill = "#FEE0D2",
  sig_fill = "#C7E9C0",
  alpha_ci = 0.35,
  alpha_sig = 0.35,
  base_size = 14,
  standardized = FALSE,
  engine = c("mc", "boot")
)

Arguments

result

A result returned by wsMed() with a continuous moderator.

path_name

One stored effect identifier, such as "indirect_effect_1_2", "total_indirect" or "b_1_2". "indirect_1_2" is also accepted as an alias.

title

Optional plot title.

x_label, y_label

Axis labels. The default x axis uses raw W units.

ns_fill

Colour of the complete confidence band.

sig_fill

Colour of the regions whose pointwise CI excludes zero.

alpha_ci, alpha_sig

Opacity of the band and highlighted regions.

base_size

Base font size.

standardized

Use the stored standardized conditional results.

engine

For ci_method = "both", select "mc" or "boot".

Details

Highlighted intervals are approximate ranges on the stored grid, not exact Johnson–Neyman boundaries or simultaneous confidence regions. A label ⁠[a, b]⁠ reports the first and last qualifying grid values. An isolated qualifying point is marked by a vertical line. Missing confidence limits break a highlighted range. The displayed confidence level follows result$alpha; no fixed p-value threshold is implied.

Value

A ggplot object. Its data contains the selected curve; the wsmed_regions attribute contains raw-unit grid interval endpoints. The wsmed_plot attribute records engine, scale and confidence level.

See Also

plot_effects(), plot_conditional_effects(), plot_contrasts()


Print Method for wsMed Objects

Description

Provides a comprehensive summary of results from a wsMed object, including:

The output is formatted for clarity, ensuring an intuitive presentation of mediation analysis results, including dynamic confidence intervals, moderation keys, and C1-C2 coefficients.

Usage

## S3 method for class 'wsMed'
print(x, digits = 3, ...)

Arguments

x

A wsMed object containing the results of within-subject mediation analysis.

digits

Numeric. Number of digits to display in the results.

...

Additional arguments (not used currently).

Details

This function is specifically designed to display results from the within-subject mediation analysis conducted using the wsMed function. Key features include:

Value

Invisibly returns the input wsMed object for further use.

See Also

wsMed, sem, standardizedSolution_boot_ci

Examples


# Perform within-subject mediation analysis
data("example_data", package = "wsMed")
result1 <- wsMed(
  data = example_data,
  M_C1 = c("A1", "B1"),
  M_C2 = c("A2", "B2"),
  Y_C1 = "C1",
  Y_C2 = "C2",
  form = "P",
  Na = "FIML",
  standardized = FALSE,
  alpha = 0.05
)

# Print the results
print(result1)


Print Formatted SEM Model Syntax

Description

Formats and prints the SEM model syntax generated by GenerateModelCN, GenerateModelCP, GenerateModelP, and GenerateModelPC. It organizes the equations into labeled sections for better readability.

Usage

printGM(x, ...)

Arguments

x

A list or character string containing SEM model syntax. If x is a wsMed() result, it will extract and print the model syntax. If x is a ⁠GenerateModel*()⁠ result, it will format and print the model.

...

Additional arguments (not used).

Value

Invisibly returns the formatted SEM model syntax.

Examples

data(example_data)
head(example_data)
prepared_data <- PrepareData(
  data = example_data,
  M_C1 = c("A1", "B1"),
  M_C2 = c("A2", "B2"),
  Y_C1 = "C1",
  Y_C2 = "C2"
)
sem_model <- GenerateModelPC(prepared_data)
printGM(sem_model)

Resolve Dependencies of Defined Parameters

Description

Recursively resolves all free parameters involved in each defined parameter.

Usage

resolve_all_dependencies(def_map)

Arguments

def_map

A definition map produced by extract_all_parameters().

Value

A list mapping each defined parameter to its dependent free parameters.


Run Monte Carlo-Based Mediation Inference

Description

Performs Monte Carlo simulation to estimate confidence intervals for both unstandardized and (optionally) standardized mediation parameters, based on fitted SEM models.

Usage

run_mc_mediation(
  fit,
  data,
  standardized = FALSE,
  R = 20000,
  seed = 123,
  alpha = 0.05,
  alphastd = 0.05
)

Arguments

fit

A fitted lavaan model.

data

The dataset used for the fitted model.

standardized

Logical. If TRUE, standardized results will also be returned. Default is FALSE.

R

Integer. Number of Monte Carlo replications. Default is 20000.

seed

Integer. Random seed for reproducibility. Default is 123.

alpha

Numeric value for the confidence level of unstandardized Monte Carlo intervals. Default is 0.05.

alphastd

Numeric value for the confidence level of standardized Monte Carlo intervals. Default is 0.05.

Value

A list with unstandardized and (if requested) standardized results. Each result includes Monte Carlo estimates, SEs, and CIs.


Sort Parameters for Printing in SEM Output

Description

Sorts a parameter table by conceptual priority for presentation purposes. This function is designed to support formatted output of mediation and SEM results by organizing parameters such as a-paths, b-paths, indirect effects, contrasts, etc.

Usage

sort_parameters(df)

Arguments

df

A data frame that contains a column named Parameter (e.g., from Monte Carlo CI output).

Details

This is an internal helper function used by print.WsMed() to ensure that printed tables of standardized or unstandardized estimates appear in a logical and human-readable order.

Value

A reordered version of the same data frame, with rows sorted according to a predefined logical structure:

  1. a-paths (e.g., a1, a2, ...)

  2. b-paths (e.g., b1, b2, ...)

  3. d-paths (e.g., d1, d2, ...)

  4. indirect effects (e.g., ind1, ind2, ...)

  5. direct effect

  6. total indirect

  7. total effect

  8. contrasts (e.g., ind1-ind2, ind2-ind3)

  9. X-condition path terms (e.g., X1_b1, X0_b1, ...)


Standardize conditional mediation results using existing joint draws

Description

Standardize conditional mediation results using existing joint draws

Usage

standardize_moderation(object)

Arguments

object

A wsMed result with a moderator and stored MC or bootstrap draws.

Details

Conditional indirect and total effects are divided by the marginal model-implied SD of Ydiff. Conditional paths use their endpoint scales. Moderator probe values remain fixed in raw units; continuous interaction coefficients in mod_coeff are reported per SD of W. Dummy variables retain 0/1 units. Coefficients and scales are transformed jointly for each draw. Intervals are percentile intervals, as in the raw conditional tables. Metadata attributes record scales, probe conventions and draw diagnostics. With fixed.x=TRUE, external moments are held fixed. MI uses pooled primitive parameters and the existing first-imputation probing reference.

Value

A moderation list with the same conditional tables and curves as the raw output, or a list with mc and boot components for ci_method="both".


Summarize Monte Carlo Simulation Results

Description

Computes summary statistics for Monte Carlo simulation results, including the mean estimate, standard error (SE), and confidence intervals (CIs).

Usage

summarize_mc_ci(mc_result, alpha = 0.05)

Arguments

mc_result

A data frame where each column corresponds to a parameter, and each row represents one Monte Carlo replication of that parameter's estimate.

alpha

Numeric. Significance level used to compute the (1 - alpha) confidence interval. Default is 0.05 for a 95% confidence interval.

Value

A data frame with one row per parameter and the following columns:

Parameter

The name of the parameter.

Estimate

The average estimate across all Monte Carlo replications.

SE

The standard deviation of the estimates (standard error).

CI_lower

The lower bound of the confidence interval.

CI_upper

The upper bound of the confidence interval.


Validate user inputs for wsMed()

Description

All checks stop() with an informative message when they fail. Invisibly returns TRUE on success.

Usage

validate_wsMed_inputs(
  data,
  M_C1,
  M_C2,
  Y_C1,
  Y_C2,
  C_C1 = NULL,
  C_C2 = NULL,
  C = NULL,
  W = NULL,
  W_type = NULL,
  MP = NULL,
  form = c("P", "CN", "CP", "PC", "UD"),
  paths = NULL,
  Na = c("DE", "FIML", "MI"),
  R = 20000L,
  bootstrap = 1000L,
  m = 5L,
  ci_level = 0.95,
  ci_method = NULL,
  MCmethod = NULL
)

Within-Subject Mediation Analysis (Two-Condition)

Description

wsMed() fits a structural equation model (SEM) for two-condition within-subject mediation. It can handle missing data (DE, FIML, MI) and computes both unstandardized and standardized effects with bootstrap or Monte Carlo confidence intervals.

Usage

wsMed(
  data,
  M_C1,
  M_C2,
  Y_C1,
  Y_C2,
  C_C1 = NULL,
  C_C2 = NULL,
  C = NULL,
  C_type = NULL,
  W = NULL,
  W_type = NULL,
  MP = NULL,
  form = c("P", "CN", "CP", "PC", "UD"),
  Na = c("DE", "FIML", "MI"),
  alpha = 0.05,
  mi_args = list(),
  R = 20000L,
  bootstrap = 2000,
  boot_ci_type = "perc",
  iseed = 123,
  fixed.x = FALSE,
  ci_method = c("mc", "bootstrap", "both"),
  MCmethod = NULL,
  seed = 123,
  standardized = FALSE,
  verbose = FALSE,
  paths = NULL
)

Arguments

data

A data.frame containing the raw scores.

M_C1, M_C2

Character vectors of mediator names under condition 1 and 2.

Y_C1, Y_C2

Character scalars for the outcome under each condition.

C_C1, C_C2

Character vectors of within-subject covariates (per condition).

C

Character vector of between-subject covariates.

C_type

Character; type of C: "continuous" or "categorical".

W

Character vector of moderators. Default NULL.

W_type

Character; "continuous" or "categorical".

MP

Character vector identifying which regression paths are moderated (for example, "a1", "b_1_2", "cp"). Main effects of W already included in each regression are always used when computing conditional intercepts, whether or not a/cp is explicitly listed in MP.

form

Model type: "P", "CN", "CP", "PC", or "UD". Use "UD" to specify a user-defined mediation model. Values are case-insensitive.

Na

Missing-data method: "DE", "FIML", or "MI". Values are case-insensitive.

alpha

Numeric vector in (0, 1); two-sided significance levels.

mi_args

List of MI-specific controls:

m

Number of imputations. Default 5.

method_num

Imputation method for mice().

decomposition

Covariance-decomposition method ("eigen", "chol", "svd").

pd

Logical; positive-definiteness check.

tol

Tolerance for the positive-definiteness check.

R

Integer; number of Monte Carlo draws. Default 20000L.

bootstrap

Integer; number of bootstrap replicates (DE and FIML only).

boot_ci_type

Character; bootstrap CI type: "perc", "bc", or "bca.simple".

iseed, seed

Integer seeds for bootstrap and Monte Carlo, respectively.

fixed.x

Logical; passed to lavaan.

ci_method

CI engine: "mc", "bootstrap", or "both". Values are case-insensitive. The default (also used for NULL) is "mc". With Na = "MI", only "mc" is supported.

MCmethod

If Na = "FIML" and ci_method = "mc", choose "mc" (default) or "bootSD".

standardized

Logical; if TRUE, return standardized parameter tables (including defined effects at the reference moderator value). Raw conditional results remain in moderation; standardized conditional tables and curves are added in moderation_std. Default FALSE.

verbose

Logical; print progress messages.

paths

A character vector defining directed paths when form = "UD". Paths are specified using mediator labels M1, M2, and so on, with Y denoting the outcome. For example, c("M1 -> M3", "M3 -> Y", "M2 -> Y"). Must be NULL for the predefined model forms.

Details

Model structures:

Missing-data strategies:

Confidence-interval engines:

For Na = "FIML", you may choose MCmethod = "mc" (default) or "bootSD" to add a finite-sample SD correction.

Standardization divides differences by their marginal model-implied SDs, without recentering them. Dummy variables retain 0/1 units; interaction terms use the product of their component scale factors, not the interaction column SD. MC and bootstrap draws are transformed jointly with their scales. Standardized bootstrap intervals use type-7 quantiles; bc and bca.simple apply bias correction with zero acceleration. P-values use inversion of that distribution and require at least 1000 valid replicates.

Conditional tables use percentile intervals and fixed numerical probes. With fixed.x=TRUE, scale transformations condition on the fitted external moments rather than adding sampling uncertainty for those moments. In MI, probes/centering references use the first completed data set; fixed.x=TRUE also conditions on its external moments in the pooled transform. No new population-relative probing or imputation method is implemented here.

Workflow: (1) preprocess -> (2) generate SEM syntax -> (3) fit -> (4) compute confidence intervals -> (5) optional: standardize estimates.

Value

An object of class "wsMed" with elements:

data

Preprocessed data frame.

sem_model

Generated lavaan syntax.

mc

List with Monte Carlo draws, bootstrap tables (if any), and the fitted model.

moderation

Raw conditional or moderated effect tables.

moderation_std

Standardized conditional tables and curves when standardized=TRUE and a moderator is supplied.

form,Na,alpha

Analysis settings.

paths

The user-defined paths when form = "UD"; otherwise NULL.

input_vars

Names of all user-supplied variables.

Examples

data("example_data", package = "wsMed")
set.seed(123)
result <- wsMed(
  data = example_data,
  M_C1 = c("A2", "B2"),
  M_C2 = c("A1", "B1"),
  Y_C1 = "C1", Y_C2 = "C2",
  form = "P", Na = "DE"
)
print(result)