Tutorials › Generalized Linear Models › Computing the Sandwich Bread and Meat

Quasi-Likelihood Theory · Tutorial 684 of 981

Computing the Sandwich Bread and Meat

Build each sandwich component from the observation-level quasi-score, then check how the variance model and residuals affect the resulting covariance estimate.

Advanced 12 min read

What You'll Learn

  • Write the individual quasi-score contribution for a regression observation.
  • Construct the empirical meat from fitted residuals and score weights.
  • Distinguish observed sensitivity from expected, variance-weighted bread.
  • Derive when the model-based meat equals the expected bread.
  • Calculate sandwich covariance matrices in logistic and Poisson examples.
  • Identify assumptions and common errors in bread-and-meat calculations.

From Observation-Level Scores to Matrices

A sandwich covariance estimate is only as transparent as its two ingredients. The bread records how the estimating equation changes when the regression coefficients change; the meat records the variability of the observation-level estimating contributions. For independent regression observations, both can be assembled from one contribution per observation.

Consider independent responses \(Y_1,\ldots,Y_n\), with fixed covariate vectors \(x_i\in\mathbb{R}^p\). Write \(\eta_i=x_i^\mathsf{T}\beta\), \(\mu_i=E[Y_i]\), and \(\mu_i=m(\eta_i)\), where \(m\) is differentiable. Suppose the specified variance function is \(V(\mu_i)>0\), and the variance model is \(\operatorname{Var}(Y_i)=\phi V(\mu_i)\), with fixed positive dispersion \(\phi\). Define \(h_i=d\mu_i/d\eta_i\). The quasi-score contribution for observation \(i\) is

$$ u_i(\beta)=x_i\,\frac{h_i}{\phi V(\mu_i)}(Y_i-\mu_i), \qquad U(\beta)=\sum_{i=1}^n u_i(\beta). $$

