We start with a simple example of confirmatory factor analysis, using the
cfa() function, which is a user-friendly function for fitting CFA models.
The lavaan package contains a built-in dataset called
See the help page for this dataset by typing
at the R prompt. This is a 'classic' dataset that is used in many papers and books on Structural Equation Modeling (SEM), including some manuals of commercial SEM software packages. The data consists of mental ability test scores of seventh- and eighth-grade children from two different schools (Pasteur and Grant-White). In our version of the dataset, only 9 out of the original 26 tests are included. A CFA model that is often proposed for these 9 variables consists of three latent variables (or factors), each with three indicators:
The figure below contains a graphical representation of the three-factor model.
The corresponding lavaan syntax for specifying this model is as follows:
In this example, the model syntax only contains three ‘latent variable definitions’. Each formula has the following format:
We call these expressions latent variable definitions because they define how
the latent variables are 'manifested by' a set of observed (or manifest)
variables, often called 'indicators'. Note that the special "
=~" operator in
the middle consists of a sign ("
=") character and a tilde (
next to each other. The reason why this model syntax is so short, is that
behind the scenes, the function will take care of several things. First, by
default, the factor loading of the first indicator of a latent variable is
fixed to 1, thereby fixing the scale of the latent variable. Second, residual
variances are added automatically. And third, all exogenous latent variables
are correlated by default. This way, the model syntax can be kept concise. On
the other hand, the user remains in control, since all this 'default' behavior
can be overriden and/or switched off.
We can enter the model syntax using the single quotes:
We can now fit the model as follows:
cfa() function is a dedicated function for fitting confirmatory factor
analysis models. The first argument is the user-specified model. The second
argument is the dataset that contains the observed variables. Once the model
has been fitted, the
summary() function provides a nice summary of the fitted
The output should look familiar to users of other SEM software. If you find it confusing or esthetically unpleasing, please let us know, and we will try to improve it.
The output consists of three parts. The first six lines are called the header. The header contains the following information:
The next section contains additional fit measures, and is only shown because we
use the optional argument
fit.measures = TRUE. It starts with the line
test baseline model and ends with the value for the
SRMR. The last section
contains the parameter estimates. It starts with information about the standard
errors (if the information matrix is expected or observed, and if the standard
errors are standard, robust, or based on the bootstrap). Then, it tabulates
all free (and fixed) parameters that were included in the model. Typically,
first the latent variables are shown, followed by covariances and (residual)
variances. The first column (
Estimate) contains the (estimated or fixed)
parameter value for each model parameter; the second column (
contains the standard error for each estimated parameter; the third column
Z-value) contains the Wald statistic (which is simply obtained by dividing
the parameter value by its standard error), and the last column (
contains the p-value for testing the null hypothesis that the parameter
equals zero in the population.
To wrap up this first example, we summarize the complete code that was needed to fit this three-factor model:
Simply copying this code and pasting it in R should work. The syntax illustrates the typical workflow in the lavaan package:
Specify your model using the lavaan model syntax. In this example, only latent variable definitions have been used. In the following examples, other formula types will be used.
Fit the model. This requires a dataset containing the observed variables
(or alternatively the sample covariance matrix and the number of observations).
In this example, we have used the
cfa() function. Other funcions in the
lavaan package are
growth() for fitting full structural equation
models and growth curve models respectively. All three functions are so-called
user-friendly functions, in the sense that they take care of many details
automatically, so we can keep the model syntax simple and concise. If you wish
to fit non-standard models or if you don't like the idea that things are done
for you automatically, you can use the lower-level function
where you have full control.
Extract information from the fitted model. This can be a long verbose summary, or it can be a single number only (say, the RMSEA value). In the spirit of R, you only get what you asked for. We try to not print out unnecessary information that you would ignore anyway.