Skip to main content
v2026.11,858 entries · CC-BY 4.0

Linear Regression Calculator: Formulas, Worked Examples, Excel, R and Python

How to calculate a linear regression without a web form: the least-squares formulas, two worked examples, a computed t-critical-value table, and Excel, R and Python snippets.

Written and maintained by CASRAI Editorial Board

Last updated

A linear regression calculator takes paired x and y values and returns the straight line that best describes how y changes with x: a slope, an intercept and usually a coefficient of determination. This page gives you everything needed to do that without a web form: the formulas, two fully worked examples with the arithmetic shown, a computed t-critical-value table for slope confidence intervals, and short snippets for Excel, R and Python. All numbers were computed with code and checked against SciPy.

How to calculate linear regression: the formulas

Ordinary least squares chooses the line that minimises the sum of the squared vertical distances between the observed y values and the line. The NIST/SEMATECH e-Handbook of Statistical Methods defines the criterion as Q = Σ [yi − (b0 + b1xi)]2 and obtains the estimates by setting each partial derivative to zero (NIST e-Handbook, section 4.4.3.1). The result is two closed-form formulas:

  • Slope: b1 = Σ(xi − x̄)(yi − ȳ) / Σ(xi − x̄)2
  • Intercept: b0 = ȳ − b1x̄

Write Sxy for the numerator of the slope and Sxx for its denominator, so b1 = Sxy / Sxx. With Syy = Σ(yi − ȳ)2, the Pearson correlation is r = Sxy / √(SxxSyy) and the coefficient of determination is r2. In SciPy’s documentation, the returned rvalue is Pearson r and its square is the coefficient of determination.

“Linear” here means linear in the parameters, not necessarily in the explanatory variables (NIST e-Handbook, section 4.1.4.1). A straight line y = b0 + b1x is the simplest case, and it is the one this page calculates.

How to find the linear regression by hand: a step-by-step recipe

  1. Compute the means x̄ and ȳ.
  2. For each pair, compute x − x̄, y − ȳ, their product, and (x − x̄)2.
  3. Sum the products to get Sxy and sum the squares to get Sxx.
  4. Divide: b1 = Sxy / Sxx.
  5. Back-solve: b0 = ȳ − b1x̄.

The fitted line always passes through the point (x̄, ȳ), which gives you a quick check on step 5.

Worked example 1: five points

Illustrative data, invented for this explanation. Take x = 1, 2, 3, 4, 5 and y = 2.1, 3.9, 6.2, 7.8, 10.1. Then x̄ = 3 and ȳ = 6.02.

x y x − x̄ y − ȳ (x − x̄)(y − ȳ) (x − x̄)2
1 2.1 −2 −3.92 7.84 4
2 3.9 −1 −2.12 2.12 1
3 6.2 0 0.18 0.00 0
4 7.8 1 1.78 1.78 1
5 10.1 2 4.08 8.16 4
Sum 19.90 10

Slope b1 = 19.90 / 10 = 1.99. Intercept b0 = 6.02 − 1.99 × 3 = 0.05. The fitted line is y = 0.05 + 1.99x. With Syy = 39.708, r = 19.9 / √(10 × 39.708) = 0.9987 and r2 = 0.9973. A prediction at x = 6 is 0.05 + 1.99 × 6 = 11.99; note that x = 6 lies just outside the observed range, so treat it as an extrapolation.

Worked example 2: six points, with a confidence interval for the slope

Illustrative data, invented for this explanation. Take x = 10, 20, 30, 40, 50, 60 and y = 12.5, 20.1, 27.4, 38.2, 44.0, 53.6. Then x̄ = 35 and ȳ = 32.633.

x y x − x̄ y − ȳ (x − x̄)(y − ȳ) (x − x̄)2
10 12.5 −25 −20.133 503.333 625
20 20.1 −15 −12.533 188.000 225
30 27.4 −5 −5.233 26.167 25
40 38.2 5 5.567 27.833 25
50 44.0 15 11.367 170.500 225
60 53.6 25 20.967 524.167 625
Sum 1440.000 1750

Slope b1 = 1440 / 1750 = 0.8229 and intercept b0 = 32.633 − 0.8229 × 35 = 3.833, so the line is y = 3.833 + 0.8229x, with r2 = 0.9960. SciPy reports a slope standard error of 0.02591. With n − 2 = 4 degrees of freedom, the two-sided 95% critical value is t = 2.776 (table below), so the 95% confidence interval for the slope is 0.8229 ± 2.776 × 0.02591, which is 0.751 to 0.895.

