Home › Math & Science › Linear Regression Calculator

Linear Regression Calculator

Use this free linear regression calculator to find the straight line that best fits your data. Paste your x,y points — one pair per line — and the calculator gives you the slope, the intercept, the R² value, and the equation of the line, then predicts y for any x you choose. The scatter plot updates live so you can see how well the line fits.

Use a comma, a space, or a tab between x and y. Lines starting with # are ignored. You need at least 2 points with different x values.

y = 2.00x + 0.00
Slope 2.00 · Intercept 0.00 · R² = 1.00 · n = 5 points · r = 1.00
Prediction: when x = 6, y = 12.00

How linear regression works

Linear regression finds the straight line that best fits a cloud of data points. Suppose you record how many hours students study and the score they get on a test. Plot each student as a point, hours on the horizontal axis and score on the vertical axis, and the points will usually rise from bottom-left to top-right — more study, higher score — but they will not sit exactly on a line. Linear regression finds the one straight line that sits closest to all of them at once. That line is described by the equation y = mx + b, where m is the slope and b is the intercept.

y = mx + b
Slope m = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²
Intercept b = ȳ − m·x̄

The slope, m, is how much y changes when x goes up by one. If the slope is 2, then each extra hour of study is worth about 2 extra test points. The intercept, b, is the value of y when x is zero — the score the line predicts for zero hours of study. Together they define the line completely: pick any x, multiply by the slope, add the intercept, and you have the predicted y. That is the prediction part of this calculator.

The line is found by the method of least squares. For each point, measure the vertical gap between the point and the line — the residual — square it, and add up all the squares. The best-fit line is the one that makes that total as small as possible. Squaring stops positive and negative gaps from cancelling each other out, and it punishes big misses more than small ones. The formulas in the box above are the direct result of that minimisation: the slope is the covariance of x and y divided by the variance of x, and the intercept is chosen so the line passes through the average point (x̄, ȳ).

R², read as R-squared, measures how good the fit is. It runs from 0 to 1 and tells you what share of the variation in y the line explains. An R² of 1.00 means every point sits exactly on the line, as in the worked example below. An R² of 0.80 means the line explains 80% of the variation and the rest is noise or other factors. An R² near 0 means the line explains almost nothing — the points are a formless cloud, or the real relationship is curved rather than straight. The correlation coefficient r is the square root of R² with the sign of the slope, and it runs from −1 to +1: +1 is a perfect rising line, −1 is a perfect falling line, and 0 is no straight-line relationship at all.

Two warnings keep the tool honest. First, regression shows association, not cause. A line linking ice-cream sales to sunburn cases would fit beautifully, but ice cream does not cause sunburn — hot weather causes both. Second, predictions are only trustworthy inside the range of your data. If your data runs from x = 1 to x = 10, predicting at x = 12 is a small stretch, but predicting at x = 100 assumes the straight line keeps going forever, and real relationships usually bend, flatten, or stop. The calculator will predict anywhere you ask, but it is your job to judge whether the answer is sensible.

Worked example

Three points fall exactly on a line: (1, 2), (2, 4), and (3, 6). What is the regression line?

Step 1: Find the averages — x̄ = (1 + 2 + 3) ÷ 3 = 2, and ȳ = (2 + 4 + 6) ÷ 3 = 4.
Step 2: Work out the slope — Σ(x − x̄)(y − ȳ) = (−1)(−2) + (0)(0) + (1)(2) = 4, and Σ(x − x̄)² = 1 + 0 + 1 = 2, so m = 4 ÷ 2 = 2.
Step 3: Work out the intercept — b = ȳ − m·x̄ = 4 − 2 × 2 = 0.
Step 4: Check the fit — every point satisfies y = 2x exactly, so R² = 1.00.

So the answer is y = 2x + 0, with slope 2, intercept 0, and R² = 1.00. Predicting at x = 4 gives y = 2 × 4 + 0 = 8, which matches the pattern exactly. Try it in the calculator above, then change a point — for example, make the last point (3, 5) — and watch the slope, intercept, and R² move as the line stops fitting perfectly.

FAQ

What is linear regression used for?

Linear regression finds the straight line that best fits a set of data points, so you can see how one variable changes with another and predict new values. It is used for sales forecasts, study time versus exam scores, advertising spend versus revenue, height versus weight, and any situation where two numbers move together in roughly a straight line.

What does R² mean in linear regression?

R², read as R-squared, tells you how much of the variation in y is explained by the line. It runs from 0 to 1. An R² of 1.00 means every point sits exactly on the line, 0.80 means the line explains 80% of the variation, and 0.00 means the line explains nothing. A high R² means the straight line is a good description of your data; a low one means the relationship is weak or not straight.

How many data points do I need for linear regression?

You need at least two points with different x values to draw a line, but two points always give a perfect fit, so the result means very little. Three points are the minimum for a real check, and ten or more points give a much more trustworthy slope, intercept, and R². More points also make the prediction more reliable, as long as the points really follow a straight-line pattern.

What is the difference between slope and intercept?

The slope is how much y changes when x goes up by one — it is the steepness of the line. The intercept is the value of y when x is zero — it is where the line crosses the vertical axis. In the equation y = 2x + 3, the slope is 2, so y rises by 2 for each step in x, and the intercept is 3, so the line crosses the y-axis at 3.

Can I trust a prediction far outside my data?

Not much. Predictions inside the range of your data are usually reasonable, but predictions far outside it — called extrapolation — assume the straight-line pattern keeps going, and real relationships often bend, flatten, or stop. If your data runs from x = 1 to x = 10, a prediction at x = 12 is a small stretch, but a prediction at x = 100 is a guess, not a calculation.