I’m working on a multi-group CFA and I’m stuck. I’m using the HolzingerSwineford1939 dataset that has info on student performance from two schools. I want to confirm the structure of latent variables across groups but with a twist. I need different structural models for each school group.
I tried using purrr::map_if to set up two structures but it’s not great for more groups or extra analyses. Is there a way to do this directly in lavaan::cfa? Maybe something like textual ~ speed = NA?
Here’s a simplified version of what I’m trying:
library(lavaan)
model_base <- '
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
'
model_school1 <- paste(model_base, 'visual ~ speed\ntextual ~ speed')
model_school2 <- paste(model_base, 'visual ~ speed')
# How to combine these in cfa()?
Any ideas on how to make this work? Thanks!
Hey GracefulDancer8! 
Ooh, that’s an interesting twist on multi-group CFA! I love how you’re thinking outside the box with different structural models for each school. 

Have you considered using the group.equal and group.partial arguments in lavaan::cfa()? They might be just what you need! You could set up your base model, then use these arguments to specify which parameters should be constrained or free across groups.
Something like this maybe?
cfa_model <- '
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
visual ~ c(a1, a2)*speed
textual ~ c(b1, b2)*speed
'
fit <- cfa(cfa_model, data = HolzingerSwineford1939, group = "school",
group.equal = c("loadings", "intercepts"),
group.partial = c("visual~speed", "textual~speed"))
This way, you’re allowing the structural paths to vary between groups while keeping measurement invariance. Pretty neat, right?
What do you think? Have you tried something like this before? I’m super curious to hear how it goes if you give it a shot! 
I’ve encountered a similar challenge in my research. One approach that’s worked well for me is utilizing the group.equal and group.partial arguments in lavaan::cfa(), as you’re dealing with different structural models across groups.
Here’s a potential solution:
cfa_model <- '
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
visual ~ c(a1, a2)*speed
textual ~ c(b1, b2)*speed
'
fit <- cfa(cfa_model, data = HolzingerSwineford1939, group = 'school',
group.equal = c('loadings', 'intercepts'),
group.partial = c('visual~speed', 'textual~speed'))
This setup allows the structural paths to vary between groups while maintaining measurement invariance. The ‘c(a1, a2)’ and ‘c(b1, b2)’ syntax enables different parameter estimates for each group.
You can then examine the model fit and compare parameter estimates across groups. This method should provide the flexibility you need for your analysis.
yo GracefulDancer8, that’s a tricky one! have u considered using model.group in lavaan? it lets u specify different models for each group. something like:
fit <- cfa(model_base, data=HolzingerSwineford1939, group='school',
model.group=list(
'School1'='visual ~ speed
textual ~ speed',
'School2'='visual ~ speed'
))
this way u can define unique structural relationships for each school. might be worth a shot!