t critical values for slope confidence intervals

A confidence interval for the slope is b1 ± t × SE(b1), where t comes from the t-distribution with n − 2 degrees of freedom in simple regression. The values below are computed with scipy.stats.t.ppf and rounded to three decimals; they are one-tailed quantiles at 0.95, 0.975 and 0.995, which give two-sided 90%, 95% and 99% intervals. For how the t statistic itself is read, see how to interpret the t-statistic.

Degrees of freedom (n − 2) 90% two-sided 95% two-sided 99% two-sided
1 6.314 12.706 63.657
2 2.920 4.303 9.925
3 2.353 3.182 5.841
4 2.132 2.776 4.604
5 2.015 2.571 4.032
6 1.943 2.447 3.707
7 1.895 2.365 3.499
8 1.860 2.306 3.355
9 1.833 2.262 3.250
10 1.812 2.228 3.169
12 1.782 2.179 3.055
15 1.753 2.131 2.947
20 1.725 2.086 2.845
25 1.708 2.060 2.787
30 1.697 2.042 2.750
40 1.684 2.021 2.704
60 1.671 2.000 2.660
120 1.658 1.980 2.617

Linear regression in Excel

For “how to do linear regression in Excel”, the function is LINEST, whose syntax is LINEST(known_y's, [known_x's], [const], [stats]). Microsoft documents that LINEST uses the method of least squares. Leaving const as TRUE or omitting it computes the intercept normally; FALSE forces it to zero. Setting stats to TRUE returns extra statistics: standard errors, r2 and the standard error of y, the F statistic and degrees of freedom, and the regression and residual sums of squares. The coefficients come back slope first, then intercept. For example, with the first example’s x in A2:A6 and y in B2:B6:

=LINEST(B2:B6, A2:A6, TRUE, TRUE)

Because LINEST returns several values, it must be entered as an array formula in older versions of Excel (Microsoft Support, LINEST function). The first row of the result should read 1.99 and 0.05, matching the hand calculation.

Linear regression in R and Python

In R, lm fits linear models; the formula y ~ x includes an intercept by default (R documentation for lm):

x <- c(1, 2, 3, 4, 5)
y <- c(2.1, 3.9, 6.2, 7.8, 10.1)
fit <- lm(y ~ x)
summary(fit)

In Python, scipy.stats.linregress returns the slope, intercept, Pearson r, a p-value for the null hypothesis of zero slope (a Wald test using the t-distribution), the slope’s standard error and the intercept’s standard error (SciPy documentation):

from scipy import stats
res = stats.linregress([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1])
print(res.slope, res.intercept, res.rvalue**2)

Both should reproduce the slope 1.99, intercept 0.05 and r2 of 0.9973 from example 1. To plot the points and the line before trusting the numbers, see scatterplots in R.

Using a handheld calculator or an online solver

The phrases “linear regression on a calculator” and “calculator for linear regression” cover scientific and graphing calculators as well as web tools. Key sequences differ between models, so use the statistics or regression mode described in your calculator’s manual. Whatever tool you use, the checks are the same: the slope and intercept should match the formulas above, and the line should pass through (x̄, ȳ). Running example 1 through any solver and getting 1.99 and 0.05 confirms it is doing ordinary least squares.

Frequently asked questions

How do you calculate a linear regression?

Compute Sxy and Sxx from the deviations around the means, divide to get the slope, then set the intercept to ȳ − slope × x̄. The two worked examples above show every step.

How do you find the linear regression in Excel?

Use LINEST with the y range first and the x range second. Set the fourth argument to TRUE to get standard errors and r2 as well.

What does r2 tell me?

It is the square of the Pearson correlation between x and y for a simple regression, the coefficient of determination. It describes fit to the sample, not whether the relationship is causal. For multiple predictors, see adjusted R-squared vs R-squared, and for testing a whole model see how to interpret the F-statistic.

When is ordinary least squares the wrong calculator?

Ordinary least squares treats x as measured without error. When both variables carry measurement error, as in method-comparison studies, see Deming regression and Passing-Bablok regression versus ordinary least squares.

Can I use the line to predict outside my data?

The formulas will return a number for any x, but predictions beyond the observed x range are extrapolations that the data do not support on their own.

Sources and rounding

Primary sources, checked 2026-10-28:

Worked-example values and the t table were computed with Python and SciPy on 2026-10-28; slopes and intercepts are shown to two or four decimals, and t values to three.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Linear Regression Calculator: Formulas, Worked Examples, Excel, R and Python

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →