Skip to content
Calcapedia
Statistics & Data

Linear Regression Calculator

Find the straight line that best fits your paired x and y data, with its slope, intercept, r, R² and a prediction for any x.

Level: Grades 9–10

Printable worksheet

Linear Regression Calculator

Inputs

Separate with commas, spaces or new lines.

Same count and order as the x values.

Result

Regression line

ŷ = 4.3714x + 47.2

The least-squares line through 6 points is ŷ = 4.3714x + 47.2, with r = 0.9954 and R² = 0.9909.

Slope (b)
4.3714
Y-intercept (a)
47.2
Correlation (r)
0.9954
R² (share of variation explained)
0.9909
Number of pairs
6
Predicted y at x = 7
77.8Extrapolation: x = 7 is outside the data range 1 to 6, so treat this prediction with caution.

See the step-by-step solution ↓

Step-by-step solution

  1. Find the average (mean) of the x values, written x̄

    x̄=3.5
  2. Find the average (mean) of the y values, written ȳ

    ȳ=62.5
  3. Measure how spread out x is (Sxx): square each x value's distance from the mean, then add them up

    Sxx=17.5
  4. Measure how x and y move together (Sxy): multiply each point's x and y distances from their means, then add them up

    Sxy=76.5
  5. Find the slope b, how much y changes for each step in x: divide Sxy by Sxx

    b=76.517.5=4.371429
  6. Find the intercept a, where the line crosses the y-axis. It puts the line through the point of the two averages

    a=62.5−4.371429×3.5=47.2
  7. Put the slope and intercept into the line's equation

    ŷ = 4.3714x + 47.2
  8. Find the correlation r, how tightly the points follow a line. It compares Sxy with the spread of both x and y (Syy is the spread of y)

    r=76.517.5×337.5=0.995418
  9. Square r to get R², the share of the ups and downs in y that the line explains

    R²=0.9954182=0.990857
  10. Put your new x into the equation to predict y (written ŷ)

    ŷ=4.371429×7+47.2=77.8

Test yourself

20 quick questions built from this calculator's formula: work out results, predict what happens when an input changes, and work backward from an answer. Every answer comes with the working.

What this calculator does

Find the straight line that best fits your data, and use it to make predictions. It shows how one thing tends to change with another. Examples are test scores with hours studied, or sales with money spent on ads. "Best" has an exact meaning here. Measure how far each point sits above or below the line, square those gaps, and add them up. The best-fit (least-squares) line makes that total as small as possible.

Enter matching lists of x and y values. The calculator gives you the equation of the line and its slope and intercept. It also gives r and R², which show how closely the points follow the line. If you enter a new x, it predicts the y value the line expects for it. Every number is worked out step by step from your data.

Where it's used

Why this math matters outside the classroom:

  • Sales forecasts. A shop that fits sales = 120 + 15x, with x in hundreds of dollars of ads, predicts 120 + 15 × 4 = 180 sales at $400.
  • Spring constants. Physics students fit a line to load against stretch for a spring, and the slope estimates the spring constant.
  • Home prices. An appraiser fits price against floor area and reads the slope as the added price per extra square foot.
  • Plant growth. A student fits a line to a seedling's height each day and uses it to predict the height on day 20.
  • Fuel use. A fleet manager fits fuel used against miles driven and uses the slope as each truck's gallons per mile.

Grade level and standards: Grades 9–10; Common Core State Standards for Mathematics HSS.ID.B.6.c, HSS.ID.B.6.a, HSS.ID.C.7, HSS.ID.C.8. © Copyright 2010. National Governors Association Center for Best Practices and Council of Chief State School Officers. All rights reserved.

How to use it

  1. Enter the x values in the first box. These are the predictor: the thing you think explains or drives the other.
  2. Enter the y values in the second box, in the same order, so each x lines up with its y. These are the response you want to predict.
  3. If you like, enter a new x value to get a prediction from the line.
  4. Read the equation, then the slope, intercept, r and R² below it.

What the inputs mean

X values (predictor)
Each x pairs with the y in the same position. You need at least two pairs, and the x values can't all be the same.
Y values (response)
This list must have exactly as many values as the x list.
Predict y at x = (optional)
Predictions are most trustworthy for x values inside the range of your data. The calculator warns you when you go beyond it, which is called extrapolating.

Formula and method

