Introduction to Regression

What regression is for and the vocabulary every regression method shares. Inputs and outputs, describing a scatter plot by direction, form and strength, Anscombe's quartet, the model as a function plus error, predictions and residuals, comparing a model with the mean, and why predictions outside the data are unreliable, worked on a diabetes study of 442 patients.

A regression models a numerical outcome as a function of inputs plus error, $Y = f(X) + \varepsilon$, and uses the estimate $\hat{f}$ to describe the dependence or predict new cases. For 442 diabetes patients (Efron et al., 2004), the least-squares line $\widehat{\text{progression}} = -117.77 + 10.233 \times \text{BMI}$ ($r = +0.586$) removes 34.4% of the squared error of predicting the mean, 152.1, for everyone, and its residuals (observed minus predicted) average 51.8 in absolute value. Summaries alone can mislead: Anscombe's four datasets share a correlation of 0.816 and the line $\hat{y} = 3.00 + 0.500\,x$, yet only one is described well by it, so plot first. A fitted model is supported only inside the range of its inputs, and of their combinations; beyond that it extrapolates. Observational data describe comparisons between cases, not causes.

In a study published by Efron, Hastie, Johnstone and Tibshirani (2004), 442 diabetes patients were measured at the start on ten variables: age, sex, body mass index (BMI), average blood pressure and six blood serum measurements. One year later, the investigators recorded a single number for each patient, a quantitative measure of how far the disease had progressed. The data are distributed with scikit-learn, so anyone can reproduce what follows. A natural first question is how progression after a year relates to BMI at the start. Patients with higher BMI do tend to progress further: the correlation is $+0.586$. On average, patients whose BMI differs by one unit differ in progression by about 10.2 units, and for a new patient with a BMI of 30 the fitted line predicts a progression of 189.2. The rest of this article sets out what those three answers mean and how far they can be trusted.

Regression is the family of methods that answers questions of this kind: it models how a numerical outcome depends on one or more inputs, and uses that model to describe the dependence or to predict the outcome for new cases. This article sets out the vocabulary that every later article in Regression in Practice builds on. Simple linear regression then derives the best straight line, Linear Regression Assumptions asks when its error bars can be trusted, Multiple Linear Regression adds inputs, and Logistic Regression handles outcomes that are yes or no.

Where the word comes from

The name is historical and slightly misleading. Francis Galton (1886) compared the heights of adult children with the average height of their two parents, which he called the "mid-parentage", after multiplying women's heights by 1.08 to make them comparable with men's. He found that children of unusually tall parents were tall, but less unusual than their parents, and that children of short parents were short, but less so. In his words, the height deviation of the offspring "is, on the average, two-thirds of the height-deviate of its mid-parentage", and he called the effect "regression towards mediocrity". The tendency is now called regression to the mean. It appears whenever two measurements are imperfectly correlated, and Simple Linear Regression shows exactly why. Galton's word has stayed with the method: a fitted line is still called a regression line, and the name covers the whole family of methods, including many that have nothing to do with returning toward an average.

Inputs and outputs

A regression has one output, the quantity being explained or predicted, and one or more inputs that it is explained by. Each has several names, used interchangeably in practice. James, Witten, Hastie and Tibshirani (2021, §2.1) list predictors, independent variables, features or just variables for the inputs, and response or dependent variable for the output. The diabetes study has one output, progression after a year, and ten candidate inputs.

The roles are set by the question. In a designed experiment the investigator sets the input, for example a dose of fertilizer or a drug, and observes the output, so the roles are fixed by the design. In an observational study like the diabetes data, nothing was manipulated. BMI is treated as the input because the question is about predicting progression from what is known at the start, not because anyone believes progression could alter a BMI measured a year earlier. Calling a variable "independent" does not make it a cause. A regression of progression on BMI describes how progression differs, on average, between patients whose BMI differs. Whether changing one patient's BMI would change that patient's progression is a causal question that the data alone cannot settle (Gelman, Hill and Vehtari, 2020, §10.2). Potential Outcomes states that question formally, and Confounding and Causal Diagrams says what a regression adjustment can and cannot do about it.

