Least Squares Regression

Compare Calculations

Downloads

Includes your inputs and results for this calculation, plus any additional calculations you've compared.

Fitting a Line Through Scattered Data Points

Least squares regression finds the single straight line that best fits a scatter of data points. Enter a list of x, y pairs — one per line — and this calculator finds the line’s slope and intercept, plus how strongly the points actually follow that line.

The Formula

m=nxyxynx2(x)2\vC{m} = \frac{n\sum{xy} - \sum{x}\sum{y}}{n\sum{x^2} - \left(\sum{x}\right)^2} b=ymxn\vC{b} = \frac{\sum{y} - m\sum{x}}{n}

where mm is the slope, bb is the intercept, and nn is the number of data points — together they describe the line y=mx+by = mx + b that minimizes the total squared distance from every point to the line.

Worked Example

Fitting a line through (1, 1), (2, 2), and (3, 4):

    1. m=3(17)6(7)3(14)62=51424236=96=1.5m = \frac{3(17) - 6(7)}{3(14) - 6^2} = \frac{51 - 42}{42 - 36} = \frac{9}{6} = \vC{1.5}

    2. b=71.5(6)3=793=0.667b = \frac{7 - 1.5(6)}{3} = \frac{7 - 9}{3} = \vC{-0.667} — so the fitted line is y=1.5x0.667y = 1.5x - 0.667, with a correlation coefficient of about 0.980.98.

Key Factors to Consider

  • A strong correlation coefficient doesn’t necessarily mean x actually causes y. Two variables can be strongly correlated because one genuinely influences the other, because both are driven by a third factor, or purely by coincidence in a small dataset — correlation describes the strength of a linear relationship, not the reason behind it.
  • This method assumes the true relationship is linear, which isn’t always the right assumption. If a scatter of points actually follows a curve, forcing a straight line through it can produce a technically “best fit” line that still describes the data poorly — plotting the points visually before trusting the fit is good practice.
  • A single unusual point (an outlier) can disproportionately pull the fitted line toward it. Because least squares minimizes SQUARED distances, a point far from the general trend contributes much more to the fit than a point close to it — a small dataset with one extreme outlier is especially sensitive to this.
  • More data points generally produce a more reliable fit, all else equal. A line fit through only 3-4 points (as in the worked example) can look convincing but carries much more uncertainty than the same relationship measured across dozens of points — correlation strength alone doesn’t capture how much data actually backs up the fit.

Common Mistakes

  • Treating a high correlation coefficient as proof of causation. A strong r-value only shows the points cluster tightly around a line — it says nothing about whether one variable actually causes changes in the other.
  • Fitting a straight line to data that’s actually curved. Least squares always returns a slope and intercept, even when a linear fit badly describes the underlying pattern — plot the points first, or check whether the correlation coefficient is surprisingly low, before trusting the result.
  • Using too few data points and treating the fit as reliable. A line through just 3-4 points can look convincing but carries far more uncertainty than the same relationship measured across dozens of points.
  • Letting a single outlier dominate the fit without noticing. Because least squares minimizes squared distances, one far-off point can pull the whole line toward it — worth checking whether removing an unusual point changes the result substantially.

Useful to Know

  • Need to estimate a value between two known points instead of fitting a trend line through many? The Linear Interpolation Calculator calculator covers that more direct case.
  • Want to see how one specific value compares to the overall spread of a dataset? The Z-Score Calculator calculator measures exactly that.
  • Working with the same list of numbers for broader summary statistics? The Statistics Calculator calculator covers mean, median, and standard deviation.

Source: Simple linear regression.

Frequently Asked Questions

What is least squares regression?

Least squares regression finds the single straight line that best fits a scatter of data points, by minimizing the sum of the squared vertical distances between each point and the line. It's the standard method for describing a linear relationship between two variables.

What do the slope and intercept mean?

The slope tells you how much y changes for every 1-unit increase in x -- a slope of 2 means y goes up by 2 each time x goes up by 1. The intercept is the line's predicted y value when x is 0, which is where the line crosses the vertical axis.

What does the correlation coefficient (r) tell me?

The correlation coefficient ranges from -1 to 1 and measures how tightly the points cluster around the fitted line. A value near 1 means a strong positive relationship (as x goes up, y reliably goes up too), near -1 means a strong negative relationship, and near 0 means little to no linear relationship at all. R-squared (r²) is simply r multiplied by itself, and is often read as "the percentage of the variation in y that's explained by x."

Does a strong correlation mean x causes y?

No — correlation only measures how strongly two variables move together, not why. A strong correlation can reflect real causation, a shared underlying cause affecting both variables, or simple coincidence, especially in a small dataset. Don't treat a high correlation coefficient alone as proof of a causal relationship.

What if my data doesn't actually follow a straight line?

Least squares regression always fits a straight line, even to data that genuinely follows a curve — it will still return a slope, intercept, and correlation coefficient, but they may describe the relationship poorly. Plotting your points first, or checking whether the correlation coefficient is unexpectedly low, can flag when a linear fit is the wrong tool for your data.

Confirm Your Age

To create an account, please tell us your birth month and year.