Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The Toluca Company builds refrigeration equipment and the replacement parts that keep it running. One part had long been produced in batches, or lots, of whatever size seemed convenient at the time. When the company started a cost-cutting review, its engineers wanted to settle a basic question: what lot size is cheapest to produce? Every production run carries a fixed setup cost that does not care about lot size, plus machining and assembly work that grows with the number of parts. To find the sweet spot, the engineers first needed one ingredient: how do the labor hours a run consumes depend on the size of the lot?

They pulled records from 25 recent runs made under stable conditions over a six-month period. Each record is a pair: the lot size (units produced) and the work hours the run took. The lot sizes are all multiples of ten, a scheduling convenience, and they range from 20 to 120 units. The scatter of these 25 points is the starting picture of the whole chapter.

Scatterplot of the 25 Toluca production runs, work hours on the vertical axis against lot size on the horizontal axis, with an upward-sloping straight line fitted through the cloud of points. Work hours climb from roughly 110 at a lot size of 20 to over 500 at a lot size of 120, and the points scatter fairly evenly on both sides of the line.

Figure 1:The 25 Toluca runs with the least-squares line drawn through them. Bigger lots take more hours, the trend looks straight, and the scatter around the line is roughly the same width all across, which is exactly the situation this chapter models.

Two things stand out in Figure 1. Bigger lots take more hours, and the cloud of points is close enough to a straight line that a line is a reasonable summary. But which line? An engineer could lay a ruler on the plot and draw one by eye, and a second engineer would draw a slightly different one. This chapter replaces the ruler with a rule: a precise, defensible recipe for the single best line, an honest account of how much to trust it, and the assumptions that make the whole enterprise valid. By the end you will be able to fit that line, read its slope in the units of the problem (hours per unit of lot size), say how uncertain the slope is, and prove that under stated conditions no competing method does better.

The modeling workflow

Every chapter of this book is one pass through the same five-stage loop. Naming the stages now gives you a map to carry through the whole course, and every later chapter opens by saying which stage it serves.

A ring of five rounded boxes joined by clockwise arrows: ASK (question and data), EXPLORE (plot and summarize), FIT (estimate the model), CHECK (trust the fit?), and USE (interpret and predict). A dashed arrow curves from CHECK back to FIT, labeled a failed check sends you back.

Figure 2:The five stages as one loop. You move clockwise from ASK to USE, and a failed CHECK does not end the work: the dashed arrow sends you back to FIT or EXPLORE with a better model.

As Figure 2 shows, the stages form a loop, not a one-way street: a failed CHECK sends you back to EXPLORE or FIT with a better model. Simple linear regression is the smallest complete trip around this loop, which is why the course starts here.

2.1 The simple linear regression model

Intuition

Regression starts from a plain idea: one thing tends to move with another, and we want to pin that relationship down in numbers. Here we describe how a response YY (Toluca work hours) depends on a single predictor XX (lot size). No sane model claims the hours are an exact function of lot size: two runs of the same size took different amounts of time, because of tool wear, the crew on shift, the weather, and a hundred small things nobody recorded. So we split each observation into two pieces: a systematic part that lot size explains, and a random part that it does not.

The systematic part is a straight line. On average, a run of size XX takes some baseline amount of time plus a fixed number of extra hours per additional unit. The random part is a disturbance that pushes the actual hours above or below that average. We assume the disturbance has no built-in tendency to be positive or negative, has the same typical size regardless of lot size, and does not coordinate from one run to the next.

Formula

The simple linear regression model (Definition 2.1) for observations i=1,,ni = 1, \dots, n is

Yi=β0+β1Xi+εi.Y_i = \beta_0 + \beta_1 X_i + \varepsilon_i .

The parameters β0\beta_0 and β1\beta_1 are fixed numbers we do not know and want to estimate. The model puts four conditions on the errors:

E{εi}=0,Var{εi}=σ2,Cov{εi,εj}=0 (ij).E\{\varepsilon_i\} = 0, \qquad \operatorname{Var}\{\varepsilon_i\} = \sigma^2, \qquad \operatorname{Cov}\{\varepsilon_i, \varepsilon_j\} = 0 \ (i \neq j).

In words: each error averages to zero (the line is right on average), every error has the same variance σ2\sigma^2 (constant spread, called homoscedasticity), and distinct errors are uncorrelated (one run’s luck says nothing about another’s). Notice what is not yet assumed: no particular shape for the error distribution. Normality enters only in 2.6 Maximum likelihood under normal errors, and only when we want to build intervals and tests.

Because XiX_i is a fixed constant and β0+β1Xi\beta_0 + \beta_1 X_i is therefore also constant, the error conditions transfer directly to the responses:

E{Yi}=β0+β1Xi,Var{Yi}=σ2.E\{Y_i\} = \beta_0 + \beta_1 X_i, \qquad \operatorname{Var}\{Y_i\} = \sigma^2 .

The quantity E{Yi}E\{Y_i\} is the mean response at level XiX_i. The function E{Y}=β0+β1XE\{Y\} = \beta_0 + \beta_1 X that gives the mean response at any XX is the regression function (Definition 2.2). It is a line, and estimating it is the whole game.

Figure 3 is the mental picture to carry through the chapter: a straight mean line, with a distribution of possible YY values stacked at each XX, every stack the same width.

A rising straight line labeled the mean line E of Y equals beta-zero plus beta-one X, with three identical bell-shaped curves drawn sideways at three lot sizes. Each bell is centered on the line and has the same width, showing that the response varies around the line by the same amount at every lot size.

Figure 3:The model in one picture: the mean of Y sits exactly on the line, and at every X the response scatters around that mean by the same amount. The bells are drawn identical on purpose, that is the constant-variance assumption.

2.2 Least squares from first principles

Intuition

We have a cloud of points and want the one line that fits best. “Best” needs a definition. Least squares defines it by penalizing misses: for a candidate line, measure the vertical gap from each point to the line, square the gaps so that overshooting and undershooting both count as errors and large misses hurt disproportionately, and add them up. The best line is the one that makes this total, the sum of squared errors, as small as possible. Figure 4 shows the idea with three candidate lines through the Toluca data.

The Toluca scatterplot with three candidate lines: a solid least-squares line through the middle of the cloud, a dashed too-shallow line that sits above the low points and below the high points, and a dotted too-steep line that does the reverse. A legend reports each line's SSE, with the least-squares line's value the smallest at about 54,825.

Figure 4:Three candidate lines and their sums of squared errors. The too-shallow and too-steep lines leave large vertical gaps and large SSE; the least-squares line threads the cloud and has the smallest SSE of any line.

Formula

Write a candidate line with intercept b0b_0 and slope b1b_1. Its sum of squared errors (Definition 2.3) is

Q(b0,b1)=i=1n(Yib0b1Xi)2.Q(b_0, b_1) = \sum_{i=1}^{n} \left( Y_i - b_0 - b_1 X_i \right)^2 .

The least-squares estimates b0b_0 and b1b_1 are the values that minimize QQ. To write them compactly, define the three basic sums of squares and cross-products around the means:

Sxx=i=1n(XiXˉ)2,Sxy=i=1n(XiXˉ)(YiYˉ),Syy=i=1n(YiYˉ)2,S_{xx} = \sum_{i=1}^{n} (X_i - \bar{X})^2, \qquad S_{xy} = \sum_{i=1}^{n} (X_i - \bar{X})(Y_i - \bar{Y}), \qquad S_{yy} = \sum_{i=1}^{n} (Y_i - \bar{Y})^2,

where Xˉ\bar{X} and Yˉ\bar{Y} are the sample means of the predictor and response. The next two derivations establish the minimizers, stated here first.

Derivation by calculus

Proof (by calculus). Q(b0,b1)Q(b_0, b_1) is a sum of squares, so it is smooth and bounded below by zero. At a minimum, both partial derivatives vanish. Differentiate:

