Tutorials › Causal Inference › Robust and Clustered Inference for Weighted MSMs

Marginal Structural Models · Tutorial 746 of 972

Robust and Clustered Inference for Weighted MSMs

You will construct robust and cluster-robust standard errors for weighted MSM estimates and identify what these variance estimates do—and do not—protect against.

Advanced 12 min read

What You'll Learn

  • Write weighted estimating equations and define cluster-level score contributions.
  • Construct sandwich variance estimates with consistent score and sensitivity-matrix scaling.
  • Calculate a sandwich standard error for a weighted mean.
  • Compare row-level and cluster-level variance estimates for repeated observations.
  • Distinguish robust variance estimation from causal identification and weight-model uncertainty.

Why Weighted MSMs Need Careful Inference

A weighted marginal structural model (MSM) estimates a marginal outcome relationship by solving weighted estimating equations. The weights can create a pseudo-population in which treatment assignment is less confounded, subject to the causal assumptions and weight models used. But the weights do not make repeated records from the same person independent, nor do they guarantee that a conventional model-based standard error describes the estimator's variability.

This tutorial focuses on the variance of a fitted weighted MSM. The central decision is to define the independent sampling unit correctly, then aggregate the estimating-equation contributions over that unit before constructing the variance estimate. If outcomes are measured repeatedly on each person, the person is typically the independent unit. If treatment is assigned to clinics, schools, or other groups, the assignment cluster—not each individual record—is the relevant unit for inference about that assignment.

Definition: A sandwich variance estimator estimates the variability of an estimating-equation estimator using (1) a sensitivity matrix describing how the estimating equation changes with the parameter and (2) a variability estimate for the independent-unit score contributions. A cluster-robust sandwich uses the sum of all row-level scores within each independent cluster as one contribution to the variability estimate.

Weighted Estimating Equations and the Sandwich

Let \(g=1,\ldots,G\) index independent clusters, and let \(r\) index the rows contributed by cluster \(g\). A row might represent a person at a visit, or a person-period record in a pooled model. Write \(\psi_{gr}(\beta)\) for the row's estimating-equation contribution at parameter vector \(\beta\), including its analysis weight. For example, a weighted regression score has the form \(w_{gr}x_{gr}(Y_{gr}-x_{gr}^{T}\beta)\), where \(w_{gr}\) is the weight and \(x_{gr}\) is the design vector.

The fitted estimate \(\hat{\beta}\) solves the total estimating equation. Define the cluster-level score \(U_g(\beta)\) as the sum of the row contributions within cluster \(g\). The sum of the cluster scores is therefore the same estimating equation as the sum over rows, but its contributions preserve within-cluster dependence.

$$ U_g(\beta)=\sum_r \psi_{gr}(\beta), \qquad \sum_{g=1}^{G} U_g(\hat{\beta})=0. $$

For the total-score convention used here, define the sensitivity matrix \(A\) as the negative derivative of the total score, evaluated at the estimate. The empirical cluster meat \(B\) is the sum of the outer products of the fitted cluster scores. The uncorrected cluster-robust sandwich estimate is \(A^{-1}BA^{-T}\), where \(A^{-T}\) is the transpose of \(A^{-1}\). For a scalar parameter, the transpose notation has no effect.

$$ \hat{A} = -\sum_{g=1}^{G} \left. \frac{\partial U_g(\beta)}{\partial \beta^{T}} \right|_{\beta=\hat{\beta}}, \qquad \hat{B} = \sum_{g=1}^{G} U_g(\hat{\beta})U_g(\hat{\beta})^{T}, \qquad \widehat{\operatorname{Var}}(\hat{\beta}) = \hat{A}^{-1}\hat{B}\hat{A}^{-T}. $$

The same scaling must be used throughout. These expressions use total scores: both \(\hat{A}\) and \(\hat{B}\) are sums across clusters. Equivalently, one can formulate the estimator using average scores, but then the corresponding factors of \(G\) must be included consistently. Mixing an average-scale sensitivity matrix with a total-scale meat gives an incorrect variance.

Why the Sandwich Has This Form

A short first-order argument explains the two pieces of the formula. Suppose the estimating equation is correctly centered at a target parameter \(\beta_0\), the clusters are independent, and the total sensitivity is nonsingular. A first-order expansion of the score around \(\beta_0\) gives an approximation to the estimation error. Taking its covariance yields the sandwich form.

$$ 0 \ \approx\ \sum_g U_g(\beta_0) - A(\hat{\beta}-\beta_0), \qquad \hat{\beta}-\beta_0 \ \approx\ A^{-1}\sum_g U_g(\beta_0). $$

Because independent cluster scores have zero covariance across different clusters, the covariance of their sum is the sum of their covariances. If each cluster score has covariance \(B_0\), then the covariance of the total score is \(G B_0\)—not \(B_0\). More generally, it is \(\sum_g \operatorname{Var}\{U_g(\beta_0)\}\). Substituting this total-score covariance into the preceding approximation gives the population sandwich; the empirical sum of score outer products estimates its middle component.

