Linear Regression: Line of Best Fit

April 12, 2026

Problem

Fit a line to data: (1,2), (2,4), (3,5), (4,4), (5,5). Show residual squares minimized.

Explanation

What is linear regression?

Linear regression finds the line y^=mx+b\hat{y} = mx + b that minimizes the sum of squared residuals — the squared vertical distances from each point to the line.

Step-by-step: {(1,2), (2,4), (3,5), (4,4), (5,5)}

Step 1 — Compute sums. n=5n = 5, x=15\sum x = 15, y=20\sum y = 20, xy=67\sum xy = 67, x2=55\sum x^2 = 55.

Step 2 — Slope:

m=nxyxynx2(x)2=5(67)15(20)5(55)225=335300275225=3550=0.7m = \frac{n\sum xy - \sum x \sum y}{n\sum x^2 - (\sum x)^2} = \frac{5(67) - 15(20)}{5(55) - 225} = \frac{335 - 300}{275 - 225} = \frac{35}{50} = 0.7

Step 3 — Intercept: b=yˉmxˉ=40.7(3)=1.9b = \bar{y} - m\bar{x} = 4 - 0.7(3) = 1.9

Result: y^=0.7x+1.9\hat{y} = 0.7x + 1.9

Step 4 — R2R^2: Measures how well the line fits. R2=1R^2 = 1 is perfect; R2=0R^2 = 0 means the line explains nothing.

Residuals

A residual is ei=yiy^ie_i = y_i - \hat{y}_i (actual minus predicted). The regression line minimizes ei2\sum e_i^2.

Try it in the visualization

Scatter plot with the regression line. Residual lines connect each point to the line. Toggle "squared residuals" to see them as literal squares — the line minimizes total square area.

Interactive Visualization

Parameters

1.00
0.70
1.90
1.00
Your turn

Got your own math or physics problem?

Turn any problem into an interactive visualization like this one — powered by AI, generated in seconds. Free to try, no credit card required.

Sign Up Free to Try It30 free visualizations every day
Linear Regression: Line of Best Fit | MathSpin