Tutorials › Regression Analysis (Undergraduate) › Hand-Fitting a Line from Raw Observations

Fitting a Simple Linear Regression · Tutorial 79 of 991

Hand-Fitting a Line from Raw Observations

Work from raw observations to a least-squares fitted line, keeping each mean, centered product, and coefficient calculation visible.

Beginner 9 min read

What You'll Learn

  • Organize raw predictor-response pairs for a hand calculation.
  • Calculate sample means and deviations from those means.
  • Find the centered cross-product sum and the predictor’s centered sum of squares.
  • Calculate the least-squares slope and intercept from the raw data.
  • Write and interpret the fitted line with the variables’ units.

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\).

Model and calculation conditions: For observations \((x_i,y_i)\), the simple linear regression model represents the response with a straight-line component, \(y_i=\beta_0+\beta_1x_i+\varepsilon_i\). In hand-fitting, the least-squares fitted line is \(\hat{y}=b_0+b_1x\), where \(b_0\) and \(b_1\) are calculated from the observed data. The arithmetic requires quantitative \(x\) and \(y\), and at least two distinct observed \(x\)-values, so that the predictor has variation. No normality or independence assumption is needed just to calculate this descriptive least-squares line.

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.

$$ b_1=\frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})} {\sum_{i=1}^{n}(x_i-\bar{x})^2}, \qquad b_0=\bar{y}-b_1\bar{x}, \qquad \hat{y}=b_0+b_1x. $$

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.

1
Record the pairs and units.
Write one row for each observed unit, with its predictor value \(x_i\) and response value \(y_i\).
2
Calculate the means.
Add the \(x\)-values and divide by \(n\) to obtain \(\bar{x}\). Do the same with the \(y\)-values to obtain \(\bar{y}\).
3
Center both variables.
For every row, subtract \(\bar{x}\) from \(x_i\) and \(\bar{y}\) from \(y_i\).
4
Accumulate the two sums.
Multiply the centered values within each row and add the products. Separately square each centered \(x\)-value and add those squares.
5
Calculate and report the line.
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:

$$ \bar{x}=\frac{1+2+3+4+5}{5}=\frac{15}{5}=3\text{ hours}, \qquad \bar{y}=\frac{52+55+61+64+68}{5}=\frac{300}{5}=60\text{ points}. $$

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\)
152-2-8164
255-1-551
3610100
4641441
56828164
Sum4110

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:

$$ b_1=\frac{41}{10}=4.1\text{ points per hour}, \qquad b_0=60-(4.1)(3)=47.7\text{ points}. $$

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\)
210-3-399
413-1001
6121-1-11
81734129
Sum2020

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:

$$ b_1=\frac{20}{20}=1\text{ point per hour}, \qquad b_0=13-(1)(5)=8\text{ points}. $$

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\)
1018-15-7105225
2025-50025
30245-1-525
4033158120225
Sum220500

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:

$$ b_1=\frac{220}{500}=0.44\text{ mL per cm}, \qquad b_0=25-(0.44)(25)=25-11=14\text{ mL}. $$

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.
Key takeaway: From raw pairs, calculate \(\bar{x}\) and \(\bar{y}\), sum the centered cross-products and centered predictor squares, then use \(b_1=\sum (x_i-\bar{x})(y_i-\bar{y})/\sum (x_i-\bar{x})^2\) and \(b_0=\bar{y}-b_1\bar{x}\). The fitted line is \(\hat{y}=b_0+b_1x\).

Check Your Understanding

Try these questions without looking back at the worked calculations.

  1. For observations \((1,4),(2,5),(3,9)\), calculate \(\bar{x}\), \(\bar{y}\), the centered-product sum, and the centered-\(x\)-square sum.
  2. 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\).
  3. Why must the observed predictor values vary for the centered-data slope formula to give a fitted line?
  4. 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?
  5. 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?