Linear Regression: Fitting a Straight Line to Predict a Number
Definition
Linear Regression
A statistical method used to model the relationship between a dependent variable (also known as the response variable) and one or more independent variables (also known as predictor variables).
Linear regression predicts a continuous outcome, a number that can take any value in a range, such as a house price, a temperature or an exam score.
This is what separates it from classification, which predicts a discrete label such as "spam" or "not spam".
The model works by finding the straight line that comes closest to the data points, then reading predictions off that line.
Analogy
Think of plotting the size of 200 houses against their selling price on graph paper.
The points form a rough upward band rather than a neat line.
Linear regression draws the single straight line that sits closest to all of them, and that line becomes your price predictor.
The Two Kinds of Variable
The independent variable ($X$) is the predictor, the input you already know and feed into the model.
The dependent variable ($Y$) is the response, the outcome you want the model to produce.
The names carry an assumption: $X$ is treated as influencing $Y$, and $Y$ is not treated as influencing $X$.
In the house example, floor area is the independent variable and selling price is the dependent variable.
A model can take several independent variables at once, such as floor area, number of bedrooms and distance to a station, all predicting one price.
Common Mistake
Swapping the variables round gives a different model, not the same model read backwards.
Predicting price from floor area and predicting floor area from price are two separate regressions with two different lines.
The Regression Equation and What Each Term Does
The simple form of the equation is: $$ Y = \beta_0 + \beta_1X + \epsilon $$
$Y$ is the dependent variable the model predicts.
$X$ is the independent variable supplied as input.
$\beta_0$ is the intercept, the value of $Y$ when $X$ is zero.
$\beta_1$ is the slope, the change in $Y$ for a one-unit increase in $X$.
$\epsilon$ is the error term, the part of $Y$ that the straight line cannot account for.
Training the model means choosing values of $\beta_0$ and $\beta_1$ that make the errors as small as possible across the whole data set.
Note
Linear regression assumes the relationship between $X$ and $Y$ is linear, so a fixed change in $X$ always produces the same change in $Y$.
If the true relationship curves, a straight line will under-predict at some values of $X$ and over-predict at others.
Reading the Slope and the Intercept
The slope $\beta_1$ tells you both the direction and the steepness of the relationship.
A positive slope means $Y$ rises as $X$ rises, and a negative slope means $Y$ falls as $X$ rises.
A slope of 1200 in a house-price model means each extra square metre adds 1200 to the predicted price, holding everything else fixed.
The intercept $\beta_0$ is where the regression line crosses the y-axis.
It is the model's prediction when every independent variable is zero.
Common Mistake
The intercept often has no sensible real-world reading, and that does not make the model wrong.
A house with zero floor area or an employee with zero years of experience is not a real case, but the intercept still positions the line correctly over the range of data you do have.
Judging How Well the Line Fits: $r^2$
Model fit is how closely the predicted values sit to the actual observed values.
The gap between an observed value and the value the line predicts for it is called a residual.
The standard measure of fit is the coefficient of determination, written $r^2$, which reports the share of the variation in $Y$ that the model accounts for.
The formula is: $$ r^2 = 1 - \frac{SS_{res}}{SS_{tot}} $$
$SS_{res}$ is the sum of squared residuals, which measures how far the observations fall from the regression line.
$SS_{tot}$ is the total sum of squares, which measures how far the observations fall from the mean of $Y$.
An $r^2$ of 0 means the line explains none of the variation, so it is no better than always predicting the mean.
An $r^2$ of 1 means the line passes exactly through every data point.
An $r^2$ of 0.85 in the house-price model means floor area accounts for 85% of the variation in price, leaving 15% to other factors.
Common Mistake
A high $r^2$ does not by itself prove the model is good.
Adding more independent variables always pushes $r^2$ up, even when those variables carry no real information.
A model can score a high $r^2$ on its training data and still fail on new data, which is overfitting.
Where Linear Regression Is Used
Sales forecasting uses past advertising spend to predict next quarter's revenue.
Energy modelling uses outdoor temperature to predict a building's heating demand.
Sports analytics uses training load to predict an athlete's finishing time.
In each case the target is a number on a scale, which is the signal that regression rather than classification is the right tool.
Active recall
What makes an outcome continuous rather than categorical, and why does that decide between regression and classification?
In the equation $Y = \beta_0 + \beta_1X + \epsilon$, what does each of the four terms represent?
A regression of exam score on hours revised gives a slope of 3.2. What does that number mean in words?
What does an $r^2$ value of 0.4 tell you about the model?
Why can a model with a high $r^2$ still make poor predictions on new data?