From the Normal Equations to the Fitted Line
For paired observations, the least-squares line is the line that minimizes the sum of squared vertical differences between the observed responses and the fitted responses. Finding this line requires two coefficients: the slope, which determines how the fitted response changes with the predictor, and the intercept, which sets the fitted response when the predictor is zero.
The earlier material on deriving the least-squares normal equations establishes the equations that a minimizing line must satisfy. Here we solve those equations and connect their solution to means, centered sums, sample covariance, and sample variance. The focus is calculation and algebra; a probability model for the observations is not required to compute the least-squares coefficients.
For a unique slope and intercept, the observed predictor values must not all be equal. If every \(x_i\) has the same value, the data do not distinguish among slopes: multiple lines with different slopes can give the same fitted values at that one predictor value. No distributional assumption, such as normality, is needed for the algebraic least-squares fit.
Solving the Equations
The normal equations for minimizing \(Q(a,b)\) are
Define the sample means \(\bar{x}=\frac{1}{n}\sum_{i=1}^{n}x_i\) and \(\bar{y}=\frac{1}{n}\sum_{i=1}^{n}y_i\). Also define the centered predictor sum of squares \(S_{xx}\) and centered cross-product sum \(S_{xy}\) by
The first normal equation immediately gives \(\hat{\beta}_0=\bar{y}-\hat{\beta}_1\bar{x}\). Substituting this expression into the second equation gives the slope in terms of centered quantities. Thus the practical solution is:
Find \(\bar{x}\) and \(\bar{y}\) from the observed predictor and response values.
Compute \(S_{xx}\) and \(S_{xy}\), keeping the paired \(x_i\) and \(y_i\) values together.
Use \(\hat{\beta}_1=S_{xy}/S_{xx}\), followed by \(\hat{\beta}_0=\bar{y}-\hat{\beta}_1\bar{x}\).
Substitute the coefficients into the normal equations or verify that the fitted value at \(\bar{x}\) equals \(\bar{y}\).
Why the Centered-Sum Formula Holds
The centered-sum expression is not a separate fitting rule; it is the result of solving the normal equations. From the first equation, \(\hat{\beta}_0=\bar{y}-\hat{\beta}_1\bar{x}\). Put this into the second equation:
Since \(\sum x_i=n\bar{x}\), rearranging yields
Expanding the definitions of the centered sums gives \(S_{xy}=\sum x_i y_i-n\bar{x}\bar{y}\) and \(S_{xx}=\sum x_i^2-n\bar{x}^2\), so this is exactly \(\hat{\beta}_1=S_{xy}/S_{xx}\). The denominator is positive precisely when the predictor values are not all equal, which explains the condition for a unique slope.
There is also a useful covariance interpretation. If \(s_{xy}=S_{xy}/(n-1)\) is the sample covariance and \(s_x^2=S_{xx}/(n-1)\) is the sample variance of the predictor, then
The factors \(n-1\) cancel because both quantities use the same divisor. The same ratio results if both are defined using divisor \(n\). A covariance calculated with one divisor must not be divided by a sum of squares using a different scale without accounting for that difference.
Worked Calculations
Worked Example: Solving the Normal Equations from Four Observations
In a hypothetical operations study, a supervisor records the number of hours of practice \(x\) and a processing score \(y\) for four trainees. The observed pairs are \((1,4),(2,5),(3,5),(4,10)\). Find the least-squares line by solving the normal equations, and check the result.
Method. First calculate the totals needed in the normal equations:
The normal equations are \(4\hat{\beta}_0+10\hat{\beta}_1=24\) and \(10\hat{\beta}_0+30\hat{\beta}_1=69\). Multiply the first equation by \(2.5\), giving \(10\hat{\beta}_0+25\hat{\beta}_1=60\). Subtract this from the second equation:
Substitute into the first equation: \(4\hat{\beta}_0+10(1.8)=24\), so \(4\hat{\beta}_0=6\) and \(\hat{\beta}_0=1.5\). The fitted line is \(\hat{y}=1.5+1.8x\).
Check. In the first normal equation, \(4(1.5)+10(1.8)=6+18=24=\sum y_i\). In the second, \(10(1.5)+30(1.8)=15+54=69=\sum x_i y_i\). Both equations hold. The fitted value at the mean predictor is also \(1.5+1.8(2.5)=6\), which equals \(\bar{y}=24/4=6\).
Common mistake avoided. The coefficient of \(\hat{\beta}_1\) in the second equation is \(\sum x_i^2=30\), not \((\sum x_i)^2=100\). Squaring the sum in place of summing squares changes the equation and gives an incorrect fit.
Worked Example: Computing the Fit from Centered Values
In a hypothetical study of a new work procedure, the predictor is training time in hours and the response is a daily output score. The five observed pairs are \((0,3),(2,4),(4,8),(6,7),(8,13)\). Find the least-squares slope and intercept using centered sums.
The means are \(\bar{x}=(0+2+4+6+8)/5=4\) and \(\bar{y}=(3+4+8+7+13)/5=35/5=7\). The centered values and products are:
Therefore \(S_{xy}=16+6+0+0+24=46\), while
The slope is \(\hat{\beta}_1=46/40=1.15\). The intercept is \(\hat{\beta}_0=7-(1.15)(4)=7-4.6=2.4\). Thus the fitted line is \(\hat{y}=2.4+1.15x\).
Check. The line gives \(\hat{y}=2.4+1.15(4)=7\) at \(x=\bar{x}=4\), matching \(\bar{y}\). The raw totals are \(\sum x_i=20\), \(\sum y_i=35\), \(\sum x_i^2=120\), and \(\sum x_i y_i=186\). The normal-equation left and right sides are \(35\) and \(5(2.4)+1.15(20)=35\) for the first equation; for the second they are \(186\) and \(2.4(20)+1.15(120)=48+138=186\).
Common mistake avoided. The intercept is not the mean response, and it is not generally the response observed at the smallest \(x\). It is \(\bar{y}-\hat{\beta}_1\bar{x}=2.4\); the fitted line passes through the mean point \((4,7)\).
Worked Example: Using Sample Covariance and Sample Variance
A hypothetical analyst studies four equipment settings \(x\) and resulting response measurements \(y\), recording \((1,8),(3,5),(4,6),(6,1)\). Find the fitted line using sample covariance and sample variance, and explain why their denominators do not alter the slope.
The means are \(\bar{x}=(1+3+4+6)/4=3.5\) and \(\bar{y}=(8+5+6+1)/4=5\). The centered products are
Thus \(S_{xy}=-7.5+0+0.5-10=-17\). The predictor sum of squares is
Since \(n-1=3\), the sample covariance is \(s_{xy}=-17/3\), and the sample variance is \(s_x^2=13/3\). Therefore
The intercept is
To four decimal places, the fitted line is \(\hat{y}=9.5769-1.3077x\). Check: at \(\bar{x}=3.5\), the exact fitted value is \(249/26-(17/13)(7/2)=(249-119)/26=5=\bar{y}\).
Common mistake avoided. Dividing the sample covariance \(-17/3\) by the unscaled sum \(S_{xx}=13\) would give \(-17/39\), which is wrong. Use either \(S_{xy}/S_{xx}\), or divide the sample covariance by the sample variance; do not mix the two conventions.
Interpretation, Conditions, and Exam Traps
The formulas describe the least-squares line for the observed data. They do not, by themselves, establish that the response changes causally with the predictor, that a linear relationship is appropriate outside the observed range, or that a statistical model's assumptions hold. Those are separate questions from solving for the coefficients. The slope is measured in response units per predictor unit, while the intercept is measured in response units.
For calculation, the key condition is \(S_{xx}>0\). If \(S_{xx}=0\), then every predictor equals \(\bar{x}\), the denominator in the slope formula is zero, and the slope cannot be uniquely determined from the observations. When \(S_{xx}>0\), the fitted coefficients satisfy both normal equations and minimize the sum of squared residuals.
Common exam errors usually come from mixing equivalent formulas or losing a sign. Keep the following distinctions explicit:
- Sum of squares versus squared sum. \(S_{xx}=\sum x_i^2-n\bar{x}^2\), not \((\sum x_i)^2\).
- Cross-product versus covariance. \(S_{xy}\) is a sum; sample covariance divides it by \(n-1\). The matching predictor sample variance also divides by \(n-1\).
- Intercept after slope. Calculate \(\hat{\beta}_0=\bar{y}-\hat{\beta}_1\bar{x}\) only after finding the slope, preserving the sign of \(\hat{\beta}_1\).
- Pairing matters. Each \(x_i\) must remain paired with its corresponding \(y_i\) when calculating \(\sum x_i y_i\) or \(S_{xy}\).
Check Your Understanding
For each problem, calculate the requested least-squares coefficients and check your result using the indicated summary or centered quantities.
- A small study records the pairs \((1,4),(2,7),(3,5),(4,10)\). Calculate \(S_{xy}\), \(S_{xx}\), the slope, and the intercept. (Answer: \(S_{xy}=8\), \(S_{xx}=5\), \(\hat{\beta}_1=1.6\), \(\hat{\beta}_0=2.5\).)
- For the pairs \((2,11),(4,9),(6,8),(8,4)\), calculate the least-squares line from centered sums and verify that its value at \(\bar{x}\) equals \(\bar{y}\). (Answer: \(\bar{x}=5\), \(\bar{y}=8\), \(S_{xy}=-22\), \(S_{xx}=20\), \(\hat{\beta}_1=-1.1\), \(\hat{\beta}_0=13.5\); the fitted value at \(x=5\) is \(8\).)
- A data set has \(n=4\), \(\sum x_i=12\), \(\sum y_i=20\), \(\sum x_i^2=44\), and \(\sum x_i y_i=52\). Solve the normal equations and check both equations. (Answer: \(\hat{\beta}_1=-1\), \(\hat{\beta}_0=8\); \(4(8)+12(-1)=20\) and \(12(8)+44(-1)=52\).)
- A sample has \(\bar{x}=3\), \(\bar{y}=10\), sample covariance \(s_{xy}=-6\), and sample variance \(s_x^2=4\). Find the least-squares line. Explain why using the sample covariance directly as the slope would be wrong. (Answer: \(\hat{\beta}_1=-6/4=-1.5\), \(\hat{\beta}_0=10-(-1.5)(3)=14.5\); the slope is a covariance-to-variance ratio, not the covariance alone.)
- For observations with \(n=5\), suppose \(S_{xy}=12\) and \(S_{xx}=8\), while \(\bar{x}=2\) and \(\bar{y}=6\). Calculate the coefficients and give the fitted value at the predictor mean. (Answer: \(\hat{\beta}_1=12/8=1.5\), \(\hat{\beta}_0=6-(1.5)(2)=3\); the fitted value at \(x=2\) is \(6\).)