ANCOVA: comparing groups after lining up the starting point

We are testing a blood pressure drug. Twenty people were split at random into two groups, one given a placebo and one given the drug, and we measured blood pressure after treatment. But blood pressure after treatment is shaken more by "what was it before treatment?" than by the drug. Let's see what changes when we take that part out first and then compare. Every number on this page is made up, and the page explains a statistics idea, nothing more.

Short answer. Analysis of covariance (ANCOVA) compares groups after first taking out the part explained by another value measured alongside that shakes the result a lot (the covariate). It is ANOVA plus regression. In a picture, draw one line per group with the same slope, and read the height gap between the two lines as the treatment effect.

Group means only (ANOVA)Starting point lined up (ANCOVA)
Drug − placebo
Standard error
t (df)
p
Leftover spread (SD)

Each point is one person (green circles placebo, blue squares drug). The vertical pieces are the leftover spread. The buttons only change the picture; the table shows both methods side by side. The data were made up as "after = 20 + 0.8 × before − 8 (if drug) + noise", with a fixed-seed random number generator, then adjusted so both groups have a before-treatment mean of exactly 150 and a standard deviation of 10.

Definitions

There are a groups, group i has ni people, and person j has response (after-treatment value) yij and covariate (before-treatment value) xij. y¯i and x¯i are the means of group i, and x¯ is the covariate mean over everyone.

covariate
A continuous variable measured along with the response and put in the model to explain part of the variation in the response. It must not be affected by the treatment (a value measured before treatment, or one the treatment cannot change, such as age).
analysis of covariance model
The model below, which holds the group effects and a straight-line relation with the covariate together. μ is the overall mean, τi is the treatment effect of group i, β is the slope shared by all groups, and εij are independent errors from a normal distribution with mean 0 and variance σ2. With two groups coded 0 (placebo) and 1 (drug), it is the same model as the regression formula in section 2, with b1=τ1−τ0 and b2=β.

yij=μ+τi+β(xij−x¯)+εij

pooled within-group slope
The least-squares estimate b of β. In each group, take the sum of products and the sum of squares measured from that group's own means, add them over all groups, and divide.

b=∑i∑j(xij−x¯i)(yij−y¯i)∑i∑j(xij−x¯i)2

adjusted mean
The response mean of group i moved to the value it would have at the overall covariate mean x¯. The difference between the adjusted means of two groups is b1 in the section 2 formula.

adjusted meani=y¯i−b(x¯i−x¯)

adjusted mean1−adjusted mean0=(y¯1−y¯0)−b(x¯1−x¯0)

homogeneity of regression slopes
The ANCOVA assumption that the slope of the response on the covariate is the same in every group. If it fails, there is a "group × covariate" interaction, which you check by adding that term to the formula (section 6).

β1=β2=⋯=βa

1. What it does: ANOVA + regression

ANOVA compares group means and treats the spread within groups as noise. But sometimes a big part of that noise can actually be explained. Most of the reason blood pressure after treatment differs from person to person is that blood pressure before treatment differed. People who started high tend to stay high after treatment.

Think of ANCOVA in two steps. ① Draw a line that predicts blood pressure after treatment from blood pressure before (regression). ② Compare the groups on what that line does not explain (ANOVA). The value measured alongside is called the covariate, and the result you want to compare (blood pressure after treatment) is called the response variable.

Try this. (The lab at the top) ① At the start (compare group means only), drug − placebo = −8, standard error 4.41, t = −1.81 (df 18), p = 0.086. At the 5% level we cannot tell it from chance. ② Press "Line up the starting point". The estimate stays −8, but the standard error drops to 2.76, t = −2.9 (df 17), p = 0.010. The vertical pieces get shorter, and the standard deviation of the leftover spread falls from 9.86 to 6.17. ③ In this data both groups were given exactly the same before-treatment mean, so the two estimates match. When the starting values differ, the estimates differ too (the lab in section 5).

2. The picture and the formula: two lines with the same slope

Plot the before-treatment value (x) across and the after-treatment value (y) up, and the points climb to the upper right. Draw one line per group, but give them the same slope. Then the two lines are parallel, and the height gap is the same at every x. This height gap is the treatment effect: "for two people with the same blood pressure before treatment, the one who took the drug is this much lower". As a formula:

y=b0+b1·group+b2·x

PieceMeaningIn the lab at the top
groupA column holding 0 for placebo and 1 for drug. A column that turns group names into 0 and 1 is called a dummy variableplacebo 0, drug 1
b₀The height of the placebo line (the intercept)22.58
b₁The height gap between the two lines = the treatment effect−8
b₂The slope both lines share. How much higher after treatment for 1 higher before0.78

When the group column is 0 you get y = b₀ + b₂·x (the placebo line), and when it is 1 you get y = (b₀ + b₁) + b₂·x (the drug line). The slope b₂ is the same and only the height differs, by b₁. You fit this formula with ordinary least squares regression. With three or more groups, use "number of groups − 1" dummy columns. Drop x and it becomes the same calculation as an ANOVA that compares group means only.