The quasi-estimator \(\hat\beta\) solves \(U(\hat\beta)=0\). Use \(\hat\mu_i=m(x_i^\mathsf{T}\hat\beta)\), \(\hat h_i=m'(x_i^\mathsf{T}\hat\beta)\), and residual \(\hat r_i=Y_i-\hat\mu_i\) to evaluate the contributions at the fitted coefficients. The empirical meat is the sum of their outer products:

$$ \hat B=\sum_{i=1}^n \hat u_i\hat u_i^\mathsf{T} =\sum_{i=1}^n \left(\frac{\hat h_i}{\phi V(\hat\mu_i)}\right)^2 \hat r_i^2\,x_ix_i^\mathsf{T}, \qquad \hat u_i=x_i\frac{\hat h_i}{\phi V(\hat\mu_i)}\hat r_i. $$

Thus the meat depends on the squared residuals, as well as the fitted mean through the score weight. Independence is what permits a sum of individual score outer products: there are no cross-observation covariance terms in the variance of \(U\).

Two Bread Matrices to Distinguish

The sensitivity bread is the negative derivative of the total score. Its empirical version is \(\hat A_{\mathrm{obs}}=-\partial U(\beta)/\partial\beta^\mathsf{T}\) evaluated at \(\hat\beta\). Differentiating the observation contribution gives

$$ \hat A_{\mathrm{obs}} =\sum_{i=1}^n \left[ \frac{\hat h_i^2}{\phi V(\hat\mu_i)} -\hat r_i \left. \frac{d}{d\eta}\left\{\frac{h(\eta)}{\phi V(m(\eta))}\right\} \right|_{\eta=x_i^\mathsf{T}\hat\beta} \right]x_ix_i^\mathsf{T}. $$

The residual term appears because the score weight can change with \(\beta\). Another common bread is the expected sensitivity, with the residual term removed:

$$ \hat A_{\mathrm{exp}} =\sum_{i=1}^n\frac{\hat h_i^2}{\phi V(\hat\mu_i)}x_ix_i^\mathsf{T}. $$

This is the negative expected score derivative evaluated at the fitted means: under the specified mean model, \(E[Y_i-\mu_i]=0\), so the derivative term multiplied by the residual has expectation zero. The two breads need not be identical in a particular data set. State which is being used; the observed bread is the empirical derivative, whereas the expected bread is its variance-model-based counterpart.

Definition: For a chosen nonsingular bread estimate \(\hat A\) and empirical meat \(\hat B\), the empirical sandwich covariance estimate is \(\hat A^{-1}\hat B\hat A^{-\mathsf{T}}\). Here the bread is symmetric in the regression setting, so this is \(\hat A^{-1}\hat B\hat A^{-1}\). The expected bread \(\hat A_{\mathrm{exp}}\) is often used when the variance-weighted sensitivity is the intended bread.

Nonsingularity matters: if the fitted design and weights do not give a full-rank bread, its inverse does not exist and this sandwich formula cannot be used as written. Dispersion in these expressions is treated as fixed or plugged in; differentiating with respect to an estimated dispersion parameter would require a larger estimating system.

Why the Model-Based Meat Can Match the Bread

Result: Suppose the observations are independent, have \(E[Y_i]=\mu_i\) and \(\operatorname{Var}(Y_i)=\phi V(\mu_i)\), and the quasi-score contributions above are defined. Then \(E[U(\beta)]=0\), and \(\operatorname{Var}\{U(\beta)\}=\sum_i E[u_i(\beta)u_i(\beta)^\mathsf{T}]=A_{\mathrm{exp}}(\beta)\), where \(A_{\mathrm{exp}}(\beta)=\sum_i h_i^2\{\phi V(\mu_i)\}^{-1}x_ix_i^\mathsf{T}\).

Proof. Because \(x_i\), \(\mu_i\), and \(h_i\) are fixed at a fixed \(\beta\), the score contribution is a fixed vector multiplier of \(Y_i-\mu_i\). Its expectation is

$$ E[u_i(\beta)] =x_i\frac{h_i}{\phi V(\mu_i)}E[Y_i-\mu_i]=0. $$

Independence and zero expectations imply that the covariance of the sum is the sum of the individual covariance matrices. For each observation,

$$ \begin{aligned} E[u_i u_i^\mathsf{T}] &=x_ix_i^\mathsf{T} \left(\frac{h_i}{\phi V(\mu_i)}\right)^2 E[(Y_i-\mu_i)^2]\\ &=x_ix_i^\mathsf{T} \left(\frac{h_i}{\phi V(\mu_i)}\right)^2 \phi V(\mu_i)\\ &=\frac{h_i^2}{\phi V(\mu_i)}x_ix_i^\mathsf{T}. \end{aligned} $$

Summing these matrices proves the stated covariance identity. The empirical meat replaces the unknown score second moments with the observed score outer products; the identity does not say those observed products equal their expectations in a finite sample. \(\square\)

This calculation explains how a variance assumption enters each component. It determines the score weight, and hence the bread, through \(V(\mu_i)\) and \(\phi\). It also determines the model-based score covariance. In contrast, the empirical meat uses the observed squared residuals rather than replacing them by their variance-model expectations.

Worked Calculations

Worked Example: Bernoulli Intercept With a Logit Link

Suppose four independent Bernoulli observations are \(1,0,1,0\), with an intercept-only logistic model and \(\phi=1\). The fitted probability is \(\hat\mu=\bar Y=2/4=1/2\). For the logit link, \(h=\mu(1-\mu)\), and for Bernoulli data \(V(\mu)=\mu(1-\mu)\). Thus \(h/V=1\).

Each score contribution is \(u_i=Y_i-\hat\mu\). The residuals are \(1/2,-1/2,1/2,-1/2\), and their sum is zero, confirming the intercept score equation. The expected bread is

$$ \hat A_{\mathrm{exp}}=\sum_{i=1}^4 \hat\mu(1-\hat\mu) =4\left(\frac12\right)\left(\frac12\right)=1. $$

The logit score weight \(h/V\) is constant, so the residual derivative term in the observed bread is zero; consequently \(\hat A_{\mathrm{obs}}=1\) as well. The empirical meat is

$$ \hat B=\sum_{i=1}^4\hat r_i^2 =4\left(\frac12\right)^2=1. $$

The scalar sandwich estimate is \(1^{-1}(1)1^{-1}=1\). In this example the observed meat happens to equal the bread; that equality is not guaranteed for other data sets.

Worked Example: Quasi-Poisson Intercept With Fixed Dispersion

Take independent counts \(1,2,3,4\), an intercept-only log-link model, and fixed \(\phi=2\), with \(V(\mu)=\mu\). The fitted mean is \(\hat\mu=\bar Y=10/4=2.5\). For a log link, \(h=\mu\), so \(h/(\phi V)=1/2\). The residuals are \(-1.5,-0.5,0.5,1.5\), which sum to zero; hence the fitted intercept solves the quasi-score equation.

The expected bread is

$$ \hat A_{\mathrm{exp}}=\sum_{i=1}^4\frac{\hat\mu}{\phi} =4\frac{2.5}{2}=5. $$

For a log link with the Poisson variance function, the score multiplier \(h/(\phi V)=1/\phi\) is constant in \(\eta\). Therefore the residual derivative term is zero and \(\hat A_{\mathrm{obs}}=5\). The empirical meat uses the observed squared residuals:

$$ \hat B=\sum_{i=1}^4\frac{\hat r_i^2}{\phi^2} =\frac{2.25+0.25+0.25+2.25}{4} =\frac54. $$

The resulting scalar sandwich estimate is \((1/5)(5/4)(1/5)=1/20=0.05\). If instead the meat were replaced by its model-based expectation, it would be \(5\), giving covariance estimate \(1/5=0.2\). These different numbers illustrate the distinction between an empirical meat and its variance-model expectation; the small observed sample need not reproduce that expectation.

Worked Example: A Fitted Poisson Regression With Two Coefficients

Let \(x_i=(1,i)^\mathsf{T}\) for \(i=0,1,2\), and observe counts \(Y=(5,2,5)\). Fit a Poisson log-link regression with \(\phi=1\). The fitted means \((4,4,4)\) are compatible with the model: \(\eta_i=\log 4+0\cdot i\), so \(\hat\beta=(\log 4,0)^\mathsf{T}\). The residuals are \((1,-2,1)\). The intercept score is \(1-2+1=0\), and the slope score is \(0(1)+1(-2)+2(1)=0\), so these are indeed fitted means satisfying both score equations.

For Poisson log-link regression, \(h_i=\mu_i\) and \(V(\mu_i)=\mu_i\), giving score weight \(h_i/V(\mu_i)=1\). The expected bread, which also equals the observed bread for this link and variance function, is

$$ \begin{aligned} \hat A &=\sum_{i=0}^2 4x_ix_i^\mathsf{T}\\ &=4\left[ \begin{pmatrix}1&0\\0&0\end{pmatrix} +\begin{pmatrix}1&1\\1&1\end{pmatrix} +\begin{pmatrix}1&2\\2&4\end{pmatrix} \right] =\begin{pmatrix}12&12\\12&20\end{pmatrix}. \end{aligned} $$

The empirical meat uses the residual squares \(1,4,1\):

$$ \begin{aligned} \hat B &=\sum_{i=0}^2 \hat r_i^2x_ix_i^\mathsf{T}\\ &=\begin{pmatrix}1&0\\0&0\end{pmatrix} +4\begin{pmatrix}1&1\\1&1\end{pmatrix} +\begin{pmatrix}1&2\\2&4\end{pmatrix} =\begin{pmatrix}6&6\\6&8\end{pmatrix}. \end{aligned} $$

The off-diagonal entry is \(0+4+2=6\); each outer product contributes to both symmetric off-diagonal entries. The inverse bread is

$$ \hat A^{-1} =\frac{1}{96}\begin{pmatrix}20&-12\\-12&12\end{pmatrix} =\frac{1}{24}\begin{pmatrix}5&-3\\-3&3\end{pmatrix}. $$

Multiplication gives

$$ \begin{aligned} \hat A^{-1}\hat B\hat A^{-1} &=\frac{1}{576} \begin{pmatrix}5&-3\\-3&3\end{pmatrix} \begin{pmatrix}6&6\\6&8\end{pmatrix} \begin{pmatrix}5&-3\\-3&3\end{pmatrix}\\ &=\frac{1}{576} \begin{pmatrix}42&-18\\-18&18\end{pmatrix} =\begin{pmatrix}7/96&-1/32\\-1/32&1/32\end{pmatrix}. \end{aligned} $$

This example checks the full construction: the fitted means obey the regression structure and score equations, the meat is built from the correct outer products, and the final covariance uses the inverse bread on both sides.

Practical Checks and Common Mistakes

A useful way to avoid arithmetic and conceptual errors is to construct the matrices observation by observation. For each row, compute the fitted mean, residual, score multiplier, and outer product \(x_ix_i^\mathsf{T}\); then sum the weighted matrices. The following checks are especially valuable:

  • Check the fitted model. For a log link, fitted means must have log means linear in the covariates. Also verify that the fitted residuals satisfy the score equations to numerical tolerance.
  • Keep the score multiplier squared in the meat. The meat weight is \(\{h_i/[\phi V(\hat\mu_i)]\}^2\hat r_i^2\), not simply \(\hat r_i^2\) for every link and variance function.
  • Use an outer product. A contribution is \(\hat r_i^2x_ix_i^\mathsf{T}\) times its scalar weight. The off-diagonal entries are not generally zero, and symmetry is a useful arithmetic check.
  • Do not confuse expected and empirical meat. Replacing squared residuals by \(\phi V(\hat\mu_i)\) gives a model-based expectation, not the observed meat.
  • Identify the bread being used. The observed derivative can contain a residual-dependent term. Removing it is justified for expected sensitivity, not as an algebraic identity for every fitted data set.

The bread and meat are therefore not decorative labels for two interchangeable weighted cross-products. The bread describes score sensitivity, and the meat describes score variation. Their separate construction makes clear which parts depend on the fitted regression, which parts use residual evidence, and which parts follow from the assumed mean–variance relationship.

Check Your Understanding

Use the observation-level score and the stated variance assumption to answer each question.

  1. For an independent regression observation, write the quasi-score contribution in terms of \(x_i\), \(h_i\), \(\phi\), \(V(\mu_i)\), and \(Y_i-\mu_i\).
  2. Explain why the empirical meat contains a squared residual and why independence permits summing individual score outer products.
  3. What term distinguishes the observed bread from the expected bread in the general differentiable-link expression?
  4. For a Poisson log-link model with \(\phi=1\), simplify the score multiplier \(h_i/V(\mu_i)\). What does this imply for the empirical meat?
  5. Why does replacing the empirical meat by the expected score covariance change the calculation, even when the variance model is correctly specified?
Clinical Biostats