By convention the input goes on the horizontal axis of a scatter plot and the output on the vertical axis, because the model will predict vertical positions from horizontal ones. The convention matters more than it seems. The line that best predicts progression from BMI is not the same as the line that best predicts BMI from progression, as Simple Linear Regression shows, so swapping the axes changes the answer.

Regression problems are distinguished from classification problems by the type of output. When the output is a number, such as progression, blood pressure or price, the task is regression. When it is a category, such as whether a tumor is malignant or which digit an image shows, the task is classification (James et al., 2021, §2.1.5). The boundary is not sharp. Logistic regression predicts a category by first modeling a probability, which is a number, and the article on it treats it as a member of the regression family.

Describing a scatter plot

Before fitting anything, the first step is to plot the output against each input and describe what the plot shows. Three properties cover most of what matters, and a fourth catches what the other three miss.

Progression after one year against three baseline measurements for the 442 patients, each with its least-squares line. BMI shows a moderate positive relationship (r = +0.586), HDL cholesterol a weaker negative one (r = -0.395), and age only a weak one (r = +0.188), even though age ranges over sixty years.
Progression after one year against three baseline measurements for the 442 patients, each with its least-squares line. BMI shows a moderate positive relationship (r = +0.586), HDL cholesterol a weaker negative one (r = -0.395), and age only a weak one (r = +0.188), even though age ranges over sixty years.

Direction says whether the output tends to rise or fall as the input increases. Progression rises with BMI, so the relationship is positive. It falls with HDL cholesterol (the variable s3 in the dataset, often called "good" cholesterol), so that relationship is negative. A plot with no upward or downward tendency has no direction.

Form says what shape the trend follows. The BMI cloud has no visible bend, so a straight line is a reasonable summary of its trend. Other data follow a curve, such as a rise that levels off. The form decides what kind of model is appropriate: a straight line fitted to curved data will be wrong in a systematic way, too high in some regions and too low in others, no matter how carefully it is fitted.

Strength says how tightly the points cluster around the trend. The BMI relationship is moderate: the trend is clear, but patients with the same BMI differ widely in progression. For the age plot, the trend is so weak relative to the scatter that knowing a patient's age says little about their progression. The correlation coefficient $r$, defined and derived in Simple Linear Regression, measures the direction and strength of a straight-line relationship on a scale from $-1$ to $+1$. Across the ten diabetes inputs, $r$ runs from $-0.395$ for HDL to $+0.586$ for BMI, and the weakest relationship is with sex, at $+0.043$.

Unusual points are observations that do not follow the pattern of the rest. They may be recording errors, cases of a different kind, or genuine but rare values. They deserve investigation, because some of them can move a fitted model a long way.

Why the plot comes first

Anscombe (1973) made the case for plotting with four small datasets, each of eleven points, that share nearly every summary statistic. In all four the inputs have mean 9.00 and variance 11.00, the outputs have mean 7.50 and variance between 4.12 and 4.13, the correlation is 0.816 (0.817 for the fourth set, to three decimals), and the least-squares line is $\hat{y} = 3.00 + 0.500\,x$. The numbers were recomputed for this article from Anscombe's published table. The agreement is not a coincidence of rounding: the line and the correlation depend on the data only through $n$ and five sums, and Anscombe chose four sets whose sums agree to about two decimal places, as Worked problem 3 shows.

Anscombe's quartet. All four datasets have the same means, variances, correlation and least-squares line (orange), yet only the first is described well by that line. The second is a smooth curve, the third is a line with one outlier, and in the fourth a single point determines the slope.
Anscombe's quartet. All four datasets have the same means, variances, correlation and least-squares line (orange), yet only the first is described well by that line. The second is a smooth curve, the third is a line with one outlier, and in the fourth a single point determines the slope.

Only the first set is the kind of data the summary suggests. The second set follows a smooth curve exactly, so its form is not linear. The third lies on a straight line except for one point, which tilts the fitted line away from the other ten. In the fourth set, ten points share the same input value and the eleventh sits far to the right; the slope of the fitted line is determined entirely by that one point. A correlation of 0.816 describes all four equally, and describes three of them badly. The lesson is the reason for the order of work in every later article: plot, describe, then model.

The regression model

A regression model is a rule $f$ that takes the input values and returns a prediction. With one input $x$, the statistical model behind most regression methods is

$$Y = f(X) + \varepsilon,$$

where $f$ is a fixed but unknown function and $\varepsilon$ is a random error term with mean zero, independent of $X$ (James et al., 2021, eq. 2.1). The function $f$ is the systematic part, the average outcome at each input value. The error collects everything else that affects the outcome: other variables that were not measured, measurement error, and variation that is simply unpredictable. Regression estimates $f$ from data. The estimate is written $\hat{f}$, and its value at an input $x$ is the predicted value or fitted value

$$\hat{y} = \hat{f}(x).$$

The hat marks an estimate, distinguishing the model's prediction $\hat{y}$ from the observed value $y$. For the diabetes data, the least-squares line derived in Simple Linear Regression is

$$\widehat{\text{progression}} = -117.77 + 10.233 \times \text{BMI}.$$

The first patient in the dataset has a BMI of 32.1, so the model predicts $-117.77 + 10.233 \times 32.1 = 210.7$. That patient's observed progression was 151.

Residuals

The residual is the observed value minus the predicted value:

$$e_i = y_i - \hat{y}_i.$$

For the first patient, $e_1 = 151 - 210.7 = -59.7$. The sign has a fixed meaning. A negative residual means the observation fell below the model's prediction, so the model overpredicted, as here. A positive residual means the model underpredicted. On a scatter plot, a residual is the vertical distance from a point to the fitted line, measured in the units of the output. It is vertical, not the shortest distance to the line, because the model predicts the output from the input, and the error is in the prediction of the output.

The least-squares line for progression on BMI. Green segments are the residuals of 14 patients chosen at random: the vertical gaps between observed and predicted progression. The dotted blue line is the mean-only model, which ignores BMI. The shaded regions lie outside the range of BMI in the data, where the dashed continuation of the line is an extrapolation.
The least-squares line for progression on BMI. Green segments are the residuals of 14 patients chosen at random: the vertical gaps between observed and predicted progression. The dotted blue line is the mean-only model, which ignores BMI. The shaded regions lie outside the range of BMI in the data, where the dashed continuation of the line is an extrapolation.

The residuals of the 442 patients range from $-164.9$ to $+154.9$, and their absolute values average 51.8. A residual is also the most direct evidence about the error term $\varepsilon$, but it is not the same thing. The error is the deviation from the true, unknown $f$; the residual is the deviation from the estimate $\hat{f}$. The distinction matters in Linear Regression Assumptions, because the assumptions are stated about the errors and checked with the residuals.

How good is the model?

A model is only useful if it predicts better than not using the input at all. The natural baseline ignores BMI and predicts the overall mean progression, 152.1, for every patient. Among all constant predictions, the mean is the one with the smallest sum of squared residuals (Worked problem 1 proves it). Its residuals are the deviations from the mean, and the sum of their squares is 2,621,009. The line's squared residuals sum to 1,719,582, so the line removes

$$1 - \frac{1{,}719{,}582}{2{,}621{,}009} = 0.344$$

of the squared prediction error, about a third. Measured by absolute rather than squared residuals, the average miss falls from 65.8 for the mean to 51.8 for the line. The first of these, the share of squared variation removed, is the coefficient of determination $R^2$, derived properly in Simple Linear Regression. A value of 0.344 says that BMI explains a real but limited share of why patients progress differently. Most of the variation comes from elsewhere, and Multiple Linear Regression shows how much more the other baseline variables explain.

Small residuals on average are not enough. The residuals should also show no pattern. If they are mostly positive at both ends of the input range and negative in the middle, the model is missing a curve; if their spread grows with the input, its uncertainty is not uniform. Plotting the residuals against the fitted values is therefore part of every regression analysis, and Linear Regression Assumptions works through what each pattern means.

Extrapolation

A fitted model can be evaluated at any input, but it is supported by data only where data exist. Predicting for inputs inside the observed range is interpolation. Predicting for inputs outside it is extrapolation, and the model then reports a number with no evidence behind it.

The BMI values in the diabetes study run from 18.0 to 42.2. Within that range the line is an average of what was observed. Outside it, the line simply continues. At a BMI of 60 it predicts a progression of 496.2, well above the largest value in the data, 346. At the other end, the line crosses zero at a BMI of 11.5 and predicts negative progression below that, although the smallest observed value is 25. Nothing in the data says whether the true relationship continues to rise steeply for very high BMI, flattens, or changes in some other way, because no such patients were measured. The straight line is an assumption about those regions, not a finding.

Two practical rules follow. First, report the range of the inputs used to fit a model, and flag any prediction outside it. Second, with several inputs, check the combination as well as each input separately. A patient whose BMI and blood pressure are each within the observed ranges may still have a combination of the two that no patient in the data had, and a prediction for that patient is an extrapolation even though no single input is out of range. Worked problem 2 finds such a patient in the diabetes data and measures how far outside the data the combination lies.

Extrapolation can have consequences far beyond a wrong number in a table. On the night before the Challenger launch in January 1986, the forecast temperature at launch was 31 °F, while none of the 23 earlier flights in the record had launched below 53 °F (Draper, 1993). Logistic Regression analyzes those flights and shows what the data could and could not say about a launch that cold.

Worked problems

The problems extend the article on its own data. Every number is recomputed, with assertions, in the review-pass block of tools/experiments/data-science/introduction-to-regression.py.

Problem 1 (the best constant prediction). The baseline above predicts the mean for every patient. Show that the mean is the constant with the smallest sum of squared residuals, find the constant with the smallest sum of absolute residuals, and compare the two on the diabetes data.

Solution. For any constant $c$, write $y_i - c = (y_i - \bar{y}) + (\bar{y} - c)$ and square. The cross term is $2(\bar{y} - c)\sum_i (y_i - \bar{y}) = 0$, because deviations from the mean sum to zero, so

$$\sum_i (y_i - c)^2 = \sum_i (y_i - \bar{y})^2 + n(\bar{y} - c)^2.$$

The second term is zero only at $c = \bar{y}$, so the mean is the unique minimizer. For absolute residuals, $\sum_i |y_i - c|$ is piecewise linear in $c$. Moving $c$ up by a small amount raises the residual of every point below $c$ and lowers the residual of every point above it, so the sum falls while more points lie above $c$ than below, and rises once more lie below. The minimum is therefore at a median. The diabetes progressions have median 140.5, with 221 patients below it and 221 above. The median's mean absolute residual is 65.04, against 65.76 for the mean. Its sum of squared residuals is 2,680,829, which is the mean's 2,621,009 plus $442 \times (152.13 - 140.5)^2 = 59{,}819$, as the identity says. Each loss has its own best constant. $R^2$ measures improvement over the mean because it is built on squared residuals; Loss Functions for Regression follows the same pairing of loss and baseline through other losses.

Problem 2 (an extrapolation that no single input reveals). For the model of progression on BMI and blood pressure (BP), find a patient whose BMI and BP each lie within the observed ranges but whose combination lies outside the data, and say how much less precise the model's prediction is there.

