Linear Regression Calculator

X values

Y values

Slope (m)0.9000
Intercept (b)1.3000
Correlation (r)0.9000
R²0.8100
Equationy = 0.9x + 1.3
How this was calculated
  1. The means are x̄ = 3.0000 and ȳ = 4.0000 over 5 points.
  2. Slope = Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)² = 9.0000 ÷ 10.0000 = 0.9000. The intercept follows: b = ȳ − m·x̄ = 1.3000.
  3. The correlation r = 0.9000, so R² = 0.8100 — about 81.0% of the variation in y tracks the line.
  4. Use the line to predict inside the data range; outside it the trend is an extrapolation, not a measurement.

Our linear regression calculator finds the line of best fit for any set of (x, y) points using the least squares method. It returns the slope, intercept, correlation coefficient r, and R² value, so you can see both the equation of the line and how well it fits. For example, the points (1,2), (2,3), (3,5), (4,4), (5,6) produce a slope of 0.9000, an intercept of 1.3000, r = 0.9000, and R² = 0.8100, giving the line y = 0.9x + 1.3. Points that fall on a perfectly straight line, such as (0,1), (1,3), (2,5), (3,7), yield r = 1.0000. A declining trend like (1,10), (2,8), (3,7), (4,5), (5,3), (6,2) returns a slope of −1.6286, an intercept of 11.5333, and r = −0.9955.

Least squares is the accepted standard because it fits the line that minimizes the total squared vertical distance to every point. The slope equals Σ(x−x̄)(y−ȳ) divided by Σ(x−x̄)², and the intercept equals ȳ minus the slope times x̄. R² represents the share of y's variation that the line accounts for, so a value of 0.81 means about 81% of the variation tracks the line. The correlation r and the slope always share the same sign, and while r is unit-free, the slope carries the units of y per unit of x. This page shows the formula, step-by-step workings, worked examples, and sources.

Worked examples

A rising scatter

Primary result: slope 0.9000 — the line is y = 0.9x + 1.3 with r = 0.9. (5 points · 1 · 2 · 3 · 4 · 5 · 6 · 2 · 3 · 5 · 4 · 6 · 0)

Slope (m)0.9000
Intercept (b)1.3000
Correlation (r)0.9000
R²0.8100
Equationy = 0.9x + 1.3

A perfect line

Primary result: slope 2.0000 — r = 1 exactly, as it must be. (4 points · 0 · 1 · 2 · 3 · 5 · 6 · 1 · 3 · 5 · 7 · 6 · 0)

Slope (m)2.0000
Intercept (b)1.0000
Correlation (r)1.0000
R²1.0000
Equationy = 2x + 1

A falling trend

Primary result: slope −1.6286 with r = −0.9955 — a near-perfect negative fit. (6 points · 1 · 2 · 3 · 4 · 5 · 6 · 10 · 8 · 7 · 5 · 3 · 2)

Slope (m)-1.6286
Intercept (b)11.5333
Correlation (r)-0.9955
R²0.9911
Equationy = -1.6286x + 11.5333

How least-squares regression works

Least squares gets its name from what it does: it fits the line that minimises the total squared vertical distance to the points. In other words, no other straight line will sit closer, on average, to your scatter of data. The method first finds the mean of your x-values (written x̄) and the mean of your y-values (written ȳ). Every point is then measured against those two means, and those differences feed directly into the slope formula.

The slope is calculated as Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)². The numerator adds up how much each x and each y move together away from their respective means. The denominator adds up how much the x-values themselves spread out. Dividing the two gives the rate at which y changes per unit of x. Once the slope is known, the intercept is straightforward: intercept = ȳ − slope × x̄. That pins the line to the center of the data so it passes through the point (x̄, ȳ).

To see this with real numbers, take the points (1,2), (2,3), (3,5), (4,4), and (5,6). The mean of x is 3 and the mean of y is 4. Plugging each point into the slope formula gives a numerator of 9 and a denominator of 10, so the slope is 0.9000. The intercept is then 4 − 0.9 × 3 = 1.3000. The fitted line is y = 0.9x + 1.3, and a linear regression calculator would also report r = 0.9000 and R² = 0.8100 for this set. R² is the share of y's variation the line accounts for, so 0.81 means about 81% of the up-and-down movement in y tracks the line.

The correlation r and the slope always share the same sign, which tells you whether the relationship rises or falls. The value r is unit-free, while the slope carries the units of y per unit of x. When points fall perfectly on a straight line, r reaches its extreme. For example, the points (0,1), (1,3), (2,5), and (3,7) lie exactly on y = 2x + 1, so r = 1.0000. A negative slope works the same way but in the opposite direction: the falling set (1,10), (2,8), (3,7), (4,5), (5,3), and (6,2) gives slope −1.6286, intercept 11.5333, and r = −0.9955. The strong negative r confirms the line drops sharply, and it is very close to −1 because the points hug the fitted line tightly.

Worked examples

The clearest way to understand what a linear regression calculator produces is to run it on data sets with known answers. Least squares fits the line that minimises the total squared vertical distance to the points: slope = Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)², intercept = ȳ − slope × x̄. The three verified examples below cover a rising scatter, a perfect line, and a falling trend, so you can see how the slope, intercept, correlation r, and R² change together.

The first set, (1,2), (2,3), (3,5), (4,4), (5,6), produces a rising scatter with some noise around the trend. The linear regression calculator returns slope 0.9000, intercept 1.3000, r = 0.9000 and R² = 0.8100, giving the line y = 0.9x + 1.3. R² is the share of y's variation the line accounts for: 0.81 means about 81% tracks the line. The correlation r and the slope always share the same sign, and r is unit-free while the slope carries the units of y per unit of x. Here both are positive, matching the upward pattern.

The second set, (0,1), (1,3), (2,5), (3,7), is a perfect line. These points lie exactly on y = 2x + 1, so r = 1.0000. Every point sits on the fitted line, so there is no residual scatter and R² reaches its maximum. This is the benchmark case: when r equals 1, the fit is exact and the slope and intercept fully describe the data with no error.

The third set, (1,10), (2,8), (3,7), (4,5), (5,3), (6,2), shows a falling trend. The calculator gives slope −1.6286, intercept 11.5333 and r = −0.9955. The negative slope and negative r again share the same sign, and r stays unit-free while the slope carries the units of y per unit of x. An R² very close to 1 means the line accounts for nearly all of the variation in y, even though the relationship is inverse.

Data setSlopeInterceptrR²
(1,2), (2,3), (3,5), (4,4), (5,6)0.90001.30000.90000.8100
(0,1), (1,3), (2,5), (3,7)2.00001.00001.00001.0000
(1,10), (2,8), (3,7), (4,5), (5,3), (6,2)−1.628611.5333−0.99550.9910

Reading r and R²

When you use a linear regression calculator, two numbers tell you how well the line fits: r (the correlation coefficient) and R² (the coefficient of determination). The value of r runs from −1 to 1 and describes both the direction and the strength of the linear relationship. A positive r means that as x increases, y tends to increase as well; a negative r means that as x increases, y tends to decrease. The correlation r and the slope always share the same sign, so a negative slope guarantees a negative r, and a positive slope guarantees a positive r. However, r is unit-free, while the slope carries the units of y per unit of x.

The strength of the correlation matters just as much as the direction. An r of 1.0000 means every point sits perfectly on a straight line, with no scatter at all. For example, the points (0,1), (1,3), (2,5), (3,7) lie exactly on the line y = 2x + 1, giving r = 1.0000. An r of 0.9000 indicates a strong positive relationship, but the points drift slightly off the line. The points (1,2), (2,3), (3,5), (4,4), (5,6) give slope 0.9000, intercept 1.3000, and r = 0.9000 — the line y = 0.9x + 1.3 fits well, but the points do not sit perfectly on it.

On the negative side, an r of −0.9955 feels almost like a straight line but heading downward. The points (1,10), (2,8), (3,7), (4,5), (5,3), (6,2) give slope −1.6286, intercept 11.5333, and r = −0.9955. The points hug the line closely, with only tiny deviations, and the negative sign confirms the downward trend. The closer r is to −1 or 1, the tighter the scatter around the line; the closer r is to 0, the more the points spread out and the weaker the linear relationship becomes.

R² tells you the share of y's variation that the line accounts for, and it is easy to read as a percentage. An R² of 0.8100 means about 81% of the variation in y tracks the line, while the remaining 19% comes from other factors or scatter. Because R² is always between 0 and 1, it gives a straightforward sense of fit quality regardless of whether the slope is positive or negative. A higher R² means the line captures more of the pattern in the data, while a lower R² means the line leaves more of the variation unexplained.

  • r = 1.0000: points sit perfectly on the line, zero scatter
  • r = 0.9000: strong positive fit with slight scatter around the line
  • r = −0.9955: strong negative fit, points hug the line tightly as it falls
  • R² = 0.8100: about 81% of y's variation is explained by the line
The sign of r always matches the sign of the slope, but r carries no units while the slope is expressed in units of y per unit of x.

Slope and intercept in plain words

When you run data through a linear regression calculator, two numbers do most of the work: the slope and the intercept. In plain words, the slope tells you how much y grows for each extra unit of x. If the slope is 0.9000, then y rises by 0.9000 every time x increases by 1. The intercept, in turn, is simply the line's value at x = 0 — the height where the fitted line crosses the vertical axis.

The math behind these values comes from least squares, which fits the line that minimises the total squared vertical distance to the points. The slope equals Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)², and the intercept equals ȳ − slope × x̄. For the points (1,2), (2,3), (3,5), (4,4), (5,6), this gives slope 0.9000, intercept 1.3000, and the line y = 0.9x + 1.3. The correlation r is 0.9000 and R² is 0.8100, meaning about 81% of y's variation tracks the line.

The slope can also be negative. The falling set (1,10), (2,8), (3,7), (4,5), (5,3), (6,2) gives slope −1.6286, intercept 11.5333 and r = −0.9955. Here y drops by 1.6286 for each extra unit of x. The correlation r and the slope always share the same sign, but they differ in one key way: r is unit-free, while the slope carries the units of y per unit of x.

Sometimes the intercept is meaningful and sometimes it is not. When the points (0,1), (1,3), (2,5), (3,7) lie exactly on y = 2x + 1, the intercept 1.0000 is a real, observed value because x = 0 is part of the data. But if x represents something like years since 1900 or a person's age in adulthood, then x = 0 may fall outside the range you collected. In that case the intercept is just where the line mathematically starts, not a number you should interpret on its own.

Predicting with the line — and its limits

Once you have your fitted line, the natural next step is plugging in an x-value to get a predicted y. Using the verified example from above, the line y = 0.9x + 1.3 lets you estimate y for any x you choose. Say you want to predict y when x equals 3.5, which falls between 1 and 5 — the range of the original data points (1,2), (2,3), (3,5), (4,4), and (5,6). Plugging in, you get y = 0.9 × 3.5 + 1.3 = 4.45. Because 3.5 sits inside the observed range, this prediction is called interpolation, and it is generally the safest use of a linear regression calculator.

Interpolation works well because the line was fit to data spanning that exact range, so you are staying within territory the model has actually seen. The least squares method, which minimizes the total squared vertical distance to the points using slope = Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)² and intercept = ȳ − slope × x̄, produces a line that describes the relationship observed in that window. With an R² of 0.8100 for this data set, about 81% of y's variation tracks the line, meaning the remaining 19% reflects scatter the line cannot capture. A prediction at x = 3.5 will sit among the original points, so any error is likely bounded by the scatter you can already see.

Extrapolation is different. If you predict y when x equals 10, you get y = 0.9 × 10 + 1.3 = 10.3. That answer may look precise, but x = 10 lies well outside the data range of 1 to 5. The fitted line says nothing about whether the relationship holds beyond the observed window. Real-world relationships often flatten, curve, or reverse outside the range of measurement, so extrapolated predictions carry risk that grows the further you move from the data. The line y = 0.9x + 1.3 could remain accurate at x = 10, or the true relationship could look nothing like a straight line there — the model alone cannot tell you which.

A useful check is the correlation r, which for this data set equals 0.9000. Because r and the slope always share the same sign, a positive r confirms the positive slope here. Remember that r is unit-free while the slope carries the units of y per unit of x, so a slope of 0.9 means y increases by 0.9 units for every 1 unit increase in x. Even a strong r of 0.9000, though, describes the linear association within the data range only. It does not guarantee the association persists outside it.

In short, treat predictions inside the data range as reasonable estimates backed by the observed pattern, and treat predictions outside the range as educated guesses that could be quite wrong. The line is a summary of the points you have, not a guarantee about points you do not.

  • Interpolation: predicting within the data range (x between 1 and 5 in the example) — generally reliable
  • Extrapolation: predicting outside the data range (x below 1 or above 5) — increasingly unreliable the further you go
  • R² = 0.8100 means about 81% of y's variation is captured by the line; the rest is scatter
  • r and the slope always share the same sign; r is unit-free while the slope has units of y per unit of x
