Category

Least Squares Regression Line Calculator — Find the Line of Best Fit y = a·x + b

Find the line of best fit for a set of data points using the least squares method. Enter your (x, y) pairs to get the regression line y = a·x + b, the slope, intercept, correlation coefficient r, R², and a predicted-vs-actual table. Supports metric (m, kg, s…) and US customary units and 20 world currencies (USD, RUB, EUR…).

0 calculations

Calculation Parameters

One pair per line: x, y — at least 2 points required

Enter Parameters

Enter data points and click Calculate to find the least squares regression line.

How to Find the Line of Best Fit

Intuitively, you can try to draw a line that passes as close to all the points as possible. When that line is straight, we are performing a linear regression. There are several ways to do this, and the most popular and widely used is the least squares method. This least squares regression line calculator answers the question "How to find the line of best fit?" for any set of data points.

Why do we use it? With just a few data points we can roughly predict the result of a future event — for example, how a car's fuel consumption grows as its speed increases, or how much electricity you buy as the weather gets hotter or colder. Whenever an increase in A causes the growth (or decay) of B, a best-fit line helps you model and forecast that relationship. In the special case of only two points, a simple slope calculator is enough.

Least Squares Regression Line Equation

We are looking for a straight line with a slope a and an intercept b. The equation of the line of best fit is:

y = a · x + b

This is exactly the standard equation of a straight line. The magic of the least squares method lies in how we work out the two parameters a (slope) and b (intercept).

The Least Squares Method

The least squares method chooses the line that minimises the sum of the squared vertical distances (residuals) between each observed point and the line. Given n data points (x₁, y₁), …, (xₙ, yₙ), the slope and intercept are:

  • a = [ n·Σ(xy) − Σx·Σy ] / [ n·Σ(x²) − (Σx)² ]
  • b = ( Σy − a·Σx ) / n = ȳ − a·x̄

The best-fit line always passes through the mean point (x̄, ȳ). Squaring the residuals means larger errors are penalised more heavily and the result is unique and easy to compute.

How to Find the Least Squares Regression Line

  1. Compute the sums n, Σx, Σy, Σx², Σxy (and Σy² for the correlation).
  2. Use the formula above to find the slope a.
  3. Find the intercept b = ȳ − a·x̄.
  4. Write the equation y = a·x + b and use it to predict y for any x.
  5. Measure the goodness of fit with the correlation coefficient r and R².

Coefficient of Determination R²

After fitting the line we measure its quality with :

R² = 1 − SSres / SStot

where SStot = Σ(yᵢ − ȳ)² and SSres = Σ(yᵢ − ŷᵢ)². R² ranges from 0 to 1: the closer to 1, the better the line explains the variation in the data. For a straight line, R² is simply the square of Pearson's correlation coefficient r.

How to Use This Calculator

  1. Enter your data — one (x, y) pair per line, separated by a comma, space, or semicolon.
  2. Choose a measurement system — dimensionless, metric (SI), US customary, or a world currency (USD, RUB, EUR, and more).
  3. Click Calculate to instantly get the equation y = a·x + b, the slope, intercept, correlation coefficient r, R², and a full predicted-vs-actual table.
  4. Use the Share button to send results to colleagues, or Print for a clean report.

Example: Least Squares Fit Step by Step

Suppose you have the following data points:

xy
12
24
35
44
55

Computing the sums gives Σx = 15, Σy = 20, Σx² = 55, Σxy = 66, n = 5. The slope is a = (5·66 − 15·20) / (5·55 − 15²) = 30 / 50 = 0.6, and the intercept is b = (20 − 0.6·15) / 5 = 2.2. The line of best fit is y = 0.6·x + 2.2, and it passes through the mean point (3, 4).

Least Squares Fit Limitations

  • It assumes the true relationship is linear. If the scatter plot is curved, a quadratic or exponential regression may fit far better.
  • It is sensitive to outliers: a single extreme point can shift the whole line because errors are squared.
  • Correlation is not causation — a high R² does not prove that x causes y.
  • Extrapolating far beyond the range of your data can be unreliable.

For straight-line data the least squares regression line is the standard tool. If your data curves, try a quadratic or exponential regression instead.

FAQs

What is the least squares regression line?

It is the unique straight line y = a·x + b that minimises the sum of the squared vertical distances between the observed data points and the line — the best-fitting line in the least squares sense.

How many points do I need?

At least two points with different x-values. The more points you have, the more reliable the fit and the correlation coefficient become.

What does the slope a mean?

The slope is the average rate of change of y with respect to x: for every one-unit increase in x, the predicted value of y changes by a units.

Calculation History

Loading...