HomeMath › Least Squares Regression Calculator

Least Squares Regression Calculator

Paste equal-length lists of x and y values, separated by commas or spaces, to fit the least-squares line and get its slope, intercept, the equation, the correlation r, and r squared.

Example: with x values (comma or space separated) 1, 2, 3, 4, 5 · y values (same order) 2, 4, 5, 4, 6 → Best-fit line: ŷ = 0.8x + 1.8.

  • Slope (b)0.8
  • Intercept (a)1.8
  • Correlation r0.853

Computed by the calculator below using its default values. Change any input to see your own numbers.

Best-fit line
Slope (b)
Intercept (a)
Correlation r
r squared

Least squares picks the line with the smallest total squared vertical distance to your points.

The line that minimizes squared error

Ordinary least squares finds the straight line that comes closest to your points, where closeness means the sum of the squared vertical distances from each point to the line is as small as possible. Squaring the gaps punishes big misses and keeps positive and negative errors from cancelling. The slope b and intercept a that achieve this have a clean closed form built from the sums of x, y, xy, and x squared.

How well the line fits

The correlation r runs from -1 to 1 and measures the strength and direction of the linear relationship. Its square, r squared, is the fraction of the variation in y that the line explains, from 0 to 1. The slope tells you the trend; r and r squared tell you whether to trust it. A high r squared with a sensible slope is a fit worth using; a near-zero r means the line is barely better than the plain average.

How it’s calculated

Slope b = (nΣxy - ΣxΣy) / (nΣx² - (Σx)²); intercept a = ȳ - b·x̄. Correlation r = (nΣxy - ΣxΣy) / √[(nΣx² - (Σx)²)(nΣy² - (Σy)²)], and r squared is its square. The fitted line is ŷ = a + bx.

Ordinary least squares assumes a linear relationship and treats x as known without error; it is sensitive to outliers, which can pull the line noticeably.

Reading the correlation r

|r|Strength of linear fit
0.00 - 0.20Very weak or none
0.20 - 0.40Weak
0.40 - 0.60Moderate
0.60 - 0.80Strong
0.80 - 1.00Very strong

Rough guide; r near 0 can still hide a strong nonlinear pattern. r squared is the share of variance the line explains.

Common mistakes

  • Swapping x and y. The least-squares line of y on x is not the same as x on y; put the predictor in x.
  • Trusting the slope while ignoring r squared. A line always exists, but a low r squared means it explains little.
  • Extrapolating beyond the data range, where a fitted line has no evidence to stand on.

Frequently asked questions

What does the least squares regression calculator find?

It fits the line ŷ = a + bx that minimizes the sum of squared vertical distances to your points, and reports the slope, intercept, correlation r, and r squared. Paste equal-length lists of x and y values.

What is the formula for the slope?

b = (nΣxy - ΣxΣy) / (nΣx² - (Σx)²), and the intercept is a = ȳ - b·x̄. These come from setting the derivatives of the squared-error sum to zero.

What is the difference between r and r squared?

r is the correlation, from -1 to 1, showing strength and direction. r squared, its square, is the proportion of the variation in y explained by the line, from 0 to 1.

How many points do I need?

At least two to define a line, but two points fit perfectly and tell you nothing about scatter. Several points give a meaningful slope and a trustworthy r squared.

Does a good fit mean x causes y?

No. Correlation and a strong linear fit do not prove causation; a lurking third variable or coincidence can produce a high r squared without any causal link.