Linear Regression Calculator (Line of Best Fit)

Fit a least-squares line to paired X and Y data — get the slope, intercept, equation, correlation, and R².

Inputs

Independent variable, comma-separated.

Dependent variable — must have the same count as X.

Result

Line of best fit
y = 0.6000x + 2.2000
R² = 0.6000 · r = 0.7746
  • Slope (b)0.600000
  • Intercept (a)2.200000
  • Correlation r0.774597
  • R² (coefficient of determination)0.600000
  • Means (x̄, ȳ)3.0000, 4.0000
  • Points (n)5
Note — Fits a straight line by ordinary least squares. R² is the fraction of Y's variance explained by X. Correlation is not causation, and a line may fit poorly if the true relationship is curved.

Step-by-step

  1. Means: x̄ = 3.0000, ȳ = 4.0000.
  2. Slope b = Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)² = 6.0000 ÷ 10.0000 = 0.600000.
  3. Intercept a = ȳ − b·x̄ = 4.0000 − 0.6000×3.0000 = 2.200000; R² = 0.600000.

How to use this calculator

  • Enter your X (independent) values, comma-separated.
  • Enter the matching Y (dependent) values in the same order.
  • Read the best-fit equation y = bx + a.
  • Check R² to judge how well the line fits the data.

About this calculator

Linear regression finds the straight line that best summarizes the relationship between two variables, X and Y. The least-squares line minimizes the total squared vertical distance between the data points and the line, giving a slope (how much Y changes per unit of X) and an intercept (the predicted Y when X is zero). This calculator also reports the correlation coefficient r, which measures how tightly the points cluster around the line, and R², the square of r, which is the proportion of the variation in Y that the line explains. An R² near 1 means the line fits well; near 0 means X tells you little about Y. Paste your X and Y values as matching lists and the tool returns the full equation and fit statistics.

How it works — the formula

b (slope) = Σ(x−x̄)(y−ȳ) / Σ(x−x̄)² a (intercept) = ȳ − b·x̄ r = Σ(x−x̄)(y−ȳ) / √(Σ(x−x̄)²·Σ(y−ȳ)²) R² = r²

The slope is the covariance of X and Y divided by the variance of X; the intercept anchors the line through the means. r and R² quantify fit quality.

Worked examples

Example 1
X=1,2,3,4,5 Y=2,4,5,4,5
Inputs:
xs=1,2,3,4,5; ys=2,4,5,4,5
Output:
y = 0.6x + 2.2, R² = 0.6
Example 2
Perfect line X=1,2,3 Y=2,4,6
Inputs:
xs=1,2,3; ys=2,4,6
Output:
y = 2x + 0, R² = 1
Example 3
X=1,2,3,4 Y=4,3,2,1
Inputs:
xs=1,2,3,4; ys=4,3,2,1
Output:
y = −1x + 5, R² = 1

Limitations

  • Ordinary least squares only; sensitive to outliers and assumes a linear relationship.
  • Requires at least two points and non-constant X values.
  • Does not report confidence intervals or residual diagnostics.

Association, not causation; verify linearity with a scatter plot.

Frequently asked

It is the straight line that minimizes the sum of the squared vertical distances from the data points to the line — the ordinary least-squares regression line. It gives the slope and intercept that best predict Y from X.

Related calculators

More tools you might like