SEM by Hand

This chapter derives the covariance matrix that a path model, a CFA, and a full SEM imply, then estimates the models step by step in R and Python and checks the results against lavaan. It is an optional Under the hood chapter: the SEM and CFA chapters only use its results, such as \(\operatorname{cov}(x_i, x_j) = \lambda_i \lambda_j \psi\) in How CFA Estimates the Model.

The derivations use the LISREL notation. φ is the variance of an exogenous variable; in the CFA, that is the factor variance, which the parameter table calls ψ. ψ is the variance of a disturbance. In the full SEM, every factor is an η, so the variance of the exogenous factor is a ψ too, as in lavaan. c is the direct effect of X on Y, called c’ in Mediation Analysis and cp in the code.

Setup

Goal. Write the covariance matrix of the observed variables in terms of the parameters, so the parameters can be estimated from the data’s \(\mathbf S\). The CFA follows the same steps as the path model.

Known. The observed variables (the data), from which \(\mathbf S\) is computed, and the model structure (which arrows exist).

Preconditions.

  • All variables are centered, so every mean is 0. Then a covariance is the average of a product, \(\text{Cov}(U,V)=E[UV]\), and for a vector \(\text{Cov}(\mathbf v)=E[\mathbf v\mathbf v^{\top}]\).
  • X is uncorrelated with the disturbances, and the disturbances with each other. In the CFA, the factor is uncorrelated with the errors, and the errors with each other.

Path Analysis

Path diagram of the mediation model. Boxes are observed variables, circles are unobserved; loops are variances.
Symbol Meaning
\(\boldsymbol\eta\) (eta) endogenous variables, here \(M\) and \(Y\)
\(\mathbf x\) exogenous observed variable, here \(X\)
\(\boldsymbol\zeta\) (zeta) disturbances \(\zeta_M,\zeta_Y\): the part of each endogenous variable not explained by its predictors
\(\mathbf B\) coefficients of endogenous on endogenous (here \(b\))
\(\boldsymbol\Gamma\) (Gamma) coefficients of exogenous on endogenous (here \(a\), \(c\))
\(\phi\) (phi) variance of \(X\)
\(\psi\) (psi) covariance matrix of \(\boldsymbol\zeta\) (residual variances \(\psi_M,\psi_Y\) on the diagonal)

\(X\), \(M\), \(Y\) are observed. Path analysis: the covariance of all observed variables.

\[ \begin{aligned} M&=aX+0+\zeta_M\\ Y&=cX+bM+\zeta_Y \end{aligned} \]

\[ \boldsymbol\eta=\begin{bmatrix}M\\Y\end{bmatrix}\qquad\text{(endogenous / outcome variables)} \]

\[ \boldsymbol\eta=\mathbf B\boldsymbol\eta+\boldsymbol\Gamma\mathbf x+\boldsymbol\zeta, \qquad \mathbf B=\begin{bmatrix}0&0\\b&0\end{bmatrix},\quad \boldsymbol\Gamma=\begin{bmatrix}a\\c\end{bmatrix},\quad \boldsymbol\zeta=\begin{bmatrix}\zeta_M\\\zeta_Y\end{bmatrix} \]

The right side also contains \(M\) and \(Y\): \(M\) predicts \(Y\), and that endogenous-on-endogenous part is \(\mathbf B\boldsymbol\eta\). Reading row by row gives back the two equations above.

\[ \text{Cov}(\mathbf x)=\phi,\qquad\text{Cov}(\boldsymbol\zeta)=\psi,\qquad\text{Cov}(\mathbf x,\boldsymbol\zeta)=0 \]

Solve for \(\boldsymbol\eta\), so that \(M\) and \(Y\) contain only exogenous variables (their covariances are the ones we know):

\[ \begin{aligned} (\mathbf I-\mathbf B)\,\boldsymbol\eta&=\boldsymbol\Gamma\mathbf x+\boldsymbol\zeta\\ \boldsymbol\eta&=(\mathbf I-\mathbf B)^{-1}(\boldsymbol\Gamma\mathbf x+\boldsymbol\zeta)\qquad\text{let }\mathbf A=(\mathbf I-\mathbf B)^{-1}\\ &=\mathbf A(\boldsymbol\Gamma\mathbf x+\boldsymbol\zeta)=\mathbf A\boldsymbol\Gamma\mathbf x+\mathbf A\boldsymbol\zeta\\ \mathbf x&=\mathbf I\cdot\mathbf x+\mathbf 0\cdot\boldsymbol\zeta \end{aligned} \]

