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.
Number of data points
Σx, Σy
Σxy, Σx²
Slope m = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²)
Y-intercept b = (Σy − mΣx) / n
Best-fit line
- 1
Σxy — sum of x × y products
Σ(xᵢ × yᵢ) across 5 pairs = 66 - 2
nΣxy − ΣxΣy (slope numerator)
5 × 66 − 15 × 20 = 30 - 3
nΣx² − (Σx)² (slope denominator)
5 × 55 − 15² = 50This is the total variance in x — zero only when all x values are identical. - 4
Slope m = numerator ÷ denominator
30 ÷ 50 = 0.6
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
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.
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
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.
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
@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?
