Three constructed delivery routes of 0, 10 and 20 kilometers took 1, 2 and 2 hours. No straight line passes through all three points, yet a line that predicts duration from distance is still useful, provided we can say which line is best and how far to trust it. This lesson fits that line by least squares, derives its slope and intercept, reads its residuals, and marks where a prediction stops being supported by the data.
Key terms
Simple linear regression
Definition Simple linear regression predicts a numerical target from one numerical feature with the line
where the intercept and the slope are parameters fitted to training pairs . When the line is read as a model of the population, it says that the conditional mean is a straight-line function of . That is an assumption, and a fitted line does not prove it. Computing the least-squares line needs no assumption about the distribution of the errors, in particular no Gaussian assumption. Such assumptions matter only for particular statements about uncertainty, such as confidence intervals.
In plain words One input, one output and a straight line between them. Finding the best line is a calculation on the data that works for any data with at least two different feature values. Whether a line describes the relationship well, and whether it predicts new cases well, are separate questions.
Example In the constructed data, is route distance in tens of kilometers and is duration in hours, with observations , and . The intercept is then in hours and the slope in hours per ten kilometers. A route of distance zero can still take time for loading and handling, so a nonzero intercept is not absurd here.
Fitted value and residual
Definition For a line , the fitted value of training case is . On this page the residual is the fitted value minus the observed target,
so a positive residual means the line overpredicts and a negative one means it underpredicts. Many texts, including the loss examples of Loss Functions and Empirical Risk, use the opposite sign . Squared residuals are the same under both conventions, but the meaning of a positive residual flips. A residual is measured vertically, at the case’s own feature value, not as the perpendicular distance from the point to the line. The same difference for a case that was not used in fitting is a prediction error on new data, not a residual.
In plain words The fitted value is what the line says at a training point, and the residual says by how much and in which direction the line missed that point.
Example Try the candidate line on the three routes. Its fitted values are 1, 1.5 and 2 hours, so its residuals are , and hours, and it underpredicts the 10-kilometer route by half an hour. Its squared residuals add to 0.25. At the point the vertical residual has size 0.5, while the perpendicular distance from the point to the line is about 0.45. Least squares uses the vertical one, because it compares the prediction and the observation at the same distance.
Least squares
Definition The least-squares line is the line whose intercept and slope minimize the sum of squared residuals
For any fixed slope , setting the derivative with respect to to zero gives , so the best intercept is . With that intercept every residual equals , and
where and
If , which holds exactly when at least two feature values differ, this is an upward parabola in , and the unique least-squares line has
The line passes through , and its residuals satisfy and , which are the two first-order conditions for and . These sums describe the fit and say nothing about whether the errors are independent. If , every line through gives the same smallest SSE, so the slope is not determined.
In plain words For any slope, the best intercept puts the line through the point of averages. What remains is the tilt, and the best tilt compares how the two variables move together with how much the feature varies on its own. If the feature never varies, the data say nothing about the tilt.
Example For the three routes the means are and . The centered distances are , and , and the centered durations are , and , so and . The slope is and the intercept is . The fitted values are , and hours, and the residuals are , and hour. They add to zero, their sum weighted by distance is also zero, and the SSE is , smaller than the 0.25 of the candidate line. The MSE is squared hour. A few lines of NumPy reproduce the fit.
import numpy as np
x = np.array([0.0, 1.0, 2.0]) # distance, tens of km
y = np.array([1.0, 2.0, 2.0]) # duration, hours
w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean()) ** 2)
b = y.mean() - w * x.mean()
r = (b + w * x) - y # residuals, fitted minus observed
print(b, w) # 1.1666666666666667 0.5
print(r.sum(), (x * r).sum()) # both zero up to rounding, about 1e-16Slope and intercept
Definition In a fitted line , the slope is the change in the predicted value when increases by one unit, measured in target units per feature unit. The intercept is the predicted value at , measured in target units. Both describe the fitted model. The slope compares predictions for cases whose feature values differ by one unit. It is not by itself the effect of changing for a given case, which needs an experiment or a separate causal argument. When lies outside the observed range of the feature, the intercept is a value of the extended line rather than a prediction the data support.
In plain words Read the slope with its units, as a rate inside the model. Read the intercept as the height of the line at zero, a situation the data may or may not cover. A pattern of association in the data is not the same as what would happen after an intervention.
Example The fitted slope of hour per ten kilometers, which is 3 minutes per kilometer, says that among these routes a route 10 kilometers longer has a predicted duration half an hour longer. It does not show that adding 10 kilometers to a particular route adds half an hour, because longer routes may also differ in road type, stops or traffic. The intercept of hours, or 70 minutes, is the predicted duration at distance zero, and here the value was observed. For the routes , and , by contrast, the least-squares line is . Its intercept 0 does not show that a delivery over no distance takes no time, since no route shorter than 10 kilometers was observed.
Extrapolation
Definition Using a fitted model at a feature value outside the range of the training feature values is extrapolation. For one feature the range is the interval from the smallest to the largest observed , and a prediction inside it is an interpolation. Inside the range the prediction rests on data on both sides, although a large gap in the observed values can still leave it poorly supported. Outside the range the prediction relies entirely on the line continuing, which the data cannot check.
In plain words Inside the observed range, the line fills in between known cases. Beyond it, the line keeps going only because the formula does, and nothing in the data says the relationship stays straight. Neither kind of prediction is guaranteed, but an extrapolation carries an extra assumption that the data cannot test.
Example The line was fitted to routes of 10 to 30 kilometers, so the observed range is . At , a 25-kilometer route, it predicts 3.75 hours, an interpolation. At , a 50-kilometer route, it predicts 7.5 hours, an extrapolation. A longer route may involve a highway, a driver break or a different vehicle, and the three routes contain no information about any of that. The intercept at is itself an extrapolation for these data.
Check yourself
- A line fitted to constructed routes is , with in tens of kilometers and in hours. A route of 30 kilometers took 2.5 hours. Find its fitted value and its residual, fitted minus observed, and say whether the line overpredicts or underpredicts this route.
Show the answer
The distance is , so the fitted value is hours. The residual is hour. It is positive, so the line overpredicts this route by 0.4 hour, or 24 minutes.
- Fit the least-squares line to the constructed routes , , and , in the same units. Check the two residual sums, predict the durations at and at , and say which prediction is an extrapolation. Does the slope show what adding 10 kilometers to a given route would do?
Show the answer
The means are and . The centered distances are , , and , and the centered durations are , , and . So and , which give and . The line is .
The fitted values are 1.9, 3.3, 4.7 and 6.1 hours, so the residuals, fitted minus observed, are , , and . They add to zero, and their sum weighted by distance is . The SSE is 0.2 squared hour.
At the prediction is hours, an interpolation inside the observed range from 1 to 4. At , a 100-kilometer route, it is hours, an extrapolation far beyond the longest observed route of 40 kilometers.
The slope of 1.4 hours per ten kilometers describes how predicted duration differs between routes of different lengths in these data. It does not show what lengthening one particular route would do, because routes of different lengths may differ in other ways too.
- Three constructed routes all have , with durations of 2, 4 and 6 hours. What goes wrong with the slope formula? Find the smallest possible SSE and every line that reaches it. What can these data say about the duration at ?
Show the answer
All centered distances are zero, so and the formula for divides by zero. Every line predicts the same value for all three routes, and the SSE is smallest when that value is the mean duration 4, giving . Every line with reaches this minimum, for example , and . These are exactly the lines through the point .
At those three lines predict 4, 6 and 2 hours, and other minimizing lines predict any other value, so the data say nothing about the duration at . More routes of the same distance would not help. Only routes of a different distance can determine the slope.