What a Model Learns separated prediction from inference. Linear regression is the method where the two meet most clearly: it predicts, and its coefficients come with standard errors, confidence intervals and p-values that say how much to trust them. Those numbers are also some of the most often misread in all of statistics.

This post fits linear regressions to a real data set, the fuel economy of 32 cars from 1974 (mtcars; Henderson & Velleman, 1981), and checks every interpretation against simulations in which the true coefficients are known: what “95% confidence” actually guarantees, what a p-value is and is not, why R² can mislead, and what happens to p-values when predictors are chosen by looking at them.


1. The Model

Simple linear regression assumes that the outcome is a straight-line function of one predictor, plus noise:

is the intercept (the mean of when ) and the slope (the change in the mean of per unit of ). Both are unknown constants, estimated from data as and ; a prediction is .

The running example: miles per gallon (mpg) against weight (in 1,000 lb) for the 32 cars of mtcars. Heavier cars use more fuel, and the question is how much more, and how sure we can be.


2. Least Squares

For a candidate line, the residual of observation is , its vertical distance from the line. Least squares (Legendre, 1805; Gauss, 1809) chooses the line that minimises the residual sum of squares,

which has a closed-form minimum:

For mtcars this gives and : 5.34 fewer miles per gallon for every extra 1,000 lb, on average. Try to beat it:

The RSS surface is a bowl with a single lowest point. It is a long, tilted valley, because the data sit far from weight 0: a higher intercept with a steeper slope fits almost as well, so the two estimates are strongly correlated. Centring the predictor removes this.

With several predictors, the same idea has a geometric form. Stack the outcomes into a vector and the predictors, with a column of ones, into a matrix . Every choice of coefficients gives a prediction vector in the space spanned by the columns of , and least squares picks the one closest to :

the plane of all predictions X·β (here: spanned by 1 and weight)yŷ = Xβ̂residual e = y − ŷ⟂ to the planeŷ is the point of the planeclosest to y: that is whatminimising the RSS = |e|²meansso the residuals are orthogonalto every predictor: Xᵀe = 0with an intercept they sumto zero, and ȳ lies on the linenormal equations:XᵀXβ̂ = Xᵀy

Two useful facts follow from the picture: the residuals are orthogonal to every predictor (, the normal equations ), and with an intercept they sum to zero, so the fitted line passes through .


3. What the Formulas Assume

Least squares can be computed for any data. The standard errors, intervals and p-values below, though, rest on assumptions about the noise:

AssumptionWhat breaks if it failsHow to check
Linearity: the mean of is linear in the predictorsthe coefficients describe the wrong shaperesiduals against fitted values show a pattern
Independent errorsstandard errors are too small (e.g. repeated measurements, time series)how the data were collected
Constant variance of standard errors are wrong where the spread differsthe residuals fan out
Normal errorsonly matters for exact small-sample intervals and testsa normal quantile plot of the residuals

Linearity is the one to check first. For mtcars, adding improves the fit (its coefficient has , and R² rises from 0.753 to 0.819): the drop in mpg flattens for heavy cars. A straight line is a useful summary, not the truth. Everything below takes the model as given, which is exactly the situation in which its intervals and p-values are valid.


4. How Precise Is the Slope? Standard Errors and Confidence Intervals

A different sample of 32 cars would give a different . The standard error measures how much it would vary:

The slope is estimated more precisely with less noise, more data and a wider spread of . For mtcars, , and the 95% confidence interval is

What does “95%” mean? It is tempting to say “the true slope lies in [−6.49, −4.20] with probability 95%“. In the classical (frequentist) framework this is not what the interval guarantees: the true slope is a fixed number, and this particular interval either contains it or does not. The 95% describes the procedure: computed on many samples, 95% of the intervals it produces contain the true value (Neyman, 1937). Below, 100 intervals at a time, from data with a true slope of 0.5:

(A statement of the form “95% probability that the slope lies in this range” is a Bayesian credible interval, which needs a prior distribution for the slope; with flat priors it often coincides numerically with the confidence interval, but it answers a different question.)

”± 2 standard errors” is an approximation. The exact multiplier is the 97.5% quantile of a t distribution with degrees of freedom, which is close to 2 only for moderate . Measured over 20,000 simulated data sets per row:

nExact t multiplier · coverageCoverage with ± 2 SE · ± 1.96 SE
53.182 · 95.0%85.8% · 85.1%
102.306 · 94.9%91.8% · 91.3%
302.048 · 95.0%94.4% · 93.8%
1001.984 · 94.9%95.1% · 94.6%

With 5 observations, a “95%” interval built with ± 2 SE misses the truth one time in seven.


