← All Posts
Machine Learning · Regression · Linear Regression · Linear Track

Linear Regression: Overview and Roadmap

Linear regression estimates a continuous number, such as price, demand, or temperature, from one or more predictors. It gives each predictor a coefficient, adds an intercept, and leaves a row-specific error for variation the model does not explain:

$$y_i=\beta_0+\beta_1x_{i1}+\cdots+\beta_px_{ip}+\varepsilon_i.$$

Writing the same equation for all $n$ rows gives the compact matrix form

$$y = X\beta + \varepsilon,$$

where $X\beta$ is the part explained by the model and $\varepsilon$ is the unexplained part. The first column of $X$ contains ones, so its coefficient becomes the intercept $\beta_0$. The word linear means linear in the coefficients. The columns of $X$ may still contain squared terms, interactions, or other prepared features.

SymbolMeaning
$n$number of data rows
$p$number of predictors, not counting the intercept
$X$all model inputs, arranged as $n$ rows and $p+1$ columns; the first column contains ones
$\beta$unknown true coefficients, including the intercept
$\hat{\beta}$coefficients estimated from the available sample
$\varepsilon = y-X\beta$unobservable differences from the true population regression function
$\hat{y} = X\hat{\beta}$predictions made by the fitted model
$e = y-X\hat{\beta}$observable differences between the data and the fitted predictions
Linear regression augmented design matrix An n by p plus one matrix with a leading column of ones for the intercept, followed by predictor columns x1 through xp. X = n x (p + 1) bias x1x2...xp 1x11x12...x1p 1x21x22...x2p :::...: 1xn1xn2...xnp intercept / bias column p predictor columns
Implementations often receive a raw $n\times p$ feature matrix and prepend the ones column internally. The augmented matrix then has $p+1$ columns and multiplies $(\beta_0,\beta_1,\ldots,\beta_p)^\top$.
Intercept, error, and residual are different quantities. The intercept $\beta_0$ shifts the whole fitted line or plane. The error $\varepsilon_i$ is the unknown gap between row $i$ and the true population relationship. The residual $e_i$ is the known gap between row $i$ and the fitted relationship. The ones column creates the intercept; it does not replace the error term.

From the Question to a Checked Model

A useful regression workflow has four stages: decide what to estimate, fit the model, test its predictions, and inspect why it may fail.

  1. Target and inputs. The target may be a future average, a descriptive relationship, or a causal effect supported by a suitable study design. Each row of $X$ contains the intercept, predictors, and any justified transformations. These choices determine what the model means and what shapes it can represent.
  2. Coefficient fitting. Ordinary least squares chooses the coefficient vector that makes the squared sample errors as small as possible: $$\hat\beta=\arg\min_b\sum_{i=1}^{n}(y_i-x_i^\top b)^2 =\arg\min_b (y-Xb)^\top(y-Xb).$$ Differentiation gives $X^\top X\hat\beta=X^\top y$. Independent columns give a unique solution. QR or SVD is usually safer than explicitly forming an inverse.
  3. Prediction checks. Predictions are $\hat y=X\hat\beta$, and residuals are $e=y-\hat y$. Training error measures fit to known rows. Held-out RMSE, MAE, and $R^2$ measure performance on unseen rows. A large train-test gap suggests overfitting, leakage, instability, or distribution shift.
  4. Diagnosis and revision. Residual plots reveal curvature or changing error spread. Leverage and Cook's distance flag influential rows. Condition numbers and VIF expose unstable coefficients. Standard errors and confidence intervals are reported only when their assumptions are reasonable. Failed checks may require different features, uncertainty estimates, or estimators.

A failed check sends the workflow back to the target, feature design, or fitting method.

What Must Be Checked

Loss Functions Choose the Optimal Estimator

For a fixed input $x$, a loss function decides which summary of $Y\mid X=x$ is optimal. The table shows the population minimizer

$$a^*(x)=\arg\min_a E[L(Y,a)\mid X=x].$$
LossFormulaOptimal estimatorEstimator formula
Squared error$L(y,a)=(y-a)^2$Conditional mean$a^*(x)=E[Y\mid X=x]$
Absolute error$L(y,a)=|y-a|$Conditional median$a^*(x)=F^{-1}_{Y\mid X}(0.5\mid x)$
Pinball loss$\rho_\tau(y-a)=(y-a)\big(\tau-\mathbf1\{y<a\}\big)$Conditional $\tau$ quantile$a^*(x)=F^{-1}_{Y\mid X}(\tau\mid x)$
Asymmetric squared loss$|\tau-\mathbf1\{y<a\}|(y-a)^2$Conditional $\tau$ expectile$\tau E[(Y-a)_+\mid x]=(1-\tau)E[(a-Y)_+\mid x]$
Huber loss$\frac12r^2$ for $|r|\le\delta$; $\delta(|r|-\frac12\delta)$ otherwiseHuber robust location$E[\operatorname{clip}(Y-a,-\delta,\delta)\mid x]=0$
0-1 classification loss$L(y,a)=\mathbf1\{y\ne a\}$Most probable class$a^*(x)=\arg\max_k P(Y=k\mid X=x)$
Binary log-loss$-y\log p-(1-y)\log(1-p)$Conditional probability$p^*(x)=P(Y=1\mid X=x)$
Brier loss$L(y,p)=(y-p)^2$Conditional probability$p^*(x)=P(Y=1\mid X=x)$

With no predictors, the word “conditional” disappears: squared error gives the mean, absolute error gives the median, and pinball loss gives a quantile.

▶ One Sample, Different Optimal Summaries

For values $(1,2,3,8,9)$, each step changes the loss and marks its optimal constant prediction.

A Small Worked Fit

The simplest baseline predicts the sample mean $\bar y$ for every row. The Total Sum of Squares (SST) measures how much the observations vary around that baseline:

$$\text{SST} = \sum_i (y_i - \bar{y})^2.$$

After fitting a line, SST splits into the Regression Sum of Squares (SSR), also called explained variation, and the Error Sum of Squares (SSE), also called residual variation:

$$\text{SST} = \text{SSR} + \text{SSE}, \qquad \text{SSR}=\sum_i(\hat{y}_i-\bar{y})^2, \qquad \text{SSE}=\sum_i(y_i-\hat{y}_i)^2,$$

Therefore $R^2=\text{SSR}/\text{SST}$ is the fraction explained by the line. With one predictor, $R^2$ also equals the squared sample correlation $r^2$ between $x$ and $y$.

Notation note. This series uses SSR for Regression Sum of Squares. Some books use SSR to mean “sum of squared residuals”; in this series the residual quantity is called SSE or RSS.

▶ Raw Data to Estimand, OLS, Decomposition, and Inference

All displayed values are calculated from $x=(1,\ldots,6)$ and $y=(2.2,3.8,5.1,5.6,7.4,8.3)$. Each step advances one analysis stage and keeps the fitted and residual geometry visible.

The data behind the animation is the six points $x=(1,\ldots,6)$ and $y=(2.2,3.8,5.1,5.6,7.4,8.3)$, which give $\text{SST}=25.34$, $\text{SSE}=0.379$, $\text{SSR}=24.96$, and $R^2=0.985$. The sample correlation on the same data is $r=0.9925$, and $r^2=0.985$ to three decimal places, matching $R^2$ exactly. This equivalence is specific to simple regression with one predictor; with multiple predictors, $R^2$ still measures explained variance but no longer equals the square of a single pairwise correlation.

Interview trap. A high $R^2$ is sometimes read as proof that the model is correctly specified. $R^2$ only measures the fraction of variance explained by whatever functional form was fit; a linear model can achieve a high $R^2$ on data with genuine curvature if the curvature is mild relative to the noise, while still producing systematically biased predictions at the ends of the observed range. $R^2$ says nothing about whether the errors are unbiased, homoscedastic, or independent, which is exactly why residual diagnostics exist as a separate check.

Worked check. Here $\bar x=3.5$, $\bar y=5.4$, $S_{xx}=17.5$, and $S_{xy}=20.9$, so $\hat\beta_1=1.1943$ and $\hat\beta_0=1.2200$. The residual degrees of freedom are $6-2=4$, $\hat\sigma=\sqrt{0.3794/4}=0.3080$, and $\operatorname{se}(\hat\beta_1)=\hat\sigma/\sqrt{S_{xx}}=0.0736$. A zero-variance predictor, a duplicated column, or $n\le p+1$ blocks the usual unique fit or residual variance estimate.

Roadmap for This Track

The twelve published chapters move from the basic meaning of the model to fitting, inference, numerical methods, diagnostics, and production use.

The matching implementation series, built from scratch in standard-library C++ with an explicit fit/predict/evaluate lifecycle, starts at From Scratch · C++ · Linear Regression, including a compilable ordinary-least-squares solver and comparisons against QR and SVD based alternatives.