$$ \operatorname{Var}\!\left(\sum_g U_g(\beta_0)\right) = \sum_g \operatorname{Var}\{U_g(\beta_0)\}, \qquad \operatorname{Var}(\hat{\beta}) \ \approx\ A^{-1} \left\{\sum_g \operatorname{Var}\{U_g(\beta_0)\}\right\} A^{-T}. $$

This argument relies on independent clusters and regularity conditions for the estimating-equation approximation. In practice, the cluster sandwich is an asymptotic method: its reliability generally improves as the number of independent clusters grows. A large number of person-period rows does not substitute for a small number of independent assignment clusters.

Worked Example: A Sandwich Standard Error for a Weighted Mean

Suppose four hypothetical independent people have outcomes \(2,4,5,8\) outcome units and fixed analysis weights \(1,2,1,2\). Fit a weighted intercept-only model, so the estimated intercept is the weighted mean. For this calculation, each person is one independent unit.

The estimating equation is \(\sum_i w_i(Y_i-\beta)=0\). Thus \(\hat{\beta}=\sum_iw_iY_i/\sum_iw_i\). Substituting the data gives \((1\cdot2+2\cdot4+1\cdot5+2\cdot8)/(1+2+1+2)=31/6=5.167\) outcome units.

For this scalar equation, the sensitivity is \(\hat{A}=\sum_iw_i=6\). The fitted scores are \(w_i(Y_i-\hat{\beta})\): \(-19/6,-14/6,-1/6,34/6\). They sum to zero, as required by the fitted estimating equation. The meat and sandwich variance are \[ \hat{B} = \frac{361+196+1+1156}{36} = \frac{1714}{36} = 47.611, \qquad \widehat{\operatorname{Var}}(\hat{\beta}) = \frac{47.611}{6^2} = 1.3225. \]

The robust standard error is \(\sqrt{1.3225}=1.150\) outcome units. It estimates uncertainty using the observed weighted score contributions, rather than requiring the weighted outcomes to follow a particular variance model. The calculation treats the weights as fixed; uncertainty from estimating weights is a separate issue.

Repeated Rows: Sum Scores Within the Independent Unit

With repeated measurements, a row-level sandwich treats each row as if it were an independent unit. That is inappropriate when rows from the same person may be correlated. The cluster-robust construction instead adds the person's weighted score contributions first, then takes the outer product of that sum. In general, the outer product of a sum includes cross-products between visits; those terms are exactly what a row-level meat omits.

Worked Example: Repeated Outcomes Within Three People

Consider three hypothetical independent people, each measured at two visits. All weights are one, and the fitted MSM is an intercept-only model. The six outcomes, grouped by person, are \((2,4)\), \((5,7)\), and \((8,10)\) outcome units. The estimate is the overall mean, \(\hat{\beta}=36/6=6\), and the sensitivity is \(\hat{A}=6\).

The two residuals for person 1 are \(-4\) and \(-2\), so that person's cluster score is \(U_1=-6\). Person 2's residuals are \(-1\) and \(1\), giving \(U_2=0\); person 3's residuals are \(2\) and \(4\), giving \(U_3=6\). Therefore \(\hat{B}=(-6)^2+0^2+6^2=72\), and the cluster-robust variance and standard error are \[ \widehat{\operatorname{Var}}(\hat{\beta})=\frac{72}{6^2}=2, \qquad SE(\hat{\beta})=\sqrt{2}=1.414 \] outcome units.

If the six rows were incorrectly treated as independent for the meat, it would instead be \(16+4+1+1+4+16=42\), giving variance \(42/36=1.167\) and standard error \(1.080\) outcome units. In this example, the person-level scores reveal shared variation across visits, and the row-independent calculation is smaller. This numerical comparison illustrates the issue; the cluster-robust standard error is not guaranteed to be larger in every data set.

A conventional intercept-only model-based calculation that assumes independent, equal-variance rows would estimate the residual variance as \(42/(6-1)=8.4\), then report a standard error of \(\sqrt{8.4/6}=1.183\) outcome units. That assumption ignores the repeated-measure structure, so the resulting standard error need not reflect the sampling variability under within-person dependence.

Cluster-Level Treatment Assignment

The same rule applies when treatment is assigned at the cluster level. Even if each person's outcome is measured once, people in a shared clinic or community may not provide independent information about the treatment contrast. The score contributions should be aggregated over the independent assignment units. The next example shows the matrix calculation for a simple weighted MSM with a treatment indicator.

Worked Example: A Treatment Contrast With Cluster-Level Assignment

Suppose four hypothetical clinics are independent, with two clinics assigned to each treatment condition. For simplicity, each clinic contributes one outcome summary, all weights are one, and the summaries are \(4,6\) for control and \(9,11\) for treatment. Fit the linear model \(Y=\beta_0+\beta_1 A\), where \(A=1\) denotes treatment. The fitted control mean is \(5\), and the fitted treatment-control contrast is \(\hat{\beta}_1=10-5=5\) outcome units.

The design vectors are \(x=(1,0)^T\) for control and \(x=(1,1)^T\) for treatment. Residuals within each group are \(-1,1\). The total sensitivity for the linear estimating equation and the cluster-score meat are \[ \hat{A}=\sum_g x_gx_g^T= \begin{pmatrix}4&2\\2&2\end{pmatrix}, \qquad \hat{B}=\sum_g U_gU_g^T= \begin{pmatrix}4&2\\2&2\end{pmatrix}. \]

For example, the two treated-clinic scores are \((-1,-1)^T\) and \((1,1)^T\); their outer products sum to \(\begin{pmatrix}2&2\\2&2\end{pmatrix}\). The two control-clinic scores are \((-1,0)^T\) and \((1,0)^T\), contributing \(\begin{pmatrix}2&0\\0&0\end{pmatrix}\). Since \[ \hat{A}^{-1} = \begin{pmatrix}0.5&-0.5\\-0.5&1\end{pmatrix} \quad\text{and}\quad \hat{B}=\hat{A}, \] the sandwich covariance is \(\hat{A}^{-1}\), and the estimated variance of the treatment contrast is its bottom-right entry, \(1\). The corresponding cluster-robust standard error is \(1.000\) outcome unit.

This is a calculation of the sandwich, not a recommendation to trust large-sample inference with only four clinics. With so few independent clusters, the asymptotic approximation can be poor. The example also makes the independent unit explicit: when assignment occurs at the clinic level, the number of independent assignment units is four, not the number of patients who might have contributed to each clinic summary.

Model-Based and Robust Standard Errors

A model-based variance estimate uses the variance and dependence structure specified by the fitted outcome model. When that structure is correct, model-based inference can be efficient. If it assumes independent rows when observations are clustered, or otherwise gets the outcome variability wrong, its standard error can be too small or too large. The sandwich replaces the assumed score variability with an empirical estimate based on independent-unit scores, while retaining the estimating equation's sensitivity matrix.

“Robust” has a limited meaning here. The sandwich can provide variance inference that is less dependent on a fully correct outcome variance model, under its regularity conditions. It does not repair confounding, lack of positivity, a misspecified treatment or censoring weight model, or an inappropriate causal estimand. Those issues concern identification and estimation, not merely the variance calculation.

The displayed sandwich also treats the analysis weights as fixed. In an MSM, longitudinal weights are commonly estimated. The uncertainty from estimating those weights is not automatically included by a sandwich formed only from the outcome-model scores. A variance calculation that targets the full estimation procedure must account for the weight-estimation steps as well. The cluster unit must still reflect the actual independent sampling or assignment structure.

Practical Checks and Common Pitfalls

Before interpreting a reported standard error, check that the variance calculation matches the data structure and estimator:

  • Identify the independent unit. Repeated rows from one person are not separate independent units. For cluster-assigned treatment, aggregate at the assignment-cluster level.
  • Aggregate before taking outer products. Use \(U_g=\sum_r\psi_{gr}\), then \(U_gU_g^T\). Summing row-level outer products discards within-cluster cross-products.
  • Keep scaling consistent. If using total scores, use total-score sensitivity and meat. If using average scores, carry the corresponding sample-size factors through the variance.
  • Consider the number of clusters. The usual cluster sandwich is justified by many independent clusters; many rows within a few clusters do not provide the same asymptotic support.
  • Separate variance robustness from causal validity. A cluster-robust standard error does not establish that the weighting assumptions hold or that a fitted coefficient has the intended causal interpretation.

A frequent mistake is to fit a weighted model to person-period rows, request a conventional standard error, and assume that the weights have handled all dependence. The correction is to ask what units are independent under the sampling and assignment design, form each such unit's total weighted score, and construct the sandwich from those scores. Weighting addresses the target pseudo-population; clustering addresses dependence in the estimator's contributions. They solve different problems.

Key takeaway: For weighted MSM inference, define the weighted score at the row level, sum it within each independent unit, and use the resulting cluster scores in the sandwich meat. The result can protect against certain variance-model errors, but it cannot compensate for invalid causal assumptions, a wrong clustering level, or ignored uncertainty from estimated weights.

Check Your Understanding

Answer these questions by identifying the estimating contributions and independent units, not just by naming a variance formula.

  1. In a repeated-measures MSM with five visits per person, what is the cluster-level score used in the sandwich meat?
  2. If there are \(G\) independent clusters and each cluster score has covariance \(B_0\), what is the covariance of their total score, assuming the scores are independent?
  3. Why can summing row-level score outer products underestimate variability when rows within a person are correlated?
  4. What does a cluster-robust standard error protect against, and name one important source of uncertainty it does not automatically include when weights are estimated.
  5. If treatment is assigned to 12 clinics and each clinic contributes 100 patient records, how many independent assignment clusters are there for cluster-level inference?
Clinical Biostats