Least Squares Regression Line Calculator

Calculate least squares regression line — enter your values and get an accurate result with the underlying formula.

Quick Facts

Slope formula
b = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²)
Minimizes the sum of squared vertical distances from the points to the line.
Intercept formula
a = (Σy − bΣx) / n
The line always passes through the point (x̄, ȳ).
Goodness of fit
R² = 1 − SSE/SST
Ranges 0-1; closer to 1 means the line explains more of the variation in y.

Your Results

Calculated
Slope (b)
-
Change in y per unit x
Intercept (a)
-
Predicted y when x = 0
R² (coefficient of determination)
-
Goodness of fit, 0 to 1
Predicted Y
-
y = a + b × (your X)

Ready

Enter matching lists of x and y values, then press Calculate.

How the Least Squares Regression Line works

The least squares regression line is the straight line y = a + bx that best fits a set of (x, y) data points by minimizing the sum of the squared vertical distances ("residuals") between each point and the line. It is the standard method for finding a linear trend through scattered data in statistics, science, and business analysis.

Formula and method

For n data points, the slope and intercept that minimize the sum of squared residuals are given in closed form: the slope is b = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²) and the intercept is a = (Σy − bΣx) / n = ȳ − b·x̄, where x̄ and ȳ are the mean of the x and y values. These come from setting the partial derivatives of the sum of squared errors, Σ(y − a − bx)², with respect to a and b equal to zero and solving the resulting "normal equations." Once you have a and b, the fitted line y = a + bx lets you predict y for any x. The coefficient of determination, R² = 1 − SSE/SST (where SSE is the sum of squared residuals and SST is the total sum of squared deviations of y from its mean), tells you what fraction of the variation in y is explained by the linear relationship with x.

Common sources of error

  • Mismatched list lengths: the X and Y lists must have the same number of values, each x paired with its corresponding y in order.
  • Too few points: fewer than 3 points gives an unreliable or trivial fit — 2 points always produce a "perfect" line with no meaningful residuals.
  • Extrapolating too far: predicting y far outside the range of your x values assumes the linear trend continues, which is often not true in practice.
  • Confusing correlation with causation: a strong R² shows the two variables move together linearly — it does not prove that x causes y.

Interpreting your results

Check the sign and size of the slope first: a positive b means y tends to increase as x increases, a negative b means it decreases, and the magnitude tells you how much y changes per unit of x. Then look at R²: values near 1 indicate the points sit close to the fitted line, while values near 0 indicate a weak or non-existent linear relationship (the data may still follow a nonlinear pattern that this straight-line fit cannot capture). Always plot or scan your raw data — least squares fits a line even to data that clearly is not linear, so R² and residuals should be reviewed together with a quick look at the numbers.

Frequently Asked Questions

What is the formula for the least squares regression line?
The least squares line is y = a + bx, where the slope b = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²) and the intercept a = (Σy − bΣx) / n. These formulas minimize the sum of squared vertical distances between the data points and the line.
What does R² (coefficient of determination) tell you?
R² ranges from 0 to 1 and measures the proportion of variance in y explained by the linear relationship with x. R² = 1 means the line fits the data perfectly; R² close to 0 means the linear model explains almost none of the variation in y.
Why does least squares minimize squared errors instead of the errors themselves?
Squaring the residuals makes every error positive so they cannot cancel out, penalizes larger errors more heavily than small ones, and produces a smooth calculus problem with a unique closed-form solution for the slope and intercept.
How many data points do I need for a least squares regression?
You need at least 2 points to define a line, but at least 3-5 points are recommended so the fit reflects a genuine trend rather than passing exactly through every point with no meaningful residuals.