Theory
Linear regression and correlation
From paired data to the least-squares line, including residuals, correlation, and intervals for the slope, the mean response, and a new observation.
In a workshop, five pieces are recorded with the thickness of the material in millimeters and the time needed to cut it in seconds. From these data we want to find by how many seconds the cutting time increases on average for each additional millimeter of thickness, and what time to expect for a piece of given thickness. This chapter calculates the line that passes closest to the points (thickness, time). For the five pieces of the example used throughout the chapter, the line is , so each additional millimeter adds seconds on average. We then calculate two numbers that say how close the points are to that line, the coefficient of determination and the correlation .
The five pieces are a sample: five other pieces would give a slope different from . The second part of the chapter therefore answers the questions of statistical inference. We build an interval for the slope of the population, use a test to check whether the data are enough to conclude that the time really depends on the thickness, and calculate the interval in which the time of a new piece will fall with a given probability.
The chapter uses the mean and the standard deviation of Chapter 2. The slope calculated from the data corresponds to the slope of the regression line for two random variables, and the correlation corresponds to the coefficient , both seen in Chapter 7. The inference part uses the normal distribution of Chapter 6, the chi-square and Student’s t distributions of Chapter 8, the confidence intervals of Chapter 10, and the tests of Chapter 11. This is the last chapter of the course, and it applies to a line the tools that Chapters 8 to 11 built for a single mean.
Paired data and scatterplots
1. Paired data
In a workshop, the thickness of the material and the time needed to cut it are recorded for each piece. Each piece therefore provides two measurements, and data of this kind are called paired data. Let denote the pair observed on unit , with ; for piece , is the thickness and the time.
The regression of on looks for a function of , in this chapter a line, that gives the mean value of for each value of . In the workshop, the line gives the mean cutting time for each thickness. The variable , from which we start, is called the predictor; the variable , which we want to predict, is called the response. In the example the predictor is the thickness and the response is the time.
In a scatterplot, each pair is represented by a point in the plane, with on the horizontal axis and on the vertical axis. There is a positive association between and when the vertical coordinates tend to increase as we move through the points from left to right; there is a negative association when they tend to decrease. The scatterplot is used for three checks before any calculation.
- If the points lie around a line, a line describes well how the response varies on average.
- If they lie around a curve, a line describes the relationship poorly.
- If the height of the point cloud is similar at all predictor values, the spread of the response around its mean is approximately constant.
Reading five observations
For the pairs , , , , and , the last response is higher than the first, so the association is positive. Between and the response falls from to , so the increase holds for the points as a whole and not for every pair of neighboring points. A line describes the average trend and leaves a vertical distance to each observation.
Warning
Sorting the two columns separately pairs measurements from different units. For example, the pairs , , and show a decreasing relationship. Sorting just the vertical coordinates gives , , and , and the association becomes increasing. The means and variances of the individual columns are unchanged because each column contains the same numbers. Only the way those numbers are paired has changed.
2. Centered sums
For the five observations of the example, the means are and . Subtracting the means gives the deviations
The sum of the squared deviations of the predictor values is , and the same holds for the responses. The sum of the products of the deviations, pair by pair, is .
In general, let and denote the means of the horizontal and vertical coordinates, and call the differences and centered deviations. Define the centered sums
For the five observations, and .
The first two sums measure the spread of each variable around its mean: divided by , they give the corrected variances of Chapter 2. In the third, two deviations of the same sign contribute a positive term, while opposite signs give a negative term. Therefore when pairs with both measurements above the mean or both below predominate, as happens in a positive association. Divided by , gives the sample covariance, which equals for the five observations. The three sums are used to calculate the slope of the line, the coefficient , and the correlation of this chapter.
Remark
Given the totals , , and , one can calculate without writing the deviations, using the formula
The formula follows by expanding the product and using and :
For the five observations, , so . Setting gives .
When measurements are very large and close together, calculating from the deviations is safer. For the predictor values , , and , the numbers and become equal on a ten-digit calculator, which therefore gives , whereas the deviations , , give .
The least-squares line
1. The least-squares criterion
Consider the line for the five observations. At the predictor values the line gives the values , while the observed responses are . The vertical deviations, that is, observed response minus value of the line, are , , , , , and the sum of their squares is . Another line gives a different sum, and we look for the line for which this sum is as small as possible.
In general, for the line , write for the intercept and for the slope. At predictor value number , the line gives , so the vertical deviation from the observation is . The least-squares criterion chooses and to minimize the sum
Squaring gives positive contributions from deviations of opposite signs: two deviations and contribute , while their sum without squares would be .
The deviation is measured vertically, that is, along the response. If predictor and response are interchanged, the deviations become horizontal and the minimization problem changes, so the line found also changes.
2. The least-squares line with an intercept
Consider pairs with , that is, with at least two different predictor values. The minimum of is unique and occurs at
Write (read “y hat”) for the fitted value at any predictor value , that is, the value the fitted line gives at that predictor value. The line passes through the point of means because substituting gives .
Proof
The idea is to write as the sum of a term containing the intercept and a quadratic polynomial in the slope alone. Each deviation splits as
The first part sums to zero over , because and in the same way . The second part does not depend on . Expanding the square and summing over , the cross term equals times the sum of the first part, so it is zero. What remains is
The term is nonnegative and equals zero only for . The quadratic has leading coefficient , so it is an upward-opening parabola with a unique minimum at the vertex . Therefore is smallest when the quadratic is at its vertex and, at the same time, the first term is zero, that is, for the pair in the statement. For any other pair, either the quadratic exceeds its minimum or the first term is positive, so the minimum is unique.
The minimum point satisfies two equations that are used to study residuals. With the slope fixed, is a quadratic in the intercept, and at the minimum its derivative with respect to is zero; with the intercept fixed, the same holds for the derivative with respect to . The derivatives computed holding the other letter fixed are and . The two conditions are called the normal equations:
Slope, intercept, and units
For the five observations with , , , and , we obtain
If is measured in millimeters and in seconds, the slope is measured in seconds per millimeter: one additional millimeter of thickness corresponds to an estimated time that is longer. The intercept is measured in seconds and is the fitted value at , that is, . The observed thicknesses, however, range from to , so is the cutting time of a piece of zero thickness only if the line remains valid as far as , outside the observed predictor values.
Remark
In the regression of on , the predictor becomes , so the slope is , if . For example, with , , and , the two slopes are
Solving the line of on for would instead give slope , which differs from . The difference reflects which deviations are minimized. In the regression of on we measure deviations in at fixed ; in the regression of on we measure deviations in at fixed .
End of the free preview
The theory of this topic continues with 7 more sections
- Residuals and goodness of fit
- Pearson correlation
- The linear model and inference for the slope
- Mean response and individual prediction
- Model diagnostics and limitations
- Rank correlation
- Keep studying
Full access also includes the other 7 worked examples of this topic and the solutions of the 14 practice exercises.
Unlock Statistics and probability · €49Read the first 3 worked examples in this chapter for free.