A linear regression calculator gives you the best-fit line for the data you entered. It does not tell you whether that line still applies beyond the range of that data. Use interpolation freely; use extrapolation cautiously.

Correlation is not causation

When you run data through a linear regression calculator and see a strong correlation, it is tempting to conclude that one variable drives the other. Resist that temptation. A high r, even a value as clean as 0.9000, only tells you how closely the points cluster around the fitted line. It says nothing about whether x causes y, whether y causes x, or whether some hidden third factor moves both of them together.

Consider the verified example where the points (1,2), (2,3), (3,5), (4,4), (5,6) produce a slope of 0.9000, an intercept of 1.3000, and r = 0.9000. The R² of 0.8100 means about 81% of the variation in y tracks the line. That sounds impressive, and the math is correct. But the math cannot tell you why those two variables moved together. A lurking variable, something you did not measure, could be driving both. Ice cream sales and drowning incidents may rise together over the summer, but one does not cause the other; the temperature does.

The same caution applies when the fit is nearly perfect. The falling set (1,10), (2,8), (3,7), (4,5), (5,3), (6,2) gives a slope of −1.6286, an intercept of 11.5333, and r = −0.9955. That is a remarkably tight fit, yet it still proves nothing about cause and effect. Even the perfect case, where the points (0,1), (1,3), (2,5), (3,7) lie exactly on the line y = 2x + 1 and r = 1.0000, only confirms a mathematical relationship. Perfect correlation can exist between two variables that have no direct connection at all.

Remember what the numbers actually describe. Least squares fits the line that minimizes the total squared vertical distance to the points, using the formula slope = Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)² and intercept = ȳ − slope × x̄. The correlation r and the slope always share the same sign, and r is unit-free while the slope carries the units of y per unit of x. R² is the share of y's variation the line accounts for. None of these statistics can identify what stands behind the pattern.

  • A high r measures how tightly the points follow a line, not whether one variable causes another.
  • Lurking variables can move both x and y together, creating a strong fit with no direct link between them.
  • Even r = 1.0000 only proves a perfect mathematical relationship, never a causal one.
A linear regression calculator can show you that two variables move together and how tightly. Deciding whether one actually causes the other requires context, theory, and evidence that the math alone cannot supply.

FAQs

How do you calculate a regression line?

The slope equals Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)², and the intercept equals ȳ − slope × x̄. For the example set, these formulas produce the line y = 0.9x + 1.3. The slope shows how much y changes per unit of x, while the anchor point where the line crosses the y axis.

What does R² mean?

R² represents the share of variation in y explained by the regression line. A value of R² = 0.81 means about 81% of the variation in y is accounted for by the line. Higher values indicate a better fit, while lower values suggest the line leaves much of the variation unexplained.

What is a good correlation coefficient?

Context decides what counts as good, but |r| above 0.7 is usually called strong. The falling example's −0.9955 is near-perfect, showing an extremely tight linear relationship. What qualifies as acceptable depends on the field and purpose, so judgment matters when interpreting the strength of correlation.

Can a regression have a negative slope?

Yes, a regression line can slope downward. The six-point falling set has slope −1.6286 and r = −0.9955, meaning y decreases as x increases. Negative slopes simply indicate an inverse relationship between the variables, and they can fit data just as well as positive slopes when the pattern calls for it.

What if all my x values are the same?

No slope exists when all x values are identical. The points form a vertical stack, and least squares cannot fit a line because there is no variation in x to explain changes in y. You need at least two distinct x values to compute a meaningful regression slope.

Should I predict outside my data range?

Predicting outside your data range, called extrapolation, should be done carefully at best. Extrapolation assumes the trend continues unchanged, which real data often refuses to do. Patterns can shift, flatten, or reverse beyond the observed range, so predictions made there carry significant uncertainty and may mislead.

Sources

Related calculators

Standard Deviation Calculator
Mean, sample and population standard deviation and variance for up to eight numbers — with the n versus n−1 difference explained.
Mean Median Mode Calculator
Mean, median, mode, range and sum for up to ten numbers — with the sorted list and the working shown, so the three 'averages' finally make sense.
Average Calculator
Mean, median, mode, range, sum and count for 2 to 8 numbers — the full descriptive-statistics starter set in one pass.
Z-Score Calculator
The z-score of any value in a normal distribution, its percentile, and the share of values above and below it — the standardisation behind test scores and quality control.