lavaangui: A Visual Tool for Creating lavaan Models (CFA, SEM, Path Analysis) in R

by Arndt Regorz, MSc.
December 01, 2024

Lavaangui is a graphical user interface for the R package lavaan. This tutorial will show you how to use lavaangui to estimate a confirmatory factor analysis (CFA), a structural equation model (SEM) or a path model in lavaan without typing in R syntax, just by clicking and drawing your model.

Using lavaangui

There are two possible ways to work with lavaangui. Both are free.

You can use a shiny application in your web browser, you don’t even have to install R for that, simply by opening lavaangui.org

Or you can install lavaangui as an R package locally (for that you need to have installed R, of course).

install.packages("lavaangui")

library(lavaangui)
lavaangui()

Both approaches work the same way. If you just want to test lavaangui, then you should use the web application. However, for serious work with lavaangui the local installation has some advantages: In most cases the local installation will be faster because with a web application the individual users will have only very limited computing resources available to them. The web application has a time-out after a certain time of inactivity, possibly forcing you to start again in that case.

The lavaangui Environment

Here you can see a typical starting view of the lavaangui environment (in this case with a local installation in R):

lavaangui 1

There are different areas to be seen:

  1. Main Menu
  2. Top Toolbar
  3. Model Window
  4. Results Window
  5. Message Area
  6. Bottom Toolbar

With the Main Menu you get many options to tune your model (estimation method, layout, etc.).

The Top Toolbar allows you to insert the different graphical objects and variables into the model diagram.

The Model Window is the central part of lavaangui: here, you draw your model and after estimating it you get a visual representation of the results.

The Results Window has different functions, depending on the stage of your analysis: Here, you get a lavaan script for the model you have specified in the Model Window. After estimating the model, you get the central lavaan output in this window.

And at the start, you get a help text showing you the different keyboard shortcuts (but you don’t have to memorize those; you can use lavaangui with drag-and-drop instead).

The Message Area can display lavaan messages (errors, warnings).

The Bottom Toolbar allows you to switch between different modes (data, model, etimation).

Example: Estimating a CFA with lavaangui

As an example I will show you how to set up a simple CFA. There is a workflow for estimating a model:

  1. Importing data
  2. Drawing the model
  3. Checking the model
  4. Estimating the model

You import the data by going to the Main Menu, File-Load Data.

Then, you will get a preview of the datafile you have imported. (You can also go to the Bottom Toolbar and click on “Show Data”).

lavaangui 2

From there, you use the Bottom Toolbar to change to the “User Model” view. There, you can draw the model. After having imported your dataset you can see in the Top Toolbar the variables available in your dataset.

lavaangui 3

In order to draw your CFA model there are different options available.

You could draw the rectangles with the observed variables (x1, x2, …) into the Model Window, one by one. As an alternative you could click on “Multiple Variables” and choose all 9 observed variables, resulting in nine rectangles for these variables. In both cases you would have to add the three latent variables by dragging the round symbol for unobserved variables into the Model Window and then connect these with the one-headed arrow to the observed variables. Finally, you could right click on the latent variables and, using “Rename Variable” give them a better fitting name.

There is a faster alternative: You could draw the symbol “Factor” into the Model Window. Then you will be asked to choose which observed variables are part of that factor. This results in a factor model with factor, observed variables and arrows connecting those. Here you could rename the latent variables as well.

As a result, something like this could be shown:

lavaangui 4

The look of the model can be easily improved, either by manually changing the locations of the different elements in the graph with the mouse. Or by going to the Main Menu, Apply Layout. There you can choose a standard layout for your model graph, e.g. (with Recommended: Tree) this:

lavaangui 5

If you want to model covariances/correlations, then you can use the symbol with a double-headed arrow:

lavaangui 6

You edit the layout of specific paths (here between visual and speed) with the mouse.

lavaangui 7

These are the basic steps for setting up a model. But there are many additional options to change the model layout. For that, you can right-click on the different elements, changing parameters (e.g., fixing parameters, freeing parameters, assigning labels) and changing the visual representation of the elements (e.g., color, font).

In the results window on the right you get the lavaan code for the model you have drawn. If you want to perform the final estimation within R and lavaan (e.g., because you want one complete R code for your thesis or your article) then you will copy this code into R.

The next step is checking the model, going to the Bottom Toolbar choosing “Autocompleted Model”.

lavaangui 8

In this view you can see in dotted lines additional specifications for the model that have been automatically added. You can change the default settings for those automatic specifications by going to the Main Menu, Automatically.

For estimating the model you can go to the Bottom Toolbar and choose “Estimates”.

lavaangui 9

In the Results Window you get the global results (model test, fit indices) for your model. In this case the model fit is not really good (model test significant, CFI < .95, RMSEA > .06). For that reason you might want to look at modification indices.

You can find these by clicking on “More Results” in the Bottom Toolbar and then choosing the tab “Modification indices”. You can sort these in descending order my clicking twice on the column heading “mi”.

lavaangui 10

Here, we find two possible model changes with similar modification indices: A cross loading of item x9 on the factor visual and an error covariance between the items x7 and x8.

Let’s say you decide to allow the error covariance. In order to model that, you click again on “User Model” in the Bottom Toolbar and use the button with the double headed arrow to draw this additional covariance.

lavaangui 11

After checking “Autocompleted Model” you can click on “Estimates” to get the results for this respecified model.

lavaangui 12

In order to get the parameter estimates, you can click on “More Results” in the Bottom Toolbar and then choose the tab “Parameter Estimates”. On the left of this window you can change from unstandardized values to standardized values, and you can change the level for the confidence intervals and the number of digits in the output.

lavaangui 13

If you want to perform a robust overall model test, e.g. with the Satorra-Bentler adjustment, you can click on the tab “Exact Test” and choose “Satorra Bentler”.

lavaangui 14

If you want to change the way the results are shown in the model graph, you can go to “View” in the Main Window. There you can choose which results are shown in the model figure (e.g., unstandardized or standardized, including asterisks for significance, confidence intervals).

Finally, you might want to save your model (model alone or model including data) under “File” in the Main Window. There you can also export the visual model results as a figure in different file formats.

Reference

Karch, J. D. (2024, September 16th). lavaangui: A web-based graphical interface for specifying lavaan models by drawing path diagrams [preprint]. PsyArXiv. https://osf.io/f4ary

Citation

Regorz, A. (2024, December 01). lavaangui: A visual tool for creating lavaan models (CFA, SEM, path analysis) in R. Regorz Statistik. http://www.regorz-statistik.de/blog/lavaangui.html