Skip to content

Linear Regression Calculator

Paste your x and y values to get the least-squares line of best fit, its slope and intercept, the correlation r and R², a scatter plot, and a prediction for any x. Advanced adds standard errors, p-values, confidence and prediction intervals, residuals, Spearman’s rank correlation and exponential, log and power fits.

Errors, p-values, intervals, residuals, curve fits
How will you type the data?

Commas, spaces or new lines. Each y pairs with the x in the same place.

Decimal places
Type of fit

The usual least-squares straight line, ŷ = a + bx.

Confidence level
Names or units (optional)
ResultUpdates as you type—Line of best fit—Slope (b)—Intercept (a)—Correlation r—R²

How your line of best fit is worked out

Your numbers, step by step: the averages, how x and y move together, the slope and intercept, then how well the line fits.

  1. 1

    The averages

    x̄ = 3, ȳ = 4

  2. 2

    How they move together

    Sxx = 10, Sxy = 6

  3. 3

    Slope and intercept

    b = 6 ÷ 10 = 0.6
    a = 4 − 0.6 × 3 = 2.2

  4. 4

    How well it fits

    r = 0.7746, R² = 0.6

Read more: the formula, a worked example, what r and R² mean, and the other fits

Linear regression finds the straight line that sits closest to a set of points. “Closest” means least squares: the line that makes the sum of the squared vertical gaps (the residuals) as small as possible. The slope tells you how much y changes, on average, for each 1 more of x, and the intercept is where the line crosses x = 0.

The formula

Line: ŷ = a + b·x
Slope: b = Sxy ÷ Sxx, where Sxx = Σ(x − x̄)² = Σx² − (Σx)²/n and Sxy = Σ(x − x̄)(y − ȳ) = Σxy − Σx·Σy/n
Intercept: a = ȳ − b·x̄
Correlation: r = Sxy ÷ √(Sxx × Syy), and R² = r² = 1 − SSE ÷ Syy
Standard error of the estimate: s = √(SSE ÷ (n − 2)), SSE = Σ(y − ŷ)²
SE of slope: s ÷ √Sxx; SE of intercept: s × √(1/n + x̄²/Sxx); t = b ÷ SE(b), with n − 2 degrees of freedom
Interval at x₀: ŷ₀ ± t* × s × √(1/n + (x₀ − x̄)²/Sxx) for the mean, and √(1 + 1/n + (x₀ − x̄)²/Sxx) for one new value
Through the origin: b = Σxy ÷ Σx², with n − 1 degrees of freedom

In words: find the average of x and of y. For every point, multiply how far x is from its average by how far y is from its average and add these up (Sxy); do the same with x’s distance squared (Sxx). The slope is Sxy divided by Sxx. The line always passes through the point (x̄, ȳ), which gives the intercept.

Worked example

x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5, predicting y at x = 6.

  • Sums: n = 5, Σx = 15, Σy = 20, Σxy = 2 + 8 + 15 + 16 + 25 = 66, Σx² = 55, Σy² = 86.
  • Averages: x̄ = 15 ÷ 5 = 3, ȳ = 20 ÷ 5 = 4.
  • Sxx = 55 − 15²/5 = 55 − 45 = 10. Sxy = 66 − 15 × 20/5 = 66 − 60 = 6. Syy = 86 − 80 = 6.
  • Slope b = 6 ÷ 10 = 0.6. Intercept a = 4 − 0.6 × 3 = 2.2. The line is ŷ = 2.2 + 0.6x.
  • r = 6 ÷ √(10 × 6) = 6 ÷ 7.745967 = 0.774597, so R² = 36 ÷ 60 = 0.6: the line explains 60% of the variation in y.
  • Prediction at x = 6: ŷ = 2.2 + 0.6 × 6 = 5.8.
  • Residuals y − ŷ: −0.8, 0.6, 1, −0.6, −0.2 (they add up to 0). SSE = 0.64 + 0.36 + 1 + 0.36 + 0.04 = 2.4.
  • s = √(2.4 ÷ 3) = √0.8 = 0.894427. SE(b) = 0.894427 ÷ √10 = 0.282843, so t = 0.6 ÷ 0.282843 = 2.121320 with 3 df, giving p = 0.1240. The slope is not significant at 5%.
  • 95% confidence interval for the slope: t* = 3.182446, so 0.6 ± 3.182446 × 0.282843 = 0.6 ± 0.900132, from −0.3001 to 1.5001.
  • At x = 6, √(1/5 + (6 − 3)²/10) = √1.1 = 1.048809, so the 95% confidence interval for the mean is 5.8 ± 3.182446 × 0.894427 × 1.048809 = 5.8 ± 2.985399 (2.8146 to 8.7854). The prediction interval uses √2.1 = 1.449138: 5.8 ± 4.124922 (1.6751 to 9.9249).

