Intermediate

Least Squares Regression Calculator — Line of Best Fit

Enter your x and y data values (comma-separated) and get the least-squares regression line y = mx + b, the Pearson correlation r, and the coefficient of determination R² — with a plotted chart and step-by-step derivation.
Enter x data points separated by commas
Enter y data points in the same order as x values
Best-Fit Line (y = mx + b)
y = 0.6x + 2.2
Slope (m)
0.6
Y-intercept (b)
2.2
Correlation (r)
0.7746
R² (goodness of fit)
0.6
Data points (n)
5
RMSE
0.6928
Step-by-step derivation
1

Number of data points

n = 5
2

Σx, Σy

Σx = 15, Σy = 20
3

Σxy, Σx²

Σxy = 66, Σx² = 55
4

Slope m = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²)

m = 0.6
5

Y-intercept b = (Σy − mΣx) / n

b = 2.2
=

Best-fit line

y = 0.6x + 2.2
Step by step
  1. 1

    Σxy — sum of x × y products

    Σ(xᵢ × yᵢ) across 5 pairs = 66
  2. 2

    nΣxy − ΣxΣy (slope numerator)

    5 × 66 − 15 × 20 = 30
  3. 3

    nΣx² − (Σx)² (slope denominator)

    5 × 55 − 15² = 50
    This is the total variance in x — zero only when all x values are identical.
  4. 4

    Slope m = numerator ÷ denominator

    30 ÷ 50 = 0.6
Results are estimates for general information only and are not professional advice — always verify important results independently before relying on them. Read the full disclaimer.
Quick answer

How does this calculator work?

The least-squares line y = mx + b minimises the sum of squared residuals. Enter x and y pairs; the calculator computes slope m = (nΣxy−ΣxΣy)/(nΣx²−(Σx)²) and intercept b = (Σy−mΣx)/n, then shows the Pearson r and R² along with a plotted line through your data.

Formula
m = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²) • b = (Σy − mΣx) / n • r = (nΣxy − ΣxΣy) / √[(nΣx²−(Σx)²)(nΣy²−(Σy)²)]
How this is calculated

Least-squares regression finds the straight line that minimises the sum of the squared vertical distances ("residuals") between each data point and the line. The slope m and y-intercept b are solved directly from the five sums Σx, Σy, Σxy, Σx², and n using closed-form formulas — no iteration required. The resulting line is the unique one that makes the total squared error as small as possible.

The Pearson correlation coefficient r measures the direction and strength of the linear relationship: r = 1 is perfect positive correlation, r = −1 perfect negative, and r ≈ 0 suggests no linear trend. R² (the coefficient of determination) tells you what fraction of the variance in y is explained by the linear model — R² = 0.85 means the line accounts for 85% of the variability in y.

This calculator assumes a simple linear (one-variable) relationship and that the residuals are roughly normally distributed. It does not account for outliers, heteroscedasticity, or non-linear patterns — always examine the scatter plot to see whether a straight line is a reasonable model for your data.

Frequently asked questions

R² (coefficient of determination) is the proportion of variance in y explained by the regression line, ranging from 0 to 1. R² = 0.90 means the line explains 90% of the variation in y; the remaining 10% is due to noise or other factors not in the model.

r (Pearson correlation) measures direction and strength of the linear relationship (−1 to +1). R² = r² is r squared: it tells you the proportion of variance explained and is always between 0 and 1. R² is more directly interpretable as "goodness of fit" for regression.

No — this calculator performs simple linear regression with one predictor (x) and one response (y). For multiple predictors you need multiple linear regression, which requires matrix algebra beyond a single-formula approach.

APA

TG we-Calculate Editorial Team. (2026). Least Squares Regression Calculator — Line of Best Fit [Online calculator]. TG we-Calculate. https://we-calculate.com/calculator/least-squares-regression-calculator

Chicago

TG we-Calculate Editorial Team. "Least Squares Regression Calculator — Line of Best Fit." TG we-Calculate. 2026. https://we-calculate.com/calculator/least-squares-regression-calculator.

IEEE

TG we-Calculate Editorial Team, "Least Squares Regression Calculator — Line of Best Fit," TG we-Calculate, 2026. [Online]. Available: https://we-calculate.com/calculator/least-squares-regression-calculator

BibTeX

@misc{wecalculate_least_squares_regression_calculator, title = {Least Squares Regression Calculator — Line of Best Fit}, author = {{TG we-Calculate Editorial Team}}, howpublished = {\url{https://we-calculate.com/calculator/least-squares-regression-calculator}}, year = {2026}, note = {TG we-Calculate} }

Did this calculator help you?