ThewsMed function is designed for two conditionwithin-subject mediation analysis, incorporating SEM models through thelavaan package and Monte Carlo simulation methods. Thisdocument provides a detailed description of the function’s parameters,workflow, and usage, along with an example demonstration.
You can install the development version of wsMed fromGitHub with:
# install.packages("pak")pak::pak("Yangzhen1999/wsMed")Alternatively, if you prefer using devtools, you can install wsMed asfollows:
# install.packages("devtools")devtools::install_github("Yangzhen1999/wsMed")This is a basic example which shows you how to solve a commonproblem:
library(wsMed)# Load example datadata(example_data)set.seed(123)example_dataN<- mice::ampute(data = example_data,prop =0.1,)$amp# Perform within-subject mediation analysis (Parallel mediation model)result<-wsMed(data = example_dataN,#datasetM_C1 =c("A1","B1"),# A1/B1 is A/B mediator variable in condition 1M_C2 =c("A2","B2"),# A2/B2 is A/B mediator variable in condition 2Y_C1 ="C1",# C1 is outcome variable in condition 1Y_C2 ="C2",# C2 is outcome variable in condition 2form ="P",# Parallel mediationC_C1 ="D1",# within-subject covariate (e.g., measured under D1)C_C2 ="D2",# within-subject covariate (e.g., measured under C2)C ="D3",# between-subject covariatesNa ="MI",# Use multiple imputation for missing datastandardized =TRUE,# Request standardized path coefficients and effects)# Print summary resultsprint(result)ThewsMed() function automates the full workflow fortwo-condition within-subject mediation analysis. Its main steps are:
Validate inputs – check dataset structure,mediation model type (form), and missing-datasettings.
Prepare data – compute difference scores(Mdiff,Ydiff) and centered averages(Mavg) from the two-condition variables.
Build the model – generate SEM syntax accordingto the chosen structure:
"P": Parallel mediation
"CN": Chained / serial mediation
"CP": Chained + Parallel
"PC": Parallel + Chained
Fit the model – estimate parameters whilehandling missing data:
"DE": listwise deletion"FIML": full-information ML"MI": multiple imputationCompute inference – provide confidence intervalsusing:
ci_method = "bootstrap")ci_method = "mc")Optional: Standardization – ifstandardized = TRUE, return standardized effects withCIs.
Optional: Covariates – automatically center andinclude:
C):mean-centered and added to all regressions.C_C1,C_C2): difference scores and centered averages are computedand included.