3. Means with the starting point lined up

Results are often reported per group as means with the starting point lined up (adjusted means). The question is: "if everyone had started at the overall mean before-treatment value x̄, what would each group's mean have been?"

adjusted meani=y¯i−b(x¯i−x¯)

ȳᵢ is group i's mean after treatment, x̄ᵢ is that group's mean before treatment, x̄ is everyone's mean before treatment, and b is the shared slope (b₂ above). If a group started above the overall mean (x̄ᵢ > x̄), take that much off; if it started below, add it on. In the picture, it is the height you reach by sliding the group's mean point (x̄ᵢ, ȳᵢ) along its own line to x̄. The difference between the two adjusted means is exactly the height gap b₁ between the lines. Statistics software also calls them least-squares means or estimated marginal means.

4. How the formula is built

Let's build the formula in section 3 one step at a time. The picture uses data where the drug group happened to start 10 higher before treatment (the drug group from the lab at the top, moved 10 to the right, with its after values also raised by 0.8 × 10 = 8, following the rule the data were made with). Group 0 is placebo and group 1 is drug.

In the formula above, the orange background marks the piece added in this step.

Written as formulas only, the steps above look like this.

y¯1−y¯0=140−140=0

x¯1−x¯0=160−150=10

b=∑i∑j(xij−x¯i)(yij−y¯i)∑i∑j(xij−x¯i)2=1409.051800≈0.783

(y¯1−y¯0)−b(x¯1−x¯0)=0−0.783×10≈−7.83

adjusted mean0=140−0.783×(150−155)≈143.91,adjusted mean1=140−0.783×(160−155)≈136.09

adjusted mean1−adjusted mean0=136.09−143.91=−7.83

  1. Just subtract the after-treatment means: ȳ₁ − ȳ₀. 140 − 140 = 0. Looking only at this, the drug seems to have done nothing.
  2. The starting gap: x̄₁ − x̄₀. 160 − 150 = 10. The drug group started 10 higher. The after value follows the before value, so the gap in ① has part of this 10 mixed in.
  3. Multiply by the slope and subtract: ȳ₁ − ȳ₀ − b(x̄₁ − x̄₀). The slope b is how much the after value differs when the before value differs by 1. Get b as the slope pooled within the groups: for each group, take the sum of products Σ(x − x̄ᵢ)(y − ȳᵢ) and the sum of squares Σ(x − x̄ᵢ)² around that group's own means, add each over both groups, then divide: 1409.05 ÷ 1800 = 0.783. If you got the slope from all points without regard to group, the group difference (the drug effect and the starting gap) would leak into it, so we measure it only within groups. The carried-over part is 0.783 × 10 = 7.83, and taking it away gives 0 − 7.83 = −7.83.
  4. Do it per group to get the adjusted means: ȳᵢ − b(x̄ᵢ − x̄). The overall mean is x̄ = 155. Placebo 140 − 0.783 × (150 − 155) = 143.91, drug 140 − 0.783 × (160 − 155) = 136.09. Their gap, −7.83, is the same as in ③. Subtract the two means and b·x̄ cancels, leaving just the formula in ③.

Try this. ① At first the two rings sit at the same height, 140, and the "plain gap" on the right is 0. ② Press "Next" and the starting gap of 10 appears at the bottom. ③ Press once more and two parallel lines with slope 0.783 appear. ④ The last step shows the two adjusted means on the vertical line at x̄ = 155 and their gap, −7.83. The true effect put into the made-up data is −8.

5. Two gains

(a) Less leftover spread, so the same number of people finds more

In ANOVA, all the spread that comes from people having different blood pressure before treatment ends up in the noise (the spread within groups). ANCOVA clears that part away with a line, so the noise gets smaller. In the lab at the top, the standard deviation of the leftover spread went from 9.86 to 6.17, and the standard error of the difference from 4.41 to 2.76. A standard error shrinks with the square root of the number of people, so to get the same standard error by comparing group means only you would need roughly (4.41 ÷ 2.76)² ≈ 2.55 times as many people. The more strongly the covariate and the result move together within groups, the bigger the gain. If they hardly move together there is no gain, and you only lose one degree of freedom to estimating the slope.

(b) It fixes the part that comes from a chance difference at the start

Even with random assignment, the groups' before-treatment means differ a little by chance. With few people they can differ quite a lot. If the drug group happened to start high, its after-treatment mean also comes out that much higher, and the drug looks weaker. ANCOVA fixes this by subtracting that part (slope × starting gap).

Drag the slider and the whole drug group moves sideways; its after values follow by 0.8 × (the amount moved), as in the rule the data were made with. The rings are each group's (mean before, mean after), the black bar on the right is the plain gap, and the orange bar on the gray vertical line (x̄) is the gap with the starting point lined up.

Try this. ① At the start (0) it is the same data as the lab at the top, so the plain gap and the lined-up gap are both −8. ② Raise it to 10: the plain gap becomes 0 and the drug effect seems to vanish, but the lined-up gap is −7.83. ③ Lower it to −10: the plain gap swells to −16, and the lined-up gap is −8.17. ④ At 20 the plain gap is 8, as if the drug raised blood pressure, while the lined-up gap is −7.66. The lined-up gap drifts a little from −8 because the slope estimated from the data, 0.78, is a bit off from the true slope used to make the data, 0.8. ⑤ The standard error of the lined-up gap grows: 2.76 (0) → 3.12 (10) → 4.01 (20). The further apart the starting points, the further the lines are stretched to compare, so the error in the estimated slope counts for more.

6. Assumptions

AssumptionIn plain wordsIf it fails
The slope is the same in every group (parallel)Being 1 higher before treatment raises the after value by the same amount in both groupsThere is a group-by-covariate interaction. The gap between the lines changes with x, so one height gap cannot describe it. Report the effect at a few x values
The covariate is not affected by the treatmentIt was measured before treatment, or it is something the treatment cannot change (such as age)Putting in a value measured after treatment eats into the effect (section 7)
A straight-line relationBefore and after are linked roughly by a straight lineIf it curves, the part taken out by one straight line is wrong. Look at the picture first and add terms that fit the curve
The ANOVA assumptionsPeople are independent, the leftovers are roughly bell-shaped, and the leftover spread is similar in each groupSame as section 10 of the ANOVA page

To check for parallel lines, draw a separate line for each group in the picture, and add a "group × x" term to the formula to see whether that term is significant (the code in section 9). With few people this test misses differences easily, so look at the picture too.

7. Common mistakes

8. When to use what

MethodProsConsUse when
ANOVA (compare only the group means of the after values)Fewest assumptions, simplestThe spread from the starting values stays as noise, so the standard error is large. A chance difference at the start gets mixed into the effectYou did not measure the starting value, or it hardly moves with the result
ANCOVA (take out the part from the starting value with a line, then compare)Less leftover spread, and it fixes chance differences at the startNeeds the parallel and straight-line assumptions. Putting in a value measured after treatment gives a wrong answerRandomized experiments where, before treatment, you measured a value that moves strongly with the result
t-test on change scores (after − before for each person, then compare the two groups)Easy to explain. No slope to estimateThe same as ANCOVA with the slope fixed at 1. The further the true slope is from 1, the more leftover spread than ANCOVAWhen the after value follows the before value almost 1 to 1, or when the change itself is what you care about
Blocking (group people with similar starting values, then assign at random within each block)Prevents starting differences at the design stage. No straight-line assumptionYou need the starting value before assigning. Differences left inside a block stayWhen you can measure the starting value before assignment. Some studies block and then analyze with ANCOVA too

If you measured not just before and after but many time points, see the repeated measures page.

9. In Python

# ANCOVA in Python (statsmodels)
import statsmodels.formula.api as smf
# data: a pandas table with columns group (placebo 0, drug 1), before, after
fit = smf.ols("after ~ C(group) + before", data).fit()
print(fit.params)   # C(group)[T.1] = height gap between the lines (−8 for this page's data), before = shared slope (0.78)
# check parallel lines: add the group × before term and see whether it is significant
smf.ols("after ~ C(group) * before", data).fit().summary()

Where it is used

Frequently asked questions

It compares means, so why is it called analysis of covariance?

ANOVA splits sums of squares. ANCOVA also uses sums of products, which measure how the covariate and the result move together (the basis of covariance), to get the slope and take out that part. What it compares is still the group means.

How is it different from a t-test on change scores (after − before)?

A change-score analysis is ANCOVA with the slope fixed at 1. ANCOVA estimates the slope from the data, so the further the true slope is from 1, the smaller its leftover spread compared with the change-score analysis.

What if the slope differs between groups?

Then there is a group-by-covariate interaction. The gap between the two lines changes with the covariate value, so you do not report one height gap. Fit a model with the interaction term and report the effect at a few covariate values.

Words

Words used hereWhat books call it
comparing groups after lining up the starting pointanalysis of covariance (ANCOVA)
another value measured alongsidecovariate
the result you compareresponse variable, dependent variable
the value before treatmentbaseline
a column holding 0 and 1 for the groupsdummy variable, indicator variable
the slope both lines sharecommon slope, pooled within-group regression coefficient
mean with the starting point lined upadjusted mean, least-squares mean
the slope is the same in every grouphomogeneity of regression slopes, parallel slopes assumption
the slope differs by grouptreatment-by-covariate interaction
spread left over after the lineresidual, error
after − beforechange score
an unmeasured difference mixed into the effectconfounding

Sources

← Response surface methodologyRepeated measures and crossover designs →