Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Lavaan coding latent variable interactions

Tags:

r

r-lavaan

Can anyone show me how to code latent variable interactions in the model statement for lavaan package for structural equation models?

Suppose I had latent variable L1 and some observed variable F1 and would like to code their interaction effect on some outcome Y:

L1 =~ x1 + x2

Y ~ L1 * F1 

This doesn't work.

Thanks in advance!

like image 380
user3773375 Avatar asked Jun 25 '14 02:06

user3773375


1 Answers

Thanks to the important comment of John Madden I'll differentiate between moderation (the thing you are probably looking for) and mediation.

Moderation (interaction of variable values)

The quick answer to your question is: To my knowledge there is no lavaan-integrated possibility to do an interaction of two latent variables, but here is my go at a workaround:

  1. Define the latent variables (CFA)
  2. Extract predicted values, add them to your data frame and define an interaction variable
  3. Do your intended regression (with our without the latent variables themselves)

Here is some toy code for the workaround - the moderation doesn't make any sense with this data (mtcars which is in R base) and will give you a warning, but the structure of the workflow should be clear.

library(lavaan)

# 1. set up your measurement models
cfamodel <- "
    #defining the latent variables
    L1 =~ drat + wt
    L2 =~ disp + hp
"
fitcfa <- cfa(data = mtcars, model = cfamodel)

# 2. extract the predicted values of the cfa and add them to the dataframe
semdata <- data.frame(mtcars, predict(fitcfa))

# create a new variable with the interaction of L1 and L2
semdata <- semdata %>%
              mutate(L1L2 = L1 * L2)

# 3. now set up the regression and add the predefined interaction to the model
# a) regression with both latent variables and the interaction
semmodel1 <- "
    # define regression
    mpg ~ L1 + L2 + L1L2
"
fitsem1 <- sem(data = semdata, model = semmodel1)
summary(fitsem1)

# b) regression with only the interaction (does that even make sense? I don't know...)
semmodel2 <- "
    # define regression
    mpg ~ L1L2
"
fitsem2 <- sem(data = semdata, model = semmodel2)
summary(fitsem2)

Mediation (interaction of weights)

For Mediation you need to define a new parameter as a product of the two regression weights that are of interest. In your example with L1 as latent variable, F1 as observed variable and Y as dependend variable, that would be:

# define Regressions (direct effect)
Y ~ lambda1*X
Y ~ lambda2*M

# define Regressions (effect on mediator)
M ~ X

# define Interaction
interac := lambda1*lambda2

fit <- sem(model, data = Data)
summary(fit)

lavaan will then give you an estimate of the interaction.

The := operator "defines new parameters which take on values that are an arbitrary function of the original model parameters." Example taken from: http://lavaan.ugent.be/tutorial/mediation.html

like image 101
j_5chneider Avatar answered Oct 06 '22 12:10

j_5chneider