Why Compare Two Group Slopes?
When a regression model allows the relationship between a quantitative predictor and an outcome to differ by category, each category has its own fitted slope. A useful question is often not whether a particular interaction coefficient is zero, but how much the slope in one specified category differs from the slope in another. A linear contrast expresses that comparison directly and provides a standard error and confidence interval for it.
For example, a comparison between two categories that are both non-reference categories generally involves two interaction coefficients, not just one. The estimated difference is straightforward to calculate; its uncertainty also depends on how those coefficient estimates vary together. The covariance is therefore part of the calculation, not an optional detail.
Group Slopes and Their Contrast
Suppose there are \(K\) categories and a quantitative predictor \(x\). With one category selected as the reference, a model with a separate intercept and slope for each category can be written as
Here \(I_{ig}=1\) when observation \(i\) belongs to category \(g\), and \(I_{ig}=0\) otherwise. Category 1 is the reference, so its slope is \(\beta_1\). The slope for category \(g\), for \(g=2,\ldots,K\), is \(\beta_1+\gamma_g\). The intercept adjustment \(\delta_g\) affects the fitted intercept for category \(g\), not its slope.
Write the category slopes as \(\beta_1^{(s)},\ldots,\beta_K^{(s)}\), where the superscript distinguishes a slope from the regression coefficient \(\beta_1\). The difference for categories \(A\) and \(B\) is
Under reference-group coding, if \(A\) and \(B\) are both non-reference categories, their slopes are \(\beta_1+\gamma_A\) and \(\beta_1+\gamma_B\). Subtracting cancels the common reference slope:
If \(A\) is the reference category and \(B\) is not, the difference is \(\beta_1-(\beta_1+\gamma_B)=-\gamma_B\). Thus, an interaction coefficient by itself represents a difference from the reference category; a comparison between two other categories generally requires a contrast of two interaction coefficients.
Uncertainty for a Linear Contrast
Let \(\boldsymbol{\beta}\) denote the full vector of regression coefficients, and let \(\widehat{\boldsymbol{\beta}}\) be its estimate. Any slope difference can be written as \(L\boldsymbol{\beta}\) for an appropriate row vector \(L\). Its estimate is \(L\widehat{\boldsymbol{\beta}}\). Let \(\widehat{\operatorname{Var}}(\widehat{\boldsymbol{\beta}})\) be the estimated covariance matrix of the coefficient estimates. Then
For two non-reference categories, the contrast is \(\hat{\gamma}_A-\hat{\gamma}_B\), and its variance estimate is
The minus sign follows from taking the variance of a difference. In particular, the two coefficient estimates are usually not independent, so their covariance cannot generally be assumed to be zero.
With \(df=n-p\), where \(p\) is the number of estimated regression coefficients, a \(100(1-\alpha)\%\) confidence interval for the slope contrast is
where \(t^*_{df}\) is the positive \(t\)-critical value with \(df\) degrees of freedom that leaves probability \(\alpha/2\) in each tail. If the interval includes zero, the data are compatible with no slope difference at that confidence level; if it excludes zero, they are not. The interval describes uncertainty in the difference in outcome units per unit of \(x\).
Why the Covariance Appears
The variance formula can be seen directly by letting \(U=\hat{\gamma}_A\) and \(V=\hat{\gamma}_B\). The variance of a difference follows by expanding the square of its deviation from its mean:
Replacing the variances and covariance by their estimates gives the estimated variance used for the contrast. The formula shows why adding the two standard errors, or combining them as if the estimates were independent without checking, does not generally give the correct standard error.
Worked Example: Comparing a Category With the Reference Group
In a hypothetical model, \(x\) is weekly study time in hours and \(Y\) is a quiz score in points. Category R is the reference group and category S is another group. The fitted interaction coefficient for S is \(\hat{\gamma}_S=1.80\), with standard error \(0.60\). There are \(df=24\) residual degrees of freedom. Find and interpret a 95% confidence interval for the slope in S minus the slope in R.
The reference-group slope is \(\beta_1\), while the slope in S is \(\beta_1+\gamma_S\). Thus the requested contrast is \(\gamma_S\), with estimate \(1.80\) quiz points per additional study hour and standard error \(0.60\) quiz points per hour. For 95% confidence, \(t^*_{24}\approx2.064\). The interval is
The estimated study-time slope in S is between about \(0.562\) and \(3.038\) quiz points per hour greater than the slope in R, according to this interval procedure. The interval is wholly above zero, indicating a positive difference at the 5% two-sided level. This describes a difference in fitted associations; it does not establish that study time causes score changes.
Worked Example: Comparing Two Non-Reference Categories
In a hypothetical model of monthly energy use \(Y\), measured in kilowatt-hours, against average outdoor temperature \(x\), measured in degrees Celsius, categories A and B are both non-reference categories. The interaction estimates are \(\hat{\gamma}_A=1.20\) and \(\hat{\gamma}_B=-0.50\), with standard errors \(0.70\) and \(0.80\). Their estimated covariance is \(0.14\), and \(df=30\). Find a 95% confidence interval for the slope in A minus the slope in B.
The contrast estimate is
Its estimated variance and standard error are
Using \(t^*_{30}\approx2.042\), the interval is
The estimated slope in A is \(1.70\) kilowatt-hours per degree Celsius greater than in B, but the confidence interval includes zero. At the 5% two-sided level, this interval does not distinguish the slope difference from zero. The conclusion is about this particular A-versus-B comparison, not about every pair of categories.
Worked Example: Contrasting Two Slopes From Their Covariance
A hypothetical three-category model relates \(x\), time in minutes, to \(Y\), distance traveled in meters. The estimated slopes for categories A and C are \(-0.40\) and \(1.50\) meters per minute. Their estimated variances are \(0.36\) and \(0.25\), and their estimated covariance is \(-0.06\). With \(df=18\), calculate a 95% interval for the slope in C minus the slope in A.
The estimated contrast is \(1.50-(-0.40)=1.90\) meters per minute. Its estimated variance is
With \(t^*_{18}\approx2.101\), the interval is
The estimated slope in C exceeds the slope in A by \(1.90\) meters per minute. The interval is above zero, so the data provide evidence of a positive C-minus-A slope difference at the 5% two-sided level, under the stated model assumptions.
Using and Reporting a Slope Contrast
A contrast is useful because the scientific question can determine the comparison, rather than the arbitrary choice of reference category. The fitted values do not change when a different reference category or coding scheme is used, although the individual coefficient values may change. A correctly constructed contrast for the same two slopes gives the same estimated difference and uncertainty.
A clear report identifies the order of subtraction, gives the estimate and units, and summarizes its uncertainty. For example: “The estimated slope in category A was 1.70 kilowatt-hours per degree Celsius greater than in category B; the 95% confidence interval for A minus B was \((-0.183, 3.583)\) kilowatt-hours per degree Celsius.” Naming the contrast avoids ambiguity about whether a positive estimate means A’s slope is larger or B’s.
Common Mistake: Ignoring the Covariance
A tempting but generally incorrect calculation is to estimate the standard error of \(\hat{\gamma}_A-\hat{\gamma}_B\) as \(\sqrt{SE(\hat{\gamma}_A)^2+SE(\hat{\gamma}_B)^2}\) without checking whether the estimates are uncorrelated. The correct variance subtracts twice their covariance. In the energy-use example, omitting the covariance would give variance \(0.49+0.64=1.13\), rather than \(0.85\), and a different interval. The correction is to obtain the covariance from the fitted coefficient covariance matrix and use the full contrast formula.
Another mistake is to interpret \(\hat{\gamma}_A\) as the A-versus-B difference when both A and B are non-reference categories. It is the A-versus-reference difference. For A versus B, subtract the two interaction coefficients, then calculate the standard error of that difference.
Check Your Understanding
Try these questions without looking back.
- Under reference-group coding, what contrast gives the slope in category C minus the slope in category B when both are non-reference categories?
- Write the estimated variance formula for the difference between two interaction-coefficient estimates, including the covariance term.
- If a slope-difference confidence interval includes zero, what does that say about evidence for a difference at the corresponding two-sided significance level?
- Why is it not generally correct to calculate the standard error of a difference by treating the two coefficient estimates as independent?
- A report gives a slope contrast as “A minus B” with a negative estimate. Which category has the smaller estimated slope, and what units should accompany the estimate?