Maths

Linear Regression Calculator

Fit the least-squares line to x, y data, with slope, intercept, r, r² and a prediction.

By Nitin Raturi · Last reviewed

Your data

How to use the Linear Regression Calculator

Fit the least-squares line to paired data and predict from it.

  1. Paste x and y values, one pair per line.
  2. Enter an x value to predict at.
  3. Read the equation, r and r², then the prediction.

Good to know

  • Correlation does not show that x causes y.

Data (x y per line)

Pairs of values, the x first, one pair per line.

How to fill it in

  • Separate x and y with a space, comma or tab; paste two spreadsheet columns.
  • At least three pairs, up to 10,000.

Example

OpenStax’s third-exam and final-exam scores give ŷ = 4.8274x − 173.5134, r = 0.6631.

One pair per line: x then y, separated by a space, comma or tab.

Predict y at x

An x value to plug into the fitted line.

How to fill it in

  • Enter any number; predictions far outside the data are unreliable.

Example

At x = 73 the exam line predicts 178.89. Celsius 0 to 40 against Fahrenheit gives ŷ = 1.8000x + 32.0000 and predicts 212.00 at 100 °C.

Tip: A weak pattern still gets a line: 1 1, 2 3, 3 2, 4 4 gives r = 0.8000.

Least-squares line

ŷ = 4.8274x − 173.5134

From 11 pairs, ŷ = 4.8274x − 173.5134: a moderate positive correlation (r = 0.6631). At x = 73 the line predicts 178.8864.

Slope (b)
4.8274
Intercept (a)
-173.5134
Correlation (r)
0.6631
r²
0.4397
Residual standard error
16.4124

Ordinary least squares on y against x. r² is the share of the variation in y explained by the line. Predictions outside the range of x are extrapolations.

How this calculator works

  1. Step 1Meansx̄, ȳThe line passes through both.
  2. Step 2SlopeΣ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²Least squares.
  3. Step 3Fitr and r²How closely the points follow it.

The least-squares line is the one that makes the squared vertical distances from the points as small as possible. Its slope comes from how x and y vary together.

The correlation r runs from −1 to 1. r² is the share of the variation in y that the line explains.

Worked example: OpenStax 12.3

Third exam (x) and final exam (y), 11 students
Line
ŷ = a + b x
  1. Slope b4.8274
  2. Intercept a-173.5134
  3. r0.6631
  4. r²0.4397
  5. Predicted final at 73178.89

OpenStax gives ŷ = −173.51 + 4.83x, r = 0.6631 and r² = 0.4397.

Read the result

  • °C to °Fŷ = 1.8000x + 32.0000Perfect fit: r = 1. Predicts 212 at 100 °C.
  • A loose patternr = 0.8000Still a trend, with scatter around it.

In the exam data r² = 0.4397: the third exam explains about 44% of the variation in final scores.

Assumptions and limits

  • Straight-line patternCheck a scatter plot first.
  • Not causationCorrelation alone does not show cause.
  • OutliersOne extreme point can swing the line.
  • ExtrapolationPredictions beyond the data are risky.

A regression line summarises how two variables move together.

For the slope between just two points, use the slope calculator. The standard deviation calculator measures the spread of each variable, and the t-test calculator compares two group means.

Common mistakes

  • Mistake: Swapping x and y.

    Instead: Put the predictor first; y on x and x on y differ.

  • Mistake: Reading r = 0.66 as 66% accuracy.

    Instead: r² = 0.44 is the share of variation explained.

Checking the method

A separate script solves the normal equations by Cramer’s rule in exact fractions and reproduces OpenStax’s line, r and r².

Read how we test our calculators, or browse more maths calculators.

Frequently asked questions

What is the regression line for OpenStax’s exam data?

ŷ = 4.8274x − 173.5134, with r = 0.6631 and r² = 0.4397.

What does r² mean?

The share of the variation in y explained by the line: 0.4397 in the exam example.

Can it predict a value?

Yes. The exam line predicts 178.89 for a third-exam score of 73.

What does a perfect fit look like?

Celsius against Fahrenheit gives ŷ = 1.8000x + 32.0000 with r = 1.

Is a weak correlation still useful?

It shows a trend with scatter. 1 1, 2 3, 3 2, 4 4 gives r = 0.8000.

Sources and methodology

The formulas are standard time-value-of-money results. We check every figure against golden values worked out independently, by closed form and by month-by-month simulation; see the methodology.

Written and maintained by Nitin Raturi, a software engineer (about the author). Last reviewed 4 Nov 2026; next review due by . Found an error? Tell us.

Changelog

  • : Initial release.