I am interested in finding a procedure to simulate data that are consistent with a specified mediation model. According to the general linear structural equation model framework for testing mediation models first outlined by Barron and Kenny (1986) and described elsewhere such as Judd, Yzerbyt, & Muller (2013), mediation models for outcome $Y$, mediator $\newcommand{\med}{\rm med} \med$, and predictor $X$ and are governed by the following three regression equations: \begin{align} Y &= b_{11} + b_{12}X + e_1 \tag{1} \\ \med &= b_{21} + b_{22}X + e_2 \tag{2} \\ Y &= b_{31} + b_{32}X + b_{32} \med + e_3 \tag{3} \end{align} The indirect effect or mediation effect of $X$ on $Y$ through $\med$ can either be defined as $b_{22}b_{32}$ or, equivalently, as $b_{12}-b_{32}$. Under the old framework of testing for mediation, mediation was established by testing $b_{12}$ in equation 1, $b_{22}$ in equation 2, and $b_{32}$ in equation 3.
So far, I have attempted to simulate values of $\med$ and $Y$ that are consistent with values of the various regression coefficients using rnorm
in R
, such as the code below:
x <- rep(c(-.5, .5), 50)
med <- 4 + .7 * x + rnorm(100, sd = 1)
# Check the relationship between x and med
mod <- lm(med ~ x)
summary(mod)
y <- 2.5 + 0 * x + .4 * med + rnorm(100, sd = 1)
# Check the relationships between x, med, and y
mod <- lm(y ~ x + med)
summary(mod)
# Check the relationship between x and y -- not present
mod <- lm(y ~ x)
summary(mod)
However, it seems that sequentially generating $\med$ and $Y$ using equations 2 and 3 is not enough, since I am left with no relationship between $X$ and $Y$ in regression equation 1 (which models a simple bivariate relationship between $X$ and $Y$) using this approach. This is important because one definition of the indirect (i.e., mediation) effect is $b_{12}-b_{32}$, as I describe above.
Can anyone help me find a procedure in R to generate variables $X$, $\med$, and $Y$ that satisfy constraints that I set using equations 1, 2, and 3?