All lab tutorials

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.

Working data
xyx − mean(x)y − mean(y)
02−1.5−2
14−0.50
240.50
361.52

Notation

xi,yix_i,y_i
Observed input and response for observation i.
b0,b1b_0,b_1
Candidate intercept and slope in the minimization.
β^0,β^1\hat\beta_0,\hat\beta_1
Values of those coefficients that minimize the sample criterion.
y^i,ei\hat y_i,e_i
Fitted response and observed minus fitted response at the solution.

Derivation

(β^0,β^1)=arg⁡min⁡b0,b1∑i=1n(yi−b0−b1xi)2(\hat\beta_0,\hat\beta_1)=\arg\min_{b_0,b_1}\sum_{i=1}^n(y_i-b_0-b_1x_i)^2

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.

ei=yi−b0−b1xi,Q=∑iei2,∂Q∂b0=−2∑iei,∂Q∂b1=−2∑ixieie_i=y_i-b_0-b_1x_i,\quad Q=\sum_i e_i^2,\quad \frac{\partial Q}{\partial b_0}=-2\sum_i e_i,\quad \frac{\partial Q}{\partial b_1}=-2\sum_i x_i e_i

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.

β^1=∑i(xi−xˉ)(yi−yˉ)∑i(xi−xˉ)2=65=1.2\hat\beta_1=\frac{\sum_i(x_i-\bar x)(y_i-\bar y)}{\sum_i(x_i-\bar x)^2}=\frac{6}{5}=1.2

The fitted line passes through the two sample means. This determines its intercept and the fitted value at x = 2.

β^0=4−1.2(1.5)=2.2,y^(2)=2.2+1.2(2)=4.6\hat\beta_0=4-1.2(1.5)=2.2,\qquad \hat y(2)=2.2+1.2(2)=4.6

R analysis

Download the complete R script
d <- 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.

  1. 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.

  2. 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.