mismatch between number of groups in data, and number of groups in model.

340 views
Skip to first unread message

Maria Cyniak-Cieciura

unread,
Jul 17, 2023, 3:21:36 PM7/17/23
to lavaan
Dear lavaan community,

I could not find this particular topic among messages, therefore I added another one. Did you encounter this kind of message:

lavaan ERROR: mismatch between number of groups in data, and number of groups in model.

I am running measurement invariance analysis using the code:
config_MPFI <- cfa(fit_MPFI_2, data=MPFI_791_1, estimator="MLM", , group = "education") and get this message:
Error in lavaan::lavaan(model = fit_MPFI_2, data = MPFI_791_1, group = "education",  :
  lavaan ERROR: mismatch between number of groups in data, and number of groups in model

I do not have any missing values in my data, and have only two values for education: 1 - lower than higher education, 2 = higher education.

The same error occured while comparing gender, again with only two values and without missing values. 

Is this something wrong with my dataset? 

I hope you can help.

Maria

Edward Rigdon

unread,
Jul 17, 2023, 4:07:18 PM7/17/23
to lav...@googlegroups.com
Can you share your model syntax? The error suggests that perhaps your model syntax points to more than two groups. Perhaps you have three labels inside a "c(...)" structure, or maybe an accidental comma.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/c59e2f83-1e2f-405f-b8d3-f2f62ebb193bn%40googlegroups.com.

Maria Cyniak-Cieciura

unread,
Jul 17, 2023, 7:20:23 PM7/17/23
to lav...@googlegroups.com
Sure, this is my code for a simple analysis in a whole group:

model_MPFI_2 <- 'AC =~ MPFI_A1 + MPFI_A2 + MPFI_A3 + MPFI_A4 + MPFI_A5  
PM =~ MPFI_CH1 + MPFI_CH2 + MPFI_CH3 + MPFI_CH4 + MPFI_CH5
SC =~ MPFI_JA1 + MPFI_JA2 + MPFI_JA3 + MPFI_JA4 + MPFI_JA5
DF =~ MPFI_D1 + MPFI_D2 + MPFI_D3 + MPFI_D4 + MPFI_D5
VAL =~ MPFI_W1 + MPFI_W2 + MPFI_W3 + MPFI_W4 + MPFI_W5  
CA =~ MPFI_ZD1 + MPFI_ZD2 + MPFI_ZD3 + MPFI_ZD4 + MPFI_ZD5
EA =~ MPFI_U1 + MPFI_U2 + MPFI_U3 + MPFI_U4 + MPFI_U5
LPM =~ MPFI_BCH1 + MPFI_BCH2 + MPFI_BCH3 + MPFI_BCH4 + MPFI_BCH5
SCT =~ MPFI_SJA1 + MPFI_SJA2 + MPFI_SJA3 + MPFI_SJA4 + MPFI_SJA5
FUS =~ MPFI_F1 + MPFI_F2 + MPFI_F3 + MPFI_F4 + MPFI_F5
LVAL =~ MPFI_BW1 + MPFI_BW2 + MPFI_BW3 + MPFI_BW4 + MPFI_BW5
INAC =~ MPFI_BEZ1 + MPFI_BEZ2 + MPFI_BEZ3 + MPFI_BEZ4 + MPFI_BEZ5
GF =~ NA*AC + PM + SC + DF + VAL + CA
GINF =~ NA*EA + LPM + SCT + FUS + LVAL + INAC
GF ~~ 1*GF
GINF ~~ 1*GINF'

fit_MPFI_2 <- cfa(model_MPFI_2, estimator = "MLM", data = MPFI_791_1)

summary(fit_MPFI_2,fit.measures=TRUE,standardized=TRUE)

fitmeasures(fit_MPFI_2)

Maria

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/-rKKcg4VTOc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/CAHxMgefZbYhZzfTAdFvM7fDq-%2B35BhFYvXoz8Oyu6AWfz%2BYg0g%40mail.gmail.com.


--
Dr n. społ. Maria Cyniak-Cieciura

Łukasz Stasiełowicz

unread,
Jul 18, 2023, 7:20:58 AM7/18/23
to lavaan
Too many commas before the group argument?

config_MPFI <- cfa(fit_MPFI_2, data=MPFI_791_1, estimator="MLM", , group = "education")

Maria Cyniak-Cieciura

unread,
Jul 18, 2023, 7:26:26 AM7/18/23
to lav...@googlegroups.com
Thanks, but this is not a solution. After removing the comas I still get the same error:

config_MPFI <- cfa(fit_MPFI_2, data=MPFI_CompACT_R, estimator="MLM", group = "education")
Error in lavaan::lavaan(model = fit_MPFI_2, data = MPFI_CompACT_R, group = "education",  :
  lavaan ERROR: mismatch between number of groups in data, and number of groups in model.
>

Maria 

Edward Rigdon

unread,
Jul 18, 2023, 8:24:23 AM7/18/23
to lav...@googlegroups.com
Please show your model syntax. The problem is there, not in your function call.

rpc...@psu.edu

unread,
Jul 18, 2023, 8:32:54 AM7/18/23
to lavaan
When running the multiple group model, did you adjust the syntax for GF and GINF, or did you leave it the same as it was when running the single group model? If you didn't adjust the syntax, try c(NA,NA) for the first indicator of both variables and try c(1,1) when specifying their variance.

Maria Cyniak-Cieciura

unread,
Jul 18, 2023, 10:25:55 AM7/18/23
to lav...@googlegroups.com
I believe this is what I have already done:

model_MPFI_2 <- 'AC =~ MPFI_A1 + MPFI_A2 + MPFI_A3 + MPFI_A4 + MPFI_A5  
PM =~ MPFI_CH1 + MPFI_CH2 + MPFI_CH3 + MPFI_CH4 + MPFI_CH5
SC =~ MPFI_JA1 + MPFI_JA2 + MPFI_JA3 + MPFI_JA4 + MPFI_JA5
DF =~ MPFI_D1 + MPFI_D2 + MPFI_D3 + MPFI_D4 + MPFI_D5
VAL =~ MPFI_W1 + MPFI_W2 + MPFI_W3 + MPFI_W4 + MPFI_W5  
CA =~ MPFI_ZD1 + MPFI_ZD2 + MPFI_ZD3 + MPFI_ZD4 + MPFI_ZD5
EA =~ MPFI_U1 + MPFI_U2 + MPFI_U3 + MPFI_U4 + MPFI_U5
LPM =~ MPFI_BCH1 + MPFI_BCH2 + MPFI_BCH3 + MPFI_BCH4 + MPFI_BCH5
SCT =~ MPFI_SJA1 + MPFI_SJA2 + MPFI_SJA3 + MPFI_SJA4 + MPFI_SJA5
FUS =~ MPFI_F1 + MPFI_F2 + MPFI_F3 + MPFI_F4 + MPFI_F5
LVAL =~ MPFI_BW1 + MPFI_BW2 + MPFI_BW3 + MPFI_BW4 + MPFI_BW5
INAC =~ MPFI_BEZ1 + MPFI_BEZ2 + MPFI_BEZ3 + MPFI_BEZ4 + MPFI_BEZ5
GF =~ NA*AC + PM + SC + DF + VAL + CA
GINF =~ NA*EA + LPM + SCT + FUS + LVAL + INAC
GF ~~ 1*GF
GINF ~~ 1*GINF'

The loading of the first indicator of higher order factors is free and the variance of higher order factors is fixed to 1.

I have converted data to data.frame and checked that "education" is numeric and is coded as 1 (secondary education) and 2 (higher education). Everything looks good. 

Chesnut, Ryan

unread,
Jul 18, 2023, 10:30:20 AM7/18/23
to lav...@googlegroups.com
Try:

GF =~ c(NA,NA)*AC + PM + SC + DF + VAL + CA
GINF =~ c(NA,NA)*EA + LPM + SCT + FUS + LVAL + INAC
GF ~~ c(1,1)*GF
GINF ~~ c(1,1)*GINF

Ryan P. Chesnut, PhD

Assistant Research Professor

Clearinghouse for Military Family Readiness

The Pennsylvania State University

402 Marion Place

University Park, PA 16802

 

Phone: 814-865-9637

Email: rpc...@psu.edu

Website: www.militaryfamilies.psu.edu

 


From: lav...@googlegroups.com <lav...@googlegroups.com> on behalf of Maria Cyniak-Cieciura <mcyniak-...@swps.edu.pl>
Sent: Tuesday, July 18, 2023 10:25 AM
To: lav...@googlegroups.com <lav...@googlegroups.com>
Subject: Re: mismatch between number of groups in data, and number of groups in model.
 

Maria Cyniak-Cieciura

unread,
Jul 18, 2023, 10:37:10 AM7/18/23
to lav...@googlegroups.com
Dear Ryan,
This leads to another error: 
Error in lavaanify(model = FLAT, constraints = constraints, varTable = DataOV,  :
  lavaan ERROR: wrong number of arguments in modifier () of element GF=~AC
>
M. 

Edward Rigdon

unread,
Jul 18, 2023, 10:38:22 AM7/18/23
to lav...@googlegroups.com
Where you have a multiplier or NA* in front of a variable, that is where lavaan would look for information about how many groups you have. Your structure is fine for just one group, but if there are multiple groups, then lavaan expects multiple multipliers or modifiers. So, for example, in the line that says:
GF =~ NA*AC + PM + SC + DF + VAL + CA
with two groups, you should write:
GF =~ c(NA,NA)*AC + PM + SC + DF + VAL + CA

and in the line that says:
GF ~~ 1*GF
with two groups, you should write:
GF ~~ c(1,1)*GF

This only changes the last 4 lines of your syntax, but I think that will solve your problem.



Maria Cyniak-Cieciura

unread,
Jul 18, 2023, 10:51:40 AM7/18/23
to lav...@googlegroups.com
Dear Edward,

Following your suggestions, I have created the model:

model_MPFI_22 <- 'AC =~ MPFI_A1 + MPFI_A2 + MPFI_A3 + MPFI_A4 + MPFI_A5  

PM =~ MPFI_CH1 + MPFI_CH2 + MPFI_CH3 + MPFI_CH4 + MPFI_CH5
SC =~ MPFI_JA1 + MPFI_JA2 + MPFI_JA3 + MPFI_JA4 + MPFI_JA5
DF =~ MPFI_D1 + MPFI_D2 + MPFI_D3 + MPFI_D4 + MPFI_D5
VAL =~ MPFI_W1 + MPFI_W2 + MPFI_W3 + MPFI_W4 + MPFI_W5  
CA =~ MPFI_ZD1 + MPFI_ZD2 + MPFI_ZD3 + MPFI_ZD4 + MPFI_ZD5
EA =~ MPFI_U1 + MPFI_U2 + MPFI_U3 + MPFI_U4 + MPFI_U5
LPM =~ MPFI_BCH1 + MPFI_BCH2 + MPFI_BCH3 + MPFI_BCH4 + MPFI_BCH5
SCT =~ MPFI_SJA1 + MPFI_SJA2 + MPFI_SJA3 + MPFI_SJA4 + MPFI_SJA5
FUS =~ MPFI_F1 + MPFI_F2 + MPFI_F3 + MPFI_F4 + MPFI_F5
LVAL =~ MPFI_BW1 + MPFI_BW2 + MPFI_BW3 + MPFI_BW4 + MPFI_BW5
INAC =~ MPFI_BEZ1 + MPFI_BEZ2 + MPFI_BEZ3 + MPFI_BEZ4 + MPFI_BEZ5
GF =~ c(NA,NA)*AC + PM + SC + DF + VAL + CA
GINF =~ c(NA,NA)*EA + LPM + SCT + FUS + LVAL + INAC
GF ~~ c(1,1)*GF
GINF ~~ c(1,1)*GINF'

Then used:
fit_MPFI_22 <- cfa(model_MPFI_22, estimator = "MLM", data = MPFI_CompACT_R)

and got another error:

Error in lavaanify(model = FLAT, constraints = constraints, varTable = DataOV,  :
  lavaan ERROR: wrong number of arguments in modifier () of element GF=~AC
>

Maria 

Yves Rosseel

unread,
Jul 18, 2023, 10:57:22 AM7/18/23
to lav...@googlegroups.com
Almost. Use this syntax, but add group = "education".

Yves.
> <mailto:edward...@gmail.com>> napisał(a):
>
> Where you have a multiplier or NA* in front of a variable, that is
> where lavaan would look for information about how many groups you
> have. Your structure is fine for just one group, but if there are
> multiple groups, then lavaan expects multiple multipliers or
> modifiers. So, for example, in the line that says:
> GF =~ NA*AC + PM + SC + DF + VAL + CA
> with two groups, you should write:
> GF =~ c(NA,NA)*AC + PM + SC + DF + VAL + CA
>
> and in the line that says:
> GF ~~ 1*GF
> with two groups, you should write:
> GF ~~ c(1,1)*GF
>
> This only changes the last 4 lines of your syntax, but I think that
> will solve your problem.
>
>
>
> On Tue, Jul 18, 2023 at 10:25 AM Maria Cyniak-Cieciura
> <mcyniak-...@swps.edu.pl <mailto:mcyniak-...@swps.edu.pl>>
> wrote:
>
> I believe this is what I have already done:
>
> model_MPFI_2 <- 'AC =~ MPFI_A1 + MPFI_A2 + MPFI_A3 + MPFI_A4 +
> MPFI_A5
> PM =~ MPFI_CH1 + MPFI_CH2 + MPFI_CH3 + MPFI_CH4 + MPFI_CH5
> SC =~ MPFI_JA1 + MPFI_JA2 + MPFI_JA3 + MPFI_JA4 + MPFI_JA5
> DF =~ MPFI_D1 + MPFI_D2 + MPFI_D3 + MPFI_D4 + MPFI_D5
> VAL =~ MPFI_W1 + MPFI_W2 + MPFI_W3 + MPFI_W4 + MPFI_W5
> CA =~ MPFI_ZD1 + MPFI_ZD2 + MPFI_ZD3 + MPFI_ZD4 + MPFI_ZD5
> EA =~ MPFI_U1 + MPFI_U2 + MPFI_U3 + MPFI_U4 + MPFI_U5
> LPM =~ MPFI_BCH1 + MPFI_BCH2 + MPFI_BCH3 + MPFI_BCH4 + MPFI_BCH5
> SCT =~ MPFI_SJA1 + MPFI_SJA2 + MPFI_SJA3 + MPFI_SJA4 + MPFI_SJA5
> FUS =~ MPFI_F1 + MPFI_F2 + MPFI_F3 + MPFI_F4 + MPFI_F5
> LVAL =~ MPFI_BW1 + MPFI_BW2 + MPFI_BW3 + MPFI_BW4 + MPFI_BW5
> INAC =~ MPFI_BEZ1 + MPFI_BEZ2 + MPFI_BEZ3 + MPFI_BEZ4 + MPFI_BEZ5
> GF =~ NA*AC + PM + SC + DF + VAL + CA
> GINF =~ NA*EA + LPM + SCT + FUS + LVAL + INAC
> GF ~~ 1*GF
> GINF ~~ 1*GINF'
>
> The loading of the first indicator of higher order factors is
> free and the variance of higher order factors is fixed to 1.
>
> I have converted data to data.frame and checked that "education"
> is numeric and is coded as 1 (secondary education) and 2 (higher
> education). Everything looks good.
>
> wt., 18 lip 2023 o 14:32 rpc...@psu.edu <mailto:rpc...@psu.edu>
> <rpc...@psu.edu <mailto:rpc...@psu.edu>> napisał(a):
>
> When running the multiple group model, did you adjust the
> syntax for GF and GINF, or did you leave it the same as it
> was when running the single group model? If you didn't
> adjust the syntax, try c(NA,NA) for the first indicator of
> both variables and try c(1,1) when specifying their variance.
> On Monday, July 17, 2023 at 7:20:23 PM UTC-4
> https://groups.google.com/d/msgid/lavaan/c59e2f83-1e2f-405f-b8d3-f2f62ebb193bn%40googlegroups.com <https://groups.google.com/d/msgid/lavaan/c59e2f83-1e2f-405f-b8d3-f2f62ebb193bn%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
>
> 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/-rKKcg4VTOc/unsubscribe <https://groups.google.com/d/topic/lavaan/-rKKcg4VTOc/unsubscribe>.
> To unsubscribe from this group and all its topics,
> send an email to lavaan+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lavaan/CAHxMgefZbYhZzfTAdFvM7fDq-%2B35BhFYvXoz8Oyu6AWfz%2BYg0g%40mail.gmail.com <https://groups.google.com/d/msgid/lavaan/CAHxMgefZbYhZzfTAdFvM7fDq-%2B35BhFYvXoz8Oyu6AWfz%2BYg0g%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
>
> --
> Dr n. społ. Maria Cyniak-Cieciura
>
> --
> 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/-rKKcg4VTOc/unsubscribe <https://groups.google.com/d/topic/lavaan/-rKKcg4VTOc/unsubscribe>.
> To unsubscribe from this group and all its topics, send an
> email to lavaan+un...@googlegroups.com
> <mailto:lavaan+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lavaan/a3ea046f-a029-41b8-bdec-09d17ed838d0n%40googlegroups.com <https://groups.google.com/d/msgid/lavaan/a3ea046f-a029-41b8-bdec-09d17ed838d0n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
>
>
> --
> Dr n. społ. Maria Cyniak-Cieciura
>
> --
> 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
> <mailto:lavaan+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lavaan/CAMHZWPutuhF2%3DQkLOocj2D4Racuqg2eyQp6rb1p_vDMzO4NcCg%40mail.gmail.com <https://groups.google.com/d/msgid/lavaan/CAMHZWPutuhF2%3DQkLOocj2D4Racuqg2eyQp6rb1p_vDMzO4NcCg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
> --
> 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/-rKKcg4VTOc/unsubscribe
> <https://groups.google.com/d/topic/lavaan/-rKKcg4VTOc/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email to
> lavaan+un...@googlegroups.com
> <mailto:lavaan+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lavaan/CAHxMgectFFXV2nt-vemcxxkzRir0X_n1gYaMAgS958DMWt51mQ%40mail.gmail.com <https://groups.google.com/d/msgid/lavaan/CAHxMgectFFXV2nt-vemcxxkzRir0X_n1gYaMAgS958DMWt51mQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
>
> --
> Dr n. społ. Maria Cyniak-Cieciura
>
> --
> 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
> <mailto:lavaan+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lavaan/CAMHZWPsSYhk_HW%3DL_D7%2BLAMi4_G-enYj%3D2rdxJ%2B6Fp-N8x%2Bc9Q%40mail.gmail.com <https://groups.google.com/d/msgid/lavaan/CAMHZWPsSYhk_HW%3DL_D7%2BLAMi4_G-enYj%3D2rdxJ%2B6Fp-N8x%2Bc9Q%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Maria Cyniak-Cieciura

unread,
Jul 18, 2023, 11:03:42 AM7/18/23
to lav...@googlegroups.com
Thank you very much, this works!
Maria


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