INTERACTIVE STATISTICS & MACHINE LEARNING
TEMPORARY RENDERING VERSION
Part VI · Regression

Finding the Invisible String

The string nobody can see, but everyone can feel.

Temporary web chapter for rendering tests Based on the uploaded book PDF, Part VI

The string nobody can see, but everyone can feel

A cricket coach has years of data on his bowlers: hours of practice per week, and average bowling speed. Plotted as a scatter plot, the dots trend upward — more practice, generally, though not perfectly, goes with higher speed. He wants more than “they seem related.” He wants to hand a new trainee a number of practice hours and get a prediction of what speed to expect.

The dots do not sit on a perfect line. Real relationships rarely do. Instead, they cluster into a rough diagonal band, as though a string runs through the cloud, taut between the bottom-left and the top-right, with every dot tugging gently at it.

The question is precise: among every possible straight line that could be drawn through this cloud, which one is actually the best one? “Looks about right” is not a method. We need something a machine can check without ever looking at the picture.

For a candidate line, each observation has a gap between its observed value and the value predicted by the line. That gap is the residual. Positive residuals sit above the line; negative residuals sit below it.

Least squares: finding the best line

Regression applies the same basic idea used earlier to understand variance: measure deviations, square them so positive and negative errors cannot cancel, and add them together. Here the “centre” is an entire line rather than a single number.

For every candidate line, calculate every residual, square each residual, and add the squared residuals. Somewhere among all possible slopes and intercepts is one line with the smallest possible total.

The math

For a fitted line

ŷ = b₀ + b₁x

the residual for observation i is

eᵢ = yᵢ − ŷᵢ

and least squares chooses the coefficients that minimise

Σ(yᵢ − ŷᵢ)²

Squaring makes large misses count more heavily and prevents positive and negative residuals from cancelling each other.

Figure 6.1 — The least-squares line and residuals
Temporary image slot. The final website can insert the book figure here.
Figure 6.1 — The least-squares line and residuals: the string nobody can see.

Every straight line is fully described by two numbers. The slope tells you how steeply it rises; the intercept anchors the line. In the chapter's cricket example, the illustrative relationship is approximately:

speed = 118 + 0.8 × hours

The slope of 0.8 means that each additional hour of weekly practice is associated with an average increase of about 0.8 km/h in predicted bowling speed across this dataset.

Try it: Least Squares Drag

Adjust slope and intercept. Try to beat the least-squares solution.

Key idea

Least squares is not a matter of drawing the line that looks nicest. Given the same data and the same procedure, the minimisation produces the same fitted line.

The warning label everyone skips

Warning

A regression line describes association: how two variables move together in the data actually collected. It does not, by itself, establish that one variable causes the other.

Imagine noticing that whenever an ice-cream truck's jingle plays, more people are wearing shorts. Does the jingle cause people to wear shorts? No: summer is a hidden variable producing both observations.

A clean regression relationship can therefore coexist with confounding, reverse causation, or other explanations. Naturally gifted bowlers might practise more and also bowl faster; coaching access might influence both.

Establishing genuine causation requires experimental machinery such as randomised assignment and control groups, or dedicated causal-inference methods. A strong fit is not proof of causation.

How good is “good,” really?

A least-squares line exists even when the underlying variables have little useful relationship. The fitted line therefore needs a second number that describes how much predictive variation it accounts for.

R-squared can be understood as the fraction of the original prediction error — the error made by simply guessing the overall mean for everyone — that the fitted line removes.

The math
R² = 1 − SSE / SST

If predicting every observation with the mean produces a total squared error of 1,000, while the fitted line leaves 300, then R² = 0.70. The model has removed 70% of the baseline squared error, leaving 30% unexplained by this simple model.

R² is therefore not “70% accurate.” It describes explained variation relative to a baseline predictor. The remaining variation can reflect talent, technique, conditioning, measurement noise, omitted predictors, and other factors not represented by the model.

Where the word “regression” actually came from

The name comes from Francis Galton's work on the heights of parents and their adult children. Exceptionally tall parents tended to have children who were taller than average, but somewhat shorter than their exceptionally tall parents. Exceptionally short parents showed the corresponding movement back toward the population average.

Galton called this pattern “regression toward mediocrity,” later softened to “regression toward the mean.”

The phenomenon is not a biological force pulling extreme values toward the middle. It follows naturally from imperfect correlation: an extreme value of one variable tends, on average, to be paired with a less extreme value of the other.

Clinical intuition

A patient treated because symptoms happen to be at an unusually severe peak may improve afterward partly because extreme measurements tend to be followed by less extreme measurements. Without an appropriate control group, regression to the mean can masquerade as a treatment effect.

When the line breaks its own promise

A straight-line model is inappropriate when the outcome is constrained. Probability, for example, must remain between 0 and 1. A linear model can predict impossible values such as 1.15.

Logistic regression solves this by modelling a transformed quantity, log-odds, on an unrestricted linear scale and then converting the result back to probability with the logistic function.

The math
log(p / (1 − p)) = b₀ + b₁x

The logistic transformation maps the unrestricted linear predictor back into the valid probability range from 0 to 1.

Figure 6.3 — Logistic S-curve
Temporary image slot. The final website can insert the book figure here.
Figure 6.3 — Logistic S-curve: why probabilities need a curve, not a line.

Logistic coefficients are commonly interpreted through odds ratios. An odds ratio of 1.15 for age means each additional year multiplies the odds of the outcome by 1.15, while the corresponding change in raw probability depends on the starting probability.

Linear regression

Continuous, unbounded outcome; ordinary straight-line prediction.

Logistic regression

Binary outcome; linear predictor operates on the log-odds scale.

The number the line was hiding: Pearson's r

Before fitting a line, we may want to ask how strongly two variables move together and in which direction. Pearson's correlation coefficient, r, is a unitless measure ranging from −1 to +1.

The math
r = Σ[(xᵢ − x̄)(yᵢ − ȳ)] / √[Σ(xᵢ − x̄)² Σ(yᵢ − ȳ)²]

The sign indicates direction; the magnitude indicates the strength of a linear relationship.

Figure 6.4 — Pearson r gallery
Temporary image slot. The final website can insert the book figure here.
Figure 6.4 — Pearson r: strong positive, no linear relationship, and strong negative correlation.

For a simple one-predictor linear regression, R² is the square of Pearson's correlation. Squaring removes the sign, so R² alone cannot tell you whether the relationship is positive or negative.

When one string isn't the whole story

Real prediction problems rarely have a single useful predictor. A medical researcher predicting blood pressure might need age, weight, sodium intake, family history, and other variables simultaneously.

In multiple regression, each coefficient describes the predictor's contribution holding the other included predictors fixed. This is a different quantity from the coefficient in a simple regression.

The math
ŷ = b₀ + b₁x₁ + b₂x₂ + ··· + bₖxₖ

Adding predictors always increases or leaves unchanged the training R², even if the new variables are useless. Adjusted R² therefore penalises model complexity and only rises when an additional predictor improves the fit enough to justify its inclusion.

Multicollinearity

When predictors are strongly correlated with each other, the model can struggle to distinguish their individual contributions. Coefficients may become unstable. The variance inflation factor (VIF) is a standard diagnostic.

One idea, wearing three different outfits

The chapter's broader lesson is that regression does not need to be reinvented for every kind of outcome. A generalized linear model combines three components:

Random component

The probability distribution appropriate for the outcome.

Systematic component

A linear combination of predictors.

Link function

The transformation connecting the linear predictor to the valid outcome scale.

Examples

Linear regression, logistic regression, and count models use the same structural recipe with different outcome assumptions and links.

For counts, which cannot be negative, a log link can be used. The model works on the logarithm of the expected count and exponentiates the result, ensuring the predicted count remains positive.

This is one of the chapter's bridges toward machine learning: the model's output transformation is often what converts an unconstrained internal computation into a valid probability, count, or class.

Four datasets, one perfect disguise

In 1973, Francis Anscombe constructed four datasets with nearly identical summary statistics: the same means, variances, regression line, and R². Yet their actual structures are radically different.

Figure 6.7 — Anscombe's Quartet
Temporary image slot. Insert the book's figure for the final website.
Figure 6.7 — Anscombe's Quartet: identical summary statistics, different underlying stories.

One dataset is genuinely linear. Another is curved. A third is dominated by an influential outlier. The fourth has almost all its x-values clustered together, with one point determining the apparent slope.

The lesson is not that regression is unreliable. It is that a fitted model and its diagnostic checks are two halves of one complete practice.

Diagnostic habit

Look at the residual plot. A well-fitting model should leave a roughly patternless cloud around zero. Curves, funnels, or influential observations are signals that the numerical summary is hiding structure.

Anscombe Quartet

Compare datasets that share nearly identical regression summaries.

Least Squares Drag

Move a candidate line and see its squared residual cost.

Logistic Curve

See why binary outcomes need a bounded S-shaped response.

Pearson r

Explore direction and strength of linear association.

Multicollinearity

See what happens when predictors overlap.

Try it yourself: Part VI

Practice module

  1. A cricket coach fits speed = 118 + 0.8 × hours. What does 0.8 mean in plain language?
  2. The coach's model has R² = 0.7. What does this number tell you, and what does it not tell you?
  3. Why did Galton observe regression toward the mean without invoking a biological “correction” force?
  4. A sports team fires its coach after an unusually bad losing streak and then improves. Why might the improvement have nothing to do with the new coach?
  5. Two datasets have R² = 0.7, but one has r = +0.84 and the other r = −0.84. What differs?
  6. A blood-pressure model has an age coefficient of 0.9. After adding weight, it becomes 0.5. Has age stopped mattering? Explain.

Explore further

  1. Construct a small fictional dataset matching speed = 118 + 0.8 × hours and R² = 0.7.
  2. Construct another dataset with the same regression line and R² but an obviously curved relationship.
  3. Design a scenario in which regression to the mean creates an apparent improvement without a real intervention effect.
  4. Explain why logistic regression uses log-odds rather than probability directly.
  5. Simulate age and weight correlated at 0.6, fit simple and multivariable regression, then repeat at 0.9.