At the heart of the lavaan package is the 'model syntax'. The model syntax is a description of the model to be estimated. In this section, we briefly explain the elements of the lavaan model syntax. More details are given in the examples that follow.

In the R environment, a regression formula has the following form:

In this formula, the tilde ("`~`

") is the regression operator. On
the left-hand side of the operator, we have the dependent variable (`y`

),
and on the right-hand side, we have the independent variables, separated
by the "`+`

" operator. In lavaan, a typical model is simply a set (or system)
of regression formulas, where some variables (starting with an '`f`

'
below) may be latent. For example:

If we have latent variables in any of the regression formulas, we must 'define'
them by listing their (manifest or latent) indicators. We do this by using the
special operator "`=~`

", which can be read as *is measured by*. For example,
to define the three latent variabels `f1`

, `f2`

and `f3`

, we can use something
like:

Furthermore, variances and covariances are specified using a 'double tilde' operator, for example:

And finally, intercepts for observed and latent variables are simple
regression formulas with only an intercept (explicitly denoted by the
number '`1`

') as the only predictor:

Using these four *formula types*, a large variety of latent variable models can
be described. The current set of formula types is summarized in the table
below.

formula type | operator | mnemonic |
---|---|---|

latent variable definition | `=~` |
is measured by |

regression | `~` |
is regressed on |

(residual) (co)variance | `~~` |
is correlated with |

intercept | `~ 1` |
intercept |

A complete lavaan model syntax is simply a combination of these formula types,
enclosed between *single* quotes. For example:

You can type this syntax interactively at the R prompt, but it is much more convenient to type the whole model syntax first in an external text editor. And when you are done, you can copy/paste it to the R console. If you are using RStudio, open a new 'R script', and type your model syntax (and all other R commands needed for this session) in the source editor of RStudio. And save your script, so you can reuse it later on.

The code piece above will produce a model syntax object, called `myModel`

that
can be used later when calling a function that actually estimates this model
given a dataset. Note that formulas can be split over multiple lines, and you
can use comments (starting with the `#`

character) and blank lines within the
single quotes to improve the readability of the model syntax.

If your model syntax is rather long, or you need to reuse the model syntax over
and over again, you may prefer to store it in a separate text file called, say,
`myModel.lav`

. This text file should be in a human readable format (not a Word
document). Within R, you can then read the model syntax from the file as
follows:

The argument of `readLines`

is the full path to the file containing the model
syntax. Again, the model syntax object can be used later to fit this model
given a dataset.