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!
Thanks to the important comment of John Madden I'll differentiate between moderation (the thing you are probably looking for) and mediation.
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:
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With