STATISTICS GUIDE
Linear regression worked example
Fit a least-squares regression line to five paired observations, showing every intermediate sum. Then open the same data in the live calculator to check the result.
The data
Five students report study hours (X) and exam scores (Y):
| Student | Study hours (X) | Exam score (Y) |
|---|---|---|
| 1 | 1 | 52 |
| 2 | 2 | 57 |
| 3 | 3 | 65 |
| 4 | 4 | 68 |
| 5 | 5 | 76 |
Formulas
For a straight line ŷ = a + bx fitted by ordinary least squares:
- b = Σ((x − x̄)(y − ȳ)) / Σ((x − x̄)²)
- a = ȳ − bx̄
Equivalently, using centered sums:
- Sxx = Σ(x − x̄)²
- Sxy = Σ(x − x̄)(y − ȳ)
- b = Sxy / Sxx
Step-by-step calculation
First compute the means:
- x̄ = (1 + 2 + 3 + 4 + 5) / 5 = 3
- ȳ = (52 + 57 + 65 + 68 + 76) / 5 = 63.6
Then the centered sums for each row:
| x | y | x − x̄ | y − ȳ | (x − x̄)(y − ȳ) | (x − x̄)² |
|---|---|---|---|---|---|
| 1 | 52 | −2 | −11.6 | 23.2 | 4 |
| 2 | 57 | −1 | −6.6 | 6.6 | 1 |
| 3 | 65 | 0 | 1.4 | 0 | 0 |
| 4 | 68 | 1 | 4.4 | 4.4 | 1 |
| 5 | 76 | 2 | 12.4 | 24.8 | 4 |
Summing the last two columns:
- Sxy = 23.2 + 6.6 + 0 + 4.4 + 24.8 = 59.0
- Sxx = 4 + 1 + 0 + 1 + 4 = 10.0
Therefore:
- b = 59.0 / 10.0 = 5.9
- a = 63.6 − 5.9 × 3 = 63.6 − 17.7 = 45.9
The fitted line is:
ŷ = 45.9 + 5.9x
For one extra study hour, the model predicts an exam score about 5.9 points higher on this sample.
Check with the calculator
Paste the data into the linear regression calculator and click Calculate. It returns the same slope and intercept and adds R², RMSE and a residual table.
Study hours,Exam score
1,52
2,57
3,65
4,68
5,76
R² and RMSE for this fit
Using the fitted values ŷ and residuals y − ŷ:
| x | y | ŷ | Residual | Residual² |
|---|---|---|---|---|
| 1 | 52 | 51.8 | 0.2 | 0.04 |
| 2 | 57 | 57.7 | −0.7 | 0.49 |
| 3 | 65 | 63.6 | 1.4 | 1.96 |
| 4 | 68 | 69.5 | −1.5 | 2.25 |
| 5 | 76 | 75.4 | 0.6 | 0.36 |
- SSE = 0.04 + 0.49 + 1.96 + 2.25 + 0.36 = 5.10
- SST = Σ(y − ȳ)² = (−11.6)² + (−6.6)² + (1.4)² + (4.4)² + (12.4)² = 365.20
- R² = 1 − 5.10 / 365.20 ≈ 0.986
- RMSE = √(5.10 / 5) ≈ 1.01
R² is high because the points lie close to a straight line in this sample.
Limitations
- This line describes the five observations. It does not prove that study hours cause higher scores.
- Predictions outside the observed range (1–5 hours) assume the same straight-line relationship continues.
- Five observations are enough for a worked example but not enough to assess real-world reliability.
- Ordinary least squares minimises squared vertical errors; other loss functions would give different fits.
Back to statistics guides · Open the linear regression calculator