Linear Regression Calculator

Enter up to eight x and y pairs, then an x value to predict from. You get the least-squares line y = mx + b, r squared, the standard error of the estimate and the predicted y.

Updated

Embed this calculator

Paste this code into your page. The calculator runs inside the frame and links back to the full version.

Keep the credit link in place. Do not resell the calculator.

Formula

For n pairs with means x-bar and y-bar, let Sxx = Σ(x - x-bar)², Syy = Σ(y - y-bar)² and Sxy = Σ(x - x-bar)(y - y-bar).

  • slope m = Sxy / Sxx
  • intercept b = y-bar - m × x-bar
  • r² = Sxy² / (Sxx × Syy)
  • standard error of the estimate = sqrt( (Syy - m × Sxy) / (n - 2) )
  • predicted y = b + m × x

Worked example

x = 1 to 8 and y = 2, 4, 5, 4, 5, 7, 8, 9. Here x-bar = 4.5, y-bar = 5.5, Sxx = 42, Sxy = 38, Syy = 46.

  • slope = 38 / 42 = 0.9048
  • intercept = 5.5 - 0.9048 × 4.5 = 1.4286
  • equation: y = 0.9048x + 1.4286
  • r² = 38² / (42 × 46) = 0.9048
  • standard error of the estimate = 0.7766
  • predicted y at x = 10: 0.9048 × 10 + 1.4286 = 10.476

If every x is the same, the slope is undefined and an error is shown.

Frequently asked questions

How is the regression line calculated?

The line minimises the sum of squared vertical distances to the points. The slope is Σ(x - x-bar)(y - y-bar) / Σ(x - x-bar)² and the intercept is y-bar minus slope times x-bar. For x = 1 to 8 and y = 2, 4, 5, 4, 5, 7, 8, 9 the line is y = 0.9048x + 1.4286.

What does r squared tell me?

r squared is the share of the variation in y that the straight line explains. An r squared of 0.9048 means about 90.5% of the variation in y is accounted for by x in that data set.

What is the standard error of the estimate?

It is the typical size of the gap between the actual y values and the line, sqrt(SSE / (n - 2)). In the example it is 0.7766, so predictions miss by roughly 0.78 units in y.

How do I predict y for a new x?

Put the new x in the line equation. With y = 0.9048x + 1.4286 and x = 10, y = 0.9048 x 10 + 1.4286 = 10.476. Predictions far outside your x values are unreliable.

Why does the calculator need at least 3 pairs?

Two points always fit a line exactly, so there is nothing left to estimate the error from. The standard error divides by n - 2, so n must be at least 3, and the x values must not all be the same.