# Blog

18 November 2020 | by Tim Bock

# How to Fit a Structural Equation Model in Q

In this post I am going to walk through the steps of fitting a structural equation model (SEM) in Q. The post assumes that you already know what a SEM is and how to interpret it.

## Case study

In the post I am going to analyze Bollen’s famous Political Democracy data set (Kenneth Bollen (1989), Structural Equations with Latent Variables, Wiley.)

## Step 1: Load the data

Typically data sets are loaded into Q from raw data files. But, in this case we will load some data that is stored in an R package.

• File > Data Sets > Add to Project > From R
• Name: BollenPoliticalDemocracy
• Paste in the code below into the R CODE box
• Press the blue play button (the triangle at the top of the code)
• Choose the Automatically detect data file structure option and click OK.

## Step 2: Fit the model

The hard step is fitting the model, as this requires you to specify the measurement model, the relationships to be tested (i.e., the regressions), and the correlation structure of the model. For more information about this, please check out the lavaan website.

To do this:

• Create > R Output
• Paste in the code below
• Press Calculate

## Step 3: Review the path diagram

In order to check that the model has been correctly specified it’s a good idea to review the path diagram.

• Create > R Output
• Paste in the code below
• Press Calculate

## Step 4: Extract the summary statistics

• Create > R Output
• Paste in the code below
• Press Calculate
• In the Object inspector, on the right of your screen, click Inputs > APPEARANCE and set the font to Courier New, so that the text aligns neatly.

#### Author: Tim Bock

Tim Bock is the founder of Displayr. Tim is a data scientist, who has consulted, published academic papers, and won awards, for problems/techniques as diverse as neural networks, mixture models, data fusion, market segmentation, IPO pricing, small sample research, and data visualization. He has conducted data science projects for numerous companies, including Pfizer, Coca Cola, ACNielsen, KFC, Weight Watchers, Unilever, and Nestle. He is also the founder of Q www.qresearchsoftware.com, a data science product designed for survey research, which is used by all the world’s seven largest market research consultancies. He studied econometrics, maths, and marketing, and has a University Medal and PhD from the University of New South Wales (Australia’s leading research university), where he was an adjunct member of staff for 15 years.