From Raw Observations to a Fitted Line
A fitted line can be calculated directly from a short list of observed predictor-response pairs. The important work is orderly arithmetic: calculate each variable’s sample mean, center the observations by subtracting those means, and combine the centered values to obtain the slope. The intercept then places the line at the correct vertical level.
The procedure summarizes the observed relationship between a quantitative predictor \(x\) and a quantitative response \(y\). The line describes a straight-line pattern in the data; by itself, it does not establish that changes in \(x\) cause changes in \(y\).
For \(n\) observed pairs, the sample means are \(\bar{x}=\sum x_i/n\) and \(\bar{y}=\sum y_i/n\). For each pair, calculate the centered values \(x_i-\bar{x}\) and \(y_i-\bar{y}\). The least-squares slope is the sum of the products of those centered values divided by the sum of squared centered predictor values; the intercept is found from the two means.
The numerator in the slope formula accumulates whether the two variables tend to be above or below their means together. The denominator measures the predictor’s centered variation. The resulting slope is expressed in response units per predictor unit; the intercept is expressed in response units.
A Reliable Hand-Calculation Routine
Use a table to keep the calculations visible and to reduce sign errors. The columns for centered values and their products are not extra observations: they are arithmetic steps based on the original pairs.
Write one row for each observed unit, with its predictor value \(x_i\) and response value \(y_i\).
Add the \(x\)-values and divide by \(n\) to obtain \(\bar{x}\). Do the same with the \(y\)-values to obtain \(\bar{y}\).
For every row, subtract \(\bar{x}\) from \(x_i\) and \(\bar{y}\) from \(y_i\).
Multiply the centered values within each row and add the products. Separately square each centered \(x\)-value and add those squares.
Divide the centered-product sum by the centered-\(x\)-square sum for \(b_1\), calculate \(b_0=\bar{y}-b_1\bar{x}\), and write \(\hat{y}=b_0+b_1x\).
This is the centered-data slope formula for least squares. The short check for the intercept is that the fitted line passes through the data centroid \((\bar{x},\bar{y})\), a result established in the earlier material on the data centroid. Substituting \(x=\bar{x}\) into the fitted line gives \(b_0+b_1\bar{x}\). Choosing \(b_0=\bar{y}-b_1\bar{x}\) makes this value exactly \(\bar{y}\). Thus the calculated intercept puts the line through the centroid.
Worked Calculations
Worked Example: Study Time and Quiz Score
Suppose a hypothetical instructor records study time and quiz score for five students. Study time is measured in hours and quiz score in points. The observations are \((1,52),(2,55),(3,61),(4,64),(5,68)\). Find the least-squares fitted line.
First calculate the means:
For each observation, subtract its mean values, multiply the centered values, and square the centered predictor value.
| \(x_i\) (hours) | \(y_i\) (points) | \(x_i-\bar{x}\) | \(y_i-\bar{y}\) | Centered product | \((x_i-\bar{x})^2\) |
|---|---|---|---|---|---|
| 1 | 52 | -2 | -8 | 16 | 4 |
| 2 | 55 | -1 | -5 | 5 | 1 |
| 3 | 61 | 0 | 1 | 0 | 0 |
| 4 | 64 | 1 | 4 | 4 | 1 |
| 5 | 68 | 2 | 8 | 16 | 4 |
| Sum | 41 | 10 |
The centered-product sum is \(16+5+0+4+16=41\) point-hours, and the centered-\(x\)-square sum is \(4+1+0+1+4=10\) hours squared. Therefore:
The fitted line is \(\hat{y}=47.7+4.1x\), with \(x\) in hours and \(\hat{y}\) in points. In this hypothetical data set, the fitted response increases by 4.1 points for each additional hour of study time. The line passes through \((3,60)\), as a check: \(47.7+(4.1)(3)=60\).
Worked Example: A Small Dataset with a Simple Slope
Consider hypothetical measurements of daily practice time \(x\), in hours, and a performance score \(y\), in points: \((2,10),(4,13),(6,12),(8,17)\). Calculate the fitted line from the raw observations.
The means are \(\bar{x}=(2+4+6+8)/4=20/4=5\) hours and \(\bar{y}=(10+13+12+17)/4=52/4=13\) points.
| \(x_i\) (hours) | \(y_i\) (points) | \(x_i-\bar{x}\) | \(y_i-\bar{y}\) | Centered product | \((x_i-\bar{x})^2\) |
|---|---|---|---|---|---|
| 2 | 10 | -3 | -3 | 9 | 9 |
| 4 | 13 | -1 | 0 | 0 | 1 |
| 6 | 12 | 1 | -1 | -1 | 1 |
| 8 | 17 | 3 | 4 | 12 | 9 |
| Sum | 20 | 20 |
The centered-product sum is \(9+0-1+12=20\) point-hours. The centered-\(x\)-square sum is \(9+1+1+9=20\) hours squared. Substitution gives:
Thus the least-squares line is \(\hat{y}=8+1x\). The intercept is 8 points, and the slope is 1 point per hour. A centroid check gives \(8+(1)(5)=13\), the observed mean performance score.
Worked Example: Measurements with a Fractional Slope
Suppose a hypothetical set of four containers has widths \(x\), in centimeters, and volumes \(y\), in milliliters, recorded as \((10,18),(20,25),(30,24),(40,33)\). Find the least-squares line.
The mean width is \(\bar{x}=(10+20+30+40)/4=100/4=25\) cm. The mean volume is \(\bar{y}=(18+25+24+33)/4=100/4=25\) mL.
| \(x_i\) (cm) | \(y_i\) (mL) | \(x_i-\bar{x}\) | \(y_i-\bar{y}\) | Centered product | \((x_i-\bar{x})^2\) |
|---|---|---|---|---|---|
| 10 | 18 | -15 | -7 | 105 | 225 |
| 20 | 25 | -5 | 0 | 0 | 25 |
| 30 | 24 | 5 | -1 | -5 | 25 |
| 40 | 33 | 15 | 8 | 120 | 225 |
| Sum | 220 | 500 |
The centered-product sum is \(105+0-5+120=220\) mL-centimeters, and the centered-\(x\)-square sum is \(225+25+25+225=500\) square centimeters. Hence:
The fitted line is \(\hat{y}=14+0.44x\), where \(x\) is width in centimeters and \(\hat{y}\) is fitted volume in milliliters. The centroid check is \(14+(0.44)(25)=25\) mL, which agrees with the sample mean response.
Checks, Interpretation, and a Common Pitfall
Before reporting a hand-calculated line, check that the centered-product sum and centered-\(x\)-square sum were added correctly, that the denominator is positive, and that the coefficient units make sense. A positive centered-product sum gives a positive slope, while a negative sum gives a negative slope, provided the predictor varies. The intercept should also make the fitted line pass through \((\bar{x},\bar{y})\).
The intercept is the fitted response when \(x=0\). Whether that value has a useful interpretation depends on the context and on whether zero is meaningful and relevant for the predictor. The slope describes the change in fitted response for a one-unit increase in the predictor; it describes a linear association, not necessarily a causal effect.
- Common mistake: using uncentered values in the slope formula. A calculation such as \(\sum x_i y_i/\sum x_i^2\) is not the centered-data slope formula for the usual line with an intercept. First subtract \(\bar{x}\) and \(\bar{y}\) for each observation, then form the numerator and denominator.
- Common mistake: calculating the intercept from the slope alone. The intercept is not generally zero. Use \(b_0=\bar{y}-b_1\bar{x}\), which ensures the line passes through the data centroid.
- Common mistake: dropping units or overstating meaning. Report a slope in response units per predictor unit and describe an observed linear relationship without claiming that the predictor caused the response.
Check Your Understanding
Try these questions without looking back at the worked calculations.
- For observations \((1,4),(2,5),(3,9)\), calculate \(\bar{x}\), \(\bar{y}\), the centered-product sum, and the centered-\(x\)-square sum.
- Using your sums from question 1, calculate the least-squares slope and intercept, then write the fitted line with units of \(y\) per unit of \(x\).
- Why must the observed predictor values vary for the centered-data slope formula to give a fitted line?
- A student calculates the intercept as zero without checking the data means. Which formula should be used instead, and what useful check does it provide?
- If the fitted slope is positive, what does it say about the fitted response as the predictor increases? Why does it not, by itself, prove causation?