1  Introduction

1.1 What this book is, and what it is not

This is a book about lavaan, a software package for latent variable analysis, and in particular for structural equation modeling (SEM). The book documents what lavaan does and, perhaps more importantly, how it does it: the data structures it builds internally, the choices it makes when translating a model from formula syntax to a numerical computation, the estimators and test statistics it implements, and the various ways the results can be accessed and interpreted.

The book is not a textbook on structural equation modeling. Readers who are new to SEM are encouraged to first work through an introduction to the field. Many good introductions are available, and any of them provides the background (latent variables, path diagrams, estimation, model fit) that this book takes for granted.

The book is also not a tutorial for first-time users of lavaan. Such material is already available elsewhere: Rosseel (2012) is the original Journal of Statistical Software paper, Rosseel and Vidal (2025) is a recent tutorial that also explains the statistical theory behind SEM, and the lavaan website (https://lavaan.org) provides an extensive, step-by-step introduction to using the package.

This book offers a single technical account of the internals of lavaan. It is written for researchers extending the package, methodologists wishing to implement their own SEM ideas, advanced students who need more than the output of summary(), and applied users who would like to know what exactly happens between calling cfa() and obtaining a parameter estimate.

The package these readers will encounter was not designed in full before it was implemented: lavaan first appeared on CRAN in 2010, and many of the design choices made in those early days are still visible in the code today. Some of them still seem sound in retrospect, and others do not. We have tried, throughout the book, to describe the package as it is (version 0.7-4 of the R package, with its current internal design) rather than as it might, in retrospect, have been designed. All examples in the book were produced with that version of lavaan, running under R 4.6.2. The history of the package is described in the epilogue (Section 20.3).

Because lavaan continues to evolve, it is worth stating explicitly which parts of this account are expected to remain valid. The model syntax, the parameter table and the meaning of its columns, the user-facing functions and their output, the public accessor functions (parTable(), lavInspect(), lavTech(), fitMeasures(), …), and above all the statistical theory: these constitute lavaan’s specification, and they are stable by design. How that specification is implemented (which internal R functions do the work, in which files they live, which numerical routines they call) is a different matter: it reflects the current implementation, and it will change as the package evolves. Throughout the book, passages that describe the current implementation rather than the specification are marked as such, usually in a section titled “Implementation notes” at the end of the chapter. Readers who want a durable mental model of lavaan can skip these sections. Readers who want to study or modify the current code base will find in them a description that is not available in any other document.

1.2 Who this book is for

When the package was first released, three groups of users were explicitly in mind. The same three groups are still useful for understanding the aims of lavaan, and for understanding who this book is written for.

The first group consists of researchers and methodologists working in the field of structural equation modeling. New developments in SEM are often hindered by the lack of open-source software in which the latest ideas can be implemented, tested, and shared with the wider community. By making its internals visible and extensible, lavaan tries to lower this barrier. Readers in this group will typically be most interested in the chapters on the parameter table, the matrix representation, and estimation and inference.

The second group consists of teachers. Teaching SEM at the graduate level should not require students to purchase a commercial license, nor should it force a choice between competing closed-source products. lavaan was designed to be free, and runnable on a student laptop. Teachers preparing course material will find a careful description of what lavaan computes, how it computes it, and where the textbook formulas appear in the output.

The third group consists of applied researchers. For this group, the priority is the opposite of the first: the syntax should be simple, the defaults should be sensible, and the output should be informative without being overwhelming. Most users of lavaan never need to read this book; the package is intended to be useful with its default settings. But for the subset of applied users who, at some point, want to know what happens internally (because a coefficient looks surprising, because two software packages disagree, or because a referee asks a difficult question), this book aims to be a reliable reference.

1.3 A first example

A complete worked example may help to fix ideas. The classical Holzinger and Swineford (1939) data set contains the scores of 301 seventh- and eighth-grade students on nine cognitive tests. A three-factor confirmatory factor model can be specified, fitted, and inspected in three steps:

HS_model <- '
  visual  =~ x1 + x2 + x3
  textual =~ x4 + x5 + x6
  speed   =~ x7 + x8 + x9
'

fit <- cfa(HS_model, data = HolzingerSwineford1939)
summary(fit)
lavaan 0.7-4 ended normally after 35 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                        21

  Number of observations                           301

Model Test User Model:
                                                      
  Test statistic                                85.306
  Degrees of freedom                                24
  P-value (Chi-square)                           0.000
                                                      
  Browne's residual (NT model-based) test             
  Test statistic                                81.368
  Degrees of freedom                                24
  P-value (Chi-square)                           0.000

Parameter Estimates:

  Standard errors                             Standard
  Information                                 Expected
  Information saturated (h1) model          Structured

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)
  visual =~                                           
    x1                1.000                           
    x2                0.554    0.100    5.554    0.000
    x3                0.729    0.109    6.685    0.000
  textual =~                                          
    x4                1.000                           
    x5                1.113    0.065   17.014    0.000
    x6                0.926    0.055   16.703    0.000
  speed =~                                            
    x7                1.000                           
    x8                1.180    0.165    7.152    0.000
    x9                1.082    0.151    7.155    0.000

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)
  visual ~~                                           
    textual           0.408    0.074    5.552    0.000
    speed             0.262    0.056    4.660    0.000
  textual ~~                                          
    speed             0.173    0.049    3.518    0.000

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)
   .x1                0.549    0.114    4.833    0.000
   .x2                1.134    0.102   11.146    0.000
   .x3                0.844    0.091    9.317    0.000
   .x4                0.371    0.048    7.779    0.000
   .x5                0.446    0.058    7.642    0.000
   .x6                0.356    0.043    8.277    0.000
   .x7                0.799    0.081    9.823    0.000
   .x8                0.488    0.074    6.573    0.000
   .x9                0.566    0.071    8.003    0.000
    visual            0.809    0.145    5.564    0.000
    textual           0.979    0.112    8.737    0.000
    speed             0.384    0.086    4.451    0.000

