Linear regression calculator
Paste your x and y values, get the line of best fit.
What does linear regression do?
It finds the straight line that best fits your points — the one with the smallest sum of squared vertical distances (least squares). For x = 1–5 and y = 2, 4, 5, 4, 5 that line is ŷ = 2.2 + 0.6x.
How is the slope calculated?
Slope = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², and the intercept = ȳ − slope × x̄. In the example the sums are 6 and 10, so the slope is 0.6 and the intercept 4 − 0.6 × 3 = 2.2.
What is R²?
The share of the variation in y that the line explains: 0.6 means 60%. R² = 1 is a perfect fit; 0 means the line explains nothing.
How do I make a prediction?
Put an x into the equation: at x = 6, ŷ = 2.2 + 0.6 × 6 = 5.8. Predictions far outside your data range are much less reliable.
Does a strong fit prove cause and effect?
No — regression shows how two things move together, not that one causes the other.
What is the difference between r and R²?
r is the correlation coefficient, from −1 to +1; its sign matches the slope, and its size shows how tightly the points follow a line. R² is r squared, the share of the variation in y that the line accounts for. For a simple one-variable fit like this, R² is always just r × r.
How many points do I need?
Two points always give a perfect line, which tells you nothing about the scatter, so the standard error needs at least three. More points make the slope steadier. All x values the same is not allowed, because the slope would divide by zero.
Does it matter which variable is x?
Yes. The line minimizes errors in y, so swapping the columns gives a different line, not the same one rearranged. Put the variable you want to predict in y.
What the fit assumes
Ordinary least squares chooses the intercept a and slope b that make the sum of squared vertical errors smallest. The line always passes through the point (x̄, ȳ), the means of the two columns. The model assumes the relationship is close to a straight line and that the scatter around it is roughly even along the line. The standard error shown is s = √(SSE ÷ (n − 2)), the typical size of a miss in y units. A curved pattern, one wild point or a spread that widens with x all break these assumptions, and r and R² can still look respectable. Plot the data before trusting any single number.
Worked example: hours studied and score
Take hours 1 to 6 and scores 52, 58, 61, 67, 70, 78. The means are x̄ = 3.5 and ȳ = 64.33. The fit is ŷ = 47.13 + 4.91x: each extra hour goes with about 4.9 more points. Correlation is r = 0.992, so R² = 0.984 and the line accounts for about 98.4% of the variation. The standard error is 1.29 points. At 3.5 hours, inside the data, the line predicts 64.3. At 20 hours it predicts 145, impossible on a 100-point test. That is the danger of extrapolation: the line describes the range you measured and says nothing beyond it.
Mistakes to avoid
Swapping the columns changes the answer: regressing hours on score gives a slope of 0.200, not 1 ÷ 4.914. The two slopes multiply to R² (0.984), not to 1. Do not read a high R² as proof of cause, and do not drop points only because they spoil the fit: Anscombe's four data sets share nearly the same line and r while looking nothing alike when plotted. Finally, keep units straight; the slope is in y units per one x unit.
Sources: OpenStax, Introductory Statistics 2e, section 12.3 "The Regression Equation" (least squares, the line through the means, extrapolation, residuals). Related tools: correlation coefficient calculator, Spearman correlation calculator, standard deviation calculator.