Tutorials › Regression Analysis (Undergraduate) › Testing Contrasts With Clustered Data

Grouped Data and Dependence Awareness · Tutorial 881 of 991

Testing Contrasts With Clustered Data

Use a cluster-robust covariance matrix to calculate, test, and interpret linear contrasts of regression coefficients.

Advanced 10 min read

What You'll Learn

  • Define a linear contrast and express its null hypothesis using contrast weights.
  • Calculate a contrast’s cluster-robust variance using the full coefficient covariance matrix.
  • Test a contrast with an appropriate cluster-based reference distribution.
  • Find and test the difference between two fitted effects.
  • Explain why ignoring covariance can give the wrong standard error.

Why Test a Contrast?

Regression questions often concern a combination of coefficients rather than any one coefficient by itself. A treatment effect might depend on a baseline measurement, for example, so the effect at a particular baseline value is a sum of a treatment coefficient and an interaction coefficient. Another question might ask whether two slopes are equal, or whether the fitted responses at two specified covariate profiles differ. Each question can be expressed as a linear contrast of regression coefficients.

When observations are clustered, the standard error for that contrast must reflect the dependence within clusters. A cluster-robust covariance matrix provides the estimated variances and covariances of the fitted coefficients. The contrast’s variance uses all of those entries, not just the individual coefficient variances. This matters especially for a difference between two fitted effects: their estimates may move together or in opposite directions across repeated samples.

Model, Contrast, and Conditions

Suppose the regression model has coefficient vector \(\beta\), and let \(\hat{\beta}\) be its fitted estimate. A contrast is a specified weighted sum of the coefficients, \(c^\mathsf{T}\beta\), where \(c\) is a fixed vector of weights. The contrast estimate is \(c^\mathsf{T}\hat{\beta}\). For example, \(c=(1,-1)^\mathsf{T}\) gives the difference between two coefficients, \(\beta_1-\beta_2\).

Definition: A linear contrast is a linear combination \(c^\mathsf{T}\beta\) of regression coefficients, with specified weights \(c\). A test of a contrast compares its estimated value \(c^\mathsf{T}\hat{\beta}\) with a hypothesized value, usually zero. The coefficient covariance matrix contains the estimated variances on its diagonal and estimated covariances off its diagonal.

For clarity, consider ordinary least squares with observations grouped into \(G\) clusters. Write \(X_g\) for the predictor matrix and \(e_g\) for the residual vector within cluster \(g\); the full predictor matrix is \(X\). The uncorrected cluster-robust covariance estimate has the sandwich form below. A small-sample correction, when used, multiplies this estimate by the chosen correction factor. In a test, use the same selected cluster-robust covariance estimate that is used for the corresponding standard errors.

$$ \widehat{V}_{\mathrm{CR}} = (X^\mathsf{T}X)^{-1} \left( \sum_{g=1}^{G} X_g^\mathsf{T} e_g e_g^\mathsf{T} X_g \right) (X^\mathsf{T}X)^{-1} $$

This estimate allows residuals within a cluster to be dependent and to have unequal variances. It relies on the regression mean model being suitable for the question, independent clusters, adequate variation to estimate the coefficients, and a sufficiently large number of independent clusters for the cluster-robust approximation to be credible. It does not make observations within a cluster independent. With few clusters, the resulting inference can be unreliable even when the number of observations is large.

Let \(\widehat{V}_{\mathrm{CR}}\) denote the estimated covariance matrix, including any selected small-sample correction. The estimated variance of a contrast follows from the covariance rule for a weighted sum:

$$ \widehat{\mathrm{Var}}(c^\mathsf{T}\hat{\beta}) = c^\mathsf{T}\widehat{V}_{\mathrm{CR}}c, \qquad SE(c^\mathsf{T}\hat{\beta}) = \sqrt{c^\mathsf{T}\widehat{V}_{\mathrm{CR}}c} $$

For a null value \(\delta_0\), the hypotheses are \(H_0:c^\mathsf{T}\beta=\delta_0\) and \(H_a:c^\mathsf{T}\beta\ne\delta_0\) for a two-sided test. The test statistic is the estimated contrast’s distance from the null value, measured in its cluster-robust standard errors. A common reference approximation is a \(t\) distribution with \(G-1\) degrees of freedom. This is an approximation, not an exact result for arbitrary clustered data; the chosen degrees of freedom and any small-sample correction should be reported.

$$ t = \frac{c^\mathsf{T}\hat{\beta}-\delta_0} {\sqrt{c^\mathsf{T}\widehat{V}_{\mathrm{CR}}c}} $$

Why Covariances Enter the Calculation

For two estimated coefficients, \(\hat{\beta}_1\) and \(\hat{\beta}_2\), the variance of their weighted sum is

$$ \widehat{\mathrm{Var}}(a\hat{\beta}_1+b\hat{\beta}_2) = a^2\widehat{\mathrm{Var}}(\hat{\beta}_1) +b^2\widehat{\mathrm{Var}}(\hat{\beta}_2) +2ab\widehat{\mathrm{Cov}}(\hat{\beta}_1,\hat{\beta}_2) $$

This identity follows by expanding the variance of the sum: the two individual variance terms remain, and the cross-product appears twice, once in each order. Thus, a covariance can increase or decrease the contrast variance. The sign of its contribution depends on both the covariance and the contrast weights.

Worked Example: Testing a Treatment Effect at a Chosen Baseline Value

Suppose a hypothetical clustered regression models an outcome in points using treatment \(A\) and baseline value \(x\): \(Y=\beta_0+\beta_1A+\beta_2(Ax)+\varepsilon\). The fitted coefficients are \(\hat{\beta}_1=0.20\) points and \(\hat{\beta}_2=0.05\) points per baseline unit. At baseline \(x=2\), the fitted treatment effect is \(\beta_1+2\beta_2\). There are \(G=10\) independent clusters, and the relevant part of the estimated cluster-robust covariance matrix is

$$ \widehat{V}_{\mathrm{CR}} = \begin{pmatrix} 0.0400 & -0.0100\\ -0.0100 & 0.0225 \end{pmatrix} $$

The contrast weights are \(c=(1,2)^\mathsf{T}\). Its estimate is \(0.20+2(0.05)=0.300\) points. Its variance is

$$ c^\mathsf{T}\widehat{V}_{\mathrm{CR}}c = (1)^2(0.0400)+(2)^2(0.0225) +2(1)(2)(-0.0100) = 0.0400+0.0900-0.0400 = 0.0900 $$

The negative covariance contributes negatively here because the contrast weights have the same sign; it reduces the variance by \(0.0400\). The standard error is \(\sqrt{0.0900}=0.300\) points. To test \(H_0:\beta_1+2\beta_2=0\), the statistic is \(t=(0.300-0)/0.300=1.00\), with \(df=G-1=9\). The two-sided \(p\)-value is approximately \(0.343\). At the 5% significance level, these data do not provide sufficient evidence that the fitted treatment effect at baseline value 2 differs from zero. This conclusion concerns that specific baseline value, not every possible treatment effect.

Testing a Difference Between Coefficients

A difference between two coefficients is a contrast with weights \(1\) and \(-1\). The covariance term is then subtracted twice when the covariance is positive. This reflects the fact that two positively related estimates tend to move together, which can make their difference more stable than either estimate alone. Conversely, a negative covariance can make their difference less stable.

Worked Example: Comparing Two Slopes

In a hypothetical regression, two predictors are measured in the same units. The fitted slope for predictor 1 is \(\hat{\beta}_1=0.50\) outcome units per predictor unit, and the fitted slope for predictor 2 is \(\hat{\beta}_2=0.30\) outcome units per predictor unit. The estimated cluster-robust variances are \(0.0400\) and \(0.0400\), and their covariance is \(0.0150\). There are \(G=12\) independent clusters. Test whether the slopes differ.

The contrast is \(\beta_1-\beta_2\), so \(c=(1,-1)^\mathsf{T}\), and the estimated difference is \(0.50-0.30=0.200\) outcome units per predictor unit. Its estimated variance is

$$ c^\mathsf{T}\widehat{V}_{\mathrm{CR}}c = (1)^2(0.0400)+(-1)^2(0.0400) +2(1)(-1)(0.0150) = 0.0400+0.0400-0.0300 = 0.0500 $$

Thus \(SE=\sqrt{0.0500}=0.2236\) outcome units per predictor unit. For \(H_0:\beta_1-\beta_2=0\), the statistic is \(t=0.200/0.2236=0.894\), with \(df=11\). The two-sided \(p\)-value is approximately \(0.390\). At the 5% level, there is not sufficient evidence that the two slopes differ. Testing each slope separately against zero would not answer this comparison question.

Differences Between Fitted Effects

A fitted effect at a specified covariate profile is itself a linear combination of regression coefficients. For two profiles, subtracting their fitted effects produces another linear contrast. More generally, if the fitted effect at profile A is \(r_A^\mathsf{T}\beta\) and at profile B is \(r_B^\mathsf{T}\beta\), their difference is \((r_A-r_B)^\mathsf{T}\beta\). Its variance is calculated using the same full covariance matrix as any other contrast. This is why a difference in fitted effects should not be calculated by combining two standard errors as if the fitted effects were independent.

Worked Example: Testing a Difference Between Two Fitted Responses