The remainder of this book is, in a sense, an extended commentary on this small example. Chapters 2 to 4 explain what happens to the model syntax before any data are touched. Chapters 5 and 6 explain how the data are processed and how starting values are obtained. Chapters 7 and 8 explain how the call to cfa() produces point estimates, standard errors, and test statistics. Chapter 9 explains the object fit itself and what is stored in it. Subsequent chapters explain fit measures, residuals, hypothesis tests, factor scores, and a number of more specialized topics.

1.4 A note on naming conventions

Earlier versions of lavaan used argument names such as std.lv, group.equal, and fixed.x. When lavaan was written (around 2010), this dotted style was the norm in base R itself (as in na.rm or read.csv()), and lavaan adopted the same style. Since then, the R community has adopted a different convention: the style guides that have become standard among R users (none of which existed in 2010) recommend snake_case names, using underscores instead of dots. The dot also has a technical drawback in R, because it doubles as the separator for S3 methods (print.data.frame() is the print() method for data frames), so a dot in a name can be ambiguous in a way an underscore never is.

As of version 0.7-2, the formal arguments of all user-facing lavaan functions therefore follow the snake_case convention: std_lv, group_equal, fixed_x, and so on. These are the names shown on the help pages, and they are the names used throughout this book. The change is fully backward compatible: the traditional dotted names are silently translated to their snake_case equivalents on entry, and will remain supported, so that existing scripts, syllabi, and textbook examples continue to run unchanged.

A related change concerns the names of lavaan’s helper functions. Many of these were shortened in version 0.7-2: the lav_partable_* family became lav_pt_*, the lav_matrix_* family became lav_mat_*, and so on. This book uses the short names. The longer names that were exported in earlier versions (such as lav_partable_npar() or lav_matrix_vech()) remain exported as aliases, so older code continues to work.

The same convention applies to the keywords that select a piece of output: the what= keywords of lavInspect() and lavTech(), the type= values of lavNames(), lavResiduals(), and standardizedSolution(), the standardized= values of parameterEstimates(), and the names of the fit measures requested from fitMeasures(). Since version 0.7-2, these keywords are matched without regard to case, and dots and underscores are interchangeable: "cov_lv", "cov.lv", and "COV_LV" select the same result. The help pages, and this book, use the snake_case spelling ("cov_lv", "ov_x", "std_all", "rmsea_ci_upper"). The dotted spelling remains accepted without a warning, and this will not change. Note that a keyword may thus be written with underscores while the returned name is spelled with dots: fitMeasures(fit, "rmsea_ci_upper") returns an element named rmsea.ci.upper, for the reason given below.