Reading r

|r|In wordsR²
0.9 to 1Very strong0.81 to 1
0.7 to 0.9Strong0.49 to 0.81
0.5 to 0.7Moderate0.25 to 0.49
0.3 to 0.5Weak0.09 to 0.25
0 to 0.3Very weak or none0 to 0.09

These bands are a rough guide; what counts as strong depends on the field. A sign of + means y tends to rise with x, − means it falls.

Curve fits (transformed least squares)

  • Exponential, ŷ = A·e^(Bx): fit ln y against x. A = e^a, B = b. Needs every y above 0. Good for growth and decay at a steady percentage.
  • Logarithmic, ŷ = a + b·ln x: fit y against ln x. Needs every x above 0. Good for fast early growth that levels off.
  • Power, ŷ = A·x^B: fit ln y against ln x. Needs x and y above 0. Good for areas, volumes and scaling laws.
  • For exponential and power fits the R², standard errors and p-values are on the log scale (as in Excel’s trendlines); the table also shows R² on the original y so you can compare. Intervals are worked out on the log scale and turned back, so they are not symmetric.

Things to check

  • Look at the residual plot. The dots should scatter evenly around 0. A curve means a straight line is the wrong shape; a funnel means the spread changes with x.
  • Correlation is not cause. Ice-cream sales and sunburn rise together because of the sun, not each other.
  • Don’t stretch the line too far. Predictions outside the range of your x values (extrapolation) can be badly wrong.
  • One odd point can swing the line. Spearman’s rank correlation is less affected by outliers and also picks up curved links that only go one way.

Results checked against SciPy (scipy.stats.linregress and the t distribution) for the example above and for larger messy data sets.

Common questions

How do I find the line of best fit by hand?

Work out the averages x̄ and ȳ, then Sxx = Σ(x − x̄)² and Sxy = Σ(x − x̄)(y − ȳ). The slope is b = Sxy ÷ Sxx and the intercept is a = ȳ − b·x̄. For x = 1 to 5 and y = 2, 4, 5, 4, 5: x̄ = 3, ȳ = 4, Sxx = 10, Sxy = 6, so b = 0.6, a = 2.2 and the line is ŷ = 2.2 + 0.6x.

What is the difference between r and R²?

r (Pearson’s correlation) runs from −1 to 1 and shows the direction and strength of a straight-line link. R² is r squared for a straight line: the share of the ups and downs in y that the line explains. r = 0.7746 gives R² = 0.6, so the line explains 60% of the variation in y.

What does the p-value for the slope tell me?

It is the chance of a slope at least this far from 0 if x and y had no real straight-line link. A small p-value (below 0.05 is the usual cut-off) means the slope is unlikely to be just chance. With only 5 points, even r = 0.77 gives p = 0.124, so it is not significant.

What is the difference between a confidence interval and a prediction interval?

The confidence interval is for the average y at that x (where the line really is). The prediction interval is for one new single value, so it also includes the scatter of points around the line and is always wider.

When should I force the line through the origin?

Only when y must be 0 when x is 0 for a physical reason, such as distance against time from a standing start. Its R² compares with y = 0, not with the average, so it looks higher and can’t be compared with the normal R².

How do I fit an exponential or power curve?

Take logs and fit a straight line. Exponential y = A·e^(Bx): fit ln y against x, then A = e^a and B = b. Power y = A·x^B: fit ln y against ln x. Logarithmic y = a + b·ln x: fit y against ln x. This is what Excel and most calculators do; R² is then measured on the log scale.

Related calculators

Statistics calculatorEverything about a data set in one go: mean, median, mode, quartiles, outliers, SD, skewness, percentiles, z-scores, box plot and histogram, plus correlation and regression for two sets.Standard deviation calculatorStandard deviation, variance, mean, median, mode, quartiles, outliers and confidence intervals from a list or a frequency table.Confidence interval calculatorConfidence intervals for a mean (t or z), from a summary or raw data, proportions (Wald, Wilson, exact), two means (Welch) and two proportions, with one-sided bounds and a plain-English reading.Z-score calculatorZ-score from a value, value from a z-score, percentiles, the area under the bell curve (left, right, between, outside), p-values and a z-table.

Made by Zulu Calculator, part of AlphaZulu LLC. Formulas are shown in full and rule figures are checked against official sources; see how we build calculators.

Embed this calculator on your site

Free to use on your website or blog. Paste this code where you want the calculator to appear. Please keep the credit link.

Linear regression calculator by Zulu Calculator ↗