Skip to main content

Least Squares Regression Calculator

Enter your (x, y) data points and get the best-fit line ŷ = ax + b — slope, intercept, R², correlation coefficient, predictions, a scatter plot, and residuals. All calculations happen in your browser; nothing is uploaded.

1 · Enter your data

One point per line, as x, y (comma, semicolon, tab or space separated):

3 · Worked examples

Example 1 — five points, moderate fit

Points: (1,2), (2,4), (3,5), (4,4), (5,6)

x̄ = 3, ȳ = 4.2. Slope a = 40/50 = 0.8; intercept b = 4.2 − 0.8·3 = 1.8, so ŷ = 0.8x + 1.8.

SSres = 2.4, SStot = 8.8, so R² = 1 − 2.4/8.8 ≈ 0.7273 and r ≈ 0.8528 (strong positive). Predicting at x = 6: ŷ = 0.8·6 + 1.8 = 6.6.

Example 2 — perfect linear trend

Points: (0,1), (1,3), (2,5), (3,7)

Every point lies exactly on the line, so ŷ = 2x + 1 with SSres = 0 and R² = 1.00. Predicting at x = 4: ŷ = 2·4 + 1 = 9.

Example 3 — perfect negative correlation

Points: (1,10), (2,8), (3,6), (4,4), (5,2)

The fit is ŷ = −2x + 12: each extra unit of x lowers y by 2. R² = 1.00 and r = −1 (perfect negative linear association).

Example 4 — Anscombe's first dataset (11 points)

The classic sanity check: ŷ ≈ 0.5001x + 3.0001, R² ≈ 0.6665 — matching the textbook values (slope 0.5, intercept 3, R² ≈ 0.667). All four Anscombe datasets share these summary statistics yet look completely different when plotted, so always inspect the scatter plot.

4 · Method

For n points (xi, yi):

Slope a = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²), i.e. covariance(x,y)/variance(x).

Intercept b = ȳ − a·x̄.

R² = 1 − SSres/SStot, where SSres = Σ(yi − ŷi)² and SStot = Σ(yi − ȳ)². Pearson's r = covariance/(σx·σy); for simple regression R² = r². The fitted line minimizes SSres, hence "least squares".

Educational tool — check assumptions (linearity, roughly constant spread, no dominant outliers) before relying on any regression.

Frequently asked questions

How do you calculate the least squares regression line?

For points (x_i, y_i): slope a = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²) and intercept b = ȳ − a·x̄. The line y = ax + b minimizes the sum of squared vertical residuals. Example: (1,2),(2,4),(3,5),(4,4),(5,6) gives a = 0.8, b = 1.8, R² ≈ 0.7273.

What is the least squares regression line calculator used for?

It fits the best straight-line trend to paired data — e.g., ad spend vs. sales, study hours vs. test score, age vs. blood pressure — then predicts y for any x, and shows residuals to check how well the line fits.

What do slope and intercept mean in a regression line?

The slope a is the expected change in y per one-unit increase in x. The intercept b is the predicted y when x = 0 (often a mathematical reference, not a real-world value). In y = 0.8x + 1.8, each extra unit of x adds about 0.8 units of y.

What is R-squared in linear regression?

R² = 1 − SSres/SStot is the fraction of the variance in y explained by x, from 0 to 1. R² = 0.7273 means x explains about 72.7% of the variation in y. It is undefined (shown as —) when all y values are identical.

What is the correlation coefficient r?

Pearson's r measures linear association from −1 to +1: |r| ≥ 0.9 very strong, 0.7–0.9 strong, 0.5–0.7 moderate, 0.3–0.5 weak, below 0.3 very weak. For simple linear regression, R² = r².

What is a residual in regression?

A residual is the vertical gap between an observed y and the line's predicted ŷ: residual = y − (ax + b). Positive residuals sit above the line, negative ones below. The calculator lists a residual for every point you enter.

Can I predict a value beyond my data range?

Yes — the tool predicts y for any x — but extrapolation beyond your data is risky: the linear trend may not hold outside the observed range. Predictions inside the data range (interpolation) are much more trustworthy.

How many data points do I need for a regression line?

At least 2 points, and the x values must not all be identical (a vertical spread of x is impossible to fit with y = ax + b). More points give more stable estimates of slope, intercept, and R².

What is Anscombe's quartet and why does it matter?

Four tiny datasets (11 points each) that share the same mean, variance, regression line (y = 0.5x + 3) and R² ≈ 0.667 — yet look totally different: a normal cloud, a perfect curve, a line with one outlier, and a vertical cluster with one far point. It proves you should always plot your data, not trust summary stats alone.

Why square the errors instead of using absolute values?

Squaring gives a smooth, differentiable objective with a closed-form solution (the normal equations), and it penalizes large errors more heavily. Least absolute deviations (L1) is an alternative that resists outliers, but has no simple formula.

Does correlation imply causation?

No. A high R² only says x and y move together linearly — a third variable, reverse causation, or coincidence can explain it. Regression quantifies association; experiments or domain knowledge establish cause.

What is the difference between regression and correlation?

Correlation (r) measures the strength of a linear relationship in a symmetric way. Regression builds a directional prediction model (y from x) with slope, intercept, residuals, and predicted values.

When is the regression slope zero?

When y has no linear trend with x — the fitted line is flat (horizontal). Then the intercept equals the mean of y and R² is 0 (unless y is constant, in which case R² is undefined).

Can outliers break a least squares fit?

Yes — because errors are squared, one far point can drag the line strongly (see Anscombe's third dataset, where a single outlier shifts an otherwise perfect line). Check the residual table and scatter plot; large residuals flag influential points.

What does the orange dashed line on the plot mean?

Each dashed orange segment connects a data point to the regression line at the same x — its length is the absolute residual for that point. Longer dashes mean the line fits that observation poorly.

Is my data processed on a server?

No. Everything runs in your browser with JavaScript — your data points never leave your device. Nothing is uploaded, stored, or shared.

Need more than one predictor? Try the Multiple Linear Regression Calculator — OLS with 2–8 predictors, p-values, VIF, leverage, Cook's D and residual diagnostics.

Free tool by NRTechStudio