Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Data Science & Statistics with Python — Alpha School

Lesson 15: Multiple Regression and Confounding

A single predictor rarely tells the whole story. House prices depend on size, location and age together; exam scores depend on study time, prior knowledge and sleep. Multiple regression handles this by fitting a model with several predictors at once: the outcome is predicted by a weighted sum of all of them, plus an intercept. Each coefficient then has a useful meaning: it is the expected change in the outcome when that predictor rises by one unit, holding the others fixed. That phrase — holding the others fixed — is what makes multiple regression powerful, and what makes it easy to misread.

The code below fits a multiple regression with Python's statsmodels library, predicting a score from hours studied and hours slept. The output gives a coefficient for each predictor, a p-value for each, and an R-squared for the whole model — the share of the variation in the outcome the model explains. R-squared always rises when you add predictors, even useless ones, so a high R-squared alone is not evidence of a good model; the adjusted R-squared, which penalises extra predictors, is the fairer comparison.

import pandas as pd
import statsmodels.formula.api as smf

df = pd.read_csv("students.csv")
model = smf.ols("score ~ hours_studied + hours_slept", data=df).fit()
print(model.summary())

Confounding: The Hidden Third Variable

Confounding is the trap that multiple regression is built to handle — and also the trap it can fall into. A confounder is a third variable that influences both the predictor and the outcome, creating a misleading link between them. Ice-cream sales and drownings rise together in summer, not because one causes the other, but because hot weather drives both. If you regress drownings on ice-cream sales alone, the link looks strong; add temperature to the model and the ice-cream coefficient collapses. Including the confounder reveals the truth; omitting it hides it.

But including every variable is not the answer either. Some variables are consequences of the outcome, not causes, and controlling for them can distort the result — a problem called collider bias, which is beyond this course but worth knowing exists. The practical rule is to think before you model: draw the relationships you believe in, decide which variables are confounders, and include those. A regression is only as good as the thinking that chose its variables.

Reading a Regression Honestly

When you read a regression, check four things: the coefficients and their direction, the p-values, the R-squared, and — most importantly — whether the variables included make sense. A coefficient is an association, not a cause, unless the data comes from a fair experiment. And a model that fits the data it was built on may still fail on new data, so check it on held-out data, as in every other lesson. Regression is a tool for careful thinking, not a machine that produces truth.

The Dangers of a Kitchen-Sink Model

Adding every available variable to a regression is tempting, because each one might help — but it creates three problems. The first is overfitting: with enough predictors, a model can fit the noise in the training data and fail on new data, just as a neural network can. The second is interpretation: with dozens of coefficients, each "holding the others fixed", the story becomes impossible to follow, and a reader cannot tell which variables matter. The third is instability: when predictors are correlated with each other, their coefficients can swing wildly with small changes in the data, so the same model fitted twice can tell two different stories.

The remedy is to choose variables by thinking, not by availability. Draw the relationships you believe in, include the confounders, and leave out variables that are consequences of the outcome. Then check the model on held-out data, and prefer the simpler model when two perform equally well. A regression with three well-chosen predictors that you can explain is worth more than one with thirty that you cannot.

Interpreting Coefficients Carefully

A coefficient in a multiple regression is easy to misread, so it is worth stating the reading rule precisely. The coefficient on hours studied is the expected change in the score when hours studied rises by one, holding all the other predictors in the model fixed. The phrase 'holding the others fixed' is doing real work: it means the comparison is between two students who are alike in every other measured way. If the model omits an important variable — prior knowledge, say — then the coefficient absorbs its effect, and the reading is wrong. The coefficient is only as good as the set of variables around it.

A second caution concerns the units. A coefficient of 2.5 means 2.5 points per hour if hours are the unit, but the same relationship would be 0.04 points per minute — the number changes, the relationship does not. Comparing coefficients across predictors measured in different units is therefore meaningless without standardising them first. Report the units alongside every coefficient, and be suspicious of any comparison that does not.

Key Terms

Multiple regression
Predicting an outcome from several predictors at once.
Coefficient
The expected change in the outcome when one predictor rises by one unit, holding the others fixed.
R-squared
The share of the variation in the outcome that a model explains.
Adjusted R-squared
R-squared penalised for the number of predictors, for fair comparison between models.
Confounder
A third variable that influences both predictor and outcome, creating a misleading link.
Intercept
The predicted outcome when all predictors are zero.

Discussion Questions

  1. Ice-cream sales and drownings rise together. What is the confounder, and how does adding it change the conclusion?
  2. Why does R-squared always rise when you add predictors, even useless ones?
  3. A coefficient is an association, not a cause. What kind of evidence would turn it into a causal claim?

Practice Questions

  1. What does a coefficient mean in multiple regression?
  2. What is a confounder? Give an example.
  3. Why is the adjusted R-squared fairer than R-squared when comparing models?
  4. In the code, what does score ~ hours_studied + hours_slept mean?
  5. Why is it wrong to include every available variable in a regression?
Show answers
  1. The expected change in the outcome when that predictor rises by one unit, holding the other predictors fixed.
  2. A third variable that influences both the predictor and the outcome, creating a misleading link — for example, hot weather drives both ice-cream sales and drownings.
  3. Because R-squared always rises when you add predictors, even useless ones, while the adjusted version penalises extra predictors.
  4. Predict the score from hours studied and hours slept, in a multiple regression. The tilde separates the outcome from the predictors.
  5. Because some variables are consequences of the outcome rather than causes, and controlling for them can distort the result; the variables must be chosen by thinking about the relationships.
Keep reading to complete this lesson