5. Is There a Relationship? t-Tests and p-Values

To test the null hypothesis (no linear relationship) against , compute how many standard errors the estimate is from 0:

If is true (and the assumptions hold), follows a t distribution with degrees of freedom. The p-value is the probability, computed under , of a at least as far from 0 as the one observed. For mtcars, and : such a strong slope would almost never arise by chance if weight were unrelated to mpg.

Five things a p-value is not (Wasserstein & Lazar, 2016; Greenland et al., 2016):

  1. It is not the probability that is true. It is computed assuming is true. Turning it into needs a prior.
  2. It is not the size or importance of an effect. With 100,000 observations, a true slope of 0.02 (estimated 0.024), explaining 0.06% of the variance (R² = 0.00058), had .
  3. A large p-value is not evidence of no effect, only a lack of evidence for one; small samples often cannot detect real effects.
  4. “Significant at 5%” is a convention. When is true, p-values are uniform between 0 and 1: in 20,000 data sets with no effect, 4.97% had , and 0.97% had , exactly as designed.
  5. The thresholds for depend on the degrees of freedom. The often-quoted ” for 5%, for 1%” holds for about 30 degrees of freedom:
Degrees of freedom for p = 0.05 for p = 0.01
102.2283.169
302.0422.750
∞ (normal)1.9602.576
mpg ~ wt + hp, 32 cars: the row for weight (1,000 lb)wtestimate−3.878std. error0.633t value−6.13p-value1.1e−695% CI[−5.17, −2.58]estimatemean mpg is 3.88 lower per extra 1,000 lbamong cars with the same hp: an association,not the effect of adding weight to one carstd. errorhow much the estimate would varyacross new samples of 32 cars(it assumes the model is right)t valueestimate ÷ SE: 6.13 standard errorsaway from 0p-valueP(|T| ≥ 6.13) if the true slope were 0;not the probability that it is 0,and not the size of the effect95% CIbuilt so that 95% of such intervalscontain the true slope; this oneeither does or does not

In simple regression, the t-test of the slope and the test of the correlation coefficient are the same test: for mtcars both give , and .


6. How Well Does the Line Fit? RSE and R²

Two summaries describe the fit as a whole. The residual standard error estimates , the typical size of a residual, in the units of : 3.05 mpg for mtcars. The coefficient of determination

is the share of the variance of explained by the model: 0.753 for weight alone. Three properties are often forgotten:

  • R² never falls when a predictor is added, even a column of random noise: the least-squares fit can always ignore it, and usually uses it a little.
  • Adjusted R², , charges for every parameter and can fall.
  • R² lies between 0 and 1 only on the training data of a model with an intercept. On new data, a model that predicts worse than the mean of has negative R².

One real predictor plus columns of pure noise, 50 observations, averaged over 2,000 data sets:

Noise predictors kTraining R² · adjusted R²Test R² on new data
00.276 · 0.2610.232
100.425 · 0.2590.029
200.578 · 0.262−0.342
400.881 · 0.269−4.144

7. Multiple Regression

With predictors the model is , still fitted by least squares (now with matrix algebra). Adding horsepower to the mtcars model:

Coefficient (mpg ~ wt + hp)Estimate · SEt · p-value
Intercept37.23 · 1.6023.28 · 2.6 × 10⁻²⁰
Weight (1,000 lb)−3.878 · 0.633−6.13 · 1.1 × 10⁻⁶
Horsepower−0.0318 · 0.0090−3.52 · 0.0015

(RSE 2.59, R² = 0.827, adjusted 0.815; F = 69.2 with .)

Interpreting a coefficient. is the average difference in between observations that differ by one unit in and have the same values of the other predictors. The weight coefficient changed from −5.34 alone to −3.88 with horsepower in the model, because heavier cars also tend to be more powerful (): part of what weight “explained” alone was horsepower. This is the confounding of Describing Data, handled by putting the confounder in the model.

Two cautions that the phrase “holding the others fixed” hides:

  • It is an association, not an effect. Nothing in observational data says what would happen if one car were made heavier. Causal claims need an experiment, or explicit causal assumptions.
  • “Fixed” may not exist. When predictors are strongly correlated, data with one varying and the others fixed are rare, and the coefficient is estimated from little information.

The F-test: is any predictor useful?

tests in a single test. Individual t-tests cannot replace it: with many predictors, some will have small p-values by chance.

Collinearity

Adding displacement and the number of cylinders to the model, both strongly related to weight and horsepower, gives an F-test with , while three of the four predictors are individually “not significant” (displacement , horsepower 0.10, cylinders 0.06). The predictors carry overlapping information, so the data cannot tell them apart, and their standard errors grow. The variance inflation factor , where comes from regressing on the other predictors, measures this: 10.4 for displacement. Simulated, with two predictors of correlation ρ:

