CivilSolve

Statistics

Linear Regression (Least Squares)

Fits y = a + b·x by least squares: slope, intercept, correlation coefficient r, r², standard error of the estimate, and predictions at requested x values (with extrapolation warnings). Use for: calibration lines, trend fitting, 'find the regression equation and r²'. Not for: multiple or nonlinear regression.

Direct solving is free and needs no account. Have a word problem instead? Submit it as text.

Inputs

Load a sample problem:
pointsThe (x, y) data pairs, at least 3
points 1
predict atOptional x values to evaluate the fitted line at

Worked examples

These are the solver's own reference problems — the answers come from published sources or independent hand computation, never from the solver itself. The solution below is the live solver output for each.

Example 1

perfect proportional fit: (1,2),(2,4),(3,6)

Given

points
  1. 1.x 1y 2
  2. 2.x 2y 4
  3. 3.x 3y 6

Assumptions

  • Simple least squares: errors in y only, equal weight per point, straight-line model.

Solution steps

  1. Fit the line with centered sums

    The slope is the ratio of the x–y co-variation to the x variation; the line passes through (x̄, ȳ).

    b = Sxy/Sxx; a = ȳ − b·x̄

    x̄ = 2; ȳ = 4; Sxx = 2; Sxy = 4; slope b = 2; intercept a = 0

    = 2

  2. Goodness of fit

    r² is the fraction of the y variation explained by the line; the standard error of the estimate is the typical residual size.

    r² = 1 − SSres/Syy; SEE = √(SSres/(n − 2))

    SSres = 0; r = 1; r² = 1; SEE = 0

    = 1

Data and fitted lineData and fitted line60013x ()y ()

Results

Slope b

2

Intercept a

0

Correlation coefficient r

1

Coefficient of determination r²

1

Standard error of the estimate

0

Where this answer was checked
source
Exact rational least squaresy = 2x exactly
verified by
hand-recomputed
derivation
x̄=2, ȳ=4. Sxy = (−1)(−2)+0+(1)(2) = 4; Sxx = 2. slope = 2, intercept = 0, r = 1, SEE = 0.

Example 2

imperfect fit with exact rationals: (1,1),(2,2),(3,2)

Given

points
  1. 1.x 1y 1
  2. 2.x 2y 2
  3. 3.x 3y 2

Assumptions

  • Simple least squares: errors in y only, equal weight per point, straight-line model.

Solution steps

  1. Fit the line with centered sums

    The slope is the ratio of the x–y co-variation to the x variation; the line passes through (x̄, ȳ).

    b = Sxy/Sxx; a = ȳ − b·x̄

    x̄ = 2; ȳ = 1.6667; Sxx = 2; Sxy = 1; slope b = 0.5; intercept a = 0.66667

    = 0.5

  2. Goodness of fit

    r² is the fraction of the y variation explained by the line; the standard error of the estimate is the typical residual size.

    r² = 1 − SSres/Syy; SEE = √(SSres/(n − 2))

    SSres = 0.16667; r = 0.86603; r² = 0.75; SEE = 0.40825

    = 0.75

Data and fitted lineData and fitted line2.1670013x ()y ()

Results

Slope b

0.5

Intercept a

0.66667

Correlation coefficient r

0.86603

Coefficient of determination r²

0.75

Standard error of the estimate

0.40825

Where this answer was checked
source
Exact rational least squaresslope 1/2, intercept 2/3, r² = 3/4
verified by
hand-recomputed
derivation
x̄=2, ȳ=5/3. Sxy = (−1)(−2/3)+0+(1)(1/3) = 1; Sxx = 2 → slope = 0.5; intercept = 5/3 − 1 = 2/3 = 0.66667. Syy = 4/9+1/9+1/9 = 2/3. SSres: residuals −1/6, +1/3, −1/6 → 1/36+1/9+1/36 = 1/6. r² = 1 − (1/6)/(2/3) = 3/4. r = +0.86603. SEE = √((1/6)/1) = 0.40825.

Example 3

perfect negative fit with prediction: (0,10),(1,8),(2,6) at x=1.5

Given

points
  1. 1.x 0y 10
  2. 2.x 1y 8
  3. 3.x 2y 6
predict at
  1. 1.1.5

Assumptions

  • Simple least squares: errors in y only, equal weight per point, straight-line model.

Solution steps

  1. Fit the line with centered sums

    The slope is the ratio of the x–y co-variation to the x variation; the line passes through (x̄, ȳ).

    b = Sxy/Sxx; a = ȳ − b·x̄

    x̄ = 1; ȳ = 8; Sxx = 2; Sxy = -4; slope b = -2; intercept a = 10

    = -2

  2. Goodness of fit

    r² is the fraction of the y variation explained by the line; the standard error of the estimate is the typical residual size.

    r² = 1 − SSres/Syy; SEE = √(SSres/(n − 2))

    SSres = 0; r = -1; r² = 1; SEE = 0

    = 1

  3. Prediction 1

    Evaluate the fitted line at the requested x.

    ŷ = a + b·x

    ŷ(1.5) = 10 + -2 × 1.5 = 7

    = 7

Data and fitted lineData and fitted line100002x ()y ()

Results

Slope b

-2

Intercept a

10

Correlation coefficient r

-1

Coefficient of determination r²

1

Standard error of the estimate

0

Predicted y at the requested x

7

Where this answer was checked
source
Exact rational least squaresy = 10 − 2x; ŷ(1.5) = 7
verified by
hand-recomputed
derivation
slope = −2, intercept = 10, r = −1. Prediction: 10 − 2·1.5 = 7.