Linear regression and the line of best fit

Topic guide · Updated 2026-11-26

Quick answer

Linear regression finds the straight line y = mx + b that best fits a set of data points by minimising the sum of the squared vertical distances from the points to the line (least squares). The slope is m = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)² and the intercept is b = ȳ − m·x̄. R² tells you the share of the variation in y the line explains.

12345678910405060708090
  • (x_1,y_1)
  • y_1 ~ m x_1 + b
Open in graphing calculator

Run a regression in the graphing calculator

  1. Click the table button and enter your data in the x_1 and y_1 columns.
  2. On a new line type y_1 ~ m x_1 + b. The tilde ~ means “fit this model”.
  3. Read m, b and R² under the expression; the best-fit line is drawn through your points.
  4. Use m and b elsewhere: y = m x + b or evaluate a prediction such as m·10 + b.

Prefer a form? The regression calculator takes pasted data and fits six models.

Worked example

Hours studied (x) and test score (y): (1, 52), (2, 55), (3, 61), (4, 64), (5, 70), (6, 73), (7, 79), (8, 83). The means are x̄ = 4.5 and ȳ = 67.125. Σ(x − x̄)(y − ȳ) = 189.5 and Σ(x − x̄)² = 42, so m = 189.5 / 42 ≈ 4.51 and b = 67.125 − 4.51·4.5 ≈ 46.82. Each extra hour is associated with about 4.5 more points.

R² and the correlation coefficient r

rBetween −1 and 1. Its sign matches the slope; values near ±1 mean a strong linear relationship.
R²Between 0 and 1. For a linear fit R² = r². R² = 0.95 means the line explains 95% of the variation in y.
ResidualActual y minus predicted y. A good model leaves residuals with no pattern.

Cautions

  • Correlation is not causation: a strong fit does not show that x causes y.
  • Extrapolating far beyond your data is risky; the trend may not continue.
  • A single outlier can swing the line. Always plot the data.
  • If the points curve, try a quadratic, exponential or logarithmic model instead.

Frequently asked questions

What is the difference between regression and correlation?

Correlation (r) measures how strongly two variables move together. Regression gives the equation of the best-fit line, which you can use to predict y from x.

Why square the residuals?

Squaring stops positive and negative errors from cancelling and penalises large misses more, which gives a unique best line with simple formulas.

Can I fit a curve instead of a line?

Yes. Type any model with ~, such as y_1 ~ a x_1^2 + b x_1 + c or y_1 ~ a b^x_1.