Math & Geometry
Linear Regression Calculator (Least Squares, r and R²)
Fit a straight line to a set of data points and measure how well it describes them. Simple linear regression finds the line y = a + bx that minimises the sum of the squared vertical distances between the points and the line, a method known as ordinary least squares that goes back to Legendre and Gauss in the early nineteenth century. The slope b tells you how much y changes, on average, for each one-unit increase in x, and the intercept a is the predicted value of y when x is zero. The Pearson correlation coefficient r, between −1 and 1, measures the strength and direction of the linear relationship, and R², its square, gives the proportion of the variation in y that the line explains. Enter your data as x and y pairs, one pair per line, and the calculator gives the regression equation, the slope and intercept, r, R², the standard error of the estimate, a prediction for any value of x, and a table of fitted values and residuals that helps you spot points the line does not describe well.
How to use this calculator
- Enter one pair of values per line, x first, then y, separated by a space or comma.
- Enter at least three pairs.
- Optionally enter an x value to predict y.
- Read the equation, slope, intercept, r and R².
- Check the residuals for patterns or unusual points.
Using regression wisely
Regression describes association, not causation: a strong correlation between two variables can arise because both depend on a third factor. Plotting the data first is always worthwhile, since very different patterns can produce the same r, as Anscombe’s quartet famously shows.
Outliers can pull the line strongly, and a curved relationship can give a low r even when the variables are closely related. Looking at the residuals, which should scatter randomly around zero, helps check whether a straight line is appropriate.
Common uses
Linear regression is used to estimate trends over time, calibrate instruments, relate costs to production volume, forecast sales and in countless scientific analyses.
For several explanatory variables, multiple regression extends the same least-squares idea; statistical software also provides confidence intervals and significance tests for the slope.
Formula & method
Slope b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²Intercept a = ȳ − b·x̄r = Σ(x − x̄)(y − ȳ) ÷ √(Σ(x − x̄)² · Σ(y − ȳ)²); R² = r²Standard error of the estimate = √(Σ(y − ŷ)² ÷ (n − 2))With n points (x, y), x̄ and ȳ their means: Sxx = Σ(x − x̄)², Syy = Σ(y − ȳ)², Sxy = Σ(x − x̄)(y − ȳ). Slope b = Sxy ÷ Sxx. Intercept a = ȳ − b·x̄. Pearson correlation r = Sxy ÷ √(Sxx · Syy). Coefficient of determination R² = r² for simple linear regression. Fitted value ŷ = a + b·x; residual = y − ŷ. Standard error of the estimate = √(Σ(y − ŷ)² ÷ (n − 2)). Prediction at x0 = a + b·x0. Predictions outside the range of the data (extrapolation) are less reliable. The usual rule of thumb describes |r| of 0.7 or more as strong, 0.4 to 0.7 as moderate and 0.2 to 0.4 as weak, but interpretation depends on the field.
Worked example
Points (1, 2), (2, 4), (3, 6)
- x̄ = 2, ȳ = 4; Sxx = 2, Sxy = 4.
- Slope b = 4 ÷ 2 = 2; intercept a = 4 − 2 × 2 = 0.
- r = 1 and R² = 1: the points lie exactly on y = 2x.
Answer: y = 0 + 2x; at x = 4 the predicted y is 8.
Frequently asked questions
What is the line of best fit?
The line that minimises the sum of squared differences between the observed and predicted y values.
What does the slope mean?
The average change in y for each one-unit increase in x.
What is a good R²?
It depends on the field; closer to 1 means the line explains more of the variation.
Can r be negative?
Yes. A negative r means y tends to decrease as x increases.
How many points do I need?
At least three, but more give a more reliable estimate.
Does correlation prove causation?
No. It shows association only.