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 simulate b
as a function of a
, but then regress a
on b
. But following from that example:
mod <- ' group: 1
a ~ b + c
group: 2
a ~ b
'
fit <- sem(mod, data = dtest, group = "group")
The interpretation of other slopes is still the same (i.e., "For a given age/c
, a 1-unit increase in b
is associated with a slope-unit increase in a
"). 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 from a
and b
.
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 of b
is the same across groups. From your description:
I scale the age in group one as a deviation from the age in group 2
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.
mod0 <- ' group: 1
a ~ slope*b + c
group: 2
a ~ slope*b
'
fit0 <- sem(mod0, data = dtest, group = "group")
lavTestLRT(fit, fit0)