Qb0=i=1n2(Yib0b1Xi)(1),Qb1=i=1n2(Yib0b1Xi)(Xi).\frac{\partial Q}{\partial b_0} = \sum_{i=1}^{n} 2\left(Y_i - b_0 - b_1 X_i\right)(-1), \qquad \frac{\partial Q}{\partial b_1} = \sum_{i=1}^{n} 2\left(Y_i - b_0 - b_1 X_i\right)(-X_i).

Setting each to zero and dividing by -2 gives the normal equations:

i=1n(Yib0b1Xi)=0,i=1nXi(Yib0b1Xi)=0.\sum_{i=1}^n \left(Y_i - b_0 - b_1 X_i\right) = 0, \qquad \sum_{i=1}^n X_i\left(Y_i - b_0 - b_1 X_i\right) = 0 .

Expand the sums. The first equation becomes Yi=nb0+b1Xi\sum Y_i = n b_0 + b_1 \sum X_i, and dividing by nn gives Yˉ=b0+b1Xˉ\bar{Y} = b_0 + b_1 \bar{X}, so

b0=Yˉb1Xˉ.b_0 = \bar{Y} - b_1 \bar{X}.

Substitute this into the second normal equation XiYi=b0Xi+b1Xi2\sum X_i Y_i = b_0 \sum X_i + b_1 \sum X_i^2. Replacing b0b_0 and collecting the b1b_1 terms,

XiYiYˉXi=b1(Xi2XˉXi).\sum X_i Y_i - \bar{Y}\sum X_i = b_1\left(\sum X_i^2 - \bar{X}\sum X_i\right).

The left side equals XiYinXˉYˉ=(XiXˉ)(YiYˉ)=Sxy\sum X_i Y_i - n \bar{X}\bar{Y} = \sum (X_i - \bar X)(Y_i - \bar Y) = S_{xy}, and the right side’s bracket equals Xi2nXˉ2=(XiXˉ)2=Sxx\sum X_i^2 - n\bar{X}^2 = \sum (X_i - \bar X)^2 = S_{xx}. Therefore

b1=SxySxx.b_1 = \frac{S_{xy}}{S_{xx}} .

This is the only stationary point, and it is a minimum by the second-derivatives test for a function of two variables (the same test you would use in calculus for one variable, extended to two). The three second partial derivatives of QQ are constants: Qb0b0=2nQ_{b_0 b_0} = 2n, Qb1b1=2Xi2Q_{b_1 b_1} = 2\sum X_i^2, and Qb0b1=2XiQ_{b_0 b_1} = 2\sum X_i. Form the test quantity D=Qb0b0Qb1b1(Qb0b1)2=4(nXi2(Xi)2)=4nSxxD = Q_{b_0 b_0}\,Q_{b_1 b_1} - (Q_{b_0 b_1})^2 = 4\big(n\sum X_i^2 - (\sum X_i)^2\big) = 4n\,S_{xx}. Since D>0D > 0 whenever the XiX_i are not all equal, and Qb0b0=2n>0Q_{b_0 b_0} = 2n > 0, the stationary point is a local minimum. Because QQ is a sum of squares (so it is bounded below by zero) and this is its only stationary point, that local minimum is also the global minimum. (Chapter 7 revisits this same fact in matrix form, once you have the tools of Chapter 6.) \blacksquare

Derivation by an algebraic identity

The calculus argument finds the minimizer but leaves the geometry a little hidden. Here is a second proof that uses no derivatives and shows directly that no line beats the least-squares line. It rests on two facts we will prove in 2.3 Fitted values and the properties of residuals: writing the fitted values as Y^i=b0+b1Xi\hat{Y}_i = b_0 + b_1 X_i and the residuals as ei=YiY^ie_i = Y_i - \hat{Y}_i, the residuals satisfy ei=0\sum e_i = 0 and Xiei=0\sum X_i e_i = 0.

Proof (by an algebraic identity). Take any competing line with intercept aa and slope cc. Split the gap from each point to that line by routing it through the least-squares fitted value:

YiacXi=(YiY^i)ei+(Y^iacXi)gi,gi=(b0a)+(b1c)Xi.Y_i - a - c X_i = \underbrace{(Y_i - \hat{Y}_i)}_{e_i} + \underbrace{(\hat{Y}_i - a - c X_i)}_{g_i}, \qquad g_i = (b_0 - a) + (b_1 - c) X_i .

Square and sum. The sum of squared errors for the competing line is

(YiacXi)2=ei2+gi2+2eigi.\sum \left(Y_i - a - c X_i\right)^2 = \sum e_i^2 + \sum g_i^2 + 2 \sum e_i g_i .

The cross term vanishes:

eigi=(b0a)ei+(b1c)Xiei=(b0a)0+(b1c)0=0.\sum e_i g_i = (b_0 - a)\sum e_i + (b_1 - c)\sum X_i e_i = (b_0 - a)\cdot 0 + (b_1 - c)\cdot 0 = 0 .

So for every competing line,

(YiacXi)2=ei2SSE of the LS line+gi2    ei2,\sum \left(Y_i - a - c X_i\right)^2 = \underbrace{\sum e_i^2}_{\text{SSE of the LS line}} + \sum g_i^2 \; \ge \; \sum e_i^2 ,

with equality only when gi2=0\sum g_i^2 = 0, that is when gi=0g_i = 0 for all ii, which (since the XiX_i are not all equal) forces a=b0a = b_0 and c=b1c = b_1. The least-squares line is the unique minimizer. \blacksquare

The two normal equations have a clean picture. Each one is a line in the (b0,b1)(b_0, b_1) plane, and the least-squares estimates sit where they cross, at the bottom of the SSE bowl. Figure 5 shows the bowl and Figure 6 shows it from above.

A three-dimensional surface plot of SSE as a function of the intercept b0 and slope b1, shaped like a smooth bowl or valley. A red dot marks the single lowest point of the bowl, labeled as the minimum at b0 about 62.4 and b1 about 3.57.

Figure 5:Because SSE is a convex (bowl-shaped) function of the intercept and slope, it has exactly one lowest point, and that point is the least-squares solution. There are no other valleys to get stuck in.

A contour map of SSE over the intercept b0 (horizontal) and slope b1 (vertical), showing nested elliptical contours. Two straight lines, a dashed one for the first normal equation and a dotted one for the second, cross at a red star marking the minimum at intercept about 62.4 and slope about 3.57.

Figure 6:The same SSE surface seen from directly above. Each normal equation is a straight line in the (b0, b1) plane; the two lines intersect at the star, the least-squares estimates, sitting at the center of the innermost contour.

R

Fitting the model in R uses lm, which reads a formula response ~ predictor. First read the data and look at it.

toluca <- read.csv("data/toluca.csv")
dim(toluca)
head(toluca, 3)
[1] 25  2
  lotsize hours
1      80   399
2      30   121
3      50   221

Being able to reproduce lm from the raw sums SxxS_{xx} and SxyS_{xy} matters: it means you can compute a regression anywhere, check software output, and, later, extend the idea when there is no lm button for the model you need.

Small datasets are worth computing by hand once, so the formulas stop being black boxes. The file toluca_mini.csv is a six-row slice of the Toluca data, small enough to do on paper. Figure 7 shows its points and their deviations from the means, the raw material of SxxS_{xx} and SxyS_{xy}.

A scatterplot of six points from the hand-computation dataset with a vertical dashed line at the mean lot size and a horizontal dashed line at the mean work hours, dividing the plane into quadrants. For each point, short horizontal and vertical segments show its deviations from the two means; points in the upper-right and lower-left quadrants (positive cross-products) are green, and are the majority.

Figure 7:The six hand-computation points with the mean cross drawn in. A point’s horizontal deviation times its vertical deviation is its contribution to Sxy; most points sit in the two quadrants where that product is positive, so Sxy and the slope come out positive.

Before you take the formulas on trust, spend a minute trying to beat them by hand.

Two sliders set your own intercept b0b_0 and slope b1b_1 on the 25 Toluca runs. The readouts compare the Q(b0,b1)Q(b_0, b_1) your line achieves against the smallest value any line can achieve.

