Three constructed trips cover 1, 2 and 3 kilometers and take 2, 3 and 5 minutes. Which straight line best describes how duration changes with distance? Many lines pass near the three points, so the question needs a precise meaning of “best”. This lesson measures how far a line misses each point, chooses the line with the smallest sum of squared misses, reads its slope and intercept with units, and uses R-squared to say how much of the variation in the response the line accounts for within the data.
Key terms
Predictor and response
Definition Regression data consist of pairs measured on the same individuals. The response is the variable to be described or predicted, and the predictor , also called the explanatory variable, is the variable used to describe or predict it. Simple linear regression summarizes how the response changes across values of one predictor by a straight line .
In plain words The predictor goes on the horizontal axis and the response on the vertical axis. The names describe roles in the analysis, not causes. Calling distance the predictor does not claim that distance produces the duration. Unless the points lie exactly on a line, swapping the two roles gives a different least-squares line, not the same line read backward.
Example For the three trips, distance in kilometers is the predictor and duration in minutes is the response, so the data are the pairs , and . The least-squares line found below has slope 1.5 minutes per kilometer. If distance is predicted from duration instead, the least-squares slope is kilometer per minute, and solving that line for duration gives a slope of minutes per kilometer, a different line.
Fitted value and residual
Definition For a line and observation , the fitted value is the height of the line at that observation’s predictor value. The residual of observation is
the observed response minus the fitted value, in the units of the response. It is positive when the point lies above the line, negative when it lies below and zero when it lies on the line. A residual is a vertical distance taken at the fixed value , not the perpendicular distance from the point to the line.
In plain words The fitted value is what the line says at a given predictor value, and the residual is how far the actual response is from it. The hat marks a value produced by the line, not a measurement.
Example The candidate line gives fitted values of 2, 3 and 4 minutes at 1, 2 and 3 kilometers. The observed durations are 2, 3 and 5 minutes, so the residuals are 0, 0 and 1 minute. The third trip took one minute longer than this line says.
Residual sum of squares
Definition For a line with intercept and slope , the residual sum of squares is
where . It is measured in squared response units. Squaring keeps positive and negative residuals from canceling, and it makes a large miss count much more than a small one.
In plain words Square every miss and add. A smaller total means that the line passes closer to the points in this vertical, squared sense. It is a chosen criterion, and other criteria, such as the sum of absolute residuals, can pick a different line.
Example For the candidate line , the residuals 0, 0 and 1 give squared minute. The horizontal line at the mean duration minutes has residuals , and . They add to zero, yet its SSE is squared minutes. A residual total of zero does not by itself show a good fit.
Least-squares line
Definition For pairs with at least two distinct predictor values, the least-squares line is the line whose intercept and slope make the SSE as small as possible. It is unique, and its coefficients are
where
Equivalently , which equals when . The line passes through the point , and its residuals satisfy and . These are the two equations obtained by setting the partial derivatives of the SSE with respect to and equal to zero. If all are equal, then , every line through gives the same smallest SSE, and the slope is not determined.
In plain words The slope compares how the two variables move together with how much the predictor varies, and the intercept then places the line through the point of means. Computing the line needs no assumptions about a population. Those enter only when the line is used for inference about one.
Example For the three trips, kilometers and minutes. The centered distances are , and , and the centered durations are , and . So and , which give
The fitted values at 1, 2 and 3 kilometers are , and minutes, and the residuals are , and minute. They add to zero, and the SSE is squared minute, below the value 1 of the candidate line .
Slope and intercept
Definition In the line , the slope is the change in the fitted value when increases by one unit, measured in response units per predictor unit. The intercept is the fitted value at , measured in response units. For a least-squares line both are summaries of the data. The slope compares the fitted values of individuals whose predictor values differ by one unit, and without a suitable design it should not be read as the effect of changing for any one individual. When lies outside the observed range of the predictor, the intercept comes from extending the line beyond the data and does not describe any observed situation. When is not a possible value, the intercept describes no real situation at all.
In plain words Read the slope with its units, as in “1.5 minutes per kilometer”, and read it as a comparison within the data. Read the intercept as the height of the line at zero, which may be a value nobody observed or could observe. Adding a constant to every response shifts the intercept by that constant and leaves the slope and the residuals unchanged. Changing the unit of the predictor rescales the slope but describes the same relationship.
Example For the trips, minutes per kilometer. Among these trips, a distance one kilometer greater goes with a fitted duration 1.5 minutes longer. That does not show that making a given trip one kilometer longer adds exactly 1.5 minutes. The intercept of minute is the value of the line at 0 kilometers, outside the observed range of 1 to 3 kilometers, so it is a feature of the fitted line rather than an estimate of how long a trip of length zero takes. If every duration were 10 minutes longer, the intercept would become minutes while the slope and the residuals stayed the same. With distance in meters the slope would be minute per meter.
R-squared
Definition The total sum of squares is the SSE of the horizontal line , which ignores the predictor. For a least-squares line with an intercept, fitted to data with ,
For such a fit , so , and is the fraction of the total sum of squares accounted for by the fitted values. With at least two distinct predictor values, , the square of the Pearson correlation. When all responses are equal, and the ratio is undefined.
In plain words R-squared compares the line with the simplest prediction, the mean response for everyone, and gives the share of that baseline’s squared error that the line removes within these data. It is an in-sample description. A high value does not show that a straight line is the right shape, that the predictor causes the response, or that new observations will be predicted as well. With two points at different predictor values, the line passes through both, so whenever the two responses differ.
Example For the trips, squared minutes and squared minute, so
Within these three trips, the line removes about 96.4 percent of the squared error of the mean-only prediction. For contrast, the points , , and lie on the curve . Their least-squares line has , yet its residuals , , and are positive at both ends and negative in the middle, the mark of a curve that a straight line misses.
Check yourself
- In another constructed data set of trips from 2 to 10 kilometers, the least-squares line is , with in kilometers and in minutes. Interpret the slope, and find the fitted value and the residual for a 6 kilometer trip that took 21 minutes.
Show the answer
Among these trips, each additional kilometer goes with a fitted duration 2.5 minutes longer. This is a comparison within the data, not a claim about what lengthening one trip would do.
At the fitted value is minutes. The residual is minutes. It is positive, so the point lies above the line and the trip took 2 minutes longer than the line says.
- Use the trip line , fitted to distances from 1 to 3 kilometers, to predict the duration of a 4 kilometer trip. A new 4 kilometer trip took 7 minutes. Find the prediction error without refitting the line, and say what limits the trust in this prediction.
Show the answer
The prediction is minutes. The prediction error, observed minus predicted, is minute, so the trip took about 0.67 minute longer than predicted.
A distance of 4 kilometers lies outside the observed range of 1 to 3 kilometers, so this prediction is an extrapolation. The three trips cannot show whether durations keep rising by about 1.5 minutes per kilometer beyond 3 kilometers, and a line fitted to only three points could change a great deal with one more trip.
- Fit the least-squares line to the points , , and , and find the residuals and . A classmate reads as the percentage accuracy of future predictions from this line. Is that right?
Show the answer
The means are and . The centered values are , , and , and the centered values are , , and . So and , which give and . The line is .
The fitted values are 3.9, 5.3, 6.7 and 8.1, and the residuals are 0.1, 0.7, and 0.9, which add to zero. Their squares 0.01, 0.49, 2.89 and 0.81 add to , and the squared centered responses 4, 0, 1 and 9 add to , so
The classmate is wrong. The value 0.7 says that within these four points the line removes 70 percent of the squared error of predicting every response by the mean 6. It is not a percentage of correct predictions, and because it is computed from the same points used to fit the line, it says nothing certain about how well the line will predict new observations. With only four points, a fairly high can also arise by chance.