multi-group analysis using covariance matrix as input

874 views
Skip to first unread message

Xiaod

unread,
Apr 2, 2015, 10:06:22 PM4/2/15
to lav...@googlegroups.com
Hi there,

 I am a new user of R and lavaan, so this question may be naive but I am trying to using covariance matrix as input (not the raw data) for multi-group analysis in lavaan. In the tutorial of lavann project, it says that "If you have multiple groups, the sample.cov argument must be a list containing the sample variance-covariance matrix of each group as a separate element in the list. If a mean structure is needed, the sample.mean argument must be a list containing the sample means of each group..." (http://lavaan.ugent.be/tutorial/cov.html). But I have no idea how to impliment this. Any advice? Is there any example/script  I can learn from?

Thanks in advance!

Shawn  

Edward Rigdon

unread,
Apr 2, 2015, 10:33:19 PM4/2/15
to lav...@googlegroups.com

Here is an example, with two groups, using the sem function, and specifying scalar invariance.  The lists are declared with the c() “concatenate” function.  The two covariance matrices are objects named covlo and covhi, and the sample mean vectors are objects named meanslo and meanshi.  Notice that you also need a list of sample sizes.

--Ed Rigdon

 

<- sem(model=model.syntax,
sample.cov=list(covlo,covhi),
sample.mean=list(meanslo,meanshi),
sample.nobs=list(200, 160),
group.equal=c("loadings","intercepts"))

--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
To post to this group, send email to lav...@googlegroups.com.
Visit this group at http://groups.google.com/group/lavaan.
For more options, visit https://groups.google.com/d/optout.

Yves Rosseel

unread,
Apr 3, 2015, 2:28:37 AM4/3/15
to lav...@googlegroups.com
> Is there any example/script I can learn from?

An example using the Holzinger & Swineford data:


library(lavaan)

HS.model <- ' visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
'

# using full data
fit <- cfa(HS.model, data=HolzingerSwineford1939, group="school")
summary(fit)

# Using the sample.cov interface
S.Pasteur <- cov(subset(HolzingerSwineford1939, school=="Pasteur")[,7:15])
S.GrantWhite <- cov(subset(HolzingerSwineford1939,
school=="Grant-White")[,7:15])

fit2 <- cfa(HS.model, sample.cov=list(Pasteur=S.Pasteur,
`Grant-White`=S.GrantWhite),
sample.nobs=c(156,145), meanstructure=TRUE)
summary(fit2)

Yves.

Xiaod

unread,
Apr 3, 2015, 8:39:59 AM4/3/15
to lav...@googlegroups.com
Dear Ed and Yves,

Thanks for the sample/example codes. It works perfectly for my case.

Shawn

Robin Beaumont

unread,
Feb 16, 2017, 7:13:09 PM2/16/17
to lavaan
Many thanks for that - fantastic

Ted Barker

unread,
Mar 11, 2019, 8:58:47 AM3/11/19
to lavaan
Hi Ed,

I have a follow-up question to the very helpful info that you provided (a while back now!). 

I am seeking to test "strong" invariance with two groups based on summary (covariance) input. I am having difficulty finding the correct format for sample.mean=list(means1,means2). In other words, is there a 'mean' equivalent to the getCov ? I've tried various approaches to creating a list of means, without luck. Any information will be greatly appreciated! 

UK_p<-'
1.00                        
0.23  1.00                  
0.13  0.31  1.00            
0.29  0.30  0.23  1.00      
0.02  0.15  0.11  0.04  1.00'
c.U <- getCov(UK_p, names = c("extra", "agree", "consc",
                                   "neurt", "open"))

Terrence Jorgensen

unread,
Mar 11, 2019, 2:08:50 PM3/11/19
to lavaan
I am having difficulty finding the correct format for sample.mean=list(means1,means2).

What's wrong with that format?  It is just a list of vectors containing means, as the help-page states.

In other words, is there a 'mean' equivalent to the getCov ? 

No, means don't go in a square matrix.

library(lavaan)

HS
.model <- ' visual  =~ x1 + x2 + x3
               textual =~ x4 + x5 + x6
               speed   =~ x7 + x8 + x9
             '


# using full data
fit
<- cfa(HS.model, data=HolzingerSwineford1939, group="school")
summary
(fit)


# Using sample statistics
S
.Pasteur <- cov(subset(HolzingerSwineford1939, subset = school=="Pasteur",
                             
select = paste0("x", 1:9)))
S
.GrantWhite <- cov(subset(HolzingerSwineford1939, subset = school=="Grant-White",
                       
select = paste0("x", 1:9)))

M
.Pasteur <- colMeans(subset(HolzingerSwineford1939,
                             subset
= school=="Pasteur",
                             
select = paste0("x", 1:9)))
M
.GW <- colMeans(subset(HolzingerSwineford1939,
                        subset
= school=="Grant-White",
                       
select = paste0("x", 1:9)))


fit2
<- cfa(HS.model, sample.cov=list(Pasteur=S.Pasteur,
                                     
`Grant-White`=S.GrantWhite),

            sample
.mean = list(Pasteur=M.Pasteur, `Grant-White`= M.GW),
            sample
.nobs=c(156,145), meanstructure=TRUE)
summary
(fit2)

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Ted Barker

unread,
Mar 12, 2019, 7:30:39 AM3/12/19
to lav...@googlegroups.com
Thanks Terrence!! Worked like a charm....

--
You received this message because you are subscribed to a topic in the Google Groups "lavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lavaan/PWh2TAniWbM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.

To post to this group, send email to lav...@googlegroups.com.

Chris Castille

unread,
Aug 13, 2021, 7:39:59 AM8/13/21
to lavaan
I'm trying to do the same thing but encountering an error I need help debugging.

I have two covariance matrices:

# population moments
cov <- fitted(sem(population.model))
cov <- cov[["cov"]]
#getCov(cov, names = c("x1","x2","x3","x4","x5","x6","x7","x8"))
cov
   x1    x2    x3    x4    x5    x6    x7    x8   
x1 1.000                                          
x2 0.446 1.000                                    
x3 0.525 0.588 1.000                              
x4 0.391 0.438 0.516 1.000                        
x5 0.166 0.190 0.225 0.174 1.000                  
x6 0.153 0.175 0.206 0.159 0.472 1.000            
x7 0.158 0.180 0.212 0.162 0.525 0.552 1.000      
x8 0.148 0.168 0.199 0.154 0.428 0.438 0.490 1.000

# population moments
cov2 <- fitted(sem(population.model2))
cov2 <- cov2[["cov"]]
cov2
   x1    x2    x3    x4    x5    x6    x7    x8   
x1 0.927                                          
x2 0.360 0.900                                    
x3 0.424 0.470 0.861                              
x4 0.310 0.343 0.404 0.910                        
x5 0.065 0.072 0.085 0.062 0.860                  
x6 0.072 0.080 0.094 0.069 0.360 0.910            
x7 0.085 0.094 0.111 0.081 0.424 0.470 0.927      
x8 0.062 0.069 0.081 0.059 0.310 0.343 0.404 0.900

When I fit a model to the two covariance matrices above using the method specified previously...
fit4 <- cfa(population.model2b, sample.cov = list(NonRemdied=cov, Remedied=cov2), sample.nobs = 1000000,1000000,meanstructure=TRUE) 

I get this error:
Error in lavData(data = data, group = group, cluster = cluster, ov.names = OV.NAMES,  : 
  lavaan ERROR: data= argument is not a data.fame, but of class ‘numeric’

Advice?

Alex Schoemann

unread,
Aug 13, 2021, 9:42:25 AM8/13/21
to lavaan
sample.nobs needs to be a vector. So the cfa command should be:

fit4 <- cfa(population.model2b, sample.cov = list(NonRemdied=cov, Remedied=cov2), sample.nobs = c(1000000,1000000),meanstructure=TRUE) 

Alex

Chris Castille

unread,
Aug 13, 2021, 11:12:59 AM8/13/21
to lav...@googlegroups.com
That did it! Thanks!


fit 
<- cfa(HS.model,data=HolzingerSwineford1939,group="school") 
summary
(fit) 


# Using sample statistics 
S
.Pasteur <-cov(subset(HolzingerSwineford1939, subset = school=="Pasteur",
                             
select =paste0("x", 1:9)))
S
.GrantWhite <-cov(subset(HolzingerSwineford1939, subset = school=="Grant-White",
                        
select =paste0("x", 1:9)))


M
.Pasteur <-colMeans(subset(HolzingerSwineford1939,
                             subset 
=school=="Pasteur",

                             
select =paste0("x", 1:9)))

M
.GW <-colMeans(subset(HolzingerSwineford1939,
                        subset 
=school=="Grant-White",

                        
select =paste0("x", 1:9)))


fit2 
<- cfa(HS.model,sample.cov=list(Pasteur=S.Pasteur, 
                                      
`Grant-White`=S.GrantWhite), 
            sample
.mean =list(Pasteur=M.Pasteur, `Grant-White`=M.GW),
            sample
.nobs=c(156,145),meanstructure=TRUE) 
summary
(fit2)
Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

-- 
You received this message because you are subscribed to a topic in the Google Groups "lavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lavaan/PWh2TAniWbM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.

To post to this group, send email to lav...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to a topic in the Google Groups "lavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lavaan/PWh2TAniWbM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages