library(lavaan)
# Generate data, this is optional as lavaan also takes variance covariance matrix
sim.model <- ' x =~ x1 + 0.8*x2 + 1.2*x3
y =~ y1 + 0.5*y2 + 1.5*y3
m ~ 0.5*x
y ~ 0.5*x + 0.8*m'
set.seed(10)
data <- simulateData(sim.model, sample.nobs = 1000L)
# standardize dataset
data = data.frame(apply(data,2,scale))
# Step 1: Set up the analytic model of interest
model <- 'x =~ x1 + x2 + x3
y =~ y1 + y2 + y3
m ~ x
y ~ x + m'
# Step 2: Set up the sensitivity analysis model.
# The sensitivity parameters are phantom1, phantom2, and phantom3 in this example.
sens.model = 'x =~ x1 + x2 + x3
y =~ y1 + y2 + y3
m ~ x
y ~ x + m
x ~ phantom1*phantom
m ~ phantom2*phantom
y ~ phantom3*phantom
phantom =~ 0 # added for mean of zero
phantom ~~ 1*phantom' # added for unit variance
# Step 3: Set up the paths of interest to be evaluated in sensitivity analysis.
# Suppose we are interested in all direct and indirect paths.
paths <- 'm ~ x
y ~ x + m'
# Step 4: Perform sensitivity analysis
out <- sa.tabu(model = model,
sens.model = sens.model,
data = data,
opt.fun = 1,
max.iter = 2,
max.iter.obj = 2)
# Note, please specify larger numbers for
# max.iter (e.g., 50) and max.iter.obj (e.g., 10)
# Step 5: Summarize sensitivity analysis results.
# See sens.tables function for explanation of results.
tables <- sens.tables(out)
Run the code above in your browser using DataLab