Regression modeling
Fit a least-squares line
How does a line summarize a change in mean response?
Population and data
Estimate the mean response at a specified input. A line encodes the assumption that each additional unit changes that mean by the same amount across the observed range. The four points let us examine how fitting translates that assumption into two coefficients.
At four input levels x = 0, 1, 2, 3 the responses are 2, 4, 4, 6. Fit an intercept and slope by minimizing the sum of squared vertical residuals.
| x | y | x − mean(x) | y − mean(y) |
|---|---|---|---|
| 0 | 2 | −1.5 | −2 |
| 1 | 4 | −0.5 | 0 |
| 2 | 4 | 0.5 | 0 |
| 3 | 6 | 1.5 | 2 |
Notation
- Observed input and response for observation i.
- Candidate intercept and slope in the minimization.
- Values of those coefficients that minimize the sample criterion.
- Fitted response and observed minus fitted response at the solution.
Derivation
A unique slope requires at least two distinct input values. The normal equations are algebraic consequences of minimization. Interpreting the fitted line as an estimate of the conditional mean adds the model E[Y | X=x] = β₀ + β₁x. Normal errors are unnecessary for computing the coefficients. [1]
Choose the intercept and slope that minimize squared residuals. Differentiating with respect to each coefficient gives two conditions at the minimum. The first centers the residuals; the second makes their weighted sum with x equal zero.
Set the first derivative to zero to express the intercept in terms of the slope. Substitute into the second equation. Centering x and y leaves the slope as the ratio below. [1]
Center both variables. The slope compares their joint variation with the spread of x.
The fitted line passes through the two sample means. This determines its intercept and the fitted value at x = 2.
R analysis
Download the complete R scriptd <- data.frame(x = 0:3, y = c(2, 4, 4, 6))
fit <- lm(y ~ x, data = d)
unname(c(coef(fit), predict(fit, data.frame(x = 2))))R output
[1] 2.2 1.2 4.6
A one-unit increase in x corresponds to a 1.2-unit increase in fitted mean response. At x = 2 the observation is 0.6 below the line.
Does the fitted line satisfy its defining equations?
Inspect each fitted value and residual. Check both zero sums and use the remaining squared error to estimate the residual standard deviation.
e <- residuals(fit)
data.frame(d, fitted = round(fitted(fit), 2), residual = round(e, 2))
round(c(sum_e = sum(e), sum_xe = sum(d$x*e),
residual_SD = sqrt(sum(e^2)/(nrow(d)-2))), 3)R output
x y fitted residual
1 0 2 2.2 -0.2
2 1 4 3.4 0.6
3 2 4 4.6 -0.6
4 3 6 5.8 0.2
sum_e sum_xe residual_SD
0.000 0.000 0.632 The residuals are −0.2, 0.6, −0.6 and 0.2. Their squared sum is 0.8. Estimating two coefficients leaves two residual degrees of freedom, giving a residual standard deviation of about 0.632 response units. [1]
Try a variation
Would you use the fitted line at x = 30?
Reveal the explanation
That extrapolates far outside 0–3. These observations provide no check of linearity there.
References
The datasets and calculations are teaching examples created for this tutorial. The sources below explain the methods used.
- James G, Witten D, Hastie T, Tibshirani R. An Introduction to Statistical Learning: with Applications in R. 2nd ed. Springer, 2021. §3.1.
Least-squares estimation of the slope and intercept, residuals and residual standard error.
- Anscombe FJ. Graphs in statistical analysis. The American Statistician. 1973;27:17–21.
Why similar fitted lines and numerical summaries can conceal different data patterns.