If there are 3 groups in "a", you need to create 2 dummy codes to represent them
But if you want a multigroup model (group = "a"), then "a" cannot be a variable in your model. Alternatively, you can allow for the effect of X to be implicitly moderated by a in the multigroup model by giving the slopes different labels (and leaving "a" out of the regression equation).
Testing the interaction would involve comparing models in which the slopes are different (interaction) to a model in which the slopes are the same across groups:
mod.int <- 'y ~ c(g1, g2, g3)*x + z'
mod.no.int <- 'y ~ c(g1, g1, g1)*x + z'
Fit both models, then compare them using lavTestLRT(). Note that without labeling the z slopes above, they will differ across groups by default (implicitly, z:a interaction). To avoid that, provide identical labels for the z effect in both models: c(foo, foo, foo)*z
I assume you are using SEM instead of OLS regression with the lm() function because you have missing data. Multiple imputation is also an option if you want to run regular regression with standard formulas: lm(y ~ x * a + z). Check out the mitml package for helpful functions to facilitate imputation and pooling results.
Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam