Visualizing a two-level model by multilevel CFA

509 views
Skip to first unread message

Daiki Sato

unread,
May 10, 2021, 3:09:36 AM5/10/21
to lavaan
Hello everyone,

I've been working on multilevel CFA and trying to visualize and output both the within- and between-level models as a single figure as shown in Figure 3 here: https://francish.netlify.app/post/multilevel-cfa-mlf/. However, I found no way to do it, except the one here (Figure 11.2): http://sachaepskamp.com/dissertation/Chapter11.pdf  which seems like using Mplus output.

Does anybody know an easy way to do it? hopefully by lavaan with semPlot package?

Thanks!

Daiki

kma...@aol.com

unread,
May 10, 2021, 11:05:15 PM5/10/21
to lavaan
Daiki,
Isn't it more common to present the within and between models in separate path diagrams?  Be that as it may, if you draw the path diagram from Figure 3 in semPlot, it would not be at all difficult to add the brackets using the lines() function and the labels using the text() function.  If you opt for separate diagrams, you could use par(mfcol=c(2,1)) to break the plot area into two plot and draw both diagrams in the same image.  You could use title(main="...") or title(sub="...") to label the within and between diagrams.

Keith
------------------------
Keith A. Markus
John Jay College of Criminal Justice, CUNY
http://jjcweb.jjay.cuny.edu/kmarkus
Frontiers of Test Validity Theory: Measurement, Causation and Meaning.
http://www.routledge.com/books/details/9781841692203/

Daiki Sato

unread,
May 12, 2021, 12:14:06 PM5/12/21
to lavaan
Hi Keith,

Thanks for your reply. Your suggestions would help a lot if I could make any of within- or between-level models separately. Do you know how to do it?
So my situation is like following:
#One-level model
> onelevel <- "
fw1 =~ x1 + x2 + x3 + x4
"
> result1 <- lavaan::cfa(onelevel, data = df)
> semPaths(result1, "model", "est", intercepts = FALSE) #works well

#Two-level model
> twolevel <- "
level: 1
  fw1 =~ x1 + x2 + x3 + x4
level: 2
  fb1 =~ x1 + x2 + x3 + x4
"
> result2 <- lavaan::cfa(twolevel, data = df)
> semPaths(result2, "model", "est", intercepts = FALSE) #returns an error
Error in object@Data@ov.names[[i]] : subscript out of bounds

Is there any way to extract each of within- and between-level model separately from the variable and run semPaths function?

Any comments appreciated. Thanks!

Daiki
2021年5月11日火曜日 12:05:15 UTC+9 kma...@aol.com:

kma...@aol.com

unread,
May 12, 2021, 11:31:43 PM5/12/21
to lavaan
Daiki,
Comment out the parts you do not want to plot.

require(lavaan)
require(semPlot)

#Two-level model
twolevel <- "
#level: 1

fw1 =~ x1 + x2 + x3 + x4
#level: 2
#fb1 =~ x1 + x2 + x3 + x4
"

semPaths(lavaanify(twolevel))


Keith
------------------------
Keith A. Markus
John Jay College of Criminal Justice, CUNY
http://jjcweb.jjay.cuny.edu/kmarkus
Frontiers of Test Validity Theory: Measurement, Causation and Meaning.
http://www.routledge.com/books/details/9781841692203/

Daiki Sato

unread,
May 13, 2021, 12:23:08 AM5/13/21
to lavaan
Hi Keith, 

Thanks for your reply. 
My mistake. The code for two-level model was supposed to be like below, taking the "GroupID" into consideration as the second (between-) level.
> result2 <- lavaan::cfa(twolevel, data = df, cluster = "GroupID")

Anyway, thanks for the code! It makes a pure graph without coefficients etc. I understand the idea to write each estimate on the paths with line() or text() functions, but do you know how to extract the measurements (especially standardized coefficients and variances) from the model variable ("result2" in the above case)??

Thank you so much for your time.

Daiki
2021年5月13日木曜日 12:31:43 UTC+9 kma...@aol.com:

kma...@aol.com

unread,
May 13, 2021, 11:31:08 PM5/13/21
to lavaan
Daiki,
You can obtain the standardized estimates with lavInspect(fit, what='std').

However, semPaths() will plot the parameter values if you feed it the lavaan fit object as the object parameter value, and also use what = 'par'.

You can find more details in the help files for these two functions.


Keith
------------------------
Keith A. Markus
John Jay College of Criminal Justice, CUNY
http://jjcweb.jjay.cuny.edu/kmarkus
Frontiers of Test Validity Theory: Measurement, Causation and Meaning.
http://www.routledge.com/books/details/9781841692203/

Daiki Sato

unread,
May 14, 2021, 12:33:44 AM5/14/21
to lavaan
Hi Keith,

I have finally made it with your hint.
Thanks so much! 

Daiki

2021年5月14日金曜日 12:31:08 UTC+9 kma...@aol.com:
Reply all
Reply to author
Forward
0 new messages