Consider a hypothetical regression for test score, measured in points, with model \(Y=\beta_0+\beta_1x+\beta_2z+\varepsilon\). Compare the fitted score at profile A, \(x=1,z=0\), with the fitted score at profile B, \(x=0,z=1\). Suppose the fitted coefficients are \(\hat{\beta}_0=60\), \(\hat{\beta}_1=0.60\), and \(\hat{\beta}_2=-0.20\). The fitted scores are \(60.6\) and \(59.8\) points, respectively, so the estimated difference A minus B is \(0.80\) points.

The coefficient weights for profile A are \((1,1,0)\), and those for B are \((1,0,1)\). Subtracting gives the contrast \(c=(0,1,-1)\), so the intercept cancels. Suppose there are \(G=15\) independent clusters and the cluster-robust covariance estimate is

$$ \widehat{V}_{\mathrm{CR}} = \begin{pmatrix} 0.250 & 0.020 & 0.010\\ 0.020 & 0.090 & 0.015\\ 0.010 & 0.015 & 0.040 \end{pmatrix} $$

Using the full contrast, the variance of the fitted-score difference is

$$ c^\mathsf{T}\widehat{V}_{\mathrm{CR}}c = 0.090+0.040-2(0.015) = 0.100 $$

The standard error is \(\sqrt{0.100}=0.3162\) points. To test \(H_0:\) the fitted-score difference is zero, the statistic is \(t=(0.80-0)/0.3162=2.53\), with \(df=14\). The two-sided \(p\)-value is approximately \(0.024\). At the 5% significance level, this provides evidence that the fitted responses differ between these two specified profiles. It does not establish a difference for every pair of profiles.

Calculation and Interpretation

A reliable analysis separates the scientific question from the arithmetic. First state the quantity of interest in context; then write it as a coefficient contrast. After confirming that the contrast is estimable, obtain its variance from the cluster-robust covariance matrix. Finally, test the specified null value using the chosen cluster-based reference approximation. The resulting \(p\)-value measures how unusual a statistic at least as far from the null as the observed statistic would be under the null and the approximation. It is not the probability that the null hypothesis is true.

1
Write the estimand.
Specify the coefficient combination that answers the question, including the profile or predictor values if the target is a fitted effect.
2
Set the contrast and null value.
Express the target as \(c^\mathsf{T}\beta\), and state \(H_0:c^\mathsf{T}\beta=\delta_0\) and the alternative.
3
Calculate its uncertainty.
Use \(c^\mathsf{T}\widehat{V}_{\mathrm{CR}}c\), including all covariance terms and the selected correction.
4
Test and report in context.
Compute the statistic, state the degrees of freedom and reference approximation, and interpret the result with units and the specified target.

The same contrast variance also supports a confidence interval: the estimate plus or minus an appropriate critical value times its cluster-robust standard error. The interval procedure and the test should use compatible degrees of freedom and covariance corrections. If a two-sided test rejects at level \(\alpha\), the corresponding \(1-\alpha\) interval excludes the null value, subject to using the same approximation.

Common Mistake: Dropping the Covariance

A frequent error is to calculate the variance of a difference as only the sum of the two coefficient variances, or to treat two fitted effects as independent. For example, in the slope comparison above, omitting covariance would give \(0.0400+0.0400=0.0800\), rather than the correct \(0.0500\). That would overstate the standard error and alter the test. With a negative covariance and same-sign contrast weights, the covariance contribution is negative and reduces the variance; the direction must be determined from the product of the weights and covariance, not guessed from the word “covariance.” Always use the full matrix calculation.

Key takeaway: A clustered-data test of a coefficient contrast uses the contrast estimate and its standard error \(\sqrt{c^\mathsf{T}\widehat{V}_{\mathrm{CR}}c}\). The off-diagonal covariances are essential, particularly when comparing coefficients or fitted effects.

Check Your Understanding

For each question, identify the contrast or explain the uncertainty calculation required.

  1. In a model with coefficients \(\beta_1\) and \(\beta_2\), what contrast tests whether the coefficients are equal?
  2. If two contrast weights have the same sign and the estimated covariance is negative, does the covariance term increase or reduce the contrast variance? Explain using \(2ab\widehat{\mathrm{Cov}}(\hat{\beta}_1,\hat{\beta}_2)\).
  3. A fitted effect at profile A is \(r_A^\mathsf{T}\beta\), and at profile B is \(r_B^\mathsf{T}\beta\). Write the contrast for A minus B.
  4. Why is it generally incorrect to calculate the variance of a difference between fitted effects by adding their variances and omitting covariance?
  5. A test uses \(G\) independent clusters and the conventional \(t\) approximation with \(G-1\) degrees of freedom. Is that an exact reference distribution for every clustered regression? Explain.