R - Multigroup CFA with lavaan Example
HTML-код
- Опубликовано: 15 июл 2024
- Lecturer: Dr. Erin M. Buchanan
Harrisburg University of Science and Technology
Fall 2019
This video updates the older version of the multigroup confirmatory factor analysis examples. This version uses a newer package and shows you how to complete the steps of a multigroup analysis even if you don't want to use that package. Learn how to compare groups in CFA for configural, metric, scalar, and strict residual invariance, followed by a few ways to calculate latent means. I also talk very briefly about equivalence testing - more on that to come!
Complete outline of the SEM course is here: statisticsofdoom.com/page/str....
The materials for this lecture are on our OSF page: osf.io/2y67f/
Another superb lecture! Sums up 2 weeks of SEM class review. Brilliant!
Thank you!
This is great! Helped me so much with a project. Thank you very much for contributing.
Thanks for the kind words!
Gracias, mega útil
Thank you very much! The part with looping of partial invariance really helped as I'm using bifactor model with robust CFI and RMSEA, so the partialInvariance() is no use for me. Lovely lecture!
Glad to have helped!
That is a great video! Thanks so much for your contribution.
Quick question about the possibility to use the eqMI.main function for more than two groups.
I tried with three groups, but I got the following message: "Error in (function (...) :
projection method only applies to two groups with this function."
When I use only two, everything works fine. What would you recommend in this case?
Should I run the same procedure for each pair (i.e., 1 vs 2, 2 vs 3, 1 vs 3)?
Hmm good question. I hadn't played with it for more than two groups obviously. That would be my recommendation, as that's what you would have to do anyway to figure out where the invariance is either way.
Nice video! thank you very much. Can you paste here the link to te videos you mention at the beggining of the talk? (the links seems not to be working anymore)
I am working on the website now, been some issues with the provider. You can find the MGCFA videos here for now: ruclips.net/channel/UCMdihazndR0f9XBoSXWqnYgsearch?view_as=subscriber&query=multigroup+cfa
Thanks for the video! It saved my life (or at least thesis) so far...
I wonder if you know of any way to persuade R into equivalence testing with robust chi square values after WLSMV estimation?
Thank you in advance :)
... not sure honestly. I thought the X2 provided was always the robust one. Maybe check the lavaan help pages lavaan.ugent.be/
Thanks a lot for that great video!
I was just wondering if you have any idea how to get the CFI calculated by the WLSMV estimation method in the last partial invariance part where you list the CFIs in a decreasing way?
So I am stuck at the line:
CFI_list [i]
Oh, I do not. I would recommend probably calculating it manually - the formula is in the notes for the lecture video (check out the fit indices video here statisticsofdoom.com/page/structural-equation-modeling/).
So helpful! Thank you for this clear guide. Re 2021 status: Are the packages here in use still valid or any of them are or will be depracated soon?
I know people have had a lot of issues with equal test MI. It is still out there, but I know I get emails about it not working (unclear if it's user error or package issues). I just posted an update that shows you how to do MG totally in lavaan if you can't get this one to work. ruclips.net/video/AWRWCGbUEjU/видео.html
Hi Dr. Erin! Thank you so much for this helpful lecture. If the model was invariant until scalar, what does this mean? I’m testing the MI of a scale for paper-and-pen method vs online.
That means it’s all the same up to the scalar step. So there’s probably a difference in the intercepts between the groups.
Thank you very much. This helped me a lot. You have explained the partial invariance of residuals. I am very new in learning R. Would you please share the syntax of partial invariance for metric and scaler ? or what change I should do in the syntax of partial invariance of residuals to get it for metric and scaler? I tried by using "=~" & "~1" but it returned constant values 😞
Check out the 2020 update for examples: statisticsofdoom.com/page/structural-equation-modeling/
Hi Dr. Erin, (this might be a naive question but) I want to ask what names should I include in the code below, to also include chi pvalue, and upper and lower limit of 90% CFI in the table? Thanks heaps!
table_fit[1, ]
The p value is just "pvalue" (you can run fitmeasures(fit.MHQ) to see everything available). The only CI provided is RMSEA, so you'd have to calculate how to do the CI on CFI yourself. I actually haven't ever tried that, but this article may have some help: www.tandfonline.com/doi/abs/10.1080/10705511.2018.1562351?journalCode=hsem20
This was brilliant and so helpful to finally make a break testing for invariance in my CFA. Thank you so much! I do have a question as to whether the eqMI.main function can also be used to test for multigroup path analysis, and if the steps would be the same. I have tried dipping my toes but I keep getting an error that I have missing data (even though I already made sure to filter that).
I've had a lot of problems with that package and actually recommend just doing it manually now (I have updates to this video). And yes, you could do it with path analysis as well.
@@StatisticsofDOOMoh I'll definitely check the updated videos then! Thanks so much for pointing me to them!
Hi! Do you know how eqMI.main handles missing data? I can't seem to find documentation on it, and keep getting an error (Error in (function (..., lamb0 = NULL) :
Check missing data in raw data or NAs in the sample covariances). When I add in the argument missing = TRUE it does not seem to work either. When I test the overall model with CFA I do not get this error.
I don't think it does - you have to have a model without missing data. I seem to remember this from testing it myself for a project, but the documentation should say. cran.r-project.org/web/packages/equaltestMI/equaltestMI.pdf
Thanks for sharing brilliant lecture :) your lectures have been very helpful for starting my career as a scientist. Just have one question tho. I am trying to do invariance testing of hirarchical model. When i typed in eqmi.main code and a warning appeared that says “[.data.frame’(dotdotdot$data, , c(useVar, dotdotdot$group)) : undefined columns selected.” When i ran eqmi.main for overall factor (non-hierarchical) model, such warning did not appear. Data sets for each model are the same. Could you kindly explain why am i having this problem?
Literally don’t know what causes this error - you aren’t the first one to see it. I have some newer examples on the channel that show how to do the test manually so you don’t have to use this package.
Hi Dr. Erin! Great video! I'm trying to follow along with some of my own data, but have not been successful in running the eqMI.main function. I get this error: Error in as.character.default(x) :
no method for coercing this S4 class to a vector. I have tried changing my group variable to a factor with different levels, but the same error popped up. Do you have any suggestions?
Yes! I have a new video that I need to make visible that does not use this package because it seems to have a new error every week!
@@StatisticsofDOOM that’s great to hear! Thanks for your quick reply :)
Hello Dr. Buchanan! when fitting the model for measurement invariance in lavaan, is it possible to correlate error terms on specific groups (e.g. Item1 ~~ Item2 on group 2 only, not the whole model)? Thank you!
Yes - I would tell you to check out the section on the lavaan page: lavaan.ugent.be/tutorial/groups.html that says fix the parameters in some groups but not all. You'd do something like Item1 ~~ c(0, NA)*Item2 so the first group is not estimated (it's zero), and the second group is estimated.
@@StatisticsofDOOM Thank you very much!!!
Hi Dr. Buchanan, I am wondering how I would go about controlling for variances of other variables before fitting the models. Do you have any recommendations?
Furthermore, I am wondering how this invariance test can be conducted for second order factor structures. I have searched the web vigorously for information. I figured my best bet was to ask you. (:
You would need to custom program a model that includes the other variables but does not automatically test them. I would recommend checking out the guide on lavaan's website to determine how to write the model structure for only the specific paths you are interested in testing. The Beaujean and Brown books both cover higher order structure tests.
Can I follow this procedure for a SEM (structural model)?
Yep! Check out the full set of different options at: lavaan.ugent.be/tutorial/groups.html
If i find that my model is nit invariant at scalar model stage, do I stop there ( do partial invar test) or I still need to continue to run strict (error invariance)?
I would use partial invariance to figure out which items were the non-invariant ones.
Thanks for the video! When running eqMI.main, I get the following error : Error in if (max(abs(dtheta)) > ep) break :
missing value where TRUE/FALSE needed. I checked whether there were any missing values in my group variable but no missing values are found. Do you know what this error might mean?
You know, someone else asked this question, and I'm not sure we ever figured it out. What's the entire eq command you are trying?
@@StatisticsofDOOM I checked the data again and there appeared to be a missing value in one of my items of my subscale. So now everything is resolved but I'm having trouble with my partial invariance testing: I want to test partial invariance for my scalar invariance model, but if I run the code, the values in my CFI list are the same for every item: acc1 ~1 acc1 acc2 ~1 acc2 acc3 ~1 acc3 acc4 ~1 acc4 acc6 ~1 acc6
..... until my 17th item.
0.9488962 0.9488962 0.9488962 0.9488962 0.9488962
I don't know if I did something wrong in my code? Or does this mean that partial invariance does not improve my model?
Thank you for looking at my question!
@@chellymaes5657 That usually means that it doesn't improve the model by relaxing that constraint. Are the other values changing at all?
@@StatisticsofDOOM Yes they were, I found my items by which relaxing the constraint improved the model! Thank you for your fast response :) !
Thanks for the video!
When running eqMI.main I get the following error that I can't solve, unfortunately:
Error in `[.data.frame`(dotdotdot$data, , c(useVar, dotdotdot$group)) :
undefined columns selected
While when running eqMI.projection I get another error:
Error in FUN(X[[i]], ...) : 'x' has to be numeric
Have you checked that the group variable is in the dataframe?
@@StatisticsofDOOM yes, it is in the data frame. I don't get the error when running cfa or sem
@@TeckEnigm Dug through the code, could be an error in a few place. Can you paste the entire eqMI command?
Hi, what does it mean when the unstandardised indicator intercept estimates are higher in one group for each item, but the standardised estimates vary? Items on a depression scale for instance, male vs. female samples.
Vary how? (like they are smaller instead of larger?)
Statistics of DOOM some are slightly smaller, some are slightly larger. But unstandardised are all larger in the male group, compared with the female group.
A follow up question, if i have this configural model > iq =~ i1 + i2 + i3, + i4 and then I add
+ i2 ~~ c(NA,0)*i4 to it, does this second line free up the constraints on the residual covariance between i2 and i4 in the male sample, and fixes it to 0 in the female sample (if group 1 = male, group 2 = female)? Is this considered partial invariance? And in plain terms, if doing this does not result in a significantly poorer fit, does it mean that items 2 and 4 are somehow correlated for men, but not women?
@@BloopyBlobBob I think that should be correct yes.
@@BloopyBlobBob Then I would interpret the pattern of the data that X is larger than Y, etc.
First and foremost, i am running version 3.6.3 of R, using R studio interface and using a Windows 10 operating system.
When i run the code chunk for ## create a picture, i get the following error: "Error: package or namespace load failed for 'semPlot' in loadNamespace(i, c(lib.loc,libpath()), versionCheck =vI[[i]]:there is no package called igraph".
I don't think the package igraph is available for the version of R i am using, 3.6.3.
Is there a way i can fix this error and still use the semPlot package?
if not, is there another package i can use to create a similar picture? if so, can you tell me which package? can you also provide the code for the picture?
In any case, i love your youtube videos. They are very informative. :)
Thanks! Try installing the igraph package directly, install.packages("igraph")...I have 3.6.3, and it's available for me to download. That should solve your problem.
@@StatisticsofDOOM Thanks :) installing igraph directly solved it.