Skip to content

Instantly share code, notes, and snippets.

@scolwell
Last active May 16, 2019 22:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save scolwell/a7a57b3496d7e29dce2fa9a60b0194bb to your computer and use it in GitHub Desktop.
Simple Mediation
library(simsem)
population <- "
# Fix means of each to 1 <-- for normal distrubuted N(0,1)
x ~ 0*1
m ~ 0*1
y ~ 0*1
# Fix variance of each to 1 <-- for normal distribution N(0,1)
x ~~ 1*x
m ~~ 1*m
y ~~ 1*y
# Specify the relationships between the variables
y ~ .18*m + .22*x
m ~ .20*x
"
sample <- "
# Estimate the direct effects
y ~ b*m + c*x
m ~ a*x
# Estimate the indirect effects
ind.ab := a*b
# Estimate the total effects
total := a*b + c
"
# Run the simulation. First number is number of replications.
results <- sim(1000, sample, n = 150, generate=population, std.lv=TRUE, lavaanfun = "sem")
summary(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment