How Linear Regression Finds a Best-Fit Line

Learn least-squares linear regression: slope, intercept, R², correlation, residuals, and predictions — free regression calculator guide.

By Generatr Team

Linear regression fits a straight line y = mx + b through a scatter of points so the vertical gaps (residuals) are as small as possible in a squared sense. That line is the usual “line of best fit” for one predictor x and one response y.

This guide covers ordinary least squares, how m and b are computed from means and sums, what R² and correlation r mean, residual checks, and careful predictions. Run any data set through the free linear regression calculator — slope, intercept, R², Pearson r, residuals, and predictions client-side.

Two-point geometry slopes are exact lines; see the slope calculator guide. Means of x and y feed the formulas — the average guide refreshes that step.

Free tool

Use the Linear Regression Calculator now

Open the interactive linear regression calculator in your browser — free, instant, no signup.

Open Linear Regression Calculator

What Is Linear Regression and Least Squares?

Given n pairs (xᵢ, yᵢ), simple linear regression models:

y ≈ mx + b

Ordinary least squares (OLS) chooses m and b to minimize the sum of squared residuals:

SSE = Σ (yᵢ − (mxᵢ + b))²

Squaring penalizes large misses and makes the calculus clean; the solution is unique when the x-values are not all identical.

  • m — estimated slope (change in ŷ per unit x)
  • b — estimated intercept (ŷ when x = 0; interpret only if x = 0 is meaningful)
  • ŷ — predicted y on the line

This is not the same as forcing the line through two hand-picked points. Every point influences the fit. The free linear regression calculator implements OLS and shows m, b, and fit metrics together.

Assumptions people often list: roughly linear trend, independent errors, roughly constant variance, and (for inference) approximately normal errors. The fit still produces m and b without those assumptions; inference and R² interpretation get weaker when they fail badly.

How Are Regression Slope and Intercept Calculated?

Let x̄ and ȳ be the sample means of x and y. One standard form:

m = Σ((xᵢ − x̄)(yᵢ − ȳ)) / Σ((xᵢ − x̄)²)

b = ȳ − m x̄

So the fitted line always passes through the centroid (x̄, ȳ). That is why averages sit at the center of the algebra — compute means with care (or via the average calculator).

Equivalent computational forms use raw sums Σx, Σy, Σxy, Σx². Any of those formulas should match the calculator’s m and b within rounding.

Tiny example

Points: (1, 2), (2, 3), (3, 5). Roughly, as x rises by 1, y rises by about 1.5. Exact OLS gives m = 1.5, b = 0.333…, line ŷ = 1.5x + 1/3.

With only two distinct points, OLS recovers the same slope as the geometric two-point formula — the slope guide is the special case. With three or more non-collinear points, residuals cannot all be zero; least squares shares the miss.

Enter the table in the free linear regression calculator and confirm m, b, and that (x̄, ȳ) lies on the line.

What Do R² and the Correlation Coefficient Mean?

Pearson’s r measures linear association between x and y, from −1 to 1. Sign matches the direction of the slope; |r| near 1 means points hug a line tightly.

For simple linear regression with an intercept, R² = r² (coefficient of determination). R² is the fraction of variance in y explained by the linear model in the sample:

R² = 1 − SSE / SST

where SST = Σ(yᵢ − ȳ)². R² = 1 is a perfect fit through the points; R² = 0 means the line does no better than predicting ȳ every time.

  • r = 0.9 → R² = 0.81 → about 81% of sample y-variance linked to the linear fit
  • r = −0.5 → R² = 0.25 → moderate inverse association

High R² does not prove causation. Nonlinear trends can still yield mediocre R² for a straight line even when a curve fits well. Outliers can inflate or deflate r dramatically.

Spread of y alone is a standard-deviation story — see the standard deviation guide. Standardized points use z-scores — the z-score guide — which appear in the algebraic definition of correlation as average product of z-scores (up to sample vs population conventions).

Read r and R² from the free linear regression calculator beside the equation.

How Do Residuals and Predictions Work?

For each point, residual eᵢ = yᵢ − ŷᵢ = yᵢ − (mxᵢ + b).

  • Positive residual — point above the line
  • Negative residual — point below the line
  • OLS residuals sum to zero when an intercept is included

Plot residuals vs x or vs ŷ: a random spray is healthy; a curve suggests nonlinearity; a funnel suggests non-constant variance.

Prediction: plug a new x₀ into ŷ₀ = m x₀ + b. That is fine near the center of your x-data. Extrapolation — predicting far outside the observed x-range — is risky because the linear pattern may not continue.

Example: if training x runs from 10 to 20, predicting at x = 100 is extrapolation even if the algebra runs. Report ŷ and, when available, a standard error of prediction; classroom tools may only give ŷ.

The free linear regression calculator prediction mode should match hand evaluation of mx + b. Residual tables help you spot the largest outliers for cleaning or investigation.

Comparing a residual to the residual SD is a z-score style check for unusual points — use the z-score calculator when you standardize residual size against a scale.

When Should You Use Regression Instead of Two-Point Slope?

Use cases:

  • Two exact geometric points — slope formula m = (y₂−y₁)/(x₂−x₁); no statistics required
  • Many measurements with noise — OLS regression for a best-fit line
  • One x causes many repeated y’s — still regression (or means at each x first)

Physics labs often collect 8–12 (x, y) pairs with instrument noise; reporting the regression slope with R² is more honest than cherry-picking two endpoints. Cherry-picked endpoints ignore the middle of the data and can bias steepness.

If all points are collinear, regression m matches any two-point m. If not, endpoints alone can disagree with OLS by a lot.

Geometric midpoints and segment slopes stay in pure geometry tools: slope calculator and midpoint calculator. Statistical fit stays in the linear regression workflow.

Correlation without fitting a line answers “how linear is the association?”; regression answers “what line do we use to predict?” They share r and R² in the simple case but serve different sentences in a report.

What Linear Regression Mistakes Should You Avoid?

Frequent pitfalls:

  • Interpreting b when x = 0 is nonsense (e.g. x = year 2020 coded as 2020, intercept at year 0)
  • Assuming high R² means x causes y
  • Fitting a line to a clear curve and trusting ŷ
  • Extrapolating far past the data
  • Letting one outlier dominate m — inspect residual plots
  • Swapping x and y — regressing x on y gives a different line than y on x (unless perfect fit)
  • Using regression slope language for percent change without aligning units

Center and scale of x affect b and the numeric size of m, not necessarily the qualitative story. Standardizing variables (z-scores) makes coefficients comparable across units — see the z-score calculator guide and spread via the standard deviation guide.

Always pair the equation with a scatter sketch. The free linear regression calculator numbers should match a line you can draw through the cloud.

How Do You Use Generatr’s Linear Regression Calculator?

Enter paired data; read the fit and diagnostics.

  1. Open the free linear regression calculator.
  2. Enter each (x, y) pair in order (same n for both lists).
  3. Read slope m and intercept b for ŷ = mx + b.
  4. Note R² and Pearson r for fit strength and direction.
  5. Scan residuals for large outliers or patterns.
  6. Predict ŷ for new x values inside the data range when possible.
  7. Recompute x̄ and ȳ and verify b = ȳ − m x̄.
  8. For a pure two-point line, compare with the slope calculator.

Related tools: average calculator, standard deviation calculator, and z-score calculator.

Step-by-Step Instructions

  1. 1Open the free linear regression calculator on Generatr.
  2. 2List your paired observations (x₁, y₁), …, (xₙ, yₙ) with n ≥ 2.
  3. 3Compute means x̄ and ȳ (optional hand check).
  4. 4Enter all pairs into the calculator to obtain m and b.
  5. 5Write the model ŷ = mx + b and confirm it passes through (x̄, ȳ).
  6. 6Read R² and r to judge how tightly the points follow the line.
  7. 7Inspect residuals y − ŷ and flag large outliers.
  8. 8Predict ŷ for new x values carefully, avoiding wild extrapolation.

Frequently Asked Questions

What is least-squares linear regression?+

It is the method that chooses the slope m and intercept b of y = mx + b to minimize the sum of squared vertical residuals between observed y values and predictions on the line.

How do you interpret R²?+

R² is the proportion of sample variance in y explained by the linear model. R² = 1 is a perfect fit to the points; R² = 0 means the fit is no better than using the mean of y. It does not prove causation.

What is the difference between r and R²?+

Pearson’s r is the correlation (−1 to 1) and carries direction. In simple linear regression with intercept, R² = r², so R² drops the sign and reports explained variance fraction.

What is a residual?+

A residual is observed y minus predicted ŷ for that x: e = y − (mx + b). Residuals show how far each point sits above or below the fitted line.

Can I use the regression line to predict new values?+

Yes for x values near the range of your data: compute ŷ = mx + b. Predictions far outside that range are extrapolations and can be unreliable if the true relationship bends or changes.

Is Generatr’s linear regression calculator free?+

Yes. Fit least-squares lines, review R², correlation, residuals, and predictions in your browser without an account.

Ready to try it yourself?

Use the free Linear Regression Calculator — no download, no account.

Launch Linear Regression Calculator