Stack all observed variables into \(\mathbf v\) (\(\mathbf x\) is included because \(\mathbf S\) covers \(X\), \(M\), \(Y\)):

\[ \mathbf v=\begin{bmatrix}\mathbf x\\\boldsymbol\eta\end{bmatrix}=\begin{bmatrix}\mathbf I&\mathbf 0\\\mathbf A\boldsymbol\Gamma&\mathbf A\end{bmatrix}\begin{bmatrix}\mathbf x\\\boldsymbol\zeta\end{bmatrix} \]

Let \(\mathbf T=\begin{bmatrix}\mathbf I&\mathbf 0\\\mathbf A\boldsymbol\Gamma&\mathbf A\end{bmatrix}\), \(\mathbf u=\begin{bmatrix}\mathbf x\\\boldsymbol\zeta\end{bmatrix}\). (\(\mathbf u\) holds the exogenous variables, whose covariances are known.)

\[ \mathbf u\cdot\mathbf u^{\top}=\begin{bmatrix}\mathbf x\\\boldsymbol\zeta\end{bmatrix}\cdot\begin{bmatrix}\mathbf x^{\top}&\boldsymbol\zeta^{\top}\end{bmatrix}=\begin{bmatrix}\mathbf x\mathbf x^{\top}&\mathbf x\boldsymbol\zeta^{\top}\\\boldsymbol\zeta\mathbf x^{\top}&\boldsymbol\zeta\boldsymbol\zeta^{\top}\end{bmatrix} \]

\[ \mathbf S_u=\text{Cov}(\mathbf u)=E[\mathbf u\mathbf u^{\top}]=\begin{bmatrix}\text{Cov}(\mathbf x)&0\\0&\text{Cov}(\boldsymbol\zeta)\end{bmatrix}=\begin{bmatrix}\phi&0\\0&\psi\end{bmatrix} \]

\[ \begin{aligned} \text{Cov}(\mathbf v)&=E\big[(\mathbf T\cdot\mathbf u)(\mathbf T\cdot\mathbf u)^{\top}\big]\\ &=E\big[\mathbf T(\mathbf u\mathbf u^{\top})\mathbf T^{\top}\big] && (\mathbf T\mathbf u)^{\top}=\mathbf u^{\top}\mathbf T^{\top}\\ &=\mathbf T\cdot\mathbf S_u\cdot\mathbf T^{\top} && \mathbf T\text{ is a constant here}\\ &=\begin{bmatrix}\phi&0\\\mathbf A\boldsymbol\Gamma\phi&\mathbf A\psi\end{bmatrix}\begin{bmatrix}\mathbf I&\boldsymbol\Gamma^{\top}\mathbf A^{\top}\\0&\mathbf A^{\top}\end{bmatrix}\\ &=\begin{bmatrix}\phi&\phi\boldsymbol\Gamma^{\top}\mathbf A^{\top}\\\mathbf A\boldsymbol\Gamma\phi&\mathbf A(\boldsymbol\Gamma\phi\boldsymbol\Gamma^{\top}+\psi)\mathbf A^{\top}\end{bmatrix} \end{aligned} \]

One-Factor CFA

One-factor CFA with three indicators. Drawn with three indicators; the derivation below uses \(n\).

Compute the covariance for \(\mathbf x\). Same route as the path model; here the observed variables are the indicators, and the exogenous ones are the factor and the errors.

  • \(\phi\): factor variance
  • \(\xi\): factor; \(\lambda\): factor loading
  • \(x\): observed (endogenous) variable
  • \(\theta\): variance of the measurement error
  • \(\delta\): measurement error

\[ \begin{aligned} x_1&=\lambda_1\xi+\delta_1\\ x_2&=\lambda_2\xi+\delta_2\\ &\ \,\vdots\\ x_n&=\lambda_n\xi+\delta_n \end{aligned} \qquad\longrightarrow\qquad \mathbf x=\begin{bmatrix}\lambda_1\\\lambda_2\\\vdots\\\lambda_n\end{bmatrix}\xi+\begin{bmatrix}\delta_1\\\delta_2\\\vdots\\\delta_n\end{bmatrix} \]

Let \(\boldsymbol\Lambda=\begin{bmatrix}\lambda_1\\\lambda_2\\\vdots\\\lambda_n\end{bmatrix}\), \(\boldsymbol\delta=\begin{bmatrix}\delta_1\\\delta_2\\\vdots\\\delta_n\end{bmatrix}\).

\[ \therefore\ \mathbf x=\boldsymbol\Lambda\xi+\boldsymbol\delta=\boldsymbol\Lambda\xi+\mathbf I\cdot\boldsymbol\delta=\begin{bmatrix}\boldsymbol\Lambda&\mathbf I\end{bmatrix}\begin{bmatrix}\xi\\\boldsymbol\delta\end{bmatrix} \]

Let \(\mathbf T=\begin{bmatrix}\boldsymbol\Lambda&\mathbf I\end{bmatrix}\), \(\mathbf u=\begin{bmatrix}\xi\\\boldsymbol\delta\end{bmatrix}\). Unlike the path model, nothing needs to be substituted: the right side contains only \(\xi\) and \(\delta\), both exogenous.

\[ \mathbf x=\begin{bmatrix}\lambda_1&1&&\\\lambda_2&&\ddots&\\\vdots&&&\\\lambda_n&&&1\end{bmatrix}\begin{bmatrix}\xi\\\delta_1\\\vdots\\\delta_n\end{bmatrix} \]

\[ \begin{aligned} \text{Cov}(\mathbf x)&=E\big[\mathbf T\mathbf u\cdot(\mathbf T\mathbf u)^{\top}\big]\\ &=E\big[\mathbf T\cdot(\mathbf u\cdot\mathbf u^{\top})\cdot\mathbf T^{\top}\big] \end{aligned} \]

\[ \mathbf u\cdot\mathbf u^{\top}=\begin{bmatrix}\xi\\\delta_1\\\vdots\\\delta_n\end{bmatrix}\cdot\begin{bmatrix}\xi&\delta_1&\cdots&\delta_n\end{bmatrix}=\begin{bmatrix}\xi^2&\xi\delta_1&\cdots&\xi\delta_n\\\xi\delta_1&\delta_1^2&\cdots&\delta_1\delta_n\\\vdots&&\ddots&\vdots\\\xi\delta_n&\delta_1\delta_n&\cdots&\delta_n^2\end{bmatrix} \]

Let \(\text{Cov}(\boldsymbol\delta,\xi)=0\) and \(\text{Cov}(\delta_i,\delta_j)=0\) for \(i\neq j\) (by assumption), so every off-diagonal expectation is 0; the diagonal gives the variances.

\[ \text{Cov}(\mathbf u)=E[\mathbf u\mathbf u^{\top}]=\begin{bmatrix}\phi&&&\\&\theta_1&&\\&&\ddots&\\&&&\theta_n\end{bmatrix}=\begin{bmatrix}\phi&0\\0&\boldsymbol\Theta\end{bmatrix} \]

\[ \begin{aligned} \text{Cov}(\mathbf x)&=\mathbf T\cdot\begin{bmatrix}\phi&0\\0&\boldsymbol\Theta\end{bmatrix}\cdot\mathbf T^{\top}\\ &=\begin{bmatrix}\boldsymbol\Lambda&\mathbf I\end{bmatrix}\begin{bmatrix}\phi&0\\0&\boldsymbol\Theta\end{bmatrix}\begin{bmatrix}\boldsymbol\Lambda^{\top}\\\mathbf I\end{bmatrix}\\ &=\begin{bmatrix}\boldsymbol\Lambda\phi&\boldsymbol\Theta\end{bmatrix}\begin{bmatrix}\boldsymbol\Lambda^{\top}\\\mathbf I\end{bmatrix}\\ &=\boldsymbol\Lambda\phi\boldsymbol\Lambda^{\top}+\boldsymbol\Theta \end{aligned} \]

Cell by cell: \(\text{Cov}(x_j,x_k)=\lambda_j\lambda_k\phi\) for \(j\neq k\), and \(\text{Var}(x_j)=\lambda_j^2\phi+\theta_j\).

Full SEM

The structural model of the SEM chapter: FA1 predicts FB1 (path \(a\)), and FB1 and FA1 predict FB2 (paths \(b\) and \(c\)). FA1 is put in \(\boldsymbol\eta\) as well, with no predictors, so its “disturbance” is FA1 itself. All variables are centered, so \(\text{Cov}(\mathbf v)=E[\mathbf v\mathbf v^{\top}]\).

\[ \begin{aligned} \text{FA1}&=0+0+\zeta_{\text{FA1}}\\ \text{FB1}&=a\,\text{FA1}+0+\zeta_{\text{FB1}}\\ \text{FB2}&=c\,\text{FA1}+b\,\text{FB1}+\zeta_{\text{FB2}} \end{aligned} \qquad\longrightarrow\qquad \boldsymbol\eta=\mathbf B\boldsymbol\eta+\boldsymbol\zeta,\quad \boldsymbol\eta=\begin{bmatrix}\text{FA1}\\\text{FB1}\\\text{FB2}\end{bmatrix},\quad \mathbf B=\begin{bmatrix}0&0&0\\a&0&0\\c&b&0\end{bmatrix} \]

The disturbances are uncorrelated with each other, and their variances are estimated:

\[ \text{Cov}(\boldsymbol\zeta)=\boldsymbol\Psi =\begin{bmatrix}\psi_{\text{FA1}}&0&0\\0&\psi_{\text{FB1}}&0\\0&0&\psi_{\text{FB2}}\end{bmatrix} \]

Solve for \(\boldsymbol\eta\), so that the right side contains only \(\boldsymbol\zeta\) (whose covariance is known):

\[ \begin{aligned} (\mathbf I-\mathbf B)\,\boldsymbol\eta&=\boldsymbol\zeta\\ \boldsymbol\eta&=(\mathbf I-\mathbf B)^{-1}\boldsymbol\zeta\qquad\text{let }\mathbf A=(\mathbf I-\mathbf B)^{-1}\\ &=\mathbf A\boldsymbol\zeta \end{aligned} \]

\[ \therefore\ \text{Cov}(\boldsymbol\eta)=\mathbf A\,\boldsymbol\Psi\,\mathbf A^{\top} \]

The measurement part is the same as in the one-factor CFA, with three factors instead of one and the first loading of each factor fixed at 1:

\[ \mathbf x=\boldsymbol\Lambda\boldsymbol\eta+\boldsymbol\delta,\qquad \boldsymbol\Lambda=\begin{bmatrix}\boldsymbol\lambda_{\text{FA1}}&\mathbf 0&\mathbf 0\\\mathbf 0&\boldsymbol\lambda_{\text{FB1}}&\mathbf 0\\\mathbf 0&\mathbf 0&\boldsymbol\lambda_{\text{FB2}}\end{bmatrix}\ (18\times3) \]

\[ \therefore\ \boldsymbol\Sigma(\boldsymbol\theta)=\text{Cov}(\mathbf x)=\boldsymbol\Lambda\,\text{Cov}(\boldsymbol\eta)\,\boldsymbol\Lambda^{\top}+\boldsymbol\Theta \]

Estimating a CFA by Hand

The code below estimates the one-factor model of B1 to B5 (FB1 in the SEM chapter). Choose R or Python in the tabs; all tabs on the page switch together. The first run downloads R (webR) and Python (Pyodide) with their packages, so the first results take a while to appear.

Estimating by hand needs a complete data matrix, so this chapter uses mydata_imputed.csv, the first of five datasets imputed with mice (see Example Data). This is for the demonstration only: in a real analysis, fit the model on every imputed dataset and pool the results, or use FIML (Missing Values). The estimates therefore differ slightly from those in the SEM chapter.

Sample Covariance Matrix

\[ \mathbf S=\frac1N\,\mathbf Y_c^{\top}\mathbf Y_c \]

where \(\mathbf Y_c\) is the data with each column centered. ML divides by \(N\), not \(N-1\).

Model-Implied Covariance Matrix

Each indicator is \(x_j=\lambda_j\xi+\delta_j\) with \(\lambda_1=1\) and \(\operatorname{Var}(\xi)=\phi\), so

\[ \boldsymbol\Sigma(\boldsymbol\theta)=\phi\,\boldsymbol\lambda\boldsymbol\lambda^{\top}+\boldsymbol\Theta, \qquad \sigma_{jj}=\lambda_j^2\phi+\theta_j,\quad \sigma_{ij}=\lambda_i\lambda_j\phi . \]

Here \(\sigma_{jj}\), the \(j\)-th diagonal element of \(\boldsymbol\Sigma\), is the model-implied variance of indicator \(j\): the part explained by the latent variable (\(\lambda_j^2\phi\)) plus the residual variance (\(\theta_j\)). The off-diagonal \(\sigma_{ij}\) is the covariance between indicators \(i\) and \(j\). Fixing \(\lambda_1=1\) gives \(\xi\) the unit of the first indicator.

ML Fit Function

\[ F_{ML}=\ln|\boldsymbol\Sigma|+\operatorname{tr}(\mathbf S\boldsymbol\Sigma^{-1})-\ln|\mathbf S|-p \]

It is 0 when \(\boldsymbol\Sigma=\mathbf S\) and grows as \(\boldsymbol\Sigma\) moves away from \(\mathbf S\). So minimizing \(F_{ML}\) means searching, among all matrices the model can produce, for the \(\boldsymbol\Sigma(\boldsymbol\theta)\) closest to \(\mathbf S\).

Parameter Estimation

The ML estimates are the free loadings, the factor variance and the residual variances that minimize \(F_{ML}\), found numerically:

\[ \hat{\boldsymbol\theta}=\arg\min_{\boldsymbol\theta}F_{ML}\big(\mathbf S,\boldsymbol\Sigma(\boldsymbol\theta)\big), \qquad \boldsymbol\theta=(\lambda_2,\dots,\lambda_p,\ \phi,\ \theta_1,\dots,\theta_p) \]

res$convergence is 0 when the search converged.

Chi-Square Test

\[ T=N\cdot F_{ML}(\hat{\boldsymbol\theta}),\qquad df=\frac{p(p+1)}{2}-(\text{number of free parameters}) \]

Because the data are centered, the means and the intercepts drop out of both counts. How CFA Estimates the Model counts \(p\) means and \(p\) intercepts as well, which gives the same df.

Standardized Loadings and \(R^2\)

\[ \text{standardized loading}=\frac{\lambda_j\sqrt{\phi}}{\sqrt{\hat\sigma_{jj}}},\qquad R^2=\frac{\lambda_j^2\phi}{\hat\sigma_{jj}} \]

\(\sqrt{\phi}\) is the standard deviation of the latent variable and \(\sqrt{\hat\sigma_{jj}}\) is the model-implied standard deviation of indicator \(j\).

Check with lavaan

With complete data, cfa() uses ML and divides by \(N\), like the code above, so it gives the same χ² and df. coef() lists the free parameters: the loadings of B2 to B5, the residual variances, and the factor variance.

Estimating the Structural Model by Hand

The same steps for the full SEM, with f_ml() from above.

Implied Covariance Matrix

Estimation and Chi-Square Test

Same as for the CFA, now with 39 free parameters: 15 loadings (the first loading of each factor is fixed), 18 residual variances, the paths \(a\), \(b\), \(c\), and \(\psi_{\text{FA1}}\), \(\psi_{\text{FB1}}\), \(\psi_{\text{FB2}}\), so \(df=171-39=132\).

Paths, Indirect Effect, and Standardized Estimates

\[ \text{indirect}=a\cdot b,\qquad \text{total}=c+a\cdot b,\qquad a^*=a\cdot\frac{\operatorname{sd}(\text{FA1})}{\operatorname{sd}(\text{FB1})}\ \ (\text{same for } b^*, c^*),\qquad R^2=1-\frac{\psi}{\operatorname{Var}(\eta)} \]

Check with lavaan

The χ², the paths, the indirect and total effects, their standardized values, and R² match the code above. lavaan also gives standard errors, which this chapter does not compute.

Resources & References

  • Bollen, K. A. (1989). Structural equations with latent variables. Wiley. https://doi.org/10.1002/9781118619179
  • Brown, T. A. (2015). Confirmatory factor analysis for applied research (2nd ed.). Guilford Press.
  • Kaplan, D. (2009). Structural equation modeling: Foundations and extensions (2nd ed.). SAGE. https://doi.org/10.4135/9781452226576
  • Loehlin, J. C., & Beaujean, A. A. (2017). Latent variable models: An introduction to factor, path, and structural equation analysis (5th ed.). Routledge. https://doi.org/10.4324/9781315643199