What to notice. No pair of slider settings pushes QQ below 54,825.554{,}825.5, and the settings that come closest read 62.5 and 3.57, the least-squares estimates. Try this. Hold the slope at 3.57 and sweep the intercept alone: the sum of residuals crosses zero at exactly the setting where QQ bottoms out, which is the first normal equation happening in front of you. Back to Section 2.2.

2.3 Fitted values and the properties of residuals

Intuition

Once the line is fixed, every observed point splits into two parts. The fitted value Y^i\hat{Y}_i (Definition 2.5) is where the line says the point should be; the residual ei=YiY^ie_i = Y_i - \hat{Y}_i is how far the actual point lands from it. Residuals are the model’s leftovers, the part of YY that lot size did not explain. Figure 9 draws them as the vertical sticks connecting each point to the line. They are not just scenery: least squares forces the residuals to satisfy exact algebraic identities, and those identities are both a proof tool and a practical way to catch a mistake in a fit.

The Toluca scatterplot with the fitted line and a vertical segment from every point to the line. Points above the line, with positive residuals, are drawn as orange circles with orange segments; points below the line, with negative residuals, are drawn as purple squares with purple segments. Above and below the line the segments look balanced in total length.

Figure 9:Each residual is the signed vertical distance from a point to the fitted line: positive (orange, above) or negative (purple, below). Least squares balances them so the signed residuals sum to exactly zero.

Formula

The fitted values and residuals are

Y^i=b0+b1Xi,ei=YiY^i.\hat{Y}_i = b_0 + b_1 X_i, \qquad e_i = Y_i - \hat{Y}_i .

Do not confuse eie_i with the model error εi=Yi(β0+β1Xi)\varepsilon_i = Y_i - (\beta_0 + \beta_1 X_i). The error is the gap to the true, unknown line; the residual is the gap to our estimated line. We never see εi\varepsilon_i, but we can always compute eie_i, and it is our best stand-in. Figure 10 puts the two gaps side by side so the difference is impossible to miss.

Two side-by-side scatterplots of the same seven points. Left panel: a dashed true line labeled unknown, with orange vertical segments from each point to that line, marked as the errors epsilon i that are never observed. Right panel: a solid fitted line labeled computed, with red vertical segments from each point to that line, marked as the residuals e i that are always computable.

Figure 10:The one distinction to keep straight: the error is the gap to the true line we can never see (left), and the residual is the gap to the fitted line we compute from the data (right). Same points, two different lines, two different gaps.

Derivation (properties of residuals)

Proof. All of these follow from the two normal equations of 2.2 Least squares from first principles.

  1. The residuals sum to zero. The first normal equation is exactly (Yib0b1Xi)=ei=0\sum (Y_i - b_0 - b_1 X_i) = \sum e_i = 0.

  2. The predictor is orthogonal to the residuals. The second normal equation is exactly Xi(Yib0b1Xi)=Xiei=0\sum X_i (Y_i - b_0 - b_1 X_i) = \sum X_i e_i = 0.

  3. The fitted values are orthogonal to the residuals. Using properties 1 and 2, Y^iei=(b0+b1Xi)ei=b0ei+b1Xiei=0\sum \hat{Y}_i e_i = \sum (b_0 + b_1 X_i) e_i = b_0 \sum e_i + b_1 \sum X_i e_i = 0.

  4. The fitted values reproduce the total of YY. From property 1, ei=(YiY^i)=0\sum e_i = \sum (Y_i - \hat{Y}_i) = 0, so Y^i=Yi\sum \hat{Y}_i = \sum Y_i, and dividing by nn, the mean fitted value equals Yˉ\bar{Y}.

  5. The line passes through the center of the data. Setting X=XˉX = \bar{X} in the fitted line and using b0=Yˉb1Xˉb_0 = \bar{Y} - b_1 \bar{X} gives Y^=b0+b1Xˉ=Yˉ\hat{Y} = b_0 + b_1 \bar{X} = \bar{Y}. So (Xˉ,Yˉ)(\bar{X}, \bar{Y}) is always on the least-squares line. \blacksquare

Property 1 is why the vertical sticks in Figure 9 balance out. Properties 1 and 2 were the ingredients that made the cross term vanish in the algebraic proof of 2.2 Least squares from first principles, so the identities repay the loan we took there.

R and Python

A plot of the residuals against the predictor is the first picture to draw after any fit. If the model’s constant-variance and straight-line assumptions hold, the residuals should hover around zero with no trend and no fanning, as in Figure 11. A curve would signal that the relationship is not straight; a widening funnel would signal nonconstant variance. Chapter 9 builds a full diagnostic toolkit on this one habit.

A scatterplot of the Toluca residuals on the vertical axis against lot size on the horizontal axis, with a dashed horizontal reference line at zero. The points scatter above and below zero with no clear trend and roughly even width across all lot sizes.

Figure 11:The Toluca residuals plotted against lot size. They sit in a flat, patternless band around zero with no funnel shape, which is the picture that supports the straight-line and constant-variance assumptions.

The identities in Theorem 2.6 are exact, so the fastest way to believe them is to try to break one.

Drag any of the six hand-computation runs anywhere on the plot. The line refits instantly, and the four readouts track ei\sum e_i, Xiei\sum X_i e_i, the mean of YY, and the mean of Y^\hat{Y}.

What to notice. However badly you mangle the data, ei\sum e_i and Xiei\sum X_i e_i stay at zero and the mean of Y^\hat{Y} stays equal to Yˉ\bar{Y}. Try this. Drag one run to the very top of the plot: the residuals become huge, the fit becomes useless, and the identities do not move, which is why they check your arithmetic and never your model. Back to Section 2.3.

2.4 Estimating the error variance

Intuition

The slope and intercept describe the line, but the model has a third unknown: σ2\sigma^2, the variance of the errors, which controls how tightly the points hug the line. A small σ2\sigma^2 means predictions can be sharp; a large one means even a perfect line leaves big uncertainty. We cannot see the errors εi\varepsilon_i, but the residuals eie_i are their visible stand-ins, so we estimate σ2\sigma^2 from the spread of the residuals. Figure 13 shows what that spread looks like as a band around the fitted line.

The Toluca scatterplot with the fitted line and two shaded bands around it, a darker inner band at plus or minus one estimated standard deviation s and a lighter outer band at plus or minus two s. Most points fall inside the inner band and nearly all inside the outer band. An inset histogram in the corner shows the residuals spread symmetrically around zero.

Figure 13:The estimated error spread s = 48.8 hours drawn as bands around the fitted line. About two-thirds of the runs fall within one s of the line and almost all within two s; s summarizes the width of the residual histogram in the inset.

Formula

The error sum of squares (Definition 2.7) and the estimator of σ2\sigma^2 are

SSE=i=1nei2=i=1n(YiY^i)2,s2=MSE=SSEn2,s=MSE.\mathrm{SSE} = \sum_{i=1}^{n} e_i^2 = \sum_{i=1}^{n} (Y_i - \hat{Y}_i)^2, \qquad s^2 = \mathrm{MSE} = \frac{\mathrm{SSE}}{n-2}, \qquad s = \sqrt{\mathrm{MSE}} .

The divisor is n2n-2, not nn or n1n-1. Two degrees of freedom are spent estimating the two coefficients b0b_0 and b1b_1 that define the line the residuals are measured from. The next derivation shows that this exact divisor, and no other, makes MSE\mathrm{MSE} correct on average.

Derivation (why the divisor is n2n-2)

Proof. We show E{SSE}=(n2)σ2E\{\mathrm{SSE}\} = (n-2)\sigma^2, so that dividing by n2n-2 gives an estimator that is right on average. Start from a tidy identity for SSE. Since Y^i=Yˉ+b1(XiXˉ)\hat{Y}_i = \bar{Y} + b_1(X_i - \bar{X}) (using b0=Yˉb1Xˉb_0 = \bar{Y} - b_1 \bar X),

ei=YiY^i=(YiYˉ)b1(XiXˉ),e_i = Y_i - \hat{Y}_i = (Y_i - \bar{Y}) - b_1 (X_i - \bar{X}),

so, expanding the square and using Sxy=b1SxxS_{xy} = b_1 S_{xx},

SSE=ei2=Syy2b1Sxy+b12Sxx=Syy2b1(b1Sxx)+b12Sxx=Syyb12Sxx.\mathrm{SSE} = \sum e_i^2 = S_{yy} - 2 b_1 S_{xy} + b_1^2 S_{xx} = S_{yy} - 2 b_1 (b_1 S_{xx}) + b_1^2 S_{xx} = S_{yy} - b_1^2 S_{xx}.

Take expectations of the two pieces. For any random variable, E{W2}=Var{W}+(E{W})2E\{W^2\} = \operatorname{Var}\{W\} + (E\{W\})^2.

The slope piece. We show in 2.5 Sampling behavior and the Gauss-Markov theorem that E{b1}=β1E\{b_1\} = \beta_1 and Var{b1}=σ2/Sxx\operatorname{Var}\{b_1\} = \sigma^2 / S_{xx}. Hence

E{b12Sxx}=Sxx(Var{b1}+(E{b1})2)=Sxx(σ2Sxx+β12)=σ2+β12Sxx.E\{b_1^2 S_{xx}\} = S_{xx}\left(\operatorname{Var}\{b_1\} + (E\{b_1\})^2\right) = S_{xx}\left(\frac{\sigma^2}{S_{xx}} + \beta_1^2\right) = \sigma^2 + \beta_1^2 S_{xx}.

The total piece. Write Syy=Yi2nYˉ2S_{yy} = \sum Y_i^2 - n\bar{Y}^2. Each YiY_i has mean μi=β0+β1Xi\mu_i = \beta_0 + \beta_1 X_i and variance σ2\sigma^2, so E{Yi2}=σ2+μi2E\{Y_i^2\} = \sigma^2 + \mu_i^2 and E{Yi2}=nσ2+μi2E\{\sum Y_i^2\} = n\sigma^2 + \sum \mu_i^2. The sample mean Yˉ\bar{Y} has mean μˉ=β0+β1Xˉ\bar{\mu} = \beta_0 + \beta_1 \bar{X} and variance σ2/n\sigma^2/n, so E{nYˉ2}=n(σ2/n+μˉ2)=σ2+nμˉ2E\{n\bar{Y}^2\} = n(\sigma^2/n + \bar{\mu}^2) = \sigma^2 + n\bar{\mu}^2. Subtracting,

E{Syy}=nσ2+μi2σ2nμˉ2=(n1)σ2+(μi2nμˉ2).E\{S_{yy}\} = n\sigma^2 + \sum \mu_i^2 - \sigma^2 - n\bar{\mu}^2 = (n-1)\sigma^2 + \left(\sum \mu_i^2 - n\bar{\mu}^2\right).

The final bracket is (μiμˉ)2=(β1(XiXˉ))2=β12Sxx\sum (\mu_i - \bar{\mu})^2 = \sum \big(\beta_1 (X_i - \bar{X})\big)^2 = \beta_1^2 S_{xx}. So E{Syy}=(n1)σ2+β12SxxE\{S_{yy}\} = (n-1)\sigma^2 + \beta_1^2 S_{xx}.

Combine. Subtracting the slope piece from the total piece,

E{SSE}=E{Syy}E{b12Sxx}=[(n1)σ2+β12Sxx][σ2+β12Sxx]=(n2)σ2.E\{\mathrm{SSE}\} = E\{S_{yy}\} - E\{b_1^2 S_{xx}\} = \big[(n-1)\sigma^2 + \beta_1^2 S_{xx}\big] - \big[\sigma^2 + \beta_1^2 S_{xx}\big] = (n-2)\sigma^2 .

Therefore E{MSE}=E{SSE}/(n2)=σ2E\{\mathrm{MSE}\} = E\{\mathrm{SSE}\}/(n-2) = \sigma^2: the divisor n2n-2 is exactly what makes MSE\mathrm{MSE} an unbiased estimator of σ2\sigma^2. \blacksquare

R and Python

Theorem 2.8 says the divisor n2n-2 is the one that is right on average, and repeated sampling is the way to watch that claim settle.

Each sample is 25 fresh runs from a model whose error variance really is σ2=2383.4\sigma^2 = 2383.4. The histogram collects MSE=SSE/(n2)\mathrm{MSE} = \mathrm{SSE}/(n-2), and the readouts show what each divisor gives on average.

What to notice. Individual values of MSE\mathrm{MSE} scatter widely, most of them landing anywhere from about 1200 to about 4000, yet their average walks steadily toward 2383.4. Try this. Press Draw 1000 twice and compare the two means: the n2n-2 divisor lands on σ2\sigma^2 while the nn divisor stays about 8 percent low no matter how many samples you add, which is what bias means. Back to Section 2.4.

2.5 Sampling behavior and the Gauss-Markov theorem

Intuition

The estimates b0b_0 and b1b_1 are computed from a particular sample of 25 runs. A different 25 runs, made under the same conditions, would give slightly different estimates. So b0b_0 and b1b_1 are themselves random: they have distributions, called sampling distributions, across the repeated samples we could have drawn. Figure 15 makes this concrete by simulating 60 fresh samples at the Toluca lot sizes and fitting each: the fitted lines scatter around the true mean line, and they fan out most where XX is far from Xˉ\bar{X}.

The plotting region shows sixty faint blue fitted lines, each from a different simulated sample using the same lot sizes, forming a bundle that is narrow near the middle lot size and fans out toward both ends. A bold red line, the true mean line, runs through the center of the bundle. A vertical dotted line marks the mean lot size where the bundle is tightest.

Figure 15:Sixty samples, sixty fitted lines. They cluster around the true mean line (red) with no systematic offset, and the bundle pinches in at the mean lot size and widens at the extremes, which is exactly what the variance formulas predict.

Two questions organize this section. Are b0b_0 and b1b_1 correct on average (unbiased)? And how much do they vary from sample to sample (their variances)? The Gauss-Markov theorem then delivers a strong payoff: among all the sensible estimators one might invent, least squares has the smallest variance.

Formula

Both estimators are unbiased, and their variances are

E{b1}=β1,Var{b1}=σ2Sxx;E{b0}=β0,Var{b0}=σ2(1n+Xˉ2Sxx).E\{b_1\} = \beta_1, \quad \operatorname{Var}\{b_1\} = \frac{\sigma^2}{S_{xx}}; \qquad E\{b_0\} = \beta_0, \quad \operatorname{Var}\{b_0\} = \sigma^2\left(\frac{1}{n} + \frac{\bar{X}^2}{S_{xx}}\right).

Figure 16 makes the first fact visible. When the lot sizes are bunched together, a little noise tips the slope wildly; when they are spread wide, the same noise barely moves it.

Two side-by-side plots, each showing forty faint blue fitted lines from repeated samples plus a bold red true line, with green tick marks along the bottom showing where the lot sizes sit. Left panel, bunched lot sizes near the center, small Sxx of 407: the blue lines fan out into a wide wobbly bundle, standard error of the slope 2.23. Right panel, lot sizes spread from 20 to 120, large Sxx of 10185: the blue lines cluster tightly around the true line, standard error of the slope 0.45.

Figure 16:Why spreading X out buys precision. Both panels use the same true line and the same error size; only the spread of the lot sizes differs. Wide spread (right) makes Sxx large, and since the slope variance is sigma squared over Sxx, the slope is pinned down far more tightly.

Replacing the unknown σ2\sigma^2 by its estimate MSE\mathrm{MSE} gives the estimated variances s2{b1}=MSE/Sxxs^2\{b_1\} = \mathrm{MSE}/S_{xx} and s2{b0}=MSE(1/n+Xˉ2/Sxx)s^2\{b_0\} = \mathrm{MSE}\big(1/n + \bar{X}^2/S_{xx}\big), whose square roots are the standard errors s{b1}s\{b_1\} and s{b0}s\{b_0\} that software prints.

Derivation (unbiasedness and variances)

Proof (mean and variance of b1b_1). The key is to write b1b_1 as a weighted sum of the responses. Since (XiXˉ)Yˉ=0\sum (X_i - \bar{X}) \bar{Y} = 0,

b1=SxySxx=(XiXˉ)(YiYˉ)Sxx=i=1nkiYi,ki=XiXˉSxx.b_1 = \frac{S_{xy}}{S_{xx}} = \frac{\sum (X_i - \bar{X})(Y_i - \bar{Y})}{S_{xx}} = \sum_{i=1}^n k_i Y_i, \qquad k_i = \frac{X_i - \bar{X}}{S_{xx}} .

The weights kik_i depend only on the fixed XX values and satisfy three facts, each a one-line computation:

ki=(XiXˉ)Sxx=0,kiXi=(XiXˉ)XiSxx=SxxSxx=1,ki2=(XiXˉ)2Sxx2=1Sxx.\sum k_i = \frac{\sum (X_i - \bar X)}{S_{xx}} = 0, \qquad \sum k_i X_i = \frac{\sum (X_i - \bar X)X_i}{S_{xx}} = \frac{S_{xx}}{S_{xx}} = 1, \qquad \sum k_i^2 = \frac{\sum (X_i - \bar X)^2}{S_{xx}^2} = \frac{1}{S_{xx}} .

Unbiasedness. Using E{Yi}=β0+β1XiE\{Y_i\} = \beta_0 + \beta_1 X_i,

E{b1}=kiE{Yi}=β0ki+β1kiXi=β0(0)+β1(1)=β1.E\{b_1\} = \sum k_i E\{Y_i\} = \beta_0 \sum k_i + \beta_1 \sum k_i X_i = \beta_0 (0) + \beta_1 (1) = \beta_1 .

Variance. The YiY_i are uncorrelated with common variance σ2\sigma^2, so variances of a weighted sum add with squared weights:

Var{b1}=ki2Var{Yi}=σ2ki2=σ2Sxx.\operatorname{Var}\{b_1\} = \sum k_i^2 \operatorname{Var}\{Y_i\} = \sigma^2 \sum k_i^2 = \frac{\sigma^2}{S_{xx}} . \qquad \blacksquare

Proof (mean and variance of b0b_0). Write b0=Yˉb1Xˉ=miYib_0 = \bar{Y} - b_1 \bar{X} = \sum m_i Y_i with mi=1nXˉkim_i = \tfrac{1}{n} - \bar{X} k_i. Then mi=1\sum m_i = 1 and miXi=XˉXˉ=0\sum m_i X_i = \bar X - \bar X = 0, so E{b0}=β0mi+β1miXi=β0E\{b_0\} = \beta_0 \sum m_i + \beta_1 \sum m_i X_i = \beta_0. For the variance, using ki=0\sum k_i = 0 and ki2=1/Sxx\sum k_i^2 = 1/S_{xx},

Var{b0}=σ2mi2=σ2(1nXˉki)2=σ2(1n2Xˉnki+Xˉ2ki2)=σ2(1n+Xˉ2Sxx).\operatorname{Var}\{b_0\} = \sigma^2 \sum m_i^2 = \sigma^2 \sum \left(\frac{1}{n} - \bar{X} k_i\right)^2 = \sigma^2 \left(\frac{1}{n} - \frac{2\bar X}{n}\sum k_i + \bar{X}^2 \sum k_i^2\right) = \sigma^2 \left(\frac{1}{n} + \frac{\bar{X}^2}{S_{xx}}\right). \qquad \blacksquare

Derivation (Gauss-Markov: b1b_1 is BLUE)

Proof. Consider any linear estimator β^1=ciYi\hat\beta_1^{\ast} = \sum c_i Y_i with fixed weights cic_i. Its expectation is ci(β0+β1Xi)=β0ci+β1ciXi\sum c_i(\beta_0 + \beta_1 X_i) = \beta_0 \sum c_i + \beta_1 \sum c_i X_i. For this to equal β1\beta_1 for every possible β0\beta_0 and β1\beta_1, the weights must satisfy

ci=0andciXi=1.\sum c_i = 0 \qquad \text{and} \qquad \sum c_i X_i = 1 .

Write ci=ki+dic_i = k_i + d_i, splitting each weight into the least-squares weight kik_i and a deviation did_i. Because both cic_i and kik_i satisfy the two unbiasedness constraints, the deviations satisfy di=0\sum d_i = 0 and diXi=0\sum d_i X_i = 0. Now the cross term drops out:

kidi=XiXˉSxxdi=1Sxx(XidiXˉdi)=1Sxx(00)=0.\sum k_i d_i = \sum \frac{X_i - \bar X}{S_{xx}} d_i = \frac{1}{S_{xx}}\left(\sum X_i d_i - \bar X \sum d_i\right) = \frac{1}{S_{xx}}(0 - 0) = 0 .

Therefore

Var{β^1}=σ2ci2=σ2(ki+di)2=σ2ki2+σ2di2=Var{b1}+σ2di2.\operatorname{Var}\{\hat\beta_1^{\ast}\} = \sigma^2 \sum c_i^2 = \sigma^2 \sum (k_i + d_i)^2 = \sigma^2 \sum k_i^2 + \sigma^2 \sum d_i^2 = \operatorname{Var}\{b_1\} + \sigma^2 \sum d_i^2 .

Since σ2di20\sigma^2 \sum d_i^2 \ge 0, every linear unbiased competitor has variance at least that of b1b_1, with equality only when all di=0d_i = 0, that is when the competitor is b1b_1. The least-squares slope is BLUE. The same argument with weights mim_i shows b0b_0 is BLUE. \blacksquare

R and Python

The variance formulas are not just algebra; they describe the fanning bundle of lines in Figure 15. We can confirm both the unbiasedness and the slope-variance formula by simulation, drawing 5000 samples from the estimated model and refitting each. Figure 17 shows the histogram of the 5000 slope estimates.

A histogram of 5000 simulated slope estimates, roughly bell-shaped and centered near 3.57. An overlaid normal curve with standard deviation 0.347 matches the histogram closely. A dashed red vertical line marks the true slope 3.5702 and a dotted green line marks the mean of the estimates, and the two lines nearly coincide.

Figure 17:Five thousand simulated slope estimates. Their average lands on the true slope (the dashed and dotted lines nearly coincide), confirming unbiasedness, and their spread matches the theoretical sigma over root Sxx curve, confirming the variance formula.

set.seed(4210)
beta0 <- 62.366; beta1 <- 3.5702; sigma <- 48.82
xfix <- toluca$lotsize
Sxx_fix <- sum((xfix - mean(xfix))^2)
b1_sim <- replicate(5000, {
  ysim <- beta0 + beta1 * xfix + rnorm(length(xfix), 0, sigma)
  coef(lm(ysim ~ xfix))[["xfix"]]
})
round(c(mean_of_b1_hats = mean(b1_sim),
        true_beta1 = beta1,
        sd_of_b1_hats = sd(b1_sim),
        formula_se = sqrt(sigma^2 / Sxx_fix)), 4)
mean_of_b1_hats      true_beta1   sd_of_b1_hats      formula_se
         3.5699          3.5702          0.3479          0.3469
rng = np.random.default_rng(4210)
beta0, beta1, sigma = 62.366, 3.5702, 48.82
xfix = toluca["lotsize"].to_numpy()
Sxx_fix = np.sum((xfix - xfix.mean()) ** 2)
b1_hats = np.empty(5000)
for i in range(5000):
    ysim = beta0 + beta1 * xfix + rng.normal(0, sigma, size=xfix.size)
    b1_hats[i] = np.polyfit(xfix, ysim, 1)[0]
print(round(b1_hats.mean(), 4), beta1,
      round(b1_hats.std(ddof=1), 4),
      round(np.sqrt(sigma ** 2 / Sxx_fix), 4))
3.5675 3.5702 0.352 0.3469

The average of the 5000 slope estimates sits right on β1=3.5702\beta_1 = 3.5702 (unbiasedness), and their simulated standard deviation, 0.348 in R and 0.352 in Python, matches the formula value σ/Sxx=0.347\sigma/\sqrt{S_{xx}} = 0.347. The two languages differ only in the last digit because they use different random number generators; the agreement with theory is the point.

The bundle of fitted lines in Figure 15 is easier to believe when you build it one sample at a time.

Every sample reuses the 25 Toluca lot sizes with fresh errors around the line 62.366+3.5702X62.366 + 3.5702X. The strip on top is the newest sample of work hours; the histogram below collects the fitted slope b1b_1 from all of them.

What to notice. Draw samples one at a time and no single b1b_1 equals 3.5702, yet the pile they build is centered there. Try this. Press Draw 1000 and compare the SD of b1b_1 readout with σ/Sxx=0.3469\sigma/\sqrt{S_{xx}} = 0.3469 sitting beside it: the variance formula of Theorem 2.9 is not an approximation, it is the width of this pile. Back to Section 2.5.

2.6 Maximum likelihood under normal errors

Intuition

So far we chose least squares because squared error is a reasonable penalty, and Gauss-Markov rewarded that choice with the best-linear-unbiased property, all without assuming any shape for the error distribution. Now suppose we add one assumption: the errors are normally distributed. A different principle, maximum likelihood, asks which parameter values make the data we actually observed most probable. Remarkably, under normal errors that principle points at the very same line. Figure 19 shows the two criteria, sum of squared errors and negative log-likelihood, bottoming out at exactly the same slope.

A plot with the slope b1 on the horizontal axis and two curves sharing a common minimizer. A solid blue U-shaped curve is the SSE, read on the left axis; a dashed orange U-shaped curve is the negative log-likelihood, read on the right axis. A vertical red dotted line at slope about 3.57 passes through the bottom of both curves, labeled as the shared minimizer.

Figure 19:Least squares and maximum likelihood are the same optimization dressed differently. The SSE curve and the negative log-likelihood curve reach their minimum at the identical slope, so choosing the maximum-likelihood line is choosing the least-squares line.

Formula

The normal error regression model (Definition 2.11) keeps everything from 2.1 The simple linear regression model and adds a shape:

Yi=β0+β1Xi+εi,εiiidN(0,σ2).Y_i = \beta_0 + \beta_1 X_i + \varepsilon_i, \qquad \varepsilon_i \overset{\text{iid}}{\sim} N(0, \sigma^2) .

Under normality the uncorrelated errors are now fully independent. The likelihood is the joint density of the observed YiY_i read as a function of the parameters:

L(β0,β1,σ2)=i=1n12πσ2exp ⁣((Yiβ0β1Xi)22σ2).L(\beta_0, \beta_1, \sigma^2) = \prod_{i=1}^n \frac{1}{\sqrt{2\pi\sigma^2}}\exp\!\left(-\frac{(Y_i - \beta_0 - \beta_1 X_i)^2}{2\sigma^2}\right) .

Derivation (maximum likelihood equals least squares)

Proof. Maximizing LL is easier through its logarithm, which is maximized at the same place because log\log is increasing:

lnL=n2ln(2π)n2lnσ212σ2i=1n(Yiβ0β1Xi)2.\ln L = -\frac{n}{2}\ln(2\pi) - \frac{n}{2}\ln \sigma^2 - \frac{1}{2\sigma^2}\sum_{i=1}^n (Y_i - \beta_0 - \beta_1 X_i)^2 .

Coefficients. Fix any σ2>0\sigma^2 > 0. The first two terms do not involve β0\beta_0 or β1\beta_1, and the last term has 1/(2σ2)<0-1/(2\sigma^2) < 0 multiplying (Yiβ0β1Xi)2\sum (Y_i - \beta_0 - \beta_1 X_i)^2. Maximizing lnL\ln L over β0,β1\beta_0, \beta_1 therefore means minimizing that sum of squares, which is exactly the least-squares criterion QQ from 2.2 Least squares from first principles. So the maximum-likelihood estimates of the coefficients are the least-squares estimates:

β^0MLE=b0,β^1MLE=b1.\hat\beta_0^{\text{MLE}} = b_0, \qquad \hat\beta_1^{\text{MLE}} = b_1 .

Variance. Substitute b0,b1b_0, b_1 so the sum of squares becomes SSE\mathrm{SSE}, then differentiate lnL\ln L with respect to σ2\sigma^2 and set it to zero:

lnLσ2=n2σ2+SSE2σ4=0σ^MLE2=SSEn.\frac{\partial \ln L}{\partial \sigma^2} = -\frac{n}{2\sigma^2} + \frac{\mathrm{SSE}}{2\sigma^4} = 0 \quad\Longrightarrow\quad \hat\sigma^2_{\text{MLE}} = \frac{\mathrm{SSE}}{n} .

The maximum-likelihood variance estimate divides by nn, not n2n-2. From 2.4 Estimating the error variance we know the nn divisor is biased low, understating σ2\sigma^2, because it ignores the two degrees of freedom spent on b0b_0 and b1b_1. That is why the book, and all the inference in Chapter 3, uses the unbiased MSE=SSE/(n2)\mathrm{MSE} = \mathrm{SSE}/(n-2) rather than the maximum-likelihood σ^MLE2\hat\sigma^2_{\text{MLE}}. \blacksquare

R and Python

2.7 Chapter summary

This chapter built a simple linear regression from the ground up and proved the properties that make it trustworthy. Starting from the model Yi=β0+β1Xi+εiY_i = \beta_0 + \beta_1 X_i + \varepsilon_i and its three error assumptions, we defined the least-squares criterion and derived the estimators two independent ways, by calculus through the normal equations and by an algebraic identity that shows no other line has a smaller SSE. The residuals obey exact identities that double as a correctness check; the error variance is estimated by MSE=SSE/(n2)\mathrm{MSE} = \mathrm{SSE}/(n-2), whose n2n-2 divisor is exactly what makes it unbiased. Treating b0b_0 and b1b_1 as random over repeated samples gave their means and variances, the Gauss-Markov theorem showed they are best among linear unbiased estimators with no normality needed, and maximum likelihood under normal errors reproduced the very same line.

Key results at a glance

ResultStatement or formulaValid when
Least-squares estimates (Theorem 2.4)b1=Sxy/Sxxb_1 = S_{xy}/S_{xx},  b0=Yˉb1Xˉ\ b_0 = \bar{Y} - b_1\bar{X}the XiX_i are not all equal
Residual identities (Theorem 2.6)ei=0\sum e_i = 0,  Xiei=0\ \sum X_i e_i = 0,  Y^iei=0\ \sum \hat{Y}_i e_i = 0,  Y^i=Yi\ \sum \hat{Y}_i = \sum Y_iany least-squares fit with an intercept
Error variance estimates2=MSE=SSE/(n2)s^2 = \mathrm{MSE} = \mathrm{SSE}/(n-2), with SSE=Syyb12Sxx\mathrm{SSE} = S_{yy} - b_1^2 S_{xx}SLR model
Unbiasedness of MSE (Theorem 2.8)E{SSE}=(n2)σ2E\{\mathrm{SSE}\} = (n-2)\sigma^2, so E{MSE}=σ2E\{\mathrm{MSE}\} = \sigma^2mean-zero, constant-variance, uncorrelated errors
Means and variances (Theorem 2.9)E{b1}=β1, Var{b1}=σ2/SxxE\{b_1\}=\beta_1,\ \operatorname{Var}\{b_1\}=\sigma^2/S_{xx};  E{b0}=β0, Var{b0}=σ2(1/n+Xˉ2/Sxx)\ E\{b_0\}=\beta_0,\ \operatorname{Var}\{b_0\}=\sigma^2(1/n+\bar{X}^2/S_{xx})SLR model
Gauss-Markov theorem (Theorem 2.10)b0,b1b_0, b_1 are BLUE (minimum variance among linear unbiased estimators)SLR model, no normality needed
Maximum likelihood (Theorem 2.12)MLE coefficients =b0,b1= b_0, b_1;  σ^MLE2=SSE/n\ \hat\sigma^2_{\text{MLE}} = \mathrm{SSE}/nnormal error model

For the Toluca data these give b0=62.37b_0 = 62.37, b1=3.5702b_1 = 3.5702, SSE=54,825\mathrm{SSE} = 54{,}825, MSE=2384\mathrm{MSE} = 2384, s=48.82s = 48.82, s{b1}=0.347s\{b_1\} = 0.347, s{b0}=26.18s\{b_0\} = 26.18, and σ^MLE2=2193\hat\sigma^2_{\text{MLE}} = 2193.

Key terms

Simple linear regression model, regression function, mean response, random error, least-squares criterion, least-squares estimates, normal equations, fitted value, residual, error sum of squares (SSE), mean square error (MSE), homoscedasticity, unbiased estimator, Gauss-Markov theorem, best linear unbiased estimator (BLUE), normal error regression model, maximum likelihood.

You should now be able to

Where this fits. This chapter is the first full trip around the workflow of The modeling workflow. It carried the Toluca question through ASK and EXPLORE, settled into FIT, and took first steps into CHECK (the residual identities and the residual plot) and USE (reading the slope in hours per unit). Chapter 1 argued that regression is a way of thinking about how one quantity moves with another; this chapter turned that idea into a specific, computable line and proved the properties that make it trustworthy. Everything here is point estimation: single best guesses for β0\beta_0, β1\beta_1, and σ2\sigma^2. What we have not yet done is quantify uncertainty with intervals and tests. That is Chapter 3, which takes the sampling distributions sketched here, adds the normal error assumption in full, and builds tt intervals for the slope and intercept, the ANOVA decomposition (3.6 The analysis of variance approach), the FF test (3.7 The F test and its equivalence to the t test), and the difference between a confidence interval for a mean response and a prediction interval for a new run (3.5 Prediction interval for a new observation). The exact Toluca fit set here (b1=3.5702b_1 = 3.5702, MSE=2384\mathrm{MSE} = 2384) carries forward unchanged, now with inference attached. It travels further still: Chapter 5 reruns the same slope through a permutation test (5.2 The permutation test for the slope) and checks its p-value against Chapter 3’s, and in Chapter 7 the same estimators reappear in matrix form (7.1 The model and least squares in matrix form), where the two-line normal equations become a single equation XXb=XY\mathbf{X}'\mathbf{X}\mathbf{b} = \mathbf{X}'\mathbf{Y} and the Gauss-Markov proof (7.6 The Gauss-Markov theorem) generalizes to any number of predictors at once.

2.8 Frequently asked questions

Q1. Why square the residuals instead of taking absolute values? Both are reasonable losses. Squaring makes the objective smooth and differentiable, so calculus gives a clean closed-form solution (the normal equations), and it is exactly the loss for which the Gauss-Markov and normal-likelihood results hold. Absolute-value loss gives “least absolute deviations” regression, which is more resistant to outliers but has no simple formula and needs iterative solving. The course starts with squared error because it is where the theory is cleanest.

Q2. Is the intercept b0=62.37b_0 = 62.37 really “hours to produce zero units”? No, not meaningfully. A lot size of zero is far outside the data (the smallest run was 20 units), so the intercept is an extrapolation. Read it as the number that anchors the line’s height, computed so the line passes through (Xˉ,Yˉ)(\bar{X}, \bar{Y}), not as a physical setup time. Interpreting an intercept literally only makes sense when X=0X = 0 is inside or near the observed range.

Q3. What is the difference between an error εi\varepsilon_i and a residual eie_i? The error εi=Yi(β0+β1Xi)\varepsilon_i = Y_i - (\beta_0 + \beta_1 X_i) is the gap to the true, unknown line, and we can never observe it. The residual ei=YiY^ie_i = Y_i - \hat{Y}_i is the gap to our estimated line, and we compute it directly. Residuals are our visible estimates of the invisible errors, which is why we estimate σ2\sigma^2 from them.

Q4. Why divide SSE by n2n-2 and not n1n-1 like an ordinary sample variance? A sample variance divides by n1n-1 because it estimates one quantity, the mean, from the data. Regression estimates two, the intercept and slope, before measuring residuals, so it spends two degrees of freedom, leaving n2n-2. The derivation in 2.4 Estimating the error variance shows E{SSE}=(n2)σ2E\{\mathrm{SSE}\} = (n-2)\sigma^2 exactly, so n2n-2 is the divisor that makes MSE\mathrm{MSE} unbiased.

Q5. Do I need the errors to be normal for least squares to work? No. The least-squares estimates, their unbiasedness, their variance formulas, and the Gauss-Markov optimality all hold with no assumption about the error distribution’s shape, only mean zero, constant variance, and uncorrelated errors. Normality is an extra assumption, added in 2.6 Maximum likelihood under normal errors and used in Chapter 3 to build exact tt and FF intervals and tests.

Q6. Why does the bundle of fitted lines in Figure 15 pinch in the middle? Every least-squares line passes through (Xˉ,Yˉ)(\bar{X}, \bar{Y}) (residual property 5), so near Xˉ\bar{X} the lines can only wobble a little. Away from Xˉ\bar{X}, a small change in the slope pivots the line a large vertical distance, so the lines fan out. The variance of a fitted value grows with distance from Xˉ\bar{X}, which Chapter 3 turns into the curved shape of a confidence band.

Q7. R prints 4.45e-10 and Python prints 4.45e-10 for the slope’s p-value, but we did not assume normality. Where did that come from? Those p-values and the standard errors’ t-values do use the normal error model, and we will justify them properly in Chapter 3. This chapter shows the standard errors themselves, which come from the variance formulas and need no normality; the significance stars and p-values are a preview of the inference to come.

