I try to fit a multigroup SEM in lavaan.
In one of my groups all values for a specific covariate are the same (i.e., the people in group 2 all have the same age), in my group 1 however this covariate (age) varies between individuals. I want to compare coefficients between groups, while controling for age in group 1. (I scale the age in group one as a deviation from the age in group 2).
This seems to not be easily possible, lavaan gives the following error message: "lavaan ERROR: some variables have no variance in group 2: c".
Can someone give me advice on how to implement my intended analysis?
Here is a reproducible example:
library(lavaan)
N<-1000
a<-rnorm(N,0,1)
b<-a*0.5+rnorm(N,0,0.2)
c<-c(rnorm((N/2),0,0.2),rep(0,times=(N/2))) # covariate c has the same value for every case in group 2
group<-rep(c("1","2"),each=(N/2))
dtest<-cbind.data.frame(a,b,c,group)
m<-'a~b+c'
fit<-sem(model=m,data=dtest,group="group")
In
lavaan
, you can define different model per group, to exclude the constant from Group 2's model. The reprex is a little odd in that you simulateb
as a function ofa
, but then regressa
onb
. But following from that example:The interpretation of other slopes is still the same (i.e., "For a given age/
c
, a 1-unit increase inb
is associated with a slope-unit increase ina
"). It just so happens that in one group, everyone in fact has the same given age, so there is no age-related variance to partial out froma
andb
.But in case you use
c
as a moderator in one group, then you should in fact center age in Group 1 at whatever value everyone's age is in Group 2, so that the estimated simple effect ofb
is the same across groups. From your description:it sounds like you already do this.
If you want to test whether the effect of your focal predictor (
b
) is the same across groups, just use labels to make the slopes equal (not moderated by group) and compare models.