Calcamaze

Linear Regression Calculator

Fit a least-squares line to paired data: slope, intercept, equation, r, r², standard error, predictions and residuals.

Optional. Leave empty to skip.

Result

Regression line
ŷ = 2.02x − 0.02
Slope (b)
2.02
Change in y for each one-unit increase in x.
Intercept (a)
-0.02
Predicted y when x = 0.
Correlation (r)
0.999105
Coefficient of determination (r²)
0.998211
99.82% of the variation in y is explained by x.
Residual standard error
0.178885
√(Σ residual² ÷ (n − 2))
Number of pairs (n)
6
Fitted values and residuals
xyPredicted ŷResidual (y − ŷ)
12.120.1
23.94.02-0.12
36.26.040.16
47.88.06-0.26
510.110.080.02
612.212.10.1

Paste two columns of paired numbers — x and y — to fit the least-squares line through them. The calculator gives the regression equation, slope, intercept, correlation coefficient r, the coefficient of determination r², the residual standard error and a table of residuals, and predicts y for any x you enter.

What is linear regression?

Simple linear regression fits a straight line, ŷ = a + bx, through a set of paired observations so that it predicts y from x as well as possible. “As well as possible” means least squares: the line minimises the sum of the squared vertical distances (the residuals) between the points and the line. The slope b says how much y changes, on average, for each one-unit increase in x, and the intercept a is the predicted y when x is zero.

Alongside the line, the correlation coefficient r (from −1 to 1) measures how tightly the points cluster around it, and r² gives the share of the variation in y the line explains. The residual standard error is the typical size of a prediction error. None of these prove cause and effect, and none tell you whether a straight line is the right shape — that needs a look at the data and the residuals.

How to use the linear regression calculator

  1. Enter the x values in the first box.
  2. Enter the matching y values, in the same order, in the second box.
  3. Optionally enter an x value to predict its y.

Formula

b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² · a = ȳ − b x̄ · ŷ = a + bx

The least-squares line is the one that makes the sum of the squared residuals — the vertical distances from each point to the line — as small as possible. Its slope b and intercept a have the closed forms above, where x̄ and ȳ are the means.

r measures how closely the points follow a straight line, from −1 to 1, and r² is the share of the variation in y explained by the line. The residual standard error is the typical size of a residual, with n − 2 degrees of freedom because two parameters were estimated.

Worked examples

Hours of study and exam score

x values
1, 2, 3, 4, 5, 6, 7, 8
y values
52, 55, 61, 64, 70, 71, 78, 83
Predict y at x =
NaN
Regression line
ŷ = 4.380952x + 47.035714
Slope (b)
4.380952
Intercept (a)
47.035714

The fitted line is ŷ = 47.036 + 4.381x: each extra hour of study goes with about 4.4 more points. r = 0.9942, so r² = 0.9885 — 98.8% of the variation in scores is explained by hours studied.

Predicting sales from advertising spend

x values
10, 15, 20, 25, 30
y values
120, 150, 200, 210, 260
Predict y at x =
22
Regression line
ŷ = 6.8x + 52
Predicted y at x = 22
201.6
Slope (b)
6.8

With spend in $1,000s and sales in units, the slope is 6.8 units per $1,000 and the intercept 52. At $22,000 the line predicts 52 + 6.8 × 22 = 201.6 units.

Anscombe’s quartet: same line, very different data

Data setSlopeInterceptr
Anscombe I (linear)0.530.816
Anscombe II (curved)0.530.816
Anscombe III (one outlier)0.530.816
Anscombe IV (one high-leverage point)0.530.817

Check the residuals, not just r²

A good straight-line fit leaves residuals scattered evenly around zero. A curve in the residuals means the relationship bends and a straight line is the wrong model. A funnel shape, small residuals at one end and large at the other, means the prediction error grows with x. A single point far from the others in x can pull the whole line towards it, so check what happens to the slope if you leave it out.

Common mistakes

  • Fitting a line to curved data. Anscombe’s quartet above shows four data sets with almost identical lines and r values but completely different shapes — always plot the data.
  • Reading a strong r² as proof of causation. A third variable can drive both x and y.
  • Extrapolating far beyond the observed x values, where the straight-line pattern may not hold.
  • Swapping x and y. Regressing y on x and x on y give different lines; put the variable you want to predict in the y box.

Frequently asked questions

What is a good r² value?

It depends on the field. In physics and engineering, r² above 0.95 is common; in social science, 0.3 can be meaningful. A high r² does not prove that x causes y, and a curved pattern can have a low r² even with a strong relationship.

Can I use the line to predict outside my data?

Be careful. Extrapolating beyond the range of x values you measured assumes the straight-line pattern continues, which often isn’t true.

What is a residual?

The difference between an observed y and the value the line predicts for that x. Positive residuals are points above the line, negative ones below. A pattern in the residuals suggests a straight line is the wrong model.

Is the regression of y on x the same as x on y?

No, unless the points lie exactly on a line. The y-on-x line minimizes vertical distances and the x-on-y line horizontal ones; their slopes are r × s_y ÷ s_x and r × s_x ÷ s_y. Put the variable you want to predict on the y side.

Sources

  1. Introductory Statistics 2e, 12.3: The Regression Equation, OpenStax, Rice University
  2. Linear Least Squares Regression (NIST/SEMATECH e-Handbook of Statistical Methods), National Institute of Standards and Technology (NIST)

Last updated · Calculations checked by automated tests· Editorial policy