Solution. BMI runs from 18.0 to 42.2 and BP from 62 to 133 mm Hg, and the two are positively correlated ($r = 0.395$), so a high BMI with a low BP is rare. Take BMI 40 and BP 70. No patient lies within 2 BMI units and 5 mm Hg of that point. The distance that matters for a linear model is the leverage of the new point, $h_0 = \mathbf{x}_0^{\mathsf{T}}(X^{\mathsf{T}}X)^{-1}\mathbf{x}_0$, with $\mathbf{x}_0 = (1, 40, 70)$ and $X$ the design matrix. Writing the inputs as deviations from their means does not change the fitted plane, and then $X^{\mathsf{T}}X$ is block diagonal, with $n$ in one block and the matrix $C$ of centered sums of squares and products in the other. So $h_0 = 1/n + \mathbf{d}_0^{\mathsf{T}} C^{-1}\mathbf{d}_0$, where $\mathbf{d}_0$ is the new point's deviation from the means: a distance that accounts for the correlation between the inputs. Here $h_0 = 0.0480$. The largest leverage among the 442 patients is 0.0426 and the average is $3/442 = 0.0068$. The point lies beyond every patient in this distance, although neither coordinate is out of range. The estimated mean at the point, $\mathbf{x}_0^{\mathsf{T}}\mathbf{b}$, has variance $\sigma^2 h_0$, because the coefficients have covariance $\sigma^2(X^{\mathsf{T}}X)^{-1}$ (Theorem 1 of The Gauss–Markov Theorem; Theorem 3 of The Normal Linear Model builds the interval from it). The model predicts 234.1 there. The standard error of that estimated mean is $\sqrt{0.0480/0.0068} = 2.66$ times its value at a point of average leverage. An interval for a single new patient at that point widens far less, by a factor of $\sqrt{1.0480/1.0068} = 1.02$, because the patient's own error, with variance $\sigma^2$, dominates the variance $\sigma^2(1 + h_0)$ of a prediction error. Extrapolation in combination inflates the uncertainty about the fitted plane, and the scatter of individual patients hides it, which is why the leverage has to be computed rather than read off a prediction interval. A practical screen follows: compute $h_0$ for every new case and flag those above the largest leverage in the training data. Leverage is studied in depth in Leverage, Influence and Residual Diagnostics.

