Cross-Group Constraints

26 views
Skip to first unread message

Andrew Grotzinger

unread,
Nov 25, 2018, 3:09:08 PM11/25/18
to lavaan
Hello, 

I've been trying to run multiple group models with cross-group constraints and noticing that in some cases lavaan seems to be ignoring the ordering of the parameter labels and applying the parameter label to the wrong group. More specifically, if I try and impose a equality constraint so that the residual variances of the observed variables are equal only within Group 1 I obtain the expected output, but if I change the code so that the residual variances of the observed variables are equal only within Group 2 it seems that the equality constraints are still being applied to Group 1 and not Group 2. I feel I may be missing something obvious, but can't figure out what. Annotated code using example data is below. Thanks in advance for your time and input! 

require(lavaan)
##read in example data 
data<-data.frame(HolzingerSwineford1939)

##split the data in half for example multiple group model
data1<-data.frame(data[1:150,7:15])
data2<-data.frame(data[151:301,7:15])

##create two sets of covariance matrices for two groups
cov1<-cov(data1)
cov2<-cov(data2)
cov_List<-list(cov1,cov2)

##create two sets of means for two gorups
mean1<-colMeans(data1)
mean2<-colMeans(data2)
mean_List<-list(mean1,mean2)

##number of observations within each group
n_List<-list(nrow(data1),nrow(data2))

##run model with cross-gorup constraints where residual variance of indicators is 
##constrained to be equal across indicators for Group 1
HS.model <- ' visual  =~ c(v1,v1)*x1 + c(v2,v2)*x2 + c(v3,v3)*x3
              textual =~ c(t1,t1)*x4 + c(t2,t2)*x5 + c(t3,t3)*x6
speed   =~ c(s1,s1)*x7 + c(s2,s2)*x8 + c(s3,s3)*x9
x1 ~~ c(rv,NA)*x1
x2 ~~ c(rv,NA)*x2
x3 ~~ c(rv,NA)*x3
x4 ~~ c(rv,NA)*x4
x5 ~~ c(rv,NA)*x5
x6 ~~ c(rv,NA)*x6
x7 ~~ c(rv,NA)*x7
x8 ~~ c(rv,NA)*x8
x9 ~~ c(rv,NA)*x9'

fit <- sem(HS.model, sample.cov = cov_List, sample.mean = mean_List, meanstructure = TRUE, sample.nobs = n_List)
summary(fit)

##run model with cross-gorup constraints where residual variance of indicators is 
##constrained to be equal across indicators for Group 2
HS.model2 <- ' visual  =~ c(v1,v1)*x1 + c(v2,v2)*x2 + c(v3,v3)*x3
              textual =~ c(t1,t1)*x4 + c(t2,t2)*x5 + c(t3,t3)*x6
speed   =~ c(s1,s1)*x7 + c(s2,s2)*x8 + c(s3,s3)*x9
x1 ~~ c(NA,rv)*x1
x2 ~~ c(NA,rv)*x2
x3 ~~ c(NA,rv)*x3
x4 ~~ c(NA,rv)*x4
x5 ~~ c(NA,rv)*x5
x6 ~~ c(NA,rv)*x6
x7 ~~ c(NA,rv)*x7
x8 ~~ c(NA,rv)*x8
x9 ~~ c(NA,rv)*x9'

fit2 <- sem(HS.model2, sample.cov = cov_List, sample.mean = mean_List, meanstructure = TRUE, sample.nobs = n_List)
##Produces same output as first model, where it seems constraints for indicators are still being
##imposed for Group 1, but should be imposed for Group 2
summary(fit2)

##Models also produce exact same chi-square
lavInspect(fit2, "fit")["chisq"]-lavInspect(fit, "fit")["chisq"]

##Also confirm that covariances among indicators within groups are in fact different 
cov_List[[1]]-cov_List[[2]]

Terrence Jorgensen

unread,
Nov 29, 2018, 5:20:40 AM11/29/18
to lavaan
x1 ~~ c(rv,NA)*x1

You can't use NA in a character vector of parameter labels.  If you don't want to impose the constraint in one group, then you have to apply different labels for each residual variance in that group.  NA can only be used in numeric vectors of fixed values (where NA means "free this parameter" instead of "fix it to this value").

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam
http://www.uva.nl/profile/t.d.jorgensen


Andrew Grotzinger

unread,
Dec 4, 2018, 1:24:50 PM12/4/18
to lavaan
Perfect, thanks very much! 
Reply all
Reply to author
Forward
0 new messages