Regression and the method of least squares
A common question in practice is whether two or more numeric variables move together: does a student’s grade on the final exam track the grade on the second exam, and how closely? Data on a pair of variables is called bivariate data. This article treats the most basic case, “linear regression” with a single independent variable x, along with correlation, “which measures how strong the relationship is” [1]. It is based on chapter 12 of OpenStax’s Introductory Statistics 2e. The hypothesis test used in section 12.4 has its own article, Hypothesis testing, properly understood, and the normal distribution the line’s assumptions rely on is covered in The normal distribution and the bell curve.
Lines, scatter plots, and when a line is worth drawing
In y = a + bx, a linear equation, the letters a and b stand for constants; y depends on x, so x is called independent and y dependent. Every line except a vertical one has an equation of this kind [2]. The source’s tax preparer charges a one-time $31.50 plus $32 an hour, so y = 31.50 + 32x: the intercept a is the flat fee and the slope b the rate per hour [3]. The sign of the slope gives the direction: with b > 0 the line climbs to the right, with b = 0 it is flat, and with b < 0 it falls [4].
A scatter plot shows which way a relationship runs, and how strong it is by how tightly the points hug a line or other curve. But “for a linear relationship there is an exception”: points along a horizontal line fit it perfectly (“perfect fit”) yet “would in fact show no relationship” [5]. Linear patterns turn up often, and a regression line is computed “only … if one of the variables helps to explain or predict the other variable”; taking x as independent and y as dependent, the line then predicts y from x [6].
The least-squares line
Real data seldom lie exactly on a line, so “usually, you must be satisfied with rough predictions”. Lines drawn by eye vary with whoever draws them; the least-squares regression line removes that choice [7]. For a data point (x, y) the line supplies (x, ŷ), where ŷ (“y hat”) estimates y and “is not generally equal to y from data”. The residual y₀ − ŷ₀ = ε₀ is “not an error in the sense of a mistake”: taken without its sign, it measures the vertical gap between point and line, and it is positive for a point above the line (where the line underestimates y) and negative for one below [8]. Adding up the squares of all the residuals gives SSE, the sum of squared errors; the best-fitting line uses whichever a and b make SSE smallest, found “using calculus” [8]:
ŷ = a + bx, with b = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)² and a = ȳ − b·x̄,
with x̄ and ȳ the means of the sample. The point (x̄, ȳ) is always on the line, and the slope can be rewritten as b = r·(s_y/s_x), where s_x and s_y are the standard deviations of the x and y values, r being the correlation coefficient [9]. Put in words, the data are taken to scatter around a straight line, and “any other line you might choose would have a higher SSE than the best fit line” [11].
Residuals plots. Plotting each residual against x is a check on whether the data are linearly correlated. The scatter plot should look like a straight line, but the residuals plot “should appear random, with no pattern and no outliers”, and its spread should stay even, the residuals neither growing nor shrinking steadily as x goes up [10]. In the source’s ice-cream example (cones sold against the temperature at noon, over nine days) the scatter is straight and the residuals look random [10, 11].
The running example. For eleven statistics students, scores on the third exam (out of 80) against the final (out of 200) are fitted best by ŷ = −173.51 + 4.83x [12]. The slope “tells us how the dependent variable (y) changes for every one unit increase in the independent (x) variable, on average”: each extra point on the third exam goes with 4.83 more points on the final, on average [12]. The chapter repeats a warning: draw the scatter plot first; if it is linear, the line can sensibly predict y for x inside the range of the sample, “but not necessarily for x-values outside that domain”. The range here runs from 65 to 75, so a third-exam score of 73 can be used and one of 50 should not be [12].
Correlation and r²
The correlation coefficient r, credited in the book to Karl Pearson, measures the strength and the direction of a straight-line relationship linking x and y [13]:
r = [n·Σxy − (Σx)(Σy)] / √{[n·Σx² − (Σx)²]·[n·Σy² − (Σy)²]}, where n counts the data points.
r always lies between −1 and +1. The nearer it is to −1 or +1, the stronger the linear relationship; at exactly 1 or −1 all points sit on one line, which “will not generally happen” with real data; and at r = 0 “there is likely no linear correlation”, though data in a curve or a horizontal line can also give r = 0, which is why the scatter plot has to be looked at. With r positive, y tends to rise as x rises; with r negative, it tends to fall; r and the slope b always share a sign [13]. A box in the source warns: “Strong correlation does not suggest that x causes y or y causes x. We say ‘correlation does not imply causation.’” [13]
The coefficient of determination r², normally given as a percent, is the share of the variation in y that the regression line accounts for through x; 1 − r² is the share left over, “seen as the scattering of the observed data points about the regression line” [14]. In the exam example r = 0.6631 and r² = 0.4397: variation in third-exam grades accounts for roughly 44% of how much final-exam grades vary, leaving roughly 56% unaccounted for [15].
Is r significant?
How far the linear model can be trusted “also depends on how many observed data points are in the sample”, so r has to be weighed together with n. The sample’s r estimates ρ, the population correlation, which is unknown; the test decides whether ρ is “close to zero” or “significantly different from zero”, with H₀: ρ = 0 and Hₐ: ρ ≠ 0. A significant r lets the line model the relationship for the population; otherwise “we CANNOT use the regression line” to do so [16]. A significant r with a linear-looking scatter plot still does not make the line safe beyond the data: it “may NOT be appropriate or reliable for prediction OUTSIDE the domain of observed x values”; and when r is not significant, or the plot is not linear, the line is no basis for prediction [16].
The decision can be made in either of two equivalent ways. By p-value: the statistic t = r·√(n − 2) / √(1 − r²), which has the sign of r, is read against a t-distribution (df = n − 2), and the p-value is the two tails’ area combined; the chapter sets α = 0.05 everywhere, pointing out that a p-value works at any level whereas its table of critical values is built for 5% [17]. The exam data give p = 0.026, below 0.05, so H₀ is rejected; and as the scatter plot is linear, the line may serve to forecast scores on the final [18]. By critical values: with df = n − 2, r counts as significant when it falls outside the range from the negative critical value to the positive one. With n = 10 (df = 8, critical ±0.632), r = 0.801 is significant; with n = 14 (df = 12, ±0.532), r = −0.624 is significant; with n = 6 (df = 4, ±0.811), r = 0.776 is not, “and the line should not be used for prediction” [18]. For the exam data, with 9 degrees of freedom, the cut-offs are ±0.602, which 0.6631 exceeds [19]. However large the sample, r = 0 is never significant [19].
Assumptions the test rests on [20]: x and y are related linearly; at each x, the y values follow a normal distribution centred on the line, so their means fall on it; the spread of y around the line is equal at every x; residuals are independent of one another and show no pattern; and the data were collected through careful random sampling or a randomized experiment.
Prediction, inside and outside the data
Since r is significant, putting x = 73 into the equation gives ŷ = 179.08: on average, a student with 73 on the third exam is predicted to score 179.08 on the final [21]. For a score of 66 the prediction is 145.27. A score of 90 lies beyond the observed x values of 65 to 75, “so you cannot reliably predict the final exam score”; plugging it in anyway gives 261.19, and “The largest the final-exam score can be is 200” [22]. A prediction within the observed x values is interpolation; one beyond them is extrapolation [22].
Outliers and influential points
An outlier is a point with a large residual: the least-squares line passes far from it. Outliers “need to be examined closely”: one may be bad data, or it may “hold valuable information about the population under study and should remain included”; what matters is finding out why it is there [23]. An influential point is different: it sits far from the rest of the data along the x axis and “may have a big effect on the slope”; “to begin to identify an influential point, you can remove it” and check whether the slope shifts much [23].
The 2s rule of thumb. Flag a point if it sits further from the best-fit line, on either side, than twice the residuals’ standard deviation [23]. In numbers, s = √(SSE / (n − 2)); the divisor is n − 2 “because the regression model involves two estimates” [25]. On the exam data the calculator gives s = 16.4, so 2s = 32.8 [25, 26]. Just one residual exceeds that: 35, for the student whose scores were 65 (third exam) and 175 (final), which makes it a possible outlier. On the graph it is the single point beyond the two lines ŷ ± 2s drawn parallel to the best-fit line, “just barely” [24, 25, 26].
Deciding what to do with it takes judgment, and the source spells this out: check the data behind the point; “if there is an error, we should fix the error if possible, or delete the data. If the data is correct, we would leave it in the data set.” “Remember, we do not always delete an outlier.” If outliers are removed, “the researcher should either record that data was deleted, and why, or the researcher should provide results both with and without the deleted data” [26, 27]. For the exercise, the source treats the point as an error and removes it; refitting to the ten points left yields ŷ = −355.19 + 7.39x; r is now 0.9121, a tighter correlation than 0.6631, and the predicted final for a third-exam score of 73 goes from 179.08 to 184.28 [26, 27].
A significant r is not the whole story. In the source’s consumer price index example, the CPI for 14 years between 1915 and 1999 (the table adds 2010, 2020 and 2023, which the book suggests bringing in later to see how the model shifts) gives ŷ = −3204 + 1.662x with r = 0.8694, significant against the df = 12 critical value of 0.532, with no outliers. The book’s comment: “Although the correlation coefficient is significant, the pattern in the scatterplot indicates that a curve would be a more appropriate model to use than a line”, and “it is always important to look at the scatterplot when deciding whether a linear model is appropriate” [28].
Not covered here
Multiple regression (the source says statisticians in reality use multivariate data, but the chapter treats one x only [1]), confidence intervals for the slope, the calculator procedures that fill much of the chapter, the three regression labs (sections 12.7 to 12.9), and any claim about regression in machine learning or economics, which these pages do not make. Not used: section 12.6’s hand computation from rounded residuals (SSE = 2440, s = 16.47, 2s = 32.94) and one comparison with 31.29, which disagree with its own calculator value (16.412, shown as 16.4); the decision is the same under all of them [24, 25, 26].
Sources
Equations in this PDF are images; the formulas cited here were read from rendered pages 625, 626, 630, 632 and 638.
- [1]OpenStax, Introductory Statistics 2e, ch. 12, p. 617 (Introduction). View at source ↗
- [2]Same, §12.1, pp. 617–618 (the form y = a + bx; any non-vertical line). View at source ↗
- [3]Same, §12.1, p. 619 (Example 12.3). View at source ↗
- [4]Same, §12.1, pp. 619–620 (Figure 12.4; Example 12.4, intercept and slope interpreted). View at source ↗
- [5]Same, §12.2, p. 622 (direction and strength; the horizontal-line exception). View at source ↗
- [6]Same, §12.2, p. 623 (linear patterns; when a regression line is calculated). View at source ↗
- [7]Same, §12.3, pp. 623 and 625 (“rough predictions”; fitting by eye; Example 12.6 introduced). View at source ↗
- [8]Same, §12.3, p. 625 (ŷ; residuals; SSE; “using calculus”). View at source ↗
- [9]Same, §12.3, p. 626 (ŷ = a + bx, a and b, (x̄, ȳ), b = r·s_y/s_x; rendered). View at source ↗
- [10]Same, §12.3, p. 626 (Residuals Plots; Table 12.5). View at source ↗
- [11]Same, §12.3, p. 627 (Figures 12.11 and 12.12; Least Squares Criteria for Best Fit). View at source ↗
- [12]Same, §12.3, pp. 623 and 628 (Example 12.6, “A random sample of 11 statistics students”, p. 623; ŷ = −173.51 + 4.83x, the reminder on domain, interpretation of the slope, p. 628). View at source ↗
- [13]Same, §12.3, p. 630 (the correlation coefficient: Pearson; formula, rendered; value and sign; “correlation does not imply causation”). View at source ↗
- [14]Same, §12.3, p. 630 (The Coefficient of Determination). View at source ↗
- [15]Same, §12.3, p. 631 (r² = 0.6631² = 0.4397; 44% and 56%). View at source ↗
- [16]Same, §12.4, p. 631 (ρ and r; significant and not significant; the boxed note on prediction; H₀ and Hₐ). View at source ↗
- [17]Same, §12.4, p. 632 (two methods; α = 0.05 and the note; t-distribution with n − 2 df; test statistic, rendered; both tails). View at source ↗
- [18]Same, §12.4, pp. 632–633 (p = 0.026; Method 2; Examples 12.7, 12.8, 12.9). View at source ↗
- [19]Same, §12.4, p. 634 (critical-value method on the exam data, ±0.602; Example 12.10 d, r = 0). View at source ↗
- [20]Same, §12.4, pp. 634–635 (Assumptions in Testing the Significance of the Correlation Coefficient). View at source ↗
- [21]Same, §12.5, p. 635 (prediction at x = 73, 179.08). View at source ↗
- [22]Same, §12.5, pp. 635–636 (Example 12.11; 145.27; 261.19; interpolation and extrapolation). View at source ↗
- [23]Same, §12.6, p. 636 (outliers; influential points; the two-standard-deviation rule of thumb). View at source ↗
- [24]Same, §12.6, p. 637 (Example 12.12, graphical identification; s = 16.412; lines Y2 and Y3; “just barely”). View at source ↗
- [25]Same, §12.6, pp. 638–639 (s = √(SSE/(n − 2)), rendered; the note on n − 2; s = 16.4; Table 12.6; 2s = 32.8; the hand values not used). View at source ↗
- [26]Same, §12.6, pp. 639–640 (how the outlier affects the line; 31.29; the new line ŷ = −355.19 + 7.39x, r = 0.9121; “we do not always delete an outlier”; the note on recording deletions). View at source ↗
- [27]Same, §12.6, p. 640 (Example 12.13, 184.28 against 179.08). View at source ↗
- [28]Same, §12.6, pp. 640–642 (Example 12.14, the CPI; r = 0.8694; critical value 0.532; no outliers; the note that a curve would be more appropriate). View at source ↗
Wikipedia was not used as a source.
Works cited
- Barbara Illowsky and Susan Dean, Introductory Statistics 2e (OpenStax, Rice University, 2023). openstax.org/details/books/introductory-statistics-2e. License: CC BY-NC-SA 4.0. Access for free at openstax.org.