Least squares chooses \(\beta_0\) and \(\beta_1\) to minimize the SSE. Set each partial derivative to zero:
The two equations after the arrows are the normal equations. Dividing the first by \(n\) gives \(\beta_0 = \bar y - \beta_1 \bar x\): for any fixed slope, that intercept sets \(\partial\,\text{SSE}/\partial\beta_0\) to zero and gives the smallest SSE. At the least-squares slope it is \(\hat\beta_0 = \bar y - \hat\beta_1 \bar x\). Substituting it into the second gives
because \(\sum (x_i - \bar x)(y_i - \bar y) = \sum x_i y_i - n\bar x \bar y\) and \(\sum (x_i - \bar x)^2 = \sum x_i^2 - n\bar x^2\).
\(\text{Cov}(x, y) = \sum (x_i - \bar x)(y_i - \bar y)/(n-1)\) and \(\text{Var}(x) = \sum (x_i - \bar x)^2/(n-1)\). The \(n - 1\) cancels in their ratio, so \(\hat\beta_1 = \text{Cov}(x, y)/\text{Var}(x)\).
SSE is a quadratic in \(\beta_0\) and \(\beta_1\). Its matrix of second derivatives, the Hessian \(H\), does not depend on \(\beta_0\) or \(\beta_1\):
The determinant is positive whenever the \(x\) values are not all equal, and the diagonal is positive, so SSE is a quadratic in \(\beta_0\) and \(\beta_1\) with exactly one minimum. The solution of the normal equations is that minimum, and no iteration is needed.
The first normal equation says \(\sum e_i = 0\): the least-squares residuals always sum to zero. It also means the fitted line passes through \((\bar x, \bar y)\), since \(\hat\beta_0 + \hat\beta_1 \bar x = \bar y\). The second normal equation says \(\sum x_i e_i = 0\).
lm(y ~ x) fits the line. coef(), resid(), and sum(resid(fit)^2) return the estimates, the residuals, and the SSE. lm() solves the same equations by a QR factorization of the x matrix, which avoids squaring the numbers and so loses less to rounding. Part 1 of The Script runs these lines on this tab's points.
Write any line as the least-squares line plus a change in each coefficient, \(\Delta\beta_0 = \beta_0 - \hat\beta_0\) and \(\Delta\beta_1 = \beta_1 - \hat\beta_1\). The least-squares residuals satisfy \(\sum e_i = 0\) and \(\sum x_i e_i = 0\), so the cross terms drop out:
Here \(\text{SSE}_{\min} = \text{SSE}(\hat\beta_0, \hat\beta_1)\), the smallest SSE.
The added term is a quadratic form in the two changes, so every contour is an ellipse centered on \((\hat\beta_0, \hat\beta_1)\). The middle term tilts the ellipses when \(\bar x \neq 0\). Raising the slope by \(\Delta\beta_1\) and lowering the intercept by \(\bar x\,\Delta\beta_1\) does not change the line's height at \(\bar x\), so SSE grows slowly in that direction.
The added term involves only the \(x\) values and the two changes. The \(y\) values set \(\text{SSE}_{\min}\).
The map shows SSE over the intercept and the slope, and its lowest point is the least-squares estimate. Brighter colors are lower SSE, and each ring joins the lines whose SSE is a fixed percent above the minimum. The map spans \(\hat\beta_0 \pm 5\) and \(\hat\beta_1 \pm 3\).
\(\text{MSE} = \text{SSE}/n\) divides every point on the map by the same \(n\), so the rings and the minimum do not move. On average the minimum SSE is \((n - 2)\sigma^2\), where \(\sigma\) is the standard deviation of the noise: fitting the intercept and the slope uses two of the \(n\) residuals' degrees of freedom. It grows with \(n\), while the minimum MSE stays near \(\sigma^2\). For the Clean linear data, \(\sigma^2 = 0.8^2 = 0.64\).
In every data set \(x\) is uniform from 0 to 6. Clean linear is \(y = 2 + 1.5x\) plus noise with standard deviation 0.8, and Noisy linear is the same line with standard deviation 2. Heteroscedastic is \(y = 1 + 2x\) plus noise whose standard deviation, \(0.3x + 0.2\), grows with \(x\). Curved is \(y = 1 + 0.5x^{1.5}\) plus noise with standard deviation 1. Each data set has four seeded draws of 80 points, and a sample of size \(n\) is the first \(n\) points of the draw.
outer() evaluates SSE on a grid of intercepts and slopes, and contour() draws the rings. Part 2 of The Script draws this map in R.
Green: the smaller value in each row.
Under MSE one large residual can outweigh many small ones. The MSE line is the least-squares line: dividing SSE by \(n\) does not change which line is smallest.
For a single constant \(c\), \(\sum (y_i - c)^2\) is smallest at the mean of the \(y_i\), and \(\sum |y_i - c|\) is smallest at their median. This is why the mean is sensitive to outliers and the median is not. The MSE line and the MAE line extend the mean and the median to a line. The MAE line is also called least absolute deviations, or median regression.
MAE has a corner wherever a residual is zero, so setting its derivatives to zero does not give a formula. The MAE lines here come from R's optim() with the Nelder-Mead method, a search that tries nearby lines and moves to whichever has the smaller MAE, started at the least-squares line. quantreg::rq(y ~ x, tau = 0.5) solves the same problem exactly as a linear program; here it gives the same line to about four decimals.
The outliers are five points 15 to 21 units below the true line \(y = 1 + 1.2x\): four at the right end, with \(x\) from 6 to 9, and one at the left end, with \(x\) below 1. Points far from \(\bar x\) change the least-squares slope the most. The outliers are seeded from the first three \(x\) values, so each draw always gets the same five.
\(x\) is uniform from 0 to 8. There are four seeded draws of 80 points, and a sample of size \(n\) is the first \(n\) points of the draw.
lm() fits the MSE line, optim() or rq() fits the MAE line, and mean(resid^2) and mean(abs(resid)) fill the table. Part 3 of The Script fits both lines at every outlier count.
Each row shows your smallest revealed gap for that data set. A pass is within 5% of the minimum.
Fit each of the four data sets with the two sliders. Your only score while you fit is your line's SSE. Reveal draws the least-squares line and gives the gap, \(100\,(\text{SSE}/\text{SSE}_{\min} - 1)\) percent. Within 5% passes. The scoreboard shows your smallest revealed gap for each data set, and New challenge draws new points with the line hidden again.
Set the slope first, so the line follows the trend of the points. Then move the intercept until the line runs through the middle of the points. The residual segments get shorter as the fit improves.
The right side does not involve \(y\), so a line that misses the estimates by the same amounts has the same excess SSE on clean and on noisy data. Noisier data have a larger \(\text{SSE}_{\min}\), so the same excess is a smaller percent.
The challenges use the same four generators as the SSE Map tab with their own seeds, so the hidden lines differ from the ones shown there.
Part 4 of The Script computes the SSE of any line you choose and its gap above lm()'s.
| Call | What it returns |
|---|---|
lm(y ~ x, data = d) | The least-squares fit. summary() adds standard errors and R²; confint() gives intervals for the coefficients. |
coef(fit) | The intercept and slope, β̂₀ and β̂₁. |
resid(fit), fitted(fit) | The residuals eᵢ = yᵢ − ŷᵢ and the predictions ŷᵢ. |
deviance(fit) | The SSE, the same as sum(resid(fit)^2). |
outer(), contour() | SSE on a grid of intercepts and slopes, and its contour map. |
optim(start, f) | The values that minimize any function f; here, the MAE line by the Nelder-Mead method, a search that tries nearby lines and moves to whichever has the smaller MAE. |
quantreg::rq(y ~ x, tau = 0.5) | The MAE line, solved exactly as a linear program. |