Skip to content
Omnikalc

Linear regression calculator: line of best fit and correlation

Fit a least-squares line y = a + bx to up to 8 data points, with the correlation coefficient r, R² and a prediction.

Updated on
Your inputs

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Value out of range

Results

Slope (b)

1.9900

Intercept (a)

0.0500

Correlation coefficient (r)

0.9992

Very strong linear relationship

Predicted y

11.9900

R²
0.9983

The line of best fit

Simple linear regression finds the straight line y = a + bx that best describes how y changes with x. “Best” means it minimizes the sum of squared vertical distances between the points and the line: the least-squares criterion.

The formulas

  • Slope b = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²)
  • Intercept a = (Σy − bΣx) ÷ n
  • Correlation r = (nΣxy − ΣxΣy) ÷ √[(nΣx² − (Σx)²)(nΣy² − (Σy)²)]
  • R² = r², the share of the variance of y explained by x

Worked example

x 1 2 3 4 5
y 2 4.1 5.9 8.2 9.9
  1. Σx = 15, Σy = 30.1, Σxy = 110.2, Σx² = 55.
  2. Slope: (5 × 110.2 − 15 × 30.1) ÷ (5 × 55 − 15²) = 99.5 ÷ 50 = 1.99.
  3. Intercept: (30.1 − 1.99 × 15) ÷ 5 = 0.05.
  4. Line: y = 0.05 + 1.99x.
  5. r ≈ 0.9992, R² ≈ 0.998.
  6. Prediction for x = 6: 0.05 + 1.99 × 6 = 11.99.

Reading r

| |r| | Strength of the linear relationship | |—|—| | 0.9 to 1 | very strong | | 0.7 to 0.9 | strong | | 0.4 to 0.7 | moderate | | below 0.4 | weak |

The sign of r gives the direction: positive when y rises with x, negative when it falls.

Assumptions to check

  • Linearity: plot the data first. A curved pattern needs a different model.
  • Outliers: one extreme point can change the slope a lot. Check whether it is an error.
  • Constant spread: the scatter around the line should be similar across x values.
  • Independence: observations should not depend on each other, as they often do in time series.

Correlation is not causation

Ice cream sales and drownings both rise in summer, but neither causes the other: temperature drives both. Regression describes association; establishing cause requires experiments or careful study design.

Beyond two variables

Multiple regression adds more predictors, and polynomial or logarithmic models handle curves. Spreadsheets offer these through functions such as SLOPE, INTERCEPT, CORREL and LINEST, which give the same results as this calculator for a straight line.

Frequently asked questions

How do you calculate a linear regression line?

The least-squares slope is b = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²), and the intercept is a = (Σy − bΣx) ÷ n. The line y = a + bx minimizes the sum of squared vertical distances to the points.

What does the correlation coefficient r mean?

r ranges from −1 to 1. Values near 1 or −1 indicate a strong linear relationship, positive or negative; values near 0 indicate little linear relationship. r² is the share of the variation in y explained by the line.

Does a high r prove causation?

No. Two variables can move together because of a third factor or by coincidence. Correlation shows association, not cause and effect.

Can I predict outside the range of my data?

You can, but it is risky. The relationship may not stay linear beyond the observed x values, so extrapolated predictions are much less reliable than interpolated ones.

Was this calculator helpful?

Thanks for your feedback!

Results are estimates provided for information purposes. They do not replace professional advice.