Correlation ρMean SE of β̂₁√VIF
00.1021.00
0.90.2342.29
0.990.7227.09

8. Correlation and Regression

In simple regression, a significant slope and a significant correlation are the same thing (identical p-values, above). In multiple regression they come apart. In this simulation , with and correlated at 0.7, so that ‘s direct effect and its path through cancel in the marginal correlation:

ModelCoefficient of p-value
correlation of and r = 0.006
y ~ x₂0.0050.93
y ~ x₁ + x₂−0.680 (true −0.7)3.9 × 10⁻³¹

is uncorrelated with and one of its strongest predictors: a suppressor variable (Conger, 1974). Screening predictors by their correlation with the outcome, before a multiple regression, would have thrown it away.

The two also answer different questions. The correlation is a unit-free strength of a linear relationship; the slope says how much changes per unit of and depends on the units of both. Standardising the variables makes slopes comparable across predictors.


9. Choosing Predictors, and Why p-Values Stop Working

With candidate predictors there are possible models. Common shortcuts are forward selection (start with none and add the most useful predictor while it helps), backward selection (start with all and remove the least useful; impossible when ) and mixtures of both. A widespread version decides “useful” by p-values: add a predictor while its p-value is below 0.05.

That makes the final p-values meaningless. At each step, the procedure picks the smallest of many p-values and reports it as if it were a single test. With 20 predictors of pure noise and 100 observations (2,000 data sets):

Forward selection, “enter if p < 0.05”Result
Data sets with at least one “significant” predictor64.8% (expected 1 − 0.95²⁰ = 64.2%)
Mean number of predictors selected1.06
Median reported p-value of the first one chosen0.020

Freedman (1983) made the same point with screening regressions. The fixes are to judge models by an estimate of their test error (a validation set or cross-validation, the resampling post; or AIC and BIC), to use methods designed for many predictors (the lasso), or, when inference after selection is the goal, methods built for it (Berk et al., 2013).


10. Categorical Predictors

A categorical predictor with levels enters the model as dummy variables (0/1 indicators); the level without a dummy is the baseline, represented by the intercept, and each dummy’s coefficient is the difference from it. With an intercept, one dummy per level would make the columns linearly dependent (the “dummy variable trap”). For mtcars, cylinders (4, 6 or 8) as two dummies, with 4 cylinders as the baseline:

mpg ~ wt + cylindersEstimate · p-valueMeaning
6 cylinders−4.26 · 0.005vs. a 4-cylinder car of the same weight
8 cylinders−6.07 · 0.001vs. a 4-cylinder car of the same weight
cylinders as a number−1.51 · 0.001assumes the steps 4 → 6 → 8 are equal

Coding the categories as the numbers 4, 6, 8 forces equal effects per step (here −3.0 per two cylinders), while the dummies find a larger first step (−4.26) than second (−1.82). For nominal categories, numeric codes are simply meaningless.


11. Predicting a New Value

A prediction has three sources of error: the estimated coefficients (reducible, shrinks with more data), the model’s form if it is wrong (bias), and the noise (irreducible). Two intervals capture the first and the third:

A 3,000 lb car (mpg ~ wt)95% intervalUncertainty about
Predicted mean mpg: 21.25[20.12, 22.38]the average of all 3,000 lb cars (confidence interval)
One particular car[14.93, 27.57]a single car, including its own noise (prediction interval)

The prediction interval is more than five times wider, and it does not shrink to zero with more data: it always includes .


12. Common Claims, Checked

ClaimVerdict
”The true value lies in the 95% confidence interval with probability 95%.”In the frequentist sense, the 95% belongs to the procedure: 95% of intervals computed this way contain the truth (95.0% in 20,000 simulations). One computed interval either does or does not. The probability statement is a Bayesian credible interval, which needs a prior.
”A 95% interval is β̂ ± 2 SE.”An approximation: the exact multiplier is a t quantile (3.18 at n = 5). With ± 2 SE, coverage was 85.8% at n = 5 and 91.8% at n = 10.
”t
”A small p-value means the null hypothesis is probably false.”A p-value is computed assuming the null is true; it is not the probability that it is.
”A small p-value means a strong effect.”A slope explaining 0.06% of the variance had p = 2.6 × 10⁻¹⁴ with 100,000 observations.
”A large t means the null hypothesis is wrong.”It means the data would be unusual if it were true. With 20,000 null data sets, 4.97% gave p < 0.05.
”R² is between 0 and 1, and higher is better.”Only on training data with an intercept. Training R² rose from 0.28 to 0.88 with 40 noise columns while test R² fell to −4.1. Use adjusted R² or test error.
”βⱼ is the effect of a one-unit increase in Xⱼ, holding the others fixed.”It is an association among observations with equal other predictors, not a causal effect, and is poorly determined when predictors are collinear (VIF 10.4 for displacement in mtcars).
”A significant regression requires a significant correlation.”True in simple regression (identical tests). In multiple regression, a predictor with r = 0.006 had t = −13.9 (a suppressor).
”Forward selection with p < 0.05 finds the relevant predictors.”On 20 noise predictors it reported at least one “significant” predictor in 64.8% of data sets. p-values after selection are not valid p-values.
”Numbers are fine for a categorical predictor.”Only if the steps are truly equal. Dummies for 6 and 8 cylinders found unequal steps (−4.26, then −1.82).
”The prediction interval shrinks with more data.”The confidence interval for the mean does; the interval for a single new value always includes the noise σ: [14.9, 27.6] vs. [20.1, 22.4] for a 3,000 lb car.

Summary

IdeaIn one line
Least squaresMinimise the RSS; the fit is a projection, residuals ⟂ predictors
AssumptionsLinearity, independence, constant variance (normality for small-sample exactness); check residuals
Standard errors and intervals95% is a property of the procedure; use the t quantile, not ± 2
p-valuesP(data this extreme | H₀), not P(H₀), not effect size
FitRSE in units of Y; R² only rises; adjusted and test R² do not
Multiple regressionCoefficients are conditional associations; F-test; collinearity inflates SEs
Selectionp-values after selection are not p-values; use test error
Categories and predictionK − 1 dummies; prediction intervals include the noise

Next: Classification: Logistic Regression, Discriminant Analysis and the ROC Curve: predicting a category, two routes to the class probability, a coefficient that flips sign, and why a low error rate can describe a useless classifier.

The experiments: linear_regression.py (numpy 2.1, scipy 1.18, seeded), with the mtcars data in mtcars.py; the mtcars results match the published values (mpg ~ wt: 37.285 − 5.345 wt, R² = 0.7528; mpg ~ wt + hp: R² = 0.8268). The lab computes its regressions in the browser; its two-sided t-test p-values agree with scipy to a relative error of 1.4 × 10⁻¹², and its standard error for horsepower in mpg ~ wt + hp reproduces 0.0090 (p = 0.00145). The simulated panels draw their own random numbers, so their values differ slightly from the seeded runs in the text.


References

Least squares and regression

  • Legendre, A.-M. (1805). Nouvelles méthodes pour la détermination des orbites des comètes. Firmin Didot.
  • Gauss, C. F. (1809). Theoria motus corporum coelestium in sectionibus conicis solem ambientium. Perthes & Besser.
  • Galton, F. (1886). Regression towards mediocrity in hereditary stature. Journal of the Anthropological Institute, 15.
  • Henderson, H. V., & Velleman, P. F. (1981). Building multiple regression models interactively. Biometrics, 37(2).

Inference

  • Student [Gosset, W. S.] (1908). The probable error of a mean. Biometrika, 6(1).
  • Fisher, R. A. (1925). Statistical Methods for Research Workers. Oliver and Boyd.
  • Neyman, J. (1937). Outline of a theory of statistical estimation based on the classical theory of probability. Philosophical Transactions of the Royal Society A, 236(767).
  • Wasserstein, R. L., & Lazar, N. A. (2016). The ASA statement on p-values: Context, process, and purpose. The American Statistician, 70(2).
  • Greenland, S., Senn, S. J., Rothman, K. J., Carlin, J. B., Poole, C., Goodman, S. N., & Altman, D. G. (2016). Statistical tests, P values, confidence intervals, and power: A guide to misinterpretations. European Journal of Epidemiology, 31(4).

Predictors, collinearity and selection

  • Conger, A. J. (1974). A revised definition for suppressor variables: A guide to their identification and interpretation. Educational and Psychological Measurement, 34(1).
  • Belsley, D. A., Kuh, E., & Welsch, R. E. (1980). Regression Diagnostics: Identifying Influential Data and Sources of Collinearity. Wiley.
  • Freedman, D. A. (1983). A note on screening regression equations. The American Statistician, 37(2).
  • Berk, R., Brown, L., Buja, A., Zhang, K., & Zhao, L. (2013). Valid post-selection inference. Annals of Statistics, 41(2).
  • Harrell, F. E. (2015). Regression Modeling Strategies (2nd ed.). Springer.

Textbooks

  • James, G., Witten, D., Hastie, T., & Tibshirani, R. (2021). An Introduction to Statistical Learning (2nd ed.). Springer.