Linear Regression Calculator
Paste your paired values and this fits y = a + bx by ordinary least squares, then shows the parts most pages leave out: the fitted value for every row, the residual beside it, and a scatter with each residual drawn as a short line to the fit. The arithmetic is done from centred sums rather than from the raw sums of squares that the usual textbook formula uses, which matters more than it sounds: on four points near x = 100,000,000 that rise by exactly one, the raw-sums formula returns a slope of 0.625, and this page returns 1.000. Pearson r and R-squared are both shown, R-squared as the square of r, and the note under the tool says what that number is and what it is not. A prediction is offered only inside the x range you supplied, since the line is a description of those points and not of what lies past them. Up to 5,000 points, no account, and nothing you paste leaves the browser.
x 1 2 3 4 5 y 2 4 5 4 7 y = 1.400 + 1.000x r = 0.8704 R^2 = 0.7576 n = 5 residuals -0.4 +0.6 +0.6 -1.4 +0.6 predict x = 4.5 -> y = 5.900 (inside the fitted range)
- Slope
- Intercept
- r
- R-squared
5 points, least squares line y = 1.400 + 1.000x, r 0.8704, R-squared 0.7576, residual standard error 1.0328.
A comma, a space or a tab separates the x from the y, so two columns copied from a spreadsheet paste straight in. Bracketed pairs work too, and so do two labelled rows, the x values over the y values. Up to 5,000 points at a time. Nothing you type leaves this browser.
Residuals
Each row is one of your points, the value the fitted line gives at that x, and the gap between them. The residuals of a least squares line with an intercept add up to zero, so this column is worth reading for its pattern rather than for its total.
| x | y | Fitted | Residual |
|---|---|---|---|
| 1 | 2 | 2.4 | -0.4 |
| 2 | 4 | 3.4 | +0.6 |
| 3 | 5 | 4.4 | +0.6 |
| 4 | 4 | 5.4 | -1.4 |
| 5 | 7 | 6.4 | +0.6 |
Three cases, worked
Every line below is computed by the code that answers the calculator above, so an example and the tool cannot disagree.
Common questions
- How do I enter my data?
- One point per row, with a comma, a space or a tab between the x and the y, so two columns copied out of a spreadsheet paste in as they are. Bracketed pairs such as (1,3) (2,5) (3,7) work on a single row, and so does the shape the tool itself prints: a row of x values labelled x, with a row of y values labelled y underneath it. Up to 5,000 points at a time. A row that does not hold exactly two numbers is named by its row number rather than dropped quietly, and because the comma separates x from y, a thousands comma inside a number is reported instead of being read as two values.
- What do the slope and the intercept mean on this page?
- The slope is how much the fitted line moves in y for a step of one in x, and the intercept is where that line would sit at x = 0. On the five points the page opens with, the fit is y = 1.400 + 1.000x: a step of one in x raises the line by 1.000. The intercept is often outside the data, as it is there, which is why the tool prints it as a coefficient and will not predict at an x your rows do not cover.
- What does R-squared tell me, and what does it not?
- It is the square of Pearson r, and on your points it is the share of the variation in y that the fitted line accounts for. That is all it is. It is not a test of whether a straight line is the right shape: ten points sitting exactly on y = x squared, which is a curve, fit a line with R-squared 0.9498, and their residuals run +12, +4, -2, -6, -8, -8, -6, -2, +4, +12, a clear pattern rather than scatter. It is not a p-value, it says nothing about whether the slope would hold on new data, and it is not evidence about which variable moves which. The residual column is where the shape of the data shows itself.
- Why does it refuse when every x is the same?
- The slope of a least squares line is the centred cross product divided by the spread in x. When every x is identical that spread is zero, the points sit on a vertical line, and the division has no denominator: there is no slope to report, not even an infinite one. The tool says so and names the shared value instead of printing a number. If the values you want to predict are the x ones, swap the two columns and fit the other way round.
- Can it predict a y beyond my x range?
- No, by design. Inside the range your points cover, the line is a summary of data you actually have. Outside it, the same line is a guess about a region you measured nothing in, and the arithmetic looks identical either way, which is what makes it easy to quote by mistake. Ask for an x between your smallest and largest and the page answers it; ask for one outside and it says so and names the range. If you need the value further out, extend the data rather than the line.
- Is Pearson r the same as R-squared?
- They carry different information. Pearson r runs from -1 to 1 and keeps the direction: negative r means the line falls as x rises. R-squared is r multiplied by itself, so it runs from 0 to 1 and has lost the sign. On the five points the page opens with, r is 0.8704 and R-squared is 0.7576. A falling line with r of -1 and a rising line with r of 1 both give R-squared 1.
- Why does another calculator give me a slightly different slope?
- Most of them evaluate the textbook formula straight from the raw sums, dividing the difference of two large sums by the difference of two more. That is correct algebra and poor arithmetic: once the values sit far from zero, the squares run past the range where a double holds consecutive integers and the subtractions cancel away the digits that carried the answer. Four points at x near 100,000,000 rising by exactly one give a slope of 0.625 that way. This page subtracts the mean first, adds with a compensated sum and corrects for the rounding in the mean, and returns 1.000 for the same points.
- What is the residual standard error underneath the readout?
- It is the square root of the sum of the squared residuals divided by n minus 2, so it is roughly the typical size of a residual, in the units of y. On the five points the page opens with it is 1.0328. It needs a row to spare, so it is shown from three points upwards and left out at two, where the line passes exactly through both points and nothing is left over.
- What happens if I only give it two points?
- It fits them, and it says what that fit is worth. Two points with different x values always land exactly on one line, so the residuals are zero and R-squared is 1 whatever the two points are. That figure is arithmetic rather than evidence, and the tool prints a line saying so: a third row is the first one that can disagree with the line.
- Does my data leave the browser?
- No. The fit, the residuals, the plot and the copied report are all produced on this page, and nothing is uploaded or sent to analytics. The only copy kept anywhere is the draft this browser remembers, so your points are still in the box when you come back. Start over, above the tool, forgets it.
Ordinary least squares computed from centred sums, which keeps the arithmetic stable when the values sit far from zero. R-squared is the square of Pearson r and says how much of the variation in y the line accounts for, and nothing more: a fit here describes these points, it is not evidence that x causes y, and a prediction is only offered inside the x range you supplied.