Linear Regression
Linear Regression — Linear regression is a supervised learning algorithm that fits the best straight line through data to predict a continuous value, choosing the slope and intercept that minimize the squared error (ordinary least squares).
Linear regression finds the single straight line that gets as close as possible to all your data points at once. It’s the simplest model — and often the first one you should try.
- Data points
- Fitted line
- Residuals
- Outlier / current r²
Regression controls
The idea in plain words
Linear regression draws the one straight line that sits closest to all the points at once. “Closest” has a precise meaning: measure each point’s vertical distance to the line (its residual), square those distances so big misses count more, and pick the line that makes the total as small as possible.
Because every point pulls on that total, a single far-out leverage point can drag the whole line toward it — the same outlier sensitivity you can feel in loss functions. When there’s no closed form, the line is found with gradient descent.
Now, the math
The model is a straight line:
It minimizes the mean squared error, which has a closed-form slope:
- the predicted value (a point on the line).
- the slope — how much ŷ changes per unit of x.
- the intercept — the prediction when x = 0.
- the mean of the inputs and outputs.
▸ Show the derivation
Setting the derivative of MSE with respect to each β to zero yields the “normal equations,” whose solution is the closed form above. Because it depends on sums over every point, one extreme leverage point can shift the slope substantially — the fragility you can trigger by adding an outlier.
Trace it by hand
Four points: (1, 2), (2, 3), (3, 5), (4, 6). We fit the least-squares line with nothing but sums — every number below is exact, no rounding needed.
Step 1 — find the means
Step 2 — accumulate the slope's numerator and denominator
Each point contributes one product to each sum: the first point gives (1 minus 2.5) times (2 minus 4), which is 3, and so on.
Step 3 — slope and intercept
Solving the full normal equations with the repo's Cholesky solver returns exactly the same pair: intercept 0.5, slope 1.4.
Step 4 — check the residuals
The residuals are 0.1, minus 0.3, 0.3, minus 0.1 — they sum to zero, which is always true once an intercept is fit.
Step 5 — use the line
What just happened: Two sums fully determined the line: slope 7 over 5 equals 1.4 and intercept 0.5. No other line can beat its MSE of 0.05 on these four points — that is exactly what least squares means.
Now Break It
Try this: Add one extreme leverage outlier — it drags the entire regression line off course.
Control: Click to add a point far from the cluster
What happens: Leverage point detected! One outlier is pulling the entire line toward it, ruining the fit for everyone else.
Where linear regression is used
Linear regression is the workhorse behind an enormous range of practical predictions because it is fast, interpretable, and easy to audit. Epidemiologists use it to relate exposures like air pollution or smoking to health outcomes while adjusting for confounders such as age and income. Economists estimate how consumer spending responds to changes in interest rates, and real-estate platforms price homes from square footage, location, and bedroom count. In manufacturing, engineers fit sensor readings to product quality to catch drift on a production line. Marketing teams use it to attribute sales lift to advertising spend across channels. Because each coefficient has a clear per-unit meaning, linear regression is often preferred in regulated fields like finance and medicine where decisions must be explained.
Two misconceptions trip up beginners. First, people assume a high correlation or a good fit proves causation; in reality linear regression only quantifies association, and a strong coefficient can vanish once you add a confounding variable, so causal claims require experimental design or careful causal methods. Second, many think linear regression can only model straight lines. The model is linear in its parameters, not necessarily in the raw inputs, so you can fit curves by adding transformed features like logarithms or interaction terms. A common pitfall is ignoring the assumptions: influential outliers, correlated errors, and non-constant variance can all distort the fit, so residual plots should always be inspected before trusting the coefficients.
Frequently asked questions
What is linear regression in simple terms?
What does ordinary least squares actually minimize?
What assumptions does linear regression make?
How do I interpret a regression coefficient?
When should I not use linear regression?
Written & reviewed by the ML Visualization team · Last updated .