2.9 Practice problems

  1. (A) State the simple linear regression model and list its three assumptions on the errors, saying in one phrase what each assumption guarantees.

  2. (A) Explain the difference between the regression function E{Y}=β0+β1XE\{Y\} = \beta_0 + \beta_1 X and a fitted line Y^=b0+b1X\hat{Y} = b_0 + b_1 X. Which involves parameters and which involves estimates?

  3. (A) In the Toluca fit, interpret the slope 3.5702 in the units of the problem. Why is it unsafe to use the line to predict hours for a lot of 500 units?

  4. (A) Give a one-sentence interpretation of s=48.82s = 48.82 hours for a manager who has never taken statistics.

  5. (A) Why is the least-squares intercept for the Toluca data not a meaningful “setup time”? What would have to be true of the data for an intercept to be interpretable?

  6. (A) Without computing, state the values of ei\sum e_i and Xiei\sum X_i e_i for any least-squares fit with an intercept, and say which normal equation gives each.

  7. (A) A fitted line has ei=0\sum e_i = 0 but Xiei=120\sum X_i e_i = 120. Can it be a least-squares fit? Explain.

  8. (A) Explain why spreading the predictor values farther apart (increasing SxxS_{xx}) makes the slope estimate more precise, referring to its variance formula.

  9. (B) Starting from Q(b0,b1)=(Yib0b1Xi)2Q(b_0, b_1) = \sum (Y_i - b_0 - b_1 X_i)^2, derive both normal equations by differentiation, and solve them for b0b_0 and b1b_1 (Theorem 2.4).

  10. (B) Prove the algebraic identity (YiacXi)2=SSE+gi2\sum (Y_i - a - cX_i)^2 = \mathrm{SSE} + \sum g_i^2 for an arbitrary line (a,c)(a, c), identifying gig_i and showing the cross term is zero. Conclude that least squares is the unique minimizer.

  11. (B) Show that the least-squares line passes through (Xˉ,Yˉ)(\bar{X}, \bar{Y}), and that Y^i=Yi\sum \hat{Y}_i = \sum Y_i (Theorem 2.6).

  12. (B) Writing b1=kiYib_1 = \sum k_i Y_i with ki=(XiXˉ)/Sxxk_i = (X_i - \bar{X})/S_{xx}, prove ki=0\sum k_i = 0, kiXi=1\sum k_i X_i = 1, and ki2=1/Sxx\sum k_i^2 = 1/S_{xx}, then use them to show E{b1}=β1E\{b_1\} = \beta_1 and Var{b1}=σ2/Sxx\operatorname{Var}\{b_1\} = \sigma^2/S_{xx}.

  13. (B) Derive Var{b0}=σ2(1/n+Xˉ2/Sxx)\operatorname{Var}\{b_0\} = \sigma^2(1/n + \bar{X}^2/S_{xx}) by writing b0=miYib_0 = \sum m_i Y_i with mi=1/nXˉkim_i = 1/n - \bar{X}k_i.

  14. (B) Prove the Gauss-Markov theorem for the slope (Theorem 2.10): any linear unbiased estimator ciYi\sum c_i Y_i of β1\beta_1 has variance at least σ2/Sxx\sigma^2/S_{xx}, with equality only for b1b_1.

  15. (B) Derive E{SSE}=(n2)σ2E\{\mathrm{SSE}\} = (n-2)\sigma^2 (Theorem 2.8) using SSE=Syyb12Sxx\mathrm{SSE} = S_{yy} - b_1^2 S_{xx} and the facts E{b1}=β1E\{b_1\} = \beta_1, Var{b1}=σ2/Sxx\operatorname{Var}\{b_1\} = \sigma^2/S_{xx}.

  16. (B) Write down the log-likelihood for the normal error model and show that maximizing it over β0,β1\beta_0, \beta_1 is equivalent to least squares (Theorem 2.12). Then derive σ^MLE2=SSE/n\hat\sigma^2_{\text{MLE}} = \mathrm{SSE}/n.

  17. (B) Show that σ^MLE2=SSE/n\hat\sigma^2_{\text{MLE}} = \mathrm{SSE}/n is biased for σ2\sigma^2, and compute the exact bias in terms of σ2\sigma^2 and nn.

  18. (B) Prove the decomposition SSE=Syyb12Sxx\mathrm{SSE} = S_{yy} - b_1^2 S_{xx} starting from ei=(YiYˉ)b1(XiXˉ)e_i = (Y_i - \bar{Y}) - b_1(X_i - \bar{X}).

  19. (B) A student proposes estimating the slope by the ratio of means Yˉ/Xˉ\bar{Y}/\bar{X} (a line through the origin). Show this is generally biased for β1\beta_1, and explain which model assumption it implicitly, and wrongly, imposes.

  20. (C) Read toluca.csv in R or Python, fit the model, and reproduce b0b_0, b1b_1, and ss. Confirm b1=Sxy/Sxxb_1 = S_{xy}/S_{xx} from the raw sums.

  21. (C) Compute the residuals in software and verify numerically that ei=0\sum e_i = 0, Xiei=0\sum X_i e_i = 0, and Y^i=Yi\sum \hat{Y}_i = \sum Y_i. Report the three sums.

  22. (C) Using toluca_mini.csv, compute b0b_0 and b1b_1 by hand (show the deviation table), then reproduce them in software. Explain why they differ from the full-data estimates.

  23. (C) Predict the mean work hours for lot sizes 40, 80, and 120 using the fitted line, and mark which of these are interpolation and which (if any) are extrapolation.

  24. (C) Fit the model, extract MSE\mathrm{MSE} two ways (from summary / fit.summary() and by hand as SSE/(n2)\mathrm{SSE}/(n-2)), and confirm they match. Then compute σ^MLE2=SSE/n\hat\sigma^2_{\text{MLE}} = \mathrm{SSE}/n and report the percentage difference.

  25. (C) Write a short simulation (seed 4210) that draws 2000 samples from the fitted model at the Toluca lot sizes, refits each, and reports the mean and standard deviation of the 2000 intercept estimates b0b_0. Compare the standard deviation to the formula s{b0}s\{b_0\}.

  26. (C) Make the residuals-versus-lot-size plot in R or Python. Describe what a violation of the constant-variance assumption would look like in this plot, and say whether the Toluca residuals show it.

  27. (C) Reverse the roles: regress lotsize on hours. Show that the new slope is not the reciprocal of the original slope, and explain why regressing YY on XX and XX on YY give different lines.

  28. (B) An analyst has only the summary numbers Xˉ=70\bar{X} = 70, Yˉ=312.28\bar{Y} = 312.28, Sxx=19,800S_{xx} = 19{,}800, and Sxy=70,690S_{xy} = 70{,}690, with the raw data lost. Can they recover b0b_0 and b1b_1? Can they recover MSE\mathrm{MSE}? Explain what is and is not reconstructable from these four numbers.

2.10 Exam practice

The problems below are written in the style of this course’s exams: each asks you to explain in full sentences, not just to produce a number. On the real exam a bare answer earns little credit, and correct reasoning with a small slip earns most of it, so practice writing the reasoning out. Work each one on paper before opening its model answer. Where output is shown, it is genuine software output from toluca.csv.

EP 2.1. Explain why the line describes the mean, not the response. A first-time reader says “the model claims work hours are a straight-line function of lot size.” Explain why that is not what the simple linear regression model claims, and state precisely which quantity the straight line does describe. Then explain what job the constant-variance assumption Var{εi}=σ2\operatorname{Var}\{\varepsilon_i\} = \sigma^2 does that the mean-line equation alone does not.

EP 2.2. A student claims least squares cannot be beaten. A student writes: “The Gauss-Markov theorem proves that b1b_1 has the smallest variance of any estimator of β1\beta_1, so it is impossible to estimate the slope more precisely than least squares does.” Evaluate this claim. Where it overreaches, state exactly what the theorem does and does not promise.

EP 2.3. Interpret the reverse regression in context. An analyst reverses the chapter’s fit and regresses lot size on work hours, reusing toluca.csv. The genuine R and Python output is below.

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.85825    7.41053  -0.251    0.804
hours        0.23011    0.02236  10.290 4.45e-10 ***
---
Residual standard error: 12.4 on 23 degrees of freedom
Multiple R-squared:  0.8215,	Adjusted R-squared:  0.8138
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     -1.8583      7.411     -0.251      0.804     -17.188      13.472
hours          0.2301      0.022     10.290      0.000       0.184       0.276

(a) Interpret the reverse slope 0.2301 in the units of this problem. (b) The forward fit (hours on lot size) had slope 3.5702, whose reciprocal is 1/3.5702=0.2801/3.5702 = 0.280. Explain why the reverse slope 0.2301 is not that reciprocal. (c) Both directions report the same R2=0.8215R^2 = 0.8215. Explain why that agreement is expected, and say what the product of the two slopes equals.

EP 2.4. What would change if the lot sizes were bunched? The engineers happened to record lot sizes spread from 20 to 120 units, giving Sxx=19,800S_{xx} = 19{,}800. Suppose instead every run had been scheduled between 60 and 80 units, so the same 25 runs carried a much smaller SxxS_{xx}, about 3,0003{,}000, with the true line and σ2\sigma^2 unchanged. Explain the direction of the change, and the reason, for (a) the slope’s standard error, (b) the spread of the bundle of plausible fitted lines, and (c) your ability to interpret the intercept b0b_0. Then say what would not change.

EP 2.5. Two errors in a hand-fit argument. A classmate fits a line by hand, reports residuals that sum to exactly zero, and concludes: “Since ei=0\sum e_i = 0, my fit must be the least-squares line, and I can now estimate σ2\sigma^2 by dividing SSE by nn.” Identify the two separate errors in this reasoning, and give the correct statement in each case.

Chapter game