Linear Regression Calculator

Linear regression finds the straight line that best fits a set of data points, by making the squared vertical distances to the line as small as possible. This calculator works out the slope and intercept with exact arithmetic, the correlation coefficient r and r², and draws a scatter plot with the line.

Try:

How to use the linear regression calculator

  1. Enter the x values, separated by commas.
  2. Enter the matching y values in the same order.
  3. Press Calculate to see the line, r and the scatter plot.

Formula

m=n∑xy−∑x∑yn∑x2−(∑x)2m = \frac{n\sum xy - \sum x\sum y}{n\sum x^2 - (\sum x)^2}
c=yˉ−mxˉc = \bar{y} - m\bar{x}

Worked example: Six points

  • x values: 1, 2, 3, 4, 5, 6
  • y values: 2.1, 3.9, 6.2, 7.8, 10.1, 12.2

✓ Answer checked

y=2.02x−0.02y = 2.02x - 0.02
Slope m
10150≈2.02\frac{101}{50} \approx 2.02
Intercept c
−150≈−0.02-\frac{1}{50} \approx -0.02
Correlation r
0.9991050.999105
r² (fit)
0.998211 — a very strong positive linear relationship
−11234567851015
Scatter plot with the least-squares line. Dashed lines are the residuals, which the line makes as small as possible.
Step-by-step working (4 steps)
  1. Add up the data

    There are 6 points.

    ∑x=21,  ∑y=42.3,  ∑x2=91,  ∑xy=183.4\sum x = 21,\; \sum y = 42.3,\; \sum x^2 = 91,\; \sum xy = 183.4
  2. Slope Least squares

    Use the least-squares formula for the slope.

    m=n∑xy−∑x∑yn∑x2−(∑x)2=2.02m = \frac{n\sum xy - \sum x \sum y}{n\sum x^2 - (\sum x)^2} = 2.02
  3. Intercept

    The line passes through the mean point (x̄, ȳ).

    c=yˉ−mxˉ=−0.02c = \bar{y} - m\bar{x} = -0.02
  4. Correlation Pearson's r

    r measures how closely the points follow a straight line, from −1 to 1.

    r=0.999105r = 0.999105
Formulas used
m=n∑xy−∑x∑yn∑x2−(∑x)2m = \frac{n\sum xy - \sum x\sum y}{n\sum x^2 - (\sum x)^2}
c=yˉ−mxˉc = \bar{y} - m\bar{x}
How this was checked
  • ✓ The residuals add to exactly 0, and so do the residuals times x (the least-squares conditions).

Frequently asked questions

What does r tell me?

r measures how closely the points follow a straight line, from −1 (perfect negative) through 0 (none) to 1 (perfect positive).

What is r²?

The share of the variation in y explained by the line. An r² of 0.95 means the line explains 95% of it.

Does a strong correlation prove cause and effect?

No. Two things can move together because of a third factor or by chance.

Related calculators

Related guides