b2KIT

Least Squares Regression Visualizer

Fit lines and polynomials to data points using least squares. Visualize residuals, R-squared, and the normal equation.

Tested tool guide Tested browser tools Checked August 16, 2026

What Least Squares Regression Visualizer does, with a checked example

Enter paired x and y observations and select a straight-line or polynomial model. The visualizer finds coefficients that minimize the sum of squared vertical residuals, then shows the fitted curve, residuals, R-squared, and the corresponding normal equation. The most common surprise is that increasing the polynomial degree can improve the in-sample R-squared without producing a more useful model. A close fit may reflect overfitting, especially when there are few points.

Worked example

A concrete input and expected output from the current implementation.

Input

Model: linear
0, 1
1, 3
2, 5

Expected output

Best-fit line: y = 1 + 2x
Residuals: [0, 0, 0]
R-squared: 1
Normal equation: [[3, 3], [3, 5]] [c0, c1]^T = [9, 13]^T

All three points lie exactly on y = 1 + 2x, so every residual and the residual sum of squares are zero. Since the y-values have nonzero variation, R-squared is 1 - 0/8 = 1.

How the result is produced

1

Coefficient fit

For a line, each point contributes a row [1, x] to a design matrix X. A polynomial of degree d extends that row through x^d. The coefficient vector c minimizes the sum of (y - fitted y)^2. The normal equation (X^T X)c = X^T y expresses the condition satisfied by those least-squares coefficients.

2

Residual and R-squared display

For each point, the residual is observed y minus the y-value predicted by the fitted line or curve. Squaring and summing these residuals gives SSE. When the observed y-values have nonzero total variation SST, R-squared is 1 - SSE/SST. A value near 1 indicates a close fit to the entered points, not necessarily reliable prediction.

Good uses

  • Fit a calibration line to paired reference and measured values, then inspect whether residuals show a systematic bend.
  • Compare linear and low-degree polynomial descriptions of a small experimental data set using coefficients, residuals, and R-squared.
  • Check a hand-derived regression by comparing its coefficients and normal equation with the visualizer's result.

Limits and checks

  • A degree-d polynomial needs at least d + 1 distinct x-values for all coefficients to be uniquely determined. Repeated x-values do not supply the missing rank.
  • Squaring residuals gives unusually distant points substantial influence. The fitted curve may move toward an outlier even when most points follow another pattern.
  • If every observed y-value is identical, SST is zero and the standard R-squared expression is undefined. R-squared should not be interpreted normally in that case.

Common questions

Does the polynomial with the highest R-squared give the best model?

No. Adding polynomial terms cannot worsen the ordinary in-sample least-squares fit when the models are nested, so R-squared usually rises or stays unchanged. The added terms may merely follow noise between the entered points. Inspect the residual pattern and curve shape, and use separate validation data when predictive performance matters.

Can this fit a vertical line or minimize perpendicular distances?

No for a polynomial written as y = f(x). Its residuals are vertical differences in y, so a vertical relation x = constant cannot be represented by that model. If both coordinates contain important measurement error, or perpendicular distance is the intended error measure, an orthogonal or errors-in-variables regression is a different calculation.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools