4. Regression Analysis with Interaction: Continuous Moderator - wmmurrah/lavaanFIML GitHub Wiki

In this tutorial I demonstrate how to include an interaction with a continuous moderator using full information maximum likelihood (FIML) estimation in the R package lavaan. This tutorial builds on the previous three, looking at regression modeling with missing data. If you haven't worked through those, you will probably want to do that before trying this one.

In this tutorial I use the conscientiousness data from the Applied Missing Data Analysis website.

Import and prepare data

library(lavaan)
consc <- read.table('data/conscientiousness.dat')

names(consc) <- c("agree", "consc", "jobperf")
consc[consc==-99] <- NA

Create interaction variable.

consc$interact <- consc$agree*consc$consc

Create Model Object

model <- '
# Model mean of moderator with label
agree ~ meanm*1

# Variance of moderator with label
agree ~~ varm*agree

# Covariance of predictors and interaction
consc ~~ agree + interact
agree ~~ interact

# Regression Model
jobperf ~ Bf*consc + Bm*agree + Bint*interact

# Calculate slopes of moderator at different values
slopelow := Bf + Bint*(meanm - sqrt(varm))
slopemed := Bf + Bint*meanm   		
slopehi  := Bf + Bint*(meanm + sqrt(varm))
'

Fit the Model

fit <- cfa(model, consc, missing='fiml', estimator='MLR', fixed.x=FALSE, 
           meanstructure=TRUE)
summary(fit, fit.measures=TRUE, rsquare=TRUE, standardize=TRUE )
library(semPlot)
semPaths(fit,'model', 'est', style='ram')
inspect(fit, 'patterns')
inspect(fit, 'coverage')