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):

StudentStudy hours (X)Exam score (Y)
1152
2257
3365
4468
5576

Formulas

For a straight line ŷ = a + bx fitted by ordinary least squares:

Equivalently, using centered sums:

Step-by-step calculation

First compute the means:

Then the centered sums for each row:

xyx − x̄y − ȳ(x − x̄)(y − ȳ)(x − x̄)²
152−2−11.623.24
257−1−6.66.61
36501.400
46814.44.41
576212.424.84

Summing the last two columns:

Therefore:

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 − ŷ:

xyŷResidualResidual²
15251.80.20.04
25757.7−0.70.49
36563.61.41.96
46869.5−1.52.25
57675.40.60.36

R² is high because the points lie close to a straight line in this sample.

Limitations

Back to statistics guides · Open the linear regression calculator