Several categories of names deliberately remain in the traditional dotted style, because for each of them a rename would break something that the silent translation cannot repair:

  • Option values: the values of options, such as se = "robust.sem", missing = "two.stage", or test = "satorra.bentler", keep their dotted spelling on the help pages and in this book. These strings name a method rather than an argument, and many existing scripts contain them as literals.

  • Names in lavaan’s output: the names of the fit measures as returned by fitMeasures() (rmsea.ci.upper), the column names of parameterEstimates() and its relatives (est.std, ci.lower, …), and the element names of the lists returned by lavInspect() (res.cov, cov.x, …) are kept, because existing scripts select results by exactly these names.

  • The stored option names: internally, and in the output of lavOptions() (see appendix F), the options are still stored under their historical dotted names, because add-on packages read them from fitted objects. Both spellings are accepted in every function call.

  • Slot names of the S4 classes: slots such as @Model@con.jac or @Model@ceq.function keep their dotted names: renaming a slot changes the class definition, which would invalidate fitted objects saved with an earlier version of lavaan and break add-on packages that read slots directly.

  • S3 class names: objects returned by lavaan carry classes such as lavaan.data.frame and lavaan.matrix.symmetric; here the dot is not merely historical, it is the separator on which S3 method dispatch (print.lavaan.data.frame()) is built.

  • Names frozen by R itself: methods for dotted generics that lavaan does not own, such as fitted.values() (whose generic lives in the stats package), must keep the name and signature of their generic.

  • Data set names: the example data sets shipped with the package (Demo.twolevel, …) keep their names; renaming them would break every script and tutorial that loads them.

1.5 A roadmap of the book

Figure 1.1 summarizes the main pipeline through which lavaan transforms a model specification and a data set into a fitted lavaan object. Almost every chapter of the book corresponds to one of the boxes, or to one of the arrows between them.

Figure 1.1: From model syntax to lavaan object: the main pipeline followed by the book.

The chapters group naturally into five parts and a set of appendices:

  • Specification (Chapters 2 to 4): how a model is described in text, and how that description is converted into a parameter table and, ultimately, into a matrix representation suitable for numerical computation.
  • Preparation (Chapters 5 and 6): how data are read in, how the sample statistics and the unrestricted (\mathsf{H}_1) model are computed, and how starting values and parameter bounds are obtained.
  • Estimation and inference (Chapters 7 and 8): the estimators available in lavaan, the optimizers used to minimize the fit function, and the methods for standard errors and test statistics.
  • Working with results (Chapters 9 to 13): the lavaan object, fit measures and residual analysis, hypothesis tests beyond the global chi-square statistic, standardized solutions and correlation structures, and factor-score prediction.
  • Advanced topics (Chapters 14 to 19): categorical data, rotation and exploratory SEM, multilevel SEM, multi-stage estimators, structural-after-measurement (SAM) estimation, and using lavaan as an engine for Monte Carlo simulation studies.
  • Appendices: a summary of the mathematical notation used throughout the book (appendix A), a collection of matrix-algebra results (appendix B), the univariate and bivariate regression models behind the analysis of categorical data (appendix C), a self-contained treatment of the multivariate normal distribution (appendix D), the derivations behind the two-level model (appendix E), a thematic reference guide to the options of the fitting functions (appendix F), and a catalog of the lavInspect() and lavTech() keywords (appendix G).

The chapters can be read in order, in which case the reader follows the pipeline of Figure 1.1 from the first line of model syntax to the final diagnostic plot. They can also be consulted individually, as reference material, when a particular option, a line of output, or an unexpected result raises a question. To support this second style of reading, chapters are largely self-contained, and forward and backward cross-references are used liberally. In the same spirit, appendix A lists every mathematical symbol used in the book together with its definition, and is intended to be consulted whenever an unfamiliar symbol appears.