Intro to lavaan

Your First and Last Name required. Your Email required. Desired Euro Camp Training Course. The 2. The course starts with a concise overview of the principles and practice of structural equation modeling, followed by an introduction to the lavaan package. On the next day, we will discuss many special topics that are often needed by applied users missing data, multiple groups, non-normal data, categorical data, etc.

On the third day, we will discuss multilevel structural equation modeling, and how it can be applied to the analysis of hierarchical and longitudinal data. The course will be based on lecture-style presentations interchanged with practical sessions using the R package lavaan. His research interests include computational statistics, connectivity and causality, and structural equation modeling.

He is the author of lavaan, an R package for structural equation modeling. Register Now and Save Your Seat! Sign Up Online and Pay Later 1. Structural Equation Modeling With Lavaan. Currency Calculator. Stats Camp will move to a online interactive live streaming video format for our Albuquerque camp in June, Zoom is easy to use and provides robust interactive training features for a superior online experience. New to Zoom? Learn how it works. We are announcing this change now to allow you sufficient time to plan for the new format and continue your education without interruption.

At this time of international gravity, we at Stats Camp, wish you and your loved ones the safest of times ahead. Please do not hesitate to contact us if you have any questions or concerns.

intro to lavaan

Yes, Please Notify Me! Proceed to Course List and Registraiton.One of the most widely-used models is the confirmatory factor analysis CFA. It specifies how a set of observed variables are related to some underlying latent factor or factors.

In this post, I step through how to run a CFA in R using the lavaan package, how to interpret your output, and how to write up the results.

This tutorial uses lavaanan excellent R package for structural equation modeling. Check back at the lavaan website for updates periodically. To learn more about the data set, you can pull up its help documentation in R.

Note that because CFAs and all SEM models are based on the covariances among variances, they are susceptible to the effects of violations to the assumption of normality especially skew and outlierswhich can strongly affect covariances. Before running your model, you should examine your variables to check that there are no serious deviations from normality. The MVN package provides a handy function for plotting this as well as lots of other useful tests and plots — check out the MVN vingette for examples.

Some of these variables are note quite normal e.

Recent Posts

Including highly non-normal variables in your CFA model can wreck havoc on the estimation. The textual latent factor is measured by x4, x5, and x6.

The speed patent factor is measured by x7, x8, and x9. To estimate the model in lavaanthe easiest method is to use the cfa function. There are lots of options for controlling the way the model is interpreted, estimated, and presented, many of which are not highlighted in the documentation for either the cfa or sem functions. To see the full list, read the help documentation for lavOptions :.

The default estimator for CFA models with continuous indicators is maximum likelihood MLwhich is probably what you want. The default treatment of missing data is listwise deletion, though, which is probably not what you want. In order to come up with a unique solution, though, the estimator needs to have some scale for them.

Another option is to constrain the latent factors to have a mean of 0 and a variance of 1 i.Examples of all three models are to be presented. Almost always input a dyad dataset to an SEM program. Reading a pairwise dataset is normally highly problematic. The lavaan model converged after 63 iterations. The variance of the errors for the Women and Men are The R squared for the Women is. The partial intraclass correlation for Satisfaction controlling for the other predictor variables is equal to.

Thus, when one member of the dyad scores high low on the variable Satisfaction after controlling for the predictor variables, the other member also tends to have a high low score. The intercept the predicted score on Satisfaction when the variables of Attachment Anxiety equal zero for Women is equal to The intercept for Men is equal to The difference in intercepts is equal to The actor effect for the Women is equal to The overall standardized effect for the Women is The actor effect for the Men is equal to The overall actor effect is equal to The partner effect from Men to Women is equal to The partner effect from Women to Men is equal to The overall partner effect is equal to Next, the relative sizes of the actor and partner effects are considered.

If the standardized actor effects of both Women and Men are greater than.

intro to lavaan

This is the case for this dataset. The value of k for the Women equals 0. No further conclusions can be drawn for k, because a bootstrap analysis was not chosen to calculate the confidence intervals.

Chi square with 2 df is equal to.I wrote this brief introductory post for my friend Simon. In the specific case of mediation analysis the transition to R can be very smooth because, thanks to lavaanthe R knowledge required to use the package is minimal. Analysis of mediator effects in lavaan requires only the specification of the model, all the other processes are automated by the package.

So, after reading in the data, running the test is trivial. This time, to keep the focus on the mediation analysis I will skip reading-in the data and generate a synthetic dataset instead. This is because otherwise I would have to spend the next paragraph explaining the dataset and the variables it contains and I really want to only focus on the analysis. As shown in the lavaan website performing a mediation analysis is as simple as typing in the code below:.

For multiple mediators one simply need to extend the model recycling the code of the first mediator variable:. Note that with multiple mediators we must add the covariance of the two mediators to the model. Covariances are added using the notation below:. There are two ways to test the null hypothesis that the indirect effect are equal to each other.

The first is to specify a contrast for the two indirect effects. In the definition of the contrast the two indirect effects are subtracted. If it is significant the two indirect effects differ. The second option to determine whether the indirect effects differ is to set a constrain in the model specifying the two indirect effect to be equal.

Then, with the anova function one can compare the models and determine which one is better. Including the constrain and comparing the models is simple:. In my case the test is not significant so there is no evidence that the indirect effects are different.

