Generating syntax for structural equation models

tidySEM offers a user-friendly, tidy workflow for generating syntax for SEM models. The workflow is top-down, meaning that syntax is generated based on conceptual model elements. In many cases, the generated syntax will suffice - but it is always customizable. The workflow also tries to intelligently guess which variables go together, but these defaults can be overridden.

The tidySEM workflow

The workflow underlying syntax generation in tidySEM is as follows:

  1. Give the variables in your data object short, informative names, that are easily machine readable
  2. Convert the data to a tidy_sem object by running model <- tidy_sem(data)
  3. Add elements of syntax
    • E.g., measurement(model)
  4. Optionally, access the dictionary, data, and syntax elements in the tidy_sem object by calling dictionary(model), get_data(model), or syntax(model)
  5. Optionally, modify the dictionary, data, and syntax elements in the tidy_sem object dictionary(model) <- ..., get_data(model) <- ..., and syntax(model) <- ...
  6. Run the analysis, either by:
    • Converting the tidy_sem object to lavaan syntax using as_lavaan(model) and using that as input for the lavaan functions sem, lavaan, or cfa
    • Converting the tidy_sem object to Mplus using as_mplus(model), and using that as input for MplusAutomation::mplusObject()
    • Using the functions estimate_lavaan(model) or estimate_mplus(model)

All elements of the tidy_sem object are “tidy” data, i.e., tabular data.frames, and can be modified using the familiar suite of functions in the ‘tidyverse’. Thus, the data, dictionary, and syntax are all represented as data.frames.

Example: Running a CFA

Step 1: Check the variable names

As an example, let’s make a graph for a classic lavaan tutorial example for CFA. First, we check the data names:

These names are not informative, as the items named x.. are indicators of three different latent variables. We will rename them accordingly:

Guidelines for naming variables

In general, it is good practice to name variables using the following information:

Roughly speaking, elements of the variable name should be ordered from “slow-changing” to “fast-changing”; i.e.; there are only a few scales, with possibly several measurement occasions or respondents, and many items.

Step 2: Generate a dictionary

A dictionary indicates which variables in the data belong to, for example, the same scale. When the data have informative names, it is possible to construct a data dictionary automatically:

Step 3: Generate syntax

We can automatically add basic syntax to the sem_syntax object, by passing it to a syntax-generating function like measurement(), which adds a measurement model for any scales in the object:

Step 4: Run the model

The resulting model can be evaluated as ‘Mplus’ syntax or ‘lavaan’ syntax, using the as_mplus and as_lavaan functions. For example, using lavaan:

Or, alternatively, the model can be estimated using the lavaan wrapper estimate_lavaan():

The same model can be estimated in ‘Mplus’ through the R-package MplusAutomation. This requires ‘Mplus’ to be installed.

#> Estimated using ML 
#> Number of obs: 301, number of (free) parameters: 30 
#> Model: Chi2(df = 24) = 85.306, p = 0 
#> Baseline model: Chi2(df = 36) = 918.852, p = 0 
#> Fit Indices: 
#> CFI = 0.931, TLI = 0.896, SRMR = 0.06 
#> RMSEA = 0.092, 90% CI [0.071, 0.114], p < .05 = 0.001 
#> AIC = 7535.49, BIC = 7646.703

Optional step 5: Access the dictionary, data, and syntax

The dictionary and syntax can be examined using dictionary(model) and syntax(model):

Optional step 6: Modify the dictionary and syntax

At this stage, we may want to modify the basic syntax slightly. The functions dictionary(model) <- ... and syntax(model) <- ... can be used to modify the dictionary and syntax:

For example, imagine we want to change the model, so that all items of the “spe” subscale load on the “tex” latent variable. We would first replace the latent variable “spe” with “tex”, and secondly remove all mention of the “spe” latent variable:

The modified model could then be run:

Optional step 7: Adding paths

In addition to the way of editing the data.frame with model syntax described in Step 6, it is also possible to add (or modify) paths by adding lavaan syntax. For example, imagine that - instead of having “vis” and “tex” correlate, we want to add a regression path between them:

This function accepts both quoted (character) and unquoted arguments. So, for example, if we want to add a cross-loading from “spe_1” on “vis”, in addition to the regression path before, we could use the following code: