Appendix C — Univariate and bivariate regression models

This appendix collects the technical details of the small regression models that lavaan fits one variable, or one pair of variables, at a time when some of the endogenous variables are ordinal: the univariate linear and ordered probit regressions of step 1 and the bivariate linear, ordinal, and mixed models of step 2 of the stage-wise estimator described in Section 14.4. For each model we give the log-likelihood, the casewise scores, the gradient, and the Hessian, and for the bivariate models also the derivatives with respect to the univariate parameters that step 3 (Section 14.4.3) needs. The last section (Section C.8) then assembles these ingredients into the categorical \boldsymbol{\Gamma} matrix, block by block, for the all-ordinal, mixed, and conditional settings alike. The formulas are written exactly as they are evaluated; the implementation notes at the end of the appendix (Section C.9) map them to the functions that compute them.

C.1 Setting and notation

Throughout, cases are indexed by i = 1, \ldots, N, and each case carries a nonnegative weight w_i (all equal to one unless sampling weights are supplied). Every log-likelihood is a weighted sum of casewise contributions, \ell = \sum_i w_i\, \ell_i, and the gradient and Hessian are the corresponding weighted sums of casewise first and second derivatives; we write N_w = \sum_i w_i for the sum of the weights. A case with a missing value contributes nothing to the sums of the models it cannot inform; this is how the pairwise-deletion option of Section 14.8 enters.

The exogenous covariates of case i are collected in the Q \times 1 vector \mathbf{x}_i; the linear model also uses the augmented vector \tilde{\mathbf{x}}_i = (1, \mathbf{x}_i^{\prime})^{\prime}, which carries the intercept. When no covariates are present, \mathbf{x}_i is empty and \tilde{\mathbf{x}}_i = 1. The symbols \phi and \Phi denote the standard normal density and distribution function, \phi_2 and \Phi_2 their bivariate standard normal counterparts with correlation \rho, and \omega = \sqrt{1 - \rho^2}.

The univariate models have several parameters and are fitted by a quasi-Newton optimizer with analytic gradient and Hessian. Each bivariate model has a single free parameter, the correlation \rho: the univariate parameters of both variables are held fixed at their step 1 estimates, which is the two-step estimator of Olsson (1979) discussed in Section 14.4.2. For the bivariate models, the derivatives with respect to the univariate parameters are nevertheless needed in step 3 of Section 14.4.3, where the uncertainty of the step 1 estimates is propagated into the asymptotic variability of the correlations: the casewise scores with respect to the univariate parameters, evaluated at the fixed values, form the crossproducts of the \mathbf{A}_{21} block, and for the pairs that involve a continuous variable the observed second derivatives \partial^2 \ell / \partial\rho\, \partial\boldsymbol{\theta} replace them. Both are given below.

C.2 The univariate linear regression model

For a continuous variable y, the model is the normal linear regression

y_i = \tilde{\mathbf{x}}_i^{\prime} \tilde{\boldsymbol{\beta}} + \epsilon_i, \qquad \epsilon_i \sim \mathcal{N}(0, \sigma^2), \tag{C.1}

with parameter vector \boldsymbol{\theta} = (\tilde{\boldsymbol{\beta}}^{\prime}, \sigma^2)^{\prime}, where \tilde{\boldsymbol{\beta}} = (\beta_0, \boldsymbol{\beta}^{\prime})^{\prime} holds the intercept and the slopes. Writing e_i = y_i - \tilde{\mathbf{x}}_i^{\prime}\tilde{\boldsymbol{\beta}} for the residual of case i, the log-likelihood is

\ell(\boldsymbol{\theta}) = \sum_{i=1}^{N} w_i \left[ -\frac{1}{2}\log(2\pi\sigma^2) - \frac{e_i^2}{2\sigma^2} \right]. \tag{C.2}

The casewise scores (the derivatives of \ell_i, before weighting) are

\frac{\partial \ell_i}{\partial \tilde{\boldsymbol{\beta}}} = \frac{e_i}{\sigma^2}\, \tilde{\mathbf{x}}_i, \qquad \frac{\partial \ell_i}{\partial \sigma^2} = -\frac{1}{2\sigma^2} + \frac{e_i^2}{2\sigma^4}, \tag{C.3}

and the gradient is the weighted sum \sum_i w_i\, \partial \ell_i / \partial \boldsymbol{\theta}. The Hessian has blocks

\frac{\partial^2 \ell}{\partial \tilde{\boldsymbol{\beta}}\, \partial \tilde{\boldsymbol{\beta}}^{\prime}} = -\frac{1}{\sigma^2} \sum_i w_i\, \tilde{\mathbf{x}}_i \tilde{\mathbf{x}}_i^{\prime}, \qquad \frac{\partial^2 \ell}{\partial \tilde{\boldsymbol{\beta}}\, \partial \sigma^2} = -\frac{1}{\sigma^4} \sum_i w_i\, e_i\, \tilde{\mathbf{x}}_i, \qquad \frac{\partial^2 \ell}{\partial (\sigma^2)^2} = \frac{N_w}{2\sigma^4} - \frac{1}{\sigma^6} \sum_i w_i\, e_i^2 . \tag{C.4}

The ML solution is available in closed form: \tilde{\boldsymbol{\beta}} is the weighted least squares estimate, and \sigma^2 = \sum_i w_i e_i^2 / N_w is the residual variance with the (weighted) number of cases, not the degrees of freedom, in the denominator. lavaan uses this closed-form solution as the starting value, so that the optimizer converges immediately; it is still called, so that all univariate models share one code path. At the solution, the off-diagonal block of Equation C.4 vanishes and \sum_i w_i e_i^2 = N_w \sigma^2, so the observed and expected information coincide:

\mathcal{I}(\boldsymbol{\theta}) = \begin{pmatrix} \sigma^{-2} \sum_i w_i\, \tilde{\mathbf{x}}_i \tilde{\mathbf{x}}_i^{\prime} & \mathbf{0} \\ \mathbf{0} & N_w / (2\sigma^4) \end{pmatrix}. \tag{C.5}

This is the information that step 3 uses for the continuous variables in the \mathbf{A}_{11} block (Section 14.4.3). It is the information of the normal model whether or not y is normally distributed, which is why it is preferred there over the crossproduct of the scores Equation C.3: for a non-normal y (a 0/1 dummy declared numeric is the extreme case), the scores for the intercept and the variance are nearly collinear, and their crossproduct is nearly singular.

C.3 The univariate ordered probit model

For an ordinal variable y with categories c = 1, \ldots, C, the model is the ordered probit regression of Section 14.4.1: a latent response y^{\star}_i = \mathbf{x}_i^{\prime} \boldsymbol{\beta} + \epsilon_i with \epsilon_i \sim \mathcal{N}(0, 1) falls between the thresholds \tau_{c-1} and \tau_c when y_i = c, with \tau_0 = -\infty and \tau_C = +\infty. There is no intercept: the thresholds take its place, and the residual variance is fixed to one. The parameter vector is \boldsymbol{\theta} = (\tau_1, \ldots, \tau_{C-1}, \boldsymbol{\beta}^{\prime})^{\prime}. With the linear predictor \eta_i = \mathbf{x}_i^{\prime}\boldsymbol{\beta} (zero without covariates), define the upper and lower bounds of the interval of case i,

u_i = \tau_{y_i} - \eta_i, \qquad l_i = \tau_{y_i - 1} - \eta_i, \tag{C.6}

so that the probability of the observed category and the log-likelihood are

\pi_i = \Phi(u_i) - \Phi(l_i), \qquad \ell(\boldsymbol{\theta}) = \sum_{i=1}^{N} w_i \log \pi_i . \tag{C.7}

To express the derivatives with respect to the thresholds compactly, let \delta^{u}_{ic} = 1 if \tau_c is the upper bound of case i (that is, y_i = c) and \delta^{l}_{ic} = 1 if it is the lower bound (y_i = c + 1), and zero otherwise, for c = 1, \ldots, C - 1; then \partial u_i / \partial \tau_c = \delta^{u}_{ic} and \partial l_i / \partial \tau_c = \delta^{l}_{ic}, while \partial u_i / \partial \boldsymbol{\beta} = \partial l_i / \partial \boldsymbol{\beta} = -\mathbf{x}_i. The casewise scores are

\frac{\partial \ell_i}{\partial \tau_c} = \frac{\delta^{u}_{ic}\, \phi(u_i) - \delta^{l}_{ic}\, \phi(l_i)}{\pi_i}, \qquad \frac{\partial \ell_i}{\partial \boldsymbol{\beta}} = -\frac{\phi(u_i) - \phi(l_i)}{\pi_i}\, \mathbf{x}_i . \tag{C.8}

For the Hessian, write \mathbf{a}_i for the (C-1) \times 1 vector with elements \delta^{u}_{ic}\, \phi(u_i) - \delta^{l}_{ic}\, \phi(l_i) (the numerators of the threshold scores), \mathbf{b}_i = (\phi(u_i) - \phi(l_i))\, \mathbf{x}_i, and \boldsymbol{\delta}^{u}_i and \boldsymbol{\delta}^{l}_i for the indicator vectors. Using \phi^{\prime}(z) = -z\,\phi(z),

\begin{aligned} \frac{\partial^2 \ell}{\partial \boldsymbol{\tau}\, \partial \boldsymbol{\tau}^{\prime}} &= \sum_i w_i \left[ \frac{\phi^{\prime}(u_i)\, \boldsymbol{\delta}^{u}_i \boldsymbol{\delta}^{u\prime}_i - \phi^{\prime}(l_i)\, \boldsymbol{\delta}^{l}_i \boldsymbol{\delta}^{l\prime}_i}{\pi_i} - \frac{\mathbf{a}_i \mathbf{a}_i^{\prime}}{\pi_i^2} \right], \\ \frac{\partial^2 \ell}{\partial \boldsymbol{\beta}\, \partial \boldsymbol{\beta}^{\prime}} &= \sum_i w_i \left[ \frac{\phi^{\prime}(u_i) - \phi^{\prime}(l_i)}{\pi_i}\, \mathbf{x}_i \mathbf{x}_i^{\prime} - \frac{\mathbf{b}_i \mathbf{b}_i^{\prime}}{\pi_i^2} \right], \\ \frac{\partial^2 \ell}{\partial \boldsymbol{\tau}\, \partial \boldsymbol{\beta}^{\prime}} &= \sum_i w_i \left[ -\frac{\phi^{\prime}(u_i)\, \boldsymbol{\delta}^{u}_i - \phi^{\prime}(l_i)\, \boldsymbol{\delta}^{l}_i}{\pi_i}\, \mathbf{x}_i^{\prime} + \frac{\mathbf{a}_i \mathbf{b}_i^{\prime}}{\pi_i^2} \right]. \end{aligned} \tag{C.9}

Without covariates, the log-likelihood depends on the data only through the category proportions, and the ML estimates are the closed-form thresholds \tau_c = \Phi^{-1}(\hat{p}_1 + \cdots + \hat{p}_c) of Section 14.4.1, with weighted proportions when weights are present. Without covariates, these values are the starting values. With covariates, the thresholds start at the quantiles of equally spaced probabilities and the slopes at zero. In both cases, the model is fitted by the optimizer. Three numerical details are worth recording. The infinite outer thresholds are implemented by adding +100 to u_i for the top category and -100 to l_i for the bottom category, so that \Phi evaluates to one or zero there and the same formulas apply to every case. When both bounds are far in the upper tail, \pi_i is computed from the upper tail probabilities \Phi(-l_i) - \Phi(-u_i) to avoid cancellation. Finally, the objective function returns +\infty when the first few thresholds are not in increasing order, which keeps the optimizer away from inadmissible regions.

For the ordinal variables, step 3 of Section 14.4.3 does not use the Hessian Equation C.9 but the crossproduct of the scores Equation C.8, relying on the information identity. Without covariates, the ordered probit model is saturated (it has one free parameter per category proportion), and the crossproduct of the scores equals the observed information exactly at the solution. With covariates, the two are no longer equal in the sample, but they estimate the same matrix if the probit model holds.

lavaan also defines a logistic variant of this model, obtained by replacing \Phi, \phi, and \phi^{\prime} by their logistic counterparts; it is not used by the estimators of chapter 14.

C.4 The bivariate standard normal distribution

The three bivariate models share a few facts about the bivariate standard normal distribution with correlation \rho. Its density is

\phi_2(u, v; \rho) = \frac{1}{2\pi\omega} \exp\left\{ -\frac{u^2 - 2\rho u v + v^2}{2\omega^2} \right\}, \qquad \omega^2 = 1 - \rho^2, \tag{C.10}

and \Phi_2(u, v; \rho) = \Pr(U \leq u, V \leq v) is its distribution function. The probability of a rectangle (l_1, u_1] \times (l_2, u_2] is the four-corner combination

\Pr(l_1 < U \leq u_1,\, l_2 < V \leq u_2) = \Phi_2(u_1, u_2) - \Phi_2(l_1, u_2) - \Phi_2(u_1, l_2) + \Phi_2(l_1, l_2), \tag{C.11}

where the dependence on \rho is suppressed. Three derivatives are needed. The derivative of the distribution function with respect to the correlation is the density,

\frac{\partial \Phi_2(u, v; \rho)}{\partial \rho} = \phi_2(u, v; \rho), \tag{C.12}

so that the derivative of a rectangle probability with respect to \rho is the four-corner combination of densities. The derivative with respect to a bound follows from the conditional distribution V \mid U = u \sim \mathcal{N}(\rho u, \omega^2):

\frac{\partial \Phi_2(u, v; \rho)}{\partial u} = \phi(u)\, \Phi\!\left( \frac{v - \rho u}{\omega} \right). \tag{C.13}

Finally, the derivative of the density with respect to \rho is the density times a factor,

\frac{\partial \phi_2(u, v; \rho)}{\partial \rho} = \phi_2(u, v; \rho)\, g(u, v; \rho), \qquad g(u, v; \rho) = \frac{u v\, \omega^2 - \rho\,(u^2 - 2\rho u v + v^2) + \rho\, \omega^2}{\omega^4}, \tag{C.14}

which is needed for the second derivative of a rectangle probability. In the same way, the second derivative of the rectangle probability with respect to \rho is the four-corner combination of \phi_2 \cdot g.

C.5 The bivariate ordinal model: polychoric correlations

For two ordinal variables y_1 and y_2 with fitted univariate models of Section C.3, the latent responses (y^{\star}_1, y^{\star}_2) are bivariate standard normal with correlation \rho, and the thresholds and slopes of both variables are held fixed. With the bounds Equation C.6 of each variable, u_{1i}, l_{1i} and u_{2i}, l_{2i}, the probability of the observed pair of categories is the rectangle probability

\pi_i(\rho) = \Phi_2(u_{1i}, u_{2i}) - \Phi_2(l_{1i}, u_{2i}) - \Phi_2(u_{1i}, l_{2i}) + \Phi_2(l_{1i}, l_{2i}), \tag{C.15}

and the log-likelihood is \ell(\rho) = \sum_i w_i \log \pi_i(\rho). Without covariates, the bounds are the same for all cases in the same cell of the two-way table, and the log-likelihood reduces to \sum_{a}\sum_{b} n_{ab} \log \pi_{ab}(\rho) with the (weighted) cell frequencies n_{ab} and the cell probabilities Equation 14.5 of Section 14.4.2; lavaan then evaluates \Phi_2 once per threshold crossing rather than once per case. With covariates, the bounds are case-specific, and the casewise form is used.

By Equation C.12, the derivative of \pi_i with respect to \rho is the four-corner combination of densities,

\pi_i^{\prime} = \phi_2(u_{1i}, u_{2i}) - \phi_2(l_{1i}, u_{2i}) - \phi_2(u_{1i}, l_{2i}) + \phi_2(l_{1i}, l_{2i}), \tag{C.16}

and by Equation C.14 the second derivative \pi_i^{\prime\prime} is the same combination of \phi_2 \cdot g. The score, gradient, and Hessian are then

\frac{\partial \ell_i}{\partial \rho} = \frac{\pi_i^{\prime}}{\pi_i}, \qquad \frac{\partial \ell}{\partial \rho} = \sum_i w_i \frac{\pi_i^{\prime}}{\pi_i}, \qquad \frac{\partial^2 \ell}{\partial \rho^2} = \sum_i w_i \left[ \frac{\pi_i^{\prime\prime}}{\pi_i} - \frac{(\pi_i^{\prime})^2}{\pi_i^2} \right]. \tag{C.17}

Step 3 also needs the casewise scores with respect to the univariate parameters of both variables, evaluated at their fixed values. By Equation C.13, the derivative of \pi_i with respect to the upper bound u_{1i} is

\frac{\partial \pi_i}{\partial u_{1i}} = \phi(u_{1i}) \left[ \Phi\!\left( \frac{u_{2i} - \rho u_{1i}}{\omega} \right) - \Phi\!\left( \frac{l_{2i} - \rho u_{1i}}{\omega} \right) \right], \tag{C.18}

and the derivative with respect to the lower bound l_{1i} has the opposite sign,

\frac{\partial \pi_i}{\partial l_{1i}} = -\phi(l_{1i}) \left[ \Phi\!\left( \frac{u_{2i} - \rho l_{1i}}{\omega} \right) - \Phi\!\left( \frac{l_{2i} - \rho l_{1i}}{\omega} \right) \right] \tag{C.19}

(and likewise, with the roles of the variables exchanged, for the bounds of y_2). Since the bounds depend on the thresholds and slopes of y_1 exactly as in Section C.3, the chain rule gives

\frac{\partial \ell_i}{\partial \tau_{1c}} = \frac{1}{\pi_i} \left[ \delta^{u}_{1ic}\, \frac{\partial \pi_i}{\partial u_{1i}} + \delta^{l}_{1ic}\, \frac{\partial \pi_i}{\partial l_{1i}} \right], \qquad \frac{\partial \ell_i}{\partial \boldsymbol{\beta}_1} = -\frac{1}{\pi_i} \left[ \frac{\partial \pi_i}{\partial u_{1i}} + \frac{\partial \pi_i}{\partial l_{1i}} \right] \mathbf{x}_i , \tag{C.20}

and likewise for y_2. For polychoric pairs, these scores enter the \mathbf{A}_{21} block of Section 14.4.3 through their crossproducts with the \rho-scores; no second derivatives are needed.

The correlation is estimated by the optimizer within the bounds -0.999 < \rho < 0.999, starting from the Pearson correlation of the integer codes; if the first attempt fails, a second attempt starts from \rho = 0 without derivatives. The special cases of 2 \times 2 tables (two empty cells on a diagonal, a zero odds ratio, two zero thresholds) and the treatment of empty cells are described in Section 14.4.2.

C.6 The bivariate linear model: Pearson correlations

For two continuous variables with fitted univariate models of Section C.2, the residuals e_{1i} = y_{1i} - \tilde{\mathbf{x}}_i^{\prime}\tilde{\boldsymbol{\beta}}_1 and e_{2i} are bivariate normal with zero means, variances \sigma_1^2 and \sigma_2^2, and correlation \rho; the regression coefficients and variances are held fixed. With the standardized crossproduct and the quadratic form

t_{1i} = \frac{e_{1i}\, e_{2i}}{\sigma_1 \sigma_2}, \qquad t_{2i} = \frac{e_{1i}^2}{\sigma_1^2} - 2\rho\, t_{1i} + \frac{e_{2i}^2}{\sigma_2^2}, \tag{C.21}

the casewise log-likelihood is

\ell_i(\rho) = -\log(2\pi \sigma_1 \sigma_2) - \frac{1}{2}\log \omega^2 - \frac{t_{2i}}{2\omega^2}. \tag{C.22}

Differentiating with respect to \rho (note that t_{2i} depends on \rho, with \partial t_{2i} / \partial \rho = -2 t_{1i}) gives the score and, after a second differentiation, the Hessian contribution:

\frac{\partial \ell_i}{\partial \rho} = \frac{\rho + t_{1i}}{\omega^2} - \frac{\rho\, t_{2i}}{\omega^4}, \qquad \frac{\partial^2 \ell_i}{\partial \rho^2} = \frac{(1 + \rho^2) + 4\rho\, t_{1i}}{\omega^4} - \frac{(1 + 3\rho^2)\, t_{2i}}{\omega^6}. \tag{C.23}

With complete data, the solution is the sample correlation of the two residual vectors (with the same weights), and no optimization is performed. With missing values, the two-step ML solution differs from the pairwise sample correlation, because the univariate parameters are those of the (larger) univariate samples, and the correlation is then found by the optimizer.

The casewise scores with respect to the univariate parameters follow from \partial \ell_i / \partial e_{1i} = -(e_{1i}/\sigma_1^2 - \rho\, e_{2i}/(\sigma_1\sigma_2))/\omega^2 and \partial e_{1i} / \partial \tilde{\boldsymbol{\beta}}_1 = -\tilde{\mathbf{x}}_i:

\frac{\partial \ell_i}{\partial \tilde{\boldsymbol{\beta}}_1} = \frac{1}{\omega^2} \left( \frac{e_{1i}}{\sigma_1^2} - \frac{\rho\, e_{2i}}{\sigma_1 \sigma_2} \right) \tilde{\mathbf{x}}_i, \qquad \frac{\partial \ell_i}{\partial \sigma_1^2} = -\frac{1}{2\sigma_1^2} + \frac{1}{2\omega^2} \left( \frac{e_{1i}^2}{\sigma_1^4} - \frac{\rho\, e_{1i} e_{2i}}{\sigma_1^3 \sigma_2} \right), \tag{C.24}

and symmetrically for y_2. For the pairs that involve a continuous variable, step 3 replaces the crossproducts of these scores with the \rho-scores by the observed second derivatives \partial^2 \ell / \partial\rho\, \partial\boldsymbol{\theta}_1, which are obtained by differentiating the \rho-score s_i = \partial \ell_i / \partial \rho in Equation C.23 through e_{1i} and \sigma_1^2:

\frac{\partial s_i}{\partial e_{1i}} = \frac{1}{\omega^2} \left[ \frac{e_{2i}}{\sigma_1 \sigma_2} - \frac{\rho}{\omega^2} \left( \frac{2 e_{1i}}{\sigma_1^2} - \frac{2\rho\, e_{2i}}{\sigma_1 \sigma_2} \right) \right], \qquad \frac{\partial s_i}{\partial \sigma_1^2} = \frac{1}{\omega^2} \left[ -\frac{t_{1i}}{2\sigma_1^2} - \frac{\rho}{\omega^2} \left( -\frac{e_{1i}^2}{\sigma_1^4} + \frac{\rho\, t_{1i}}{\sigma_1^2} \right) \right], \tag{C.25}

so that \partial^2 \ell / \partial\rho\, \partial\tilde{\boldsymbol{\beta}}_1 = -\sum_i w_i\, (\partial s_i / \partial e_{1i})\, \tilde{\mathbf{x}}_i and \partial^2 \ell / \partial\rho\, \partial\sigma_1^2 = \sum_i w_i\, \partial s_i / \partial \sigma_1^2.

C.7 The bivariate mixed model: polyserial correlations

For a continuous variable y_1 with fitted linear model of Section C.2 and an ordinal variable y_2 with fitted ordered probit model of Section C.3, the model is the one of Equation 14.6 in Section 14.4.2: y_1 and the latent response y^{\star}_2 are bivariate normal with correlation \rho. Let z_i = (y_{1i} - \tilde{\mathbf{x}}_i^{\prime}\tilde{\boldsymbol{\beta}}_1)/\sigma_1 be the standardized residual of y_1, and let u_{2i} and l_{2i} be the bounds Equation C.6 of y_2. Conditionally on y_1, y^{\star}_2 is normal with mean \rho z_i and variance \omega^2, so with the conditional bounds

u^{\star}_i = \frac{u_{2i} - \rho z_i}{\omega}, \qquad l^{\star}_i = \frac{l_{2i} - \rho z_i}{\omega}, \qquad p_i = \Phi(u^{\star}_i) - \Phi(l^{\star}_i), \tag{C.26}

the casewise log-likelihood is the sum of the marginal normal log-density of y_1 and the conditional log-probability of the observed category of y_2:

\ell_i(\rho) = \log \phi(z_i) - \log \sigma_1 + \log p_i . \tag{C.27}

Only p_i depends on \rho. Using \partial u^{\star}_i / \partial \rho = (\rho u_{2i} - z_i)/\omega^3 and the same for l^{\star}_i, and writing d^{u}_i = \rho u_{2i} - z_i and d^{l}_i = \rho l_{2i} - z_i, the score is

\frac{\partial \ell_i}{\partial \rho} = \frac{g_i}{p_i\, \omega^3}, \qquad g_i = \phi(u^{\star}_i)\, d^{u}_i - \phi(l^{\star}_i)\, d^{l}_i, \tag{C.28}

and, since \partial p_i / \partial \rho = g_i / \omega^3, the Hessian contribution is

\frac{\partial^2 \ell_i}{\partial \rho^2} = \frac{1}{p_i\, \omega^3} \left[ \phi(u^{\star}_i) \left( u_{2i} - \frac{u^{\star}_i (d^{u}_i)^2}{\omega^3} \right) - \phi(l^{\star}_i) \left( l_{2i} - \frac{l^{\star}_i (d^{l}_i)^2}{\omega^3} \right) \right] + \frac{3\rho\, g_i}{p_i\, \omega^5} - \frac{g_i^2}{p_i^2\, \omega^6}. \tag{C.29}

The casewise scores with respect to the univariate parameters go through z_i for the continuous variable (with \partial z_i / \partial \tilde{\boldsymbol{\beta}}_1 = -\tilde{\mathbf{x}}_i / \sigma_1 and \partial z_i / \partial \sigma_1^2 = -z_i / (2\sigma_1^2)) and through the bounds for the ordinal variable:

\begin{aligned} \frac{\partial \ell_i}{\partial \tilde{\boldsymbol{\beta}}_1} &= \frac{1}{\sigma_1} \left[ z_i + \frac{\rho}{\omega}\, \frac{\phi(u^{\star}_i) - \phi(l^{\star}_i)}{p_i} \right] \tilde{\mathbf{x}}_i, \\ \frac{\partial \ell_i}{\partial \sigma_1^2} &= \frac{1}{2\sigma_1^2} \left[ (z_i^2 - 1) + \frac{\rho z_i}{\omega}\, \frac{\phi(u^{\star}_i) - \phi(l^{\star}_i)}{p_i} \right], \\ \frac{\partial \ell_i}{\partial \tau_{2c}} &= \frac{\delta^{u}_{2ic}\, \phi(u^{\star}_i) - \delta^{l}_{2ic}\, \phi(l^{\star}_i)} {\omega\, p_i}, \qquad \frac{\partial \ell_i}{\partial \boldsymbol{\beta}_2} = -\frac{\phi(u^{\star}_i) - \phi(l^{\star}_i)}{\omega\, p_i}\, \mathbf{x}_i . \end{aligned} \tag{C.30}

As for the Pearson pairs, step 3 uses observed second derivatives for the polyserial pairs. With s_i = g_i / (p_i \omega^3) the \rho-score of Equation C.28, the derivative with respect to any univariate quantity v \in \{z_i, u_{2i}, l_{2i}\} is

\frac{\partial s_i}{\partial v} = \frac{(\partial g_i / \partial v)\, p_i - g_i\, (\partial p_i / \partial v)} {p_i^2\, \omega^3}, \tag{C.31}

with

\begin{aligned} \frac{\partial g_i}{\partial z_i} &= \frac{\rho}{\omega} \left( u^{\star}_i \phi(u^{\star}_i)\, d^{u}_i - l^{\star}_i \phi(l^{\star}_i)\, d^{l}_i \right) - \left( \phi(u^{\star}_i) - \phi(l^{\star}_i) \right), &\frac{\partial p_i}{\partial z_i} &= -\frac{\rho}{\omega} \left( \phi(u^{\star}_i) - \phi(l^{\star}_i) \right), \\ \frac{\partial g_i}{\partial u_{2i}} &= -\frac{u^{\star}_i \phi(u^{\star}_i)\, d^{u}_i}{\omega} + \rho\, \phi(u^{\star}_i), &\frac{\partial p_i}{\partial u_{2i}} &= \frac{\phi(u^{\star}_i)}{\omega}, \\ \frac{\partial g_i}{\partial l_{2i}} &= \frac{l^{\star}_i \phi(l^{\star}_i)\, d^{l}_i}{\omega} - \rho\, \phi(l^{\star}_i), &\frac{\partial p_i}{\partial l_{2i}} &= -\frac{\phi(l^{\star}_i)}{\omega}, \end{aligned} \tag{C.32}

after which the chain rule maps z_i to \tilde{\boldsymbol{\beta}}_1 and \sigma_1^2 as above, and u_{2i} and l_{2i} to the thresholds (through the indicators \delta^{u}_{2ic} and \delta^{l}_{2ic}) and to the slopes \boldsymbol{\beta}_2 (through -\mathbf{x}_i for both bounds).

The starting value is the moment estimator of Olsson et al. (1982), \rho_0 = r_{12}\, s_2 / \sum_c \phi(\tau_{2c}), with r_{12} the Pearson correlation between y_1 (its standardized residual, with covariates) and the integer codes of y_2 and s_2 the standard deviation of those codes; \rho_0 is halved when it exceeds 0.9 in absolute value. The correlation is estimated within -0.995 < \rho < 0.995 with the analytic gradient; if the optimizer fails, lavaan retries without derivatives, then from \rho = 0, and finally falls back to a one-dimensional golden-section search over the interval.

C.8 The categorical \boldsymbol{\Gamma} matrix

This section gives the complete recipe for the step 3 sandwich of Section 14.4.3, the categorical \hat{\boldsymbol{\Gamma}} of Equation 14.7, in a form that covers the all-ordinal setting, the mixed setting of Section 14.4.4, and the conditional setting of Section 14.4.5 at once. Everything below is evaluated at the step 1 and step 2 estimates. The sampling weights w_i of Section C.1 are written explicitly; without sampling weights, they are all equal to one.

C.8.1 The parameter vector and the casewise scores

Let there be P endogenous variables, P_o of them ordinal and P_c continuous, and Q exogenous covariates (Q = 0 without covariates). The vector of sample statistics \mathbf{s}, and with it every matrix of this section, is laid out in four blocks, in this order:

  1. thresholds and means: for each variable p = 1, \ldots, P in turn, its C_p - 1 thresholds if it is ordinal, or one slot holding its intercept \beta_{p0} (its mean, without covariates) if it is continuous; T = \sum_{p\, \mathrm{ordinal}} (C_p - 1) + P_c slots in all;
  2. slopes, if Q > 0: the P \times Q matrix of regression coefficients of the P variables on the Q covariates, stored column by column, so that the slopes of all variables on the first covariate come first; PQ slots;
  3. residual variances of the continuous variables, in the order of the variables; P_c slots;
  4. the P^{\star}_{\rho} = P(P-1)/2 correlations \rho_{pp^{\prime}}, p > p^{\prime}, in the column-major order of the strict lower triangle: (2,1), (3,1), \ldots, (P,1), (3,2), \ldots.

The first three blocks are the univariate parameters of step 1, with K = T + PQ + P_c slots; the fourth block holds the correlations of step 2. The model-implied vector \boldsymbol{\sigma}(\boldsymbol{\vartheta}) of Section 14.5.1 uses the same layout, and the dimension of \hat{\boldsymbol{\Gamma}} is P^{\star} = K + P^{\star}_{\rho}. (Under the default conditional_x = TRUE, the slots of block 1 hold the residual thresholds and intercepts of the regressions on the covariates; without covariates, block 2 is empty and the thresholds and means are marginal.)

For every case i, a score vector \mathbf{g}_i of length P^{\star} is assembled in the same layout from the casewise scores of this appendix:

  • for an ordinal variable, the threshold scores and the slope scores of the ordered probit model, Equation C.8;
  • for a continuous variable, the intercept score, the slope scores, and the variance score of the linear model, Equation C.3, with the intercept score in block 1 and the variance score in block 3;
  • for each pair, the \rho-score of the bivariate model of the pair: Equation C.17 for two ordinal variables, Equation C.23 for two continuous variables, and Equation C.28 for a mixed pair.

The vector \mathbf{g}_i is the unweighted score of case i. Under missing = "pairwise", a case that is missing on a variable has a zero score for that variable’s parameters and for every correlation involving it. Stacking the weighted scores w_i\, \mathbf{g}_i^{\prime} as the rows of the N\times P^{\star} score matrix \mathbf{G}, the meat of the sandwich is

\mathbf{B} = \mathbf{G}^{\prime}\mathbf{G} = \sum_{i=1}^{N} w_i^2\, \mathbf{g}_i \mathbf{g}_i^{\prime}, \tag{C.33}

a P^{\star} \times P^{\star} matrix that is dense: the scores of different variables and pairs are correlated across cases, and these cross-correlations are what make the asymptotic variances of the correlations depend on the thresholds.

C.8.2 The bread

The bread \mathbf{A} is the P^{\star} \times P^{\star} matrix of (minus) derivatives of the stacked estimating equations with respect to the parameters. The estimating equations of step 1 (the gradients of the univariate models) do not involve the correlations, and the estimating equation of each correlation (the \rho-gradient of its bivariate model) involves only \rho itself and the univariate parameters of its two variables. \mathbf{A} is therefore block lower-triangular, with the partition K + P^{\star}_{\rho} of Equation 14.7, and its three nonzero blocks are built as follows.

The block \mathbf{A}_{11} (K \times K) is block-diagonal, with one block per variable covering that variable’s slots in blocks 1 to 3 of the layout (its thresholds or intercept, its slopes, and its variance). For an ordinal variable p, the block is the crossproduct of its own scores,

[\mathbf{A}_{11}]_{pp} = \sum_i w_i\, \mathbf{g}_{pi} \mathbf{g}_{pi}^{\prime}, \qquad \mathbf{g}_{pi} = \left( \frac{\partial \ell_{pi}}{\partial \boldsymbol{\tau}_p}, \frac{\partial \ell_{pi}}{\partial \boldsymbol{\beta}_p} \right), \tag{C.34}

which estimates its information matrix by the information identity (Section C.3); without covariates, where the ordered probit model is saturated, the two are equal. For a continuous variable p, the block is the analytic information of the normal regression, Equation C.5, in the order (intercept, slopes, variance):

[\mathbf{A}_{11}]_{pp} = \begin{pmatrix} \sigma_p^{-2} \sum_i w_i\, \tilde{\mathbf{x}}_i \tilde{\mathbf{x}}_i^{\prime} & \mathbf{0} \\ \mathbf{0} & N_w / (2\sigma_p^4) \end{pmatrix}. \tag{C.35}

This is the numeric_bread = "observed" choice of Section 14.4.3; under mimic = "Mplus", the crossproduct of the scores is used for the continuous variables as well. Note that \mathbf{A}_{11} is a sparse version of the upper-left K \times K block of \mathbf{B}: without sampling weights, the within-variable blocks coincide (for the ordinal variables exactly, for the continuous variables up to the choice just made), while the between-variable blocks of \mathbf{B} are dropped, because each univariate model is estimated on its own.

The block \mathbf{A}_{22} (P^{\star}_{\rho} \times P^{\star}_{\rho}) is diagonal, one element per pair, because every correlation is estimated separately. For a pair of ordinal variables, the element is the crossproduct of the pair’s \rho-scores,

[\mathbf{A}_{22}]_{(pp^{\prime})(pp^{\prime})} = \sum_i w_i \left( \frac{\partial \ell_{pp^{\prime}i}}{\partial \rho_{pp^{\prime}}} \right)^2, \tag{C.36}

and for a pair that involves a continuous variable it is the observed information, minus the second derivative of the bivariate log-likelihood, Equation C.23 or Equation C.29 summed over the cases:

[\mathbf{A}_{22}]_{(pp^{\prime})(pp^{\prime})} = -\sum_i w_i\, \frac{\partial^2 \ell_{pp^{\prime}i}}{\partial \rho_{pp^{\prime}}^2}. \tag{C.37}

The block \mathbf{A}_{21} (P^{\star}_{\rho} \times K) has one row per pair, which is nonzero only in the columns of the univariate parameters of the pair’s two variables (their thresholds or intercepts, their slopes, and their variances). For a pair of ordinal variables, the entries are the crossproducts of the pair’s \rho-scores with its scores with respect to the univariate parameters, Equation C.20:

[\mathbf{A}_{21}]_{(pp^{\prime}),\, \theta} = \sum_i w_i\, \frac{\partial \ell_{pp^{\prime}i}}{\partial \rho_{pp^{\prime}}}\, \frac{\partial \ell_{pp^{\prime}i}}{\partial \theta}, \qquad \theta \in \{ \boldsymbol{\tau}_p, \boldsymbol{\beta}_p, \boldsymbol{\tau}_{p^{\prime}}, \boldsymbol{\beta}_{p^{\prime}} \}. \tag{C.38}

For a pair that involves a continuous variable, the entries are the observed cross-derivatives, minus Equation C.25 or Equation C.31 mapped to the univariate parameters and summed over the cases:

[\mathbf{A}_{21}]_{(pp^{\prime}),\, \theta} = -\sum_i w_i\, \frac{\partial^2 \ell_{pp^{\prime}i}}{\partial \rho_{pp^{\prime}}\, \partial \theta}, \qquad \theta \in \{ \tilde{\boldsymbol{\beta}}_p, \sigma_p^2, \boldsymbol{\tau}_{p^{\prime}}, \boldsymbol{\beta}_{p^{\prime}} \} \text{ or } \{ \tilde{\boldsymbol{\beta}}_p, \sigma_p^2, \tilde{\boldsymbol{\beta}}_{p^{\prime}}, \sigma_{p^{\prime}}^2 \}. \tag{C.39}

The two forms agree in expectation when the continuous variable is normally distributed, and differ otherwise; the observed form is the one that remains valid.

C.8.3 The sandwich

Because \mathbf{A} is block lower-triangular and \mathbf{A}_{22} is diagonal, its inverse is available without factorizing a P^{\star} \times P^{\star} matrix:

\mathbf{A}^{-1} = \begin{pmatrix} \mathbf{A}_{11}^{-1} & \mathbf{0} \\ -\mathbf{A}_{22}^{-1} \mathbf{A}_{21} \mathbf{A}_{11}^{-1} & \mathbf{A}_{22}^{-1} \end{pmatrix}, \tag{C.40}

where \mathbf{A}_{11}^{-1} is itself block-diagonal, one small inverse per variable. (When a declared category is empty and allow_empty_cell = TRUE, the slots of its pseudo-thresholds have zero scores and zero rows in \mathbf{A}_{11}, and a generalized inverse is used.) The sandwich

\mathbf{W} = \mathbf{A}^{-1}\, \mathbf{B}\, (\mathbf{A}^{-1})^{\prime} \tag{C.41}

is the estimated variance matrix of the stage-wise estimates in the metric in which they were estimated: thresholds and intercepts, slopes, variances, and correlations. Written out, its lower-right block is

\mathbf{W}_{\rho\rho} = \mathbf{A}_{22}^{-1} \left[ \mathbf{B}_{22} - \mathbf{A}_{21} \mathbf{A}_{11}^{-1} \mathbf{B}_{12} - \mathbf{B}_{21} \mathbf{A}_{11}^{-1} \mathbf{A}_{21}^{\prime} + \mathbf{A}_{21} \mathbf{A}_{11}^{-1} \mathbf{B}_{11} \mathbf{A}_{11}^{-1} \mathbf{A}_{21}^{\prime} \right] \mathbf{A}_{22}^{-1}, \tag{C.42}

with \mathbf{B} partitioned like \mathbf{A}. The first term alone, \mathbf{A}_{22}^{-1} \mathbf{B}_{22} \mathbf{A}_{22}^{-1}, is what one would obtain by treating the thresholds as known; the other three terms are the correction for their estimation, and they vanish when \mathbf{A}_{21} = \mathbf{0}.

C.8.4 From correlations to covariances, and the sign of the means

The sample statistics vector \mathbf{s} of Section 14.5.3 differs from the stage-wise parameter vector in two respects when continuous variables are present, and \mathbf{W} is transformed accordingly.

First, the covariance matrix that the model reproduces is in the mixed metric of Section 14.4.4: for a pair of continuous variables the sample statistic is the covariance \sigma_{pp^{\prime}} = \rho_{pp^{\prime}} \sigma_p \sigma_{p^{\prime}}, for a mixed pair it is \rho_{pp^{\prime}} \sigma_p with p the continuous variable, and for a pair of ordinal variables it is the correlation itself. The delta rule maps \mathbf{W} to this metric through the P^{\star} \times P^{\star} Jacobian

\mathbf{H} = \begin{pmatrix} \mathbf{I}_K & \mathbf{0} \\ \mathbf{H}_{21} & \mathbf{H}_{22} \end{pmatrix}, \qquad \mathbf{W} \leftarrow \mathbf{H}\, \mathbf{W}\, \mathbf{H}^{\prime}, \tag{C.43}

where \mathbf{H}_{22} is diagonal and \mathbf{H}_{21} is nonzero only in the variance columns (block 3) of the continuous variables. For a pair of continuous variables,

[\mathbf{H}_{22}]_{(pp^{\prime})(pp^{\prime})} = \sigma_p \sigma_{p^{\prime}}, \qquad [\mathbf{H}_{21}]_{(pp^{\prime}),\, \sigma_p^2} = \frac{\rho_{pp^{\prime}}\, \sigma_{p^{\prime}}}{2\sigma_p}, \qquad [\mathbf{H}_{21}]_{(pp^{\prime}),\, \sigma_{p^{\prime}}^2} = \frac{\rho_{pp^{\prime}}\, \sigma_p}{2\sigma_{p^{\prime}}}, \tag{C.44}

and for a mixed pair with continuous variable p,

[\mathbf{H}_{22}]_{(pp^{\prime})(pp^{\prime})} = \sigma_p, \qquad [\mathbf{H}_{21}]_{(pp^{\prime}),\, \sigma_p^2} = \frac{\rho_{pp^{\prime}}}{2\sigma_p}; \tag{C.45}

the rows of the ordinal pairs are rows of the identity. Without continuous variables, \mathbf{H} = \mathbf{I} and this step is skipped.

Second, the mean (intercept) of a continuous variable enters \mathbf{s} with a minus sign, so that its slot follows the “threshold minus mean” form of the implied thresholds Equation 14.9. The rows and columns of \mathbf{W} that belong to the intercept slots of the continuous variables are therefore multiplied by -1 (equivalently, \mathbf{W} \leftarrow \mathbf{D}\, \mathbf{W}\, \mathbf{D} with \mathbf{D} the diagonal sign matrix). Finally,

\hat{\boldsymbol{\Gamma}} = N\, \mathbf{W}, \tag{C.46}

which is Equation 14.7; its diagonal, inverted, is the DWLS weight matrix, and its full inverse the WLS weight matrix of Section 14.5.3.

C.8.5 Weights, clusters, and missing data

With sampling weights, the scores in \mathbf{G} are weighted by w_i, and the bread blocks use the weighted sums written above (in the crossproduct forms, this is \sum_i w_i \mathbf{g}_i \mathbf{g}_i^{\prime}, with the unweighted casewise scores). Two meats are then possible. The design-based meat (sampling_weights_type = "design", the default) is \mathbf{G}^{\prime}\mathbf{G} = \sum_i w_i^2\, \mathbf{g}_i \mathbf{g}_i^{\prime}, which treats the weights as inverse selection probabilities and matches Mplus; the frequency meat ("frequency") is \sum_i w_i\, \mathbf{g}_i \mathbf{g}_i^{\prime}, which treats the weights as case multiplicities, so that \hat{\boldsymbol{\Gamma}} coincides with the one computed from the data with each row replicated w_i times. Without weights the two coincide.

With clustered data (a cluster= argument in a single-level model), the bread is unchanged, and the meat is formed from the cluster totals of the scores, \mathbf{B}^{\mathrm{clus}} = \sum_{j} \mathbf{g}_{(j)} \mathbf{g}_{(j)}^{\prime} with \mathbf{g}_{(j)} = \sum_{i \in j} \mathbf{g}_i, giving \hat{\boldsymbol{\Gamma}}^{\mathrm{clus}} with an additional finite-sample factor J/(J-1) for J clusters. This matrix replaces \hat{\boldsymbol{\Gamma}} in the robust standard errors and test statistics; the weight matrix for point estimation is still derived from the independent cases version.

Under missing = "pairwise", the univariate and bivariate models are fitted to the cases available for each variable and each pair, and the casewise scores of the cases that do not inform a parameter are set to zero before \mathbf{B} and the bread blocks are formed; the factor N in Equation C.46 is the total number of cases.

C.9 Implementation notes

This section collects details that are tied to the current implementation of lavaan (the R package, version 0.7-4) rather than to the concepts of the main text. Internal file and function names may change or move at any release, while everything reachable through the user interface stays put.

Table C.1 maps the models of this appendix to the functions that compute them. The five modules share one design, defined in lav_uvbv_common.R: each model keeps its data, its fixed quantities, and the current parameter vector in a cache environment; the compute functions (log-likelihood, casewise scores, gradient, Hessian) evaluate in that environment and store their intermediate results there, so that the gradient reuses what the log-likelihood computed and the Hessian reuses what the gradient computed; and the wrappers handed to the optimizer maintain this chain. The optimizer is R’s nlminb(), which minimizes -\ell / N_w, with the gradient and Hessian scaled accordingly. The univariate models and the polychoric model use the analytic gradient and Hessian; the Pearson and polyserial models use the gradient only (optim_method = "nlminb1"), the Hessian of Equation C.23 and Equation C.29 being available but not needed for a one-parameter problem.

Table C.1: Functions behind the models of this appendix. Each lav_uv*/lav_bv* file has a two-level counterpart (suffix _2l) for the multilevel categorical setting of Section 16.11.
Model Functions File
univariate linear regression (Section C.2): fit, log-likelihood, casewise scores, gradient, Hessian lav_uvreg_fit(), lav_uvreg_loglik(), lav_uvreg_sc(), lav_uvreg_grad(), lav_uvreg_hessian() lav_uvreg.R
univariate ordered probit (Section C.3): closed-form thresholds, fit, log-likelihood, casewise scores, gradient, Hessian lav_uvord_th(), lav_uvord_fit(), lav_uvord_loglik(), lav_uvord_sc(), lav_uvord_grad_cache(), lav_uvord_hessian(); the bounds Equation C.6 are z1/z2 in the code and are computed by lav_uvord_z12() lav_uvord.R
bivariate standard normal (Section C.4): density, the factor g of Equation C.14, rectangle probabilities lav_dbinorm(), lav_dbinorm_guv(), pbinorm() (a wrapper around pbivnorm::pbivnorm()) lav_bvreg.R
polychoric correlation (Section C.5): two-step fit, cell probabilities, four-corner sums, casewise scores (including Equation C.20) lav_bvord_cor_twostep_fit(), lav_bvord_noexo_pi(), lav_bvord_corner_sum(), lav_bvord_cor_sc() lav_bvord.R
Pearson correlation (Section C.6): two-step fit, casewise scores (Equation C.24), observed cross-derivatives (Equation C.25) lav_bvreg_cor_twostep_fit(), lav_bvreg_cor_sc(), lav_bvreg_cor_hessian_uni_cache() lav_bvreg.R
polyserial correlation (Section C.7): two-step fit, casewise scores (Equation C.30), observed cross-derivatives (Equation C.31) lav_bvmix_cor_twostep_fit(), lav_bvmix_cor_sc(), lav_bvmix_cor_hessian_uni_cache() lav_bvmix.R
shared cache and optimizer wrappers lav_uvbv_wt_check(), lav_uvbv_optim_fns(), lav_uvbv_min_fns() lav_uvbv_common.R
the categorical \boldsymbol{\Gamma} (Section C.8): driver, layout, the three bread blocks, the pair scores and observed derivatives, the block inverse Equation C.40; the delta-rule matrix \mathbf{H} and the sign flip are applied in the driver muthen1984(), lav_m84_layout(), lav_m84_a11() (with lav_m84_a11_numeric()), lav_m84_a21(), lav_m84_a22(), lav_m84_pair_sc(), lav_m84_pair_obs_deriv(), lav_m84_b_inv() lav_muthen1984.R

In muthen1984(), the meat Equation C.33 is called INNER, the sandwich Equation C.41 (after the delta rule and the sign flip) is returned as WLS.W, and the multiplication by N of Equation C.46 happens when the result is stored as the NACOV slot of the sample statistics object (with a cluster-aggregated WLS.W.cluster next to it when a cluster= argument is present).

The fit objects returned by the univariate functions are plain lists that carry the parameter estimates (theta), index vectors into them (th_idx, slope_idx, beta_idx, var_idx), the data, and the fitted bounds or fitted values at the solution; the bivariate functions take two such fits as input. The step 1 and step 2 loops of Section 14.4.1 and Section 14.4.2 (lav_samp_step1() and lav_samp_step2() in lav_samplestats_step1.R and lav_samplestats_step2.R) call these functions, and muthen1984() in lav_muthen1984.R assembles the scores and derivatives into the blocks of Equation 14.7.

The formulas can be checked against the internal functions. For the polychoric model of Section C.5, the analytic gradient Equation C.17 at the solution is zero, and the casewise \rho-scores sum to the same value:

HSord <- HolzingerSwineford1939
for (v in c("x1", "x2")) {
  HSord[[v]] <- cut(HolzingerSwineford1939[[v]],
                    breaks = quantile(HolzingerSwineford1939[[v]],
                                      probs = seq(0, 1, 0.25)),
                    include.lowest = TRUE, labels = FALSE)
}
f1 <- lavaan:::lav_uvord_fit(y = HSord$x1)
f2 <- lavaan:::lav_uvord_fit(y = HSord$x2)
rho <- lavaan:::lav_bvord_cor_twostep_fit(fit_y1 = f1, fit_y2 = f2)
sc <- lavaan:::lav_bvord_cor_sc(y1 = HSord$x1, y2 = HSord$x2, rho = rho,
                                fit_y1 = f1, fit_y2 = f2)
c(rho = rho, sum_score_rho = sum(sc$dx_rho))
          rho sum_score_rho 
 3.067054e-01 -4.157652e-09 

The threshold scores of Equation C.20, in contrast, do not sum to zero at the two-step solution: the thresholds maximize the univariate likelihood of Section C.3, not the bivariate one, so the bivariate log-likelihood still has a nonzero slope in their direction. This is the quantity that the \mathbf{A}_{21} block of Section 14.4.3 captures when it propagates the sampling variability of the thresholds into that of the polychoric correlation:

round(colSums(sc$dx_th_y1), 4)
[1]  1.0107 -2.5676  0.6269

The recipe of Section C.8 can be followed by hand with the same internal functions. For a one-factor model with two ordinal and one continuous indicator and one covariate, the layout of Section C.8.1 has K = 11 univariate slots (six thresholds, one intercept, three slopes, one variance) and three correlations. Building \mathbf{G}, \mathbf{A}_{11}, \mathbf{A}_{21}, \mathbf{A}_{22}, \mathbf{H}, and the sign flip exactly as described reproduces lavInspect(fit, "gamma"):

d <- HolzingerSwineford1939[, c("x1", "x2", "x4", "ageyr")]
for (v in c("x1", "x2")) {
  d[[v]] <- cut(d[[v]], breaks = quantile(d[[v]], seq(0, 1, 0.25)),
                include.lowest = TRUE, labels = FALSE)
}
fit <- sem('f =~ x1 + x2 + x4
            f ~ ageyr', data = d, ordered = c("x1", "x2"))
N <- nrow(d); X <- as.matrix(d["ageyr"]); X1 <- cbind(1, X)
# step 1: univariate fits and casewise scores
f1 <- lavaan:::lav_uvord_fit(y = d$x1, x = X)
f2 <- lavaan:::lav_uvord_fit(y = d$x2, x = X)
f4 <- lavaan:::lav_uvreg_fit(y = d$x4, x = X)
sc1 <- lavaan:::lav_uvord_sc(y = d$x1, x = X)   # thresholds, slope
sc2 <- lavaan:::lav_uvord_sc(y = d$x2, x = X)
sc4 <- lavaan:::lav_uvreg_sc(y = d$x4, x = X)   # intercept, slope, variance
# step 2: correlations and their casewise scores
r21 <- lavaan:::lav_bvord_cor_twostep_fit(fit_y1 = f1, fit_y2 = f2)
r41 <- lavaan:::lav_bvmix_cor_twostep_fit(fit_y1 = f4, fit_y2 = f1)
r42 <- lavaan:::lav_bvmix_cor_twostep_fit(fit_y1 = f4, fit_y2 = f2)
p21 <- lavaan:::lav_bvord_cor_sc(y1 = d$x1, y2 = d$x2, rho = r21,
                                 fit_y1 = f1, fit_y2 = f2)
p41 <- lavaan:::lav_bvmix_cor_sc(y1 = d$x4, y2 = d$x1, rho = r41,
                                 fit_y1 = f4, fit_y2 = f1)
p42 <- lavaan:::lav_bvmix_cor_sc(y1 = d$x4, y2 = d$x2, rho = r42,
                                 fit_y1 = f4, fit_y2 = f2)
# layout: th x1 (1:3), th x2 (4:6), mean x4 (7), slopes (8:10),
#         var x4 (11), correlations (12:14)
G <- cbind(sc1[, 1:3], sc2[, 1:3], sc4[, 1], sc1[, 4], sc2[, 4],
           sc4[, 2], sc4[, 3], p21$dx_rho, p41$dx_rho, p42$dx_rho)
B <- crossprod(G)
# A11: crossproducts for the ordinal, analytic information for x4
i1 <- c(1:3, 8); i2 <- c(4:6, 9); i4 <- c(7, 10, 11)
A11 <- matrix(0, 11, 11)
A11[i1, i1] <- B[i1, i1]; A11[i2, i2] <- B[i2, i2]
s2 <- f4$theta[f4$var_idx]
A11[i4, i4] <- rbind(cbind(crossprod(X1) / s2, 0), c(0, 0, N / (2 * s2^2)))
# A21 and A22: crossproducts for the polychoric pair (x2, x1)
A21 <- matrix(0, 3, 11); A22 <- matrix(0, 3, 3)
A21[1, 1:3] <- crossprod(p21$dx_rho, p21$dx_th_y1)
A21[1, 4:6] <- crossprod(p21$dx_rho, p21$dx_th_y2)
A21[1, 8]   <- crossprod(p21$dx_rho, p21$dx_sl_y1)
A21[1, 9]   <- crossprod(p21$dx_rho, p21$dx_sl_y2)
A22[1, 1]   <- sum(p21$dx_rho^2)
# ... and observed second derivatives for the polyserial pairs
obs <- function(y2, rho, fit_y2) {
  cache <- lavaan:::lav_bvmix_cache_from_args(y1 = d$x4, y2 = y2,
             rho = rho, fit_y1 = f4, fit_y2 = fit_y2, scores = TRUE)
  lavaan:::lav_bvmix_logl_cache(cache); lavaan:::lav_bvmix_grad_cache(cache)
  list(d_rho = -drop(lavaan:::lav_bvmix_hessian_cache(cache)),
       uni   = lavaan:::lav_bvmix_cor_hessian_uni_cache(cache))
}
o <- obs(d$x1, r41, f1)
A22[2, 2] <- o$d_rho; A21[2, 1:3] <- o$uni$th_y2; A21[2, 8] <- o$uni$sl_y2
A21[2, c(7, 10, 11)] <- c(o$uni$beta_y1, o$uni$evar_y1)
o <- obs(d$x2, r42, f2)
A22[3, 3] <- o$d_rho; A21[3, 4:6] <- o$uni$th_y2; A21[3, 9] <- o$uni$sl_y2
A21[3, c(7, 10, 11)] <- c(o$uni$beta_y1, o$uni$evar_y1)
# block inverse and sandwich
A11i <- solve(A11); A22i <- diag(1 / diag(A22))
Ainv <- rbind(cbind(A11i, matrix(0, 11, 3)),
              cbind(-A22i %*% A21 %*% A11i, A22i))
W <- Ainv %*% B %*% t(Ainv)
# delta rule (two mixed pairs), sign of the mean slot, times N
H <- diag(14); sd4 <- sqrt(s2)
H[13, 11] <- r41 / (2 * sd4); H[13, 13] <- sd4
H[14, 11] <- r42 / (2 * sd4); H[14, 14] <- sd4
W <- H %*% W %*% t(H)
W[7, ] <- -W[7, ]; W[, 7] <- -W[, 7]
max(abs(N * W - lavInspect(fit, "gamma")))
[1] 2.620126e-14