BL2.ru

Least squares method online(linear regression)

Fit a straight line to experimental points by the least squares method. You get slope k, intercept b, correlation r, R² and the full solution with the table of sums.

Two columns separated by Tab (copied from Excel), semicolon, comma or space; a header row is skipped.
Calculate

What is the least squares method

Measured points rarely lie exactly on a line. The least squares method finds the line y = kx + b that minimises the sum of squared deviations S = Σ (yi − k·xi − b)².

Formulas

k = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²), b = (Σy − k·Σx) / n

  • Pearson r — from −1 to 1, strength and direction of the linear relationship;
  • R² — share of variance of y explained by the line;
  • standard error S — typical deviation of points from the line.

Example

x = 1…5, y = 2.1, 3.9, 6.2, 7.8, 10.1: n = 5, Σx = 15, Σy = 30.1, Σxy = 110.2, Σx² = 55. k = 99.5 / 50 = 1.99, b = 0.05, so y = 1.99x + 0.05, R² ≈ 0.998.

If the points follow a curve, use curve fitting; if the curve must pass exactly through all points, use interpolation.

FAQ

Least squares vs interpolation?
Interpolation passes exactly through all points; least squares finds the best line between noisy points.
How many points are needed?
Two define a line; at least three are needed to estimate the error, five or more for a reliable conclusion.
How to get the coefficients in Excel?
SLOPE(Y,X) and INTERCEPT(Y,X) give k and b, RSQ(Y,X) gives R², CORREL(Y,X) gives r.
What does R² = 0.9 mean?
The line explains 90 % of the variation of y. It does not prove causation.