Linear Regression Calculator
The line through your data, and the numbers that say how much to trust it. The linear regression calculator adds a residuals table, because the summary statistics alone can hide a curve or an outlier.
Line of best fit
- Line of best fit Slope 2.17255: each one-unit increase in x goes with a change of 2.17255 in y, on average. Intercept 55.1627: the line's value at x = 0, which is outside your data and may mean nothing physically.
- y = 2.17255x + 55.1627
- Correlation r A strong positive linear association, by the usual rule of thumb. r measures how closely the points follow a straight line, not how steep it is.
- 0.9272
- R-squared 86.0% of the variation in y is accounted for by the straight line in x. It says nothing about whether a straight line is the right model. Check the residuals.
- 0.8597
- Slope t statistic and p-value Testing whether the true slope could be zero: t = slope / standard error, on n - 2 = 6 degrees of freedom, two-sided. p is below 0.05. A small p says a slope this far from zero would be unusual if there were no linear relationship; it does not say the relationship is large, causal or correctly modeled.
- t = 6.064, p = 0.0009
- Standard error of the slope Residual standard error over the square root of the sum of (x - mean x) squared. With the t value of 2.447 for 6 degrees of freedom, the 95% confidence interval for the slope is 1.29585 to 3.04925.
- 0.358289
- Standard error of the intercept Grows the further x = 0 sits from the middle of your data, because the line is pinned down best at the mean of x.
- 3.12349
- Residual standard error The typical distance of a point from the line, in y's units: the square root of the sum of squared residuals (98.2039) over 6 degrees of freedom.
- 4.04565
- Predicted y at x = 8 The line's estimate of the average y at this x. Individual points scatter around it by about 4.04565 (the residual standard error).
- 72.5431
- Number of pairs Mean x 7.75, mean y 72. The line always passes through that point.
- 8
| x | y | Predicted y | Residual |
|---|---|---|---|
| 2 | 61 | 59.5078 | 1.49216 |
| 3 | 58 | 61.6804 | -3.68039 |
| 5 | 70 | 66.0255 | 3.97451 |
| 7 | 66 | 70.3706 | -4.37059 |
| 9 | 79 | 74.7157 | 4.28431 |
| 10 | 74 | 76.8882 | -2.88824 |
| 12 | 85 | 81.2333 | 3.76667 |
| 14 | 83 | 85.5784 | -2.57843 |
- Look at the residuals before trusting any figure above. If they run positive, then negative, then positive again along x, the relationship is curved and a straight line is the wrong model whatever r-squared says.
- No point sits more than twice the residual standard error from the line. A single point far out along x can still pull the line toward itself without leaving a large residual, so look at the x range too.
- Correlation is not causation. The line describes how y and x move together in this data; it cannot tell you that changing x would change y.
How the Linear Regression Calculator works
Linear regression finds the straight line that fits a set of (x, y) points best, in a specific sense: it minimizes the sum of the squared vertical distances from each point to the line. That is why it is called least squares, and the answer has a closed form.
Write the means of x and y as x-bar and y-bar. Then
slope m = sum of (x - x-bar)(y - y-bar) / sum of (x - x-bar)^2
intercept b = y-bar - m x x-bar
and the line is y = mx + b. It always passes through the point (x-bar, y-bar). For the default data (eight (x, y) pairs with x-bar = 7.75 and y-bar = 72), the cross-product sum is 277 and the sum of squares in x is 127.5, so m = 277 / 127.5 = 2.1725 and b = 72 - 2.1725 x 7.75 = 55.163. Each extra unit of x goes with about 2.17 more units of y, on average.
The correlation coefficient r measures how tightly the points cluster around a straight line, from -1 to +1. It is the same sum of cross-products divided by the square root of the product of the two sums of squares, so it has no units and does not depend on which variable is called x. R-squared is r squared, and reads as the share of the variation in y that the line accounts for.
The standard errors say how precisely the data pin the line down. The residual standard error is the typical vertical distance of a point from the line: the square root of the sum of squared residuals divided by n - 2. It is n - 2 rather than n because two numbers, the slope and the intercept, were estimated from the same data. The standard error of the slope is that residual error divided by the square root of the sum of (x - x-bar)^2. Spreading your x values wider gives a more precise slope, which is worth knowing when you design an experiment rather than analyze one.
The t statistic is the slope divided by its standard error, and the p-value comes from Student's t distribution on n - 2 degrees of freedom. It answers one narrow question: if there were truly no linear relationship, how often would data like these produce a slope at least this far from zero? This calculator computes it exactly, through the regularized incomplete beta function, rather than from a normal approximation that is wrong for small samples. The p-value relies on assumptions: the points are independent, the scatter around the line is roughly the same size everywhere, and for small samples that scatter is roughly normal.
Now the things the numbers cannot tell you.
Correlation is not causation. A strong fit between ice cream sales and drownings is real and is caused by summer, not by ice cream. Regression describes how two variables move together in the data you have. Whether changing x would change y is a question about how the data were produced. A controlled experiment answers it; an observational dataset usually cannot.
R-squared is not a verdict on the model. A curved relationship can give an r-squared of 0.9 with a straight line that is systematically wrong at both ends and in the middle. A genuine and important effect can have an r-squared of 0.1 because the outcome has many other causes. And r-squared rises when you sample a wider range of x without the relationship changing at all. The classic warning is Anscombe's quartet: four small datasets with the same fitted line, y = 3 + 0.5x, and the same r of about 0.82, one of which is a clean straight line, one a smooth curve, and two of which are driven by a single unusual point. The residuals table is there so you can see which kind of data you have.
Outliers matter more than their count suggests, because squaring the distances gives a far point a large say in where the line goes. A point that is extreme in x has the most leverage of all: it can drag the line toward itself and end up with a small residual, so a residuals check alone will not always flag it. Refit without the suspicious point and see how much the slope moves.
Extrapolation is the last trap. The line is fitted to the x range you observed. Predicting far outside it assumes the straight-line relationship continues where you have no evidence that it does, and the uncertainty grows quickly the further you go.
One more subtlety: the regression of y on x is not the same line as the regression of x on y. Least squares minimizes vertical distances, so it treats x as known exactly and puts all the error in y. Swap the variables and you get a different slope unless r is exactly 1 or -1.
Linear Regression Calculator FAQ
How do you calculate the line of best fit?
The slope is the sum of (x - mean x)(y - mean y) divided by the sum of (x - mean x) squared. The intercept is mean y minus the slope times mean x. The resulting line, y = mx + b, minimizes the sum of squared vertical distances from the points and always passes through (mean x, mean y).
What is a good r-squared value?
There is no universal threshold. Physical measurements often give r-squared above 0.95; data about people often gives 0.1 to 0.3 for real effects. A high r-squared does not show that a straight line is the right model, and a low one does not mean the slope is unimportant. Check the residuals and the p-value as well.
What is the difference between r and r-squared?
r is the correlation, from -1 to 1, and its sign gives the direction of the relationship. R-squared is r squared, from 0 to 1, and reads as the share of the variation in y the line accounts for. An r of 0.7 is an r-squared of 0.49, less than half the variation.
What does the p-value for the slope mean?
It is the probability of getting a slope at least this far from zero if there were truly no linear relationship, from a t test on n - 2 degrees of freedom. Below 0.05 is the conventional cut-off for calling the slope statistically significant. It does not measure the size of the effect, prove causation or check that a straight line fits.
Why do my x and y lists need the same number of values?
Each x is paired with the y in the same position, so the lists must be the same length. When they differ this calculator reports both counts and stops, because a missing value early in one list would shift every pair after it and produce a confidently wrong line.