Least Squares Line Calculator

Paste your X and Y data points to find the best-fit line that minimizes the squared residuals.

How it is calculated

Paste the X values and the Y values as two lists, separated by commas, spaces or semicolons; the first X pairs with the first Y, and so on. The tool fits the straight line y = m·x + b that makes the sum of squared vertical errors as small as possible, using sums over the n pairs:

m = (n·Σxy − Σx·Σy) ÷ (n·Σx² − (Σx)²)

b = (Σy − m·Σx) ÷ n

r = (n·Σxy − Σx·Σy) ÷ √[(n·Σx² − (Σx)²)(n·Σy² − (Σy)²)]

R² is r squared. You need at least two pairs. If the lists have different lengths, only as many pairs as the shorter list are used and the extra values are ignored. A non-numeric entry gives no result, and so does a data set where all X values are equal, because the slope would be vertical.

Worked example

Take x = 1, 2, 3, 4, 5 and y = 2.2, 2.8, 3.6, 4.5, 5.1.

  1. n = 5, Σx = 15, Σy = 18.2, Σxy = 62.1, Σx² = 55.
  2. Slope m = (5 × 62.1 − 15 × 18.2) ÷ (5 × 55 − 15²) = 37.5 ÷ 50 = 0.75.
  3. Intercept b = (18.2 − 0.75 × 15) ÷ 5 = 1.39.
  4. The correlation works out to r = 0.997609, so R² = 0.995223.

The best-fit line is y = 0.7500x + 1.3900: each extra unit of x adds about 0.75 to y, and the line explains over 99 percent of the variation in this data.

Try this example →

Common mistakes

  • Writing decimals with a comma, such as 2,2. Commas separate values, so 2,2 is read as two separate numbers and the pairs shift out of line. Use a dot for decimals.
  • Giving lists of different lengths and assuming every value was used. The tool silently trims to the shorter list, so a missing Y value pairs the wrong points together.
  • Swapping X and Y. Regression is not symmetric: the slope of y on x is not the reciprocal of the slope of x on y unless r is exactly 1 or -1.
  • Treating a high R² as proof of cause and effect, or of a straight-line pattern. A curved data set can still return a large R², so plot the points first.
  • Reading r = 0 for a flat set of Y values as zero correlation. If every Y is identical the true correlation is undefined, and the tool shows 0 along with a horizontal line.

Sources

About this calculator

Ordinary least squares fits the straight line through a set of X-Y data points that minimizes the sum of the squared vertical distances (residuals) between each point and the line — the reason it's called 'least squares.' The slope and intercept have closed-form solutions (slope = Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)², intercept = ȳ − slope×x̄), and the correlation coefficient r indicates how well the line actually captures the relationship in the data, from -1 (perfect negative) to +1 (perfect positive).

This calculator takes pasted X and Y data pairs and returns the fitted line's equation, slope, intercept, and correlation coefficient. Statistics and science students learning regression, researchers doing a quick trend analysis, and analysts checking a correlation before committing to a full statistical model use it as a fast way to quantify a linear relationship in a data set without opening a spreadsheet or stats package.

Was this helpful?

You can pick up to 3 reactions.

Comments (1)

  • Editorial team

    A quick way to judge a fit is to run it twice: once with all points and once without the one you suspect is an outlier. If the slope barely moves, the line is stable; if it swings, a single point is steering it. Keep in mind the tool reports r and R squared but no standard error or significance, so a high r from only three or four points proves little. Which kind of data are you fitting: lab measurements, prices, test scores?

Popular calculators

All Calculators