Multiple regression: comparing with the other variables held fixed

Do people who drink more coffee have lower blood pressure? In made-up data, it looks that way if you put in coffee alone. Add age and the coefficient flips its sign. This page carries on from the short look at multiple regression in section 11 of correlation and regression: why coefficients change, what wobbles when inputs look alike, and how to read how well the model fits. All data here is made up, and the page explains statistics ideas only.

Short answer. Multiple regression guesses y from several inputs, as in y = b₀ + b₁x₁ + b₂x₂ + …. The coefficient b₁ means "with the other inputs held fixed, when x₁ goes up by 1, y changes by b₁", which is why it is called an adjusted coefficient. When inputs are tangled together, adding or dropping one can change a coefficient a lot, and when inputs look too much alike, a coefficient wobbles a lot from sample to sample.

Definitions

The page below explains with two inputs, but the definitions are the same for p inputs. Write yi for the outcome of person i and xij for the value of input j (i = 1, …, n, j = 1, …, p).

multiple regression model
A model that writes the outcome as a linear expression in the inputs plus an error. The errors εi are taken to be independent, with mean 0 and the same variance (confidence intervals and p-values also assume a normal distribution).

yi=β0+β1xi1+β2xi2+⋯+βpxip+εi

regression coefficient
The values b0,b1,…,bp that make the sum of squared residuals as small as possible are the least-squares estimates of β0,…,βp. bj is how much the prediction y^ goes up when xj goes up by 1 with every other input held the same. If x~ij is the residual from regressing xj on the other inputs, bj equals the slope of the simple regression of y on that residual.

y^i=b0+b1xi1+⋯+bpxip,bj=∑i=1nx~ijyi∑i=1nx~ij2

residual
The difference between an observed value and its prediction.

ei=yi−y^i

coefficient of determination
The share of the variation in y that the regression explains. Adding an input never lowers it.

R2=1−∑i=1nei2∑i=1n(yi−y¯)2

adjusted R²
The sum of squared residuals and the variation in y, each divided by its degrees of freedom, then compared. It is penalised as the number of inputs p grows.

Radj2=1−∑i=1nei2/(n−p−1)∑i=1n(yi−y¯)2/(n−1)=1−(1−R2)n−1n−p−1

multicollinearity and the variance inflation factor (VIF)
Multicollinearity is a strong linear relationship among the inputs that makes the variance of the coefficient estimates large. With Rj2 the coefficient of determination from regressing xj on the other inputs, the factor by which the variance grows is the variance inflation factor VIFj (σ² is the error variance).

VIFj=11−Rj2,Var(bj)=σ2∑i=1n(xij−x¯j)2×VIFj

dummy variable
An input that shows a category with 0 and 1. With k categories, make k − 1 of them, leaving out the reference category. The dummy for category m is 1 for people in that category and 0 otherwise. Its coefficient is the mean difference between that category and the reference category when the other inputs are the same.

Dim∈{0,1},m=1,…,k−1

1. Adding a variable changes the coefficient

Here are cups of coffee a day and systolic blood pressure for 60 made-up people (20 each in their 30s, 50s and 70s). When making the data we decided that "older people drink less coffee and have higher blood pressure" and that "at the same age, each cup adds 2 mmHg".

Dot colour is the age group. With "Add age too", the three coloured lines are the blood pressure the multiple regression guesses with age held at that group's mean (about 35, 55 and 74). They all have the same slope b₁, so they are parallel.

Try this. ① "Coffee only": blood pressure = 150.7 − 3.79 × coffee. One more cup gives a guess 3.79 mmHg lower. R² = 0.21. ② "Add age too": blood pressure = 106.1 + 1.6 × coffee + 0.52 × age. Comparing people of the same age, one more cup goes with 1.6 mmHg higher blood pressure. The sign flipped, and R² rose to 0.71. ③ The true value used to make the data is 2. With a sample of 60 it was estimated at 1.6, with a standard error of 0.8 (step ④ of "how the formula is built" in section 4).

Why it changes. People in their 30s drink 3.87 cups on average and have blood pressure 130.65; people in their 70s drink 2.15 cups and have 148.85. With coffee alone, the line sees "less coffee = older = higher blood pressure" all at once. The part of blood pressure that age raised leaks into the coffee slope and makes it go downhill. Put age in, and age's coefficient takes that part, leaving the coffee coefficient with only the difference within the same age. A hidden value tied to both inputs like this is confounding (confounding and bias), and a direction that flips when groups are combined is called Simpson's paradox.

There is an exact formula linking the two. Call δ the slope that guesses age from coffee. Then slope with coffee only = b₁ + b₂ × δ. In this data δ = −10.27 years (someone who drinks one more cup is about 10 years younger on average), so 1.6 + 0.52 × (−10.27) gives the coffee-only slope −3.79 (the numbers are rounded, so multiplying them by hand gives slightly different last digits). The coefficient only changes when the left-out variable is related to y (b₂ ≠ 0) and tangled with the input you kept (δ ≠ 0). If either is 0, it makes no difference whether you add it.

2. The formula and the arithmetic with two inputs

In y = b₀ + b₁x₁ + b₂x₂ the coefficients are picked by least squares, as in simple regression. With two inputs, take the mean away from each value and solve these two equations together (the normal equations).

S11b1+S12b2=S1y

S12b1+S22b2=S2y

S₁₁ = Σ(x₁ − x̄₁)², S₂₂ = Σ(x₂ − x̄₂)², S₁₂ = Σ(x₁ − x̄₁)(x₂ − x̄₂), S₁y = Σ(x₁ − x̄₁)(y − ȳ), and S₂y has the same shape. Solving gives

b1=S22S1y−S12S2yS11S22−S122

b0=y¯−b1x¯1−b2x¯2

If the inputs are uncorrelated (S₁₂ = 0), b₁ equals the simple regression slope S₁y ÷ S₁₁. The denominator can be written S₁₁ × S₂₂ × (1 − r₁₂²), where r₁₂ is the correlation between the two inputs. The more the inputs look alike (r₁₂ near ±1), the closer the denominator gets to 0 and the shakier the coefficients become. That is section 4. The labs on this page compute exactly this formula. With three or more inputs the same thing is done with matrices, and statistics tools do it for you.

Reading a coefficient. The unit of b₁ is "unit of y ÷ unit of x₁" (mmHg per cup here). Because the units differ, do not compare the sizes of b₁ and b₂ to say which one "matters more". b₀ is the height when every input is 0, so it means nothing when that is outside the data, like age 0.

3. Putting in categories: columns marked 0 and 1 (dummy variables)

Values that are not numbers, like age group, sex or treatment group, go in as columns marked 0 and 1 (dummy variables). With k groups you need k − 1 columns, and the one group without a column becomes the baseline group (reference category).

Age group[50s] column[70s] column
30s (baseline)00
50s10
70s01

blood pressure=b0+b1×coffee+b2×[50s]+b3×[70s]

b₂ is "how much higher the 50s are than the 30s (baseline) on average, at the same number of cups", and b₃ is the gap between the 70s and the 30s. Each group gets a line at a different height, with the same slope b₁. If you make the 70s the baseline, the intercept and the dummy coefficients change, but the coffee coefficient and each person's guess stay the same.

If you make as many columns as groups (three), the three columns always add up to 1, which is exactly the intercept column. Then the formula cannot be pinned down (the dummy variable trap). With only groups and no continuous input, regression with dummy variables gives the same test as analysis of variance, which compares group means. If you think the coffee slope itself differs by age group, add a multiplied column such as coffee × [70s] (an interaction).

4. When inputs look too much alike (multicollinearity)

When two inputs move almost together, the data cannot tell which one made y go up. The coefficients wobble a lot from sample to sample (their standard errors grow), and can even come out with the wrong sign. This is multicollinearity. Below, we draw 30 people and fit y = x₁ + x₂ + noise, repeat that 200 times, and put one dot per b₁. The true value is b₁ = 1.

Moving the slider draws the 200 samples again with the same seed, changing only the correlation between the inputs. The up-down position only keeps dots from piling up and means nothing. Orange dots are b₁ below 0, with the sign the wrong way round.

Try this. ① Correlation 0: b₁ has a standard deviation of 0.2, and every one is above 0. ② 0.9: VIF = 5.26, √VIF = 2.29. The standard deviation is 0.44, 2.2 times that at correlation 0, and 3 have the wrong sign. ③ 0.99: VIF = 50.25, √VIF = 7.09. The standard deviation is 1.35, 6.9 times as wide, and 44 of the 200 are below 0. The spread grows by almost exactly √VIF.

The VIF (variance inflation factor) is a number for each input that says how many times larger this wobble gets.

VIF=11−Rj2

R²ⱼ is the R² of a regression that guesses input j from the other inputs (y is not used). With two inputs, R²ⱼ is the square of their correlation coefficient. A correlation of 0.9 gives 1 ÷ (1 − 0.81) = 5.26, and 0.99 gives 50.25. The VIF is how many times the variance of the coefficient grows, so the standard error grows √VIF times. Books give different cut-offs for when it becomes a problem. Rather than cutting at one number, look at whether that coefficient's standard error and confidence interval are still usable.

Multicollinearity makes coefficients wobble, but for inputs shaped like the data it does not shake the guesses (predictions) much. When b₁ gets bigger, b₂ gets smaller and they make up for each other. It is a problem when you need to read the coefficients.

How the formula is built: VIF

Build VIF = 1 ÷ (1 − R²ⱼ) step by step with the data from lab 1 (coffee and age). Press "Next" and a piece is added to the formula above (orange background marks the new piece), with the reason below.

Dot colours are the same age groups as in lab 1. The orange vertical lines are the part of coffee that age does not explain (the residuals).

Written only as formulas, the steps above look like this. The values in brackets come from the lab 1 data.

x1≈a+c×x2(coffee≈5.45−0.045×age),Rj2=0.47

1−Rj2=0.53

VIF=11−Rj2≈1.87,VIF≈1.37

SE(b1)=s∑(x1−x¯1)2×VIF≈0.585×1.37≈0.801

Try this. Go through all the steps: coffee ≈ 5.45 − 0.045 × age, R²ⱼ = 0.47, 1 − R²ⱼ = 0.53, VIF = 1.87, √VIF = 1.37, and the standard error of the coffee coefficient is 0.585 × 1.37 = 0.801. Coffee and age in lab 1 overlap this much, so adding age widens the coffee coefficient's standard error 1.37 times.

Step ② is what "held fixed" means. The multiple regression b₁ is exactly the slope you get by regressing y on what is left of x₁ after taking away the part the other inputs explain (the Frisch–Waugh–Lovell theorem). "Holding age fixed" is, in the arithmetic, "taking away the part of coffee that age explains".

5. How well it fits: R² and adjusted R²

R² (coefficient of determination) is, as in simple regression, "the share of the spread in y that the formula explains".

R2=1−∑residual2∑(y−y¯)2

Adding an input never lowers R². Setting the new coefficient to 0 gives the old formula back, so least squares fits at least that well. Even a column of random numbers with no relation to anything raises R² a little. That is why we also look at adjusted R², which charges for the number of inputs.

adjusted R2=1−∑residual2/(n−p−1)∑(y−y¯)2/(n−1)

p is the number of inputs (not counting the intercept), and n − p − 1 is the residual degrees of freedom. The same thing can be written 1 − (1 − R²) × (n − 1) ÷ (n − p − 1). A useless input shrinks Σresidual² a little but also shrinks n − p − 1, so adjusted R² can go down. In lab 1, coffee alone gives R² 0.21 and adjusted R² 0.2; adding age gives 0.71 and 0.7. Age was worth putting in.

Adjusted R² is still measured on the same data the model was fitted to. How well it fits new data is checked with data held back or with cross-validation (overfitting).

6. Assumptions and residual plots

The standard errors, confidence intervals and p-values of the coefficients rest on the same assumptions as simple regression. How to check them is the same as in "Assumptions, and how to read a residual plot" in section 6 of correlation and regression.

With several inputs, look at one residual plot with the guess across, and one for each input with that input across.