For these toy models there is no further need of customizing the calls to sem. However, when performing a proper analysis one might prefer to have bootstrapped confidence intervals. Bootstrap confidence interval can be extracted with the function calls 1 summary, 2 parameterEstimates, or 3 bootstrapLavaan. NOTE that bootstrapLavaan will re-compute the bootstrap samples requiring to wait as long as it took the sem function to run if called with the bootstrap option.

Since this post is longer than I wanted it to be, I will leave as a brief introduction to mediation with lavaan. In this follow up post I describe multiple mediation with lavaan using an actual dataset.

On github is the whole code in one. R file. Here is the first post. For customization of the plots which can be created using semPlot see this post too. Like Like. Ideally I would like the independent variable to be at the top or on the leftthe mediators to be in the middle, and the dependent variable to be at the bottom or on the right.

intro to lavaan

It does not seem to be possible to do this in semPlot, but I noticed in your response to Lola M on July 6th that you had suggested doing this. I would be extremely grateful if you might be able to share some code to show how to do this.Moreover, Iacobucci accompanied the paper with a data-set and step-by-step explanation of the syntax to analyze it. This allowed the reproduction of her analysis to all the adventurous readers willing to start with structural equation modeling.

intro to lavaan

Now, however, with lavaan available, when I encountered the paper again I saw an opportunity to make her approach reproducible by a wider audience since lavaan is available to anyone and its syntax is very intuitive. NOTE: all code is stored in a script file which is available here together with the data file. The data Iacobucci used are displayed in the covariance matrix in table 2 of her paper. I copied the data into a text file, which I named iacobucci2.

I pasted the data into a text file to avoid cluttering my blog post with meaningless numbers. Before transforming the data into a covariance matrix I transposed the data from column to row. Another important bit of information is the number of respondents This must be assigned to the sample. In fact, Lavaan does not require to specify matrices and such, but only the specification of which variables load on which factors. This is as simple as writing syntax resembling equations in which a factor is composed of the sum of the variables contributing to that factor.

Below is the model specification:. Note that in the model specification above the latent variable repeaT has a capital T. This is to distinguish it form the flow-control construct repeat see herea reserved word which initiates a Repeat Loop. Iacobucci first addresses the fit of the model. Lavaan prints the indexes used by Iacobucci by pasting this syntax into R console.

Using SEM (lavaan) to Estimate the APIM

The code below may appear convoluted because of the formatting instructions. To increase readability I wrapped all the indexes into a line through the command paste and print them to screen with the print command:.

A quick comparison between the output printed in the R console and Iacobucci's output shows that the two outputs are consistent and almost identical. Factor loadings can be extracted using the parameterEstimates function.This introduction to R is very brief and only geared toward providing some basics so that one can understand and run the code associated with the content.

It is geared toward an audience that likely has no programming experience whatsoever, but may have had some exposure to traditional statistics packages. If you have some basic familiarity with R you may skip this chapter, though it might serve as a refresher for some. As mentioned previously, to begin with R for your own machine, you just need to go to the R websitedownload it for your operating system, and install.

Then go to RStudiodownload and install it. From there on you only need RStudio to use R. As soon as you install it, R is already the most powerful statistical environment within which to work.

However, its real strength comes from the community, which has added thousands of packages that provide additional or enhanced functionality. You will regularly find packages that specifically do something you want, and will need to install them in order to use them. RStudio provides a Packages tab, but it is usually just as or more efficient to use the install.

At this point there are over packages available through standard sources, and many more through unofficial ones. To start getting some ideas of what you want to use, you will want to spend time at places like CRAN Task ViewsRdocumentationor with a list like this one. The main thing to note is that if you want to use a package, you have to load it with the library function. Sometimes, you only need the package for one thing and there is no reason to keep it loaded, in which case you can use the following approach.

However, note that the increasing popularity and ease of using R means that packages can vary quite a bit in terms of quality, so you may need to try out a couple packages with seemingly similar functionality to find the best for your situation. RStudio is an integrated development environment IDE specifically geared toward R though it works for other languages too. At the very least it will make your programming far easier and more efficient, at best you can create publish-ready documents, manage projects, create interactive website regarding your research, use version control, and much more.

I have an overview here. See Emacs Speaks Statistics for an alternative. The point is, base R is not an efficient way to use R, and you have at least two very powerful options to make your coding experience easier and more efficient.The lavaan package is an excellent package for structural equation models, and the DiagrammeR package is an excellent package for producing nice looking graph diagrams.

Of course, you can use DiagrammeR to build path diagrams for your models, but it requires you to build the diagram specification manually. This package exists to streamline that process, allowing you to plot your lavaan models directly, without having to translate them into the DOT language specification that DiagrammeR uses.

The package is very straightforward to use, simply call the lavaanPlot function with your lavaan model, adding whatever graph, node and edge attributes you want as a named list graph attributes are specified as a standard default value that shows you what the other attribute lists should look like.

For your reference, the available attributes can be found here:. First fit your lavaan model. The package supports plotting lavaan regression relationships and latent variable - indicator relationships. Then using that model fit object, simply call the lavaanPlot function, specifying your desired graph parameters.

And now you can label the plot edges with the coefficient values standardized or not for significant paths you can also specify whatever significance level you want so you can plot values for whatever coefficients you want. Introduction The lavaan package is an excellent package for structural equation models, and the DiagrammeR package is an excellent package for producing nice looking graph diagrams.

Package example The package is very straightforward to use, simply call the lavaanPlot function with your lavaan model, adding whatever graph, node and edge attributes you want as a named list graph attributes are specified as a standard default value that shows you what the other attribute lists should look like.