Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

12.2 SEM

As in the CFA example, we again use the HolzingerSwineford1939 dataset, which contains mental ability test scores from seventh- and eighth-grade pupils in two schools.

import semopy

data = semopy.examples.holzinger39.get_data()
data
Loading...

In Confirmatory Factor Analysis (CFA), we specify how observed variables measure latent constructs and allow latent variables to correlate. CFA focuses on the measurement model and does not include directional (regression) relationships between latent variables.

Structural Equation Modelling (SEM) extends CFA by allowing directional relationships among latent variables. An SEM therefore consists of two components:

Whenever at least one latent variable is used to predict another latent variable, the model is considered an SEM. As an example, we now specify a model in which visual ability predicts speed ability. Text-related abilities are not included in this model.

# Specify the model
desc = '''# Measurement model
          visual =~ x1 + x2 + x3
          speed =~ x7 + x8 + x9

          # Structural model
          speed ~ visual'''

# Fit the model
model = semopy.Model(desc)
results = model.fit(data)

# Visualize the model
semopy.semplot(model, plot_covs = True, std_ests=True, filename='data/sem_plot.pdf')
Loading...

Model Estimates

estimates = model.inspect(std_est=True)
print(estimates)
      lval  op    rval  Estimate  Est. Std  Std. Err    z-value   p-value
0    speed   ~  visual  0.368338  0.460510   0.08295   4.440492  0.000009
1       x1   ~  visual  1.000000  0.666995         -          -         -
2       x2   ~  visual  0.689313  0.456019  0.123415   5.585336       0.0
3       x3   ~  visual  0.984819  0.678165  0.159891   6.159304       0.0
4       x7   ~   speed  1.000000  0.571792         -          -         -
5       x8   ~   speed  1.203822  0.740606  0.169823   7.088685       0.0
6       x9   ~   speed  1.051845  0.649322  0.147314   7.140136       0.0
7    speed  ~~   speed  0.304732  0.787930  0.071634   4.254029  0.000021
8   visual  ~~  visual  0.604528  1.000000   0.12996   4.651641  0.000003
9       x1  ~~      x1  0.754319  0.555117  0.110373   6.834275       0.0
10      x2  ~~      x2  1.094042  0.792047  0.102616  10.661487       0.0
11      x3  ~~      x3  0.688536  0.540092  0.104947   6.560781       0.0
12      x7  ~~      x7  0.796166  0.673054  0.081598   9.757123       0.0
13      x8  ~~      x8  0.461362  0.451503  0.076857    6.00289       0.0
14      x9  ~~      x9  0.586986  0.578381  0.070959   8.272212       0.0

For guidance on interpreting factor loadings, (co)variances, and residual variances, please refer to the previous chapter. Here, we focus on the newly introduced structural regression: speed ~ visual

The regression coefficient is significantly different from zero (see p-value), indicating that visual ability significantly predicts speed ability within this model.


Model Fit

stats = semopy.calc_stats(model)
print(stats.T)
                      Value
DoF            8.000000e+00
DoF Baseline   1.500000e+01
chi2           4.741342e+01
chi2 p-value   1.278751e-07
chi2 Baseline  3.417214e+02
CFI            8.793669e-01
GFI            8.612512e-01
AGFI           7.398460e-01
NFI            8.612512e-01
TLI            7.738129e-01
RMSEA          1.281494e-01
AIC            2.568496e+01
BIC            7.387739e+01
LogLik         1.575197e-01

To assess how well the model reproduces the observed data, we examine the model fit indices (see the previous chapter for details).

Taken together, these indices suggest that the model provides a poor overall fit to the data. Although the regression from visual ability to speed ability is statistically significant, the model does not adequately capture the covariance structure of the observed variables.