7. Easy mistakes

These readings are wrong
  • "The coffee coefficient adjusted for age is significant, so coffee raises blood pressure": no. Only age, which is in the formula, was held fixed. Confounders you did not measure (exercise, smoking and so on) are still there (confounding and bias). Talking about causes needs an experiment where people are split at random.
  • "The more variables, the more accurate": no. R² rises just from adding (section 5), look-alike inputs make coefficients wobble (section 4), and useless inputs give a model that fits only this data (overfitting). Putting in a value that sits between the cause and the result, or a value produced by the result, makes the coefficients worse.
  • "There is a formula, so any combination can be guessed": no. Each input can be inside the data's range while the combination is almost missing from the data. For example, if there are hardly any people in their 70s who drink a lot of coffee, a guess for that combination is the same as stretching beyond the data (do not stretch the line beyond the data).
  • "The bigger coefficient matters more": with different units they cannot be compared. See "Reading a coefficient" in section 2.

What to use when

MethodGoodBadUse it when
Simple regression (one input)One picture shows it, easy to readThe part from other tangled values leaks into the slopeFirst look at a relationship; the effect of one treatment in an experiment split at random
Multiple regression (several inputs)Compares with the other measured values held fixedCannot stop confounders you did not measure. Look-alike inputs make coefficients wobbleAdjusting for confounding in observed data; guessing y from several inputs
Categories as dummy variablesGroup differences and continuous inputs in one formulaNeeds one column fewer than the groups, and coefficients are read against the baseline groupSex, age group, treatment group
Analysis of varianceTests differences between group means in one goSlopes of continuous inputs need separate handlingExperiments that only compare groups
Logistic regressionGuesses a probability between 0 and 1 when the result is yes/noCoefficients are log odds, not probabilities, so harder to readHas the disease or not, buys or not
Drop or combine one look-alike inputCoefficients settle downYou give up on telling the two apartThe VIF is large and you need to read the coefficients

In Python

# data: made-up data (a pandas table). Blood pressure bp, cups of coffee coffee, age age,
# exercise habit group ("none", "some", "often")
import statsmodels.formula.api as smf

model = smf.ols("bp ~ coffee + age + C(group)", data=data).fit()
print(model.summary())   # coefficients, standard errors, t, p, R², adjusted R²
# C(group): turns the groups into dummy columns. The baseline is the first group by name ("none" here)

Frequently asked questions

If a coefficient in a multiple regression is significant, is that variable a cause?

No. Multiple regression only holds fixed the variables you put in the formula. Confounders you did not measure are still there, and the cause may run the other way. To talk about causes you need an experiment where people are split at random, or a design built for that question.

Is it better to put in more variables?

No. Adding a variable never lowers R², so it only looks better. Useless variables make the coefficients wobble and give a model that fits only this data. Putting in a value that sits between the cause and the result, or a value produced by the result, makes the coefficients worse. Decide what goes in from the question and background knowledge, before looking at the data.

What do I do when the VIF is large?

If you need to read each coefficient, drop one of the look-alike inputs, replace the two with one combined value, or collect more data. If you only need guesses (predictions), it may not matter much for inputs shaped like the data. Books give different cut-offs for when it becomes a problem.

Words

Plain words used hereTextbook term
Regression with several inputsMultiple linear regression
The slope with the other inputs held fixedPartial regression coefficient, adjusted coefficient
The equations that set the coefficientsNormal equations
A hidden value changes the coefficient / the error from leaving it outConfounding / omitted variable bias
The direction flips when groups are combinedSimpson's paradox
Columns marked 0 and 1Dummy variable, indicator variable
The group without a columnReference category, baseline
A multiplied columnInteraction term
Inputs that look too much alikeMulticollinearity
How many times the wobble growsVariance inflation factor (VIF)
The slope seen through what is left after the other inputsFrisch–Waugh–Lovell theorem
The share the formula explains / with a charge for the number of inputsCoefficient of determination (R²) / adjusted R²
n − p − 1Residual degrees of freedom

Sources

← Correlation and regressionMultiple comparisons →