Problem 3 (why Anscombe's four sets have nearly the same summaries). Show that the least-squares line and the correlation depend on the data only through $n$ and the sums $\sum x$, $\sum y$, $\sum x^2$, $\sum y^2$, $\sum xy$, and compute those for the quartet.

Solution. The slope is $S_{xy}/S_{xx}$, the intercept $\bar{y} - b_1\bar{x}$, and $r = S_{xy}/\sqrt{S_{xx}S_{yy}}$ (Simple Linear Regression). Each centered sum is a function of the raw sums, for example $S_{xy} = \sum xy - (\sum x)(\sum y)/n$. In exact arithmetic, every set has $n = 11$, $\sum x = 99$ and $S_{xx} = 110$. The outputs give $\sum y$ of 82.51, 82.51, 82.50 and 82.51, $S_{xy}$ of 55.01, 55.00, 54.97 and 54.99, and $S_{yy}$ of 41.2727, 41.2763, 41.2262 and 41.2325. The slopes are therefore 0.5001, 0.5000, 0.4997 and 0.4999, the intercepts 3.0001, 3.0009, 3.0025 and 3.0017, and the correlations 0.8164, 0.8162, 0.8163 and 0.8165. A statistic built from these sums differs between the four sets only in its third or fourth significant digit. Residuals and leverages are not built from them, which is why the scatter plots above, and the residuals and leverages examined in Linear Regression Assumptions, separate the sets at once.

Open questions

  • Does the relationship between BMI and progression bend? The straight line was chosen by eye. A local linear fit or a smoothing spline, compared with the line by ten-fold cross-validated error over 30 seeded splits, would settle whether any curvature is large enough to matter for prediction.
  • Would a median line tell a different story? Problem 1 shows that the best constant changes with the loss. Fitting the line that minimizes absolute residuals (median regression) and comparing its slope with 10.23, with a bootstrap interval for the difference, would show whether the least-squares slope is driven by the patients with large residuals.
  • Is the maximum-leverage screen enough with ten inputs? Problem 2's screen flags points far from the center of the data in the model's metric. A point can have modest leverage and still lie outside the convex hull of the inputs. Checking hull membership for held-out patients by linear programming, and comparing the two screens' flags with the prediction errors that follow, would measure how often the screen misses an extrapolation.

The regression family

The methods in this topic share the structure set out above: a model of the outcome as a function of inputs, fitted to data by making some measure of the residuals small, judged against a simple baseline, and trusted only within the data's range.

  • Simple linear regression uses one input and a straight line, fitted by least squares.
  • Multiple linear regression uses several inputs at once. With powers and products of the inputs as extra columns, the same machinery fits curves and interactions.
  • Logistic regression models the probability of a yes-or-no outcome, so its predictions stay between 0 and 1.

Many other methods fit the same pattern with more flexible functions $f$: regression trees split the inputs into regions, neural networks compose simple nonlinear functions, and Symbolic Regression searches over formulas themselves. More flexible models can follow curves that a straight line cannot, at the cost of needing more data and being harder to interpret, a trade-off taken up in The Bias-Variance Tradeoff.

The next article, Simple Linear Regression, derives the least-squares line used throughout this one, proves that it passes through the point of means, and shows why its slope is the correlation rescaled into the data's units. The theory under the whole unit is proved in the Statistics collection: Linear Models for least squares, its geometry and its tests, and Generalized Linear Models for logistic regression and its relatives.

In Python

scikit-learn ships the diabetes data, and its LinearRegression class fits a least-squares line. load_diabetes(scaled=False) returns the variables in their original units; by default each input is centered and scaled so that its sum of squares is 1. (Checked against scikit-learn 1.5.2, which produced the numbers here, and the 1.9.1 documentation, November 2026.)

from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression

data = load_diabetes(scaled=False)
X = data.data[:, [2]]   # BMI, kept two-dimensional: one column
y = data.target         # progression after one year

model = LinearRegression().fit(X, y)
print(model.intercept_, model.coef_[0])   # about -117.77 and 10.233
y_hat = model.predict(X)                  # fitted values
residuals = y - y_hat
print(y_hat[0], residuals[0])             # about 210.7 and -59.7

The input X must be two-dimensional, with one row per patient and one column per input, even when there is a single input; selecting with [:, [2]] instead of [:, 2] keeps the column dimension. The numbers in this article are computed by tools/experiments/data-science/introduction-to-regression.py in the site's repository.

References

  • Anscombe, F. J. (1973). Graphs in statistical analysis. The American Statistician, 27(1), 17–21. record · 4 pages
  • Draper, D. (1993). Challenger USA Space Shuttle O-Ring [Data set]. UCI Machine Learning Repository. https://doi.org/10.24432/C5PW2T record · 5 pages
  • Efron, B., Hastie, T., Johnstone, I., and Tibshirani, R. (2004). Least angle regression. The Annals of Statistics, 32(2), 407–499. record · 21 pages
  • Galton, F. (1886). Regression towards mediocrity in hereditary stature. The Journal of the Anthropological Institute of Great Britain and Ireland, 15, 246–263. record
  • Gelman, A., Hill, J., and Vehtari, A. (2020). Regression and Other Stories. Cambridge University Press. record · 7 pages
  • James, G., Witten, D., Hastie, T., and Tibshirani, R. (2021). An Introduction to Statistical Learning with Applications in R (2nd ed.). Springer. record · 39 pages

Reviewed: verified, 23 Nov 2026 · citations checked, 23 Nov 2026 · editorial review, 23 Nov 2026 · 5 revisions