Sxx = Σ(x − x̄)²,  Syy = Σ(y − ȳ)²,  Sxy = Σ(x − x̄)(y − ȳ)
slope b = Sxy / Sxx
intercept a = ȳ − b × x̄
ŷ = b·x + a
r = Sxy / √(Sxx × Syy),   R² = r²

In the formulas, x̄ and ȳ are the averages (means) of the x and y values, and Σ means "add up". Sxx is how spread out the x values are, and Syy is the same for y. Sxy shows how x and y move together. The slope is b, the intercept is a, and ŷ ("y-hat") is the y value the line predicts.

The slope compares how x and y move together (Sxy) with how much x varies on its own (Sxx). The intercept then places the line so it passes through the point of the two averages (x̄, ȳ). Every best-fit line goes through that point.

R² is the share of the ups and downs in y that the line accounts for. With one predictor, it equals r squared. The number r is called the correlation, and its sign (plus or minus) matches the sign of the slope.

The sums are worked out from each value's distance from the mean. That keeps them accurate even when the values are large compared with how spread out they are.

Worked example

Six students studied for 1 to 6 hours and scored 52, 55, 61, 64, 70 and 73. What line fits, and what score does it predict for 7 hours?

  1. Averages: x̄ = 3.5 hours and ȳ = 62.5 points.
  2. Sxx = 17.5 and Sxy = 76.5.
  3. Slope: 76.5 ÷ 17.5 ≈ 4.3714 points per hour.
  4. Intercept: 62.5 − 4.3714 × 3.5 = 47.2.
  5. Equation: ŷ = 4.3714x + 47.2, with r ≈ 0.9954 and R² ≈ 0.9909.
  6. Prediction at x = 7: 4.3714 × 7 + 47.2 = 77.8.

Result: ŷ = 4.3714x + 47.2, which predicts a score of 77.8 after 7 hours. That's an extrapolation, since the data stop at 6.

Understanding the result

The slope is how much y is predicted to change each time x goes up by one. In the example, each extra hour of study goes with about 4.37 more points on average.

The intercept is the predicted y when x is zero. It only means something if x = 0 is a sensible value, inside or near your data.

An R² close to 1 means the points sit close to the line. Close to 0 means the line explains little. A high R² doesn't by itself show that x causes y.

Assumptions and limitations

  • The relationship between x and y is roughly a straight line. Plot your data first: a curve or a few far-off points (outliers) can give a misleading line.
  • The line itself doesn't depend on how your data are spread. But drawing firmer conclusions from it, like confidence intervals or tests on the slope, does. Those assume each point is independent, and that the misses from the line are roughly bell-shaped (normal) and about equally spread across the range of x.
  • The x values are treated as measured without error. Only the up-and-down (vertical) gaps to the line are made as small as possible.
  • It fits only simple linear regression, with one predictor. Curves, several predictors and weighted fits aren't covered.
  • It doesn't calculate standard errors, confidence intervals for the slope or prediction intervals.
  • The best-fit method is sensitive to outliers: one extreme point can swing the slope noticeably.
  • Predicting far outside the range of your data (extrapolation) assumes the straight-line pattern keeps going, and it often doesn't.

Frequently asked questions

Which variable should be x?

Put the thing you're predicting from, or the one you control, on x. Put the outcome on y. Swapping them gives a different line, because the method only shrinks the up-and-down gaps.

What is the difference between r and R²?

r measures how strong the straight-line relationship is and which way it goes, from −1 to 1. R² is r squared, from 0 to 1. It reads as the share of the ups and downs in y that the line explains. An r of 0.7 gives R² = 0.49, so the line explains about half.

Why does the calculator refuse when all the x values are equal?

If x never changes, there's no way to see how y changes as x changes. The points sit on a vertical line, and a vertical line has no slope you can calculate.

Does a good fit prove that x causes y?

No. Regression shows that two things go together, not that one causes the other. A third factor may drive both, or the cause may run the other way. Proving cause needs a planned experiment or careful knowledge of the subject.

How this calculator is checked

Method: Ordinary least-squares estimates for simple linear regression and the Pearson correlation coefficient, computed with centered (two-pass) sums of squares. The calculation is covered by automated tests with known reference values and edge cases. Last updated 2026-10-18. Found a problem? Tell us.

Embed this calculator

Put this Linear Regression Calculator on your own website or blog for free. Paste this code where you want it to appear: