Linear Regression Calculator
Paste your x and y values to get the line of best fit, the correlation coefficient, r², residuals and a scatter plot, or switch to an exponential, logarithmic, power or quadratic fit.
How to calculate a linear regression line
The least-squares regression line ŷ = a + bx is the line that makes the sum of the squared vertical distances from the points as small as possible.
- Find the means x̄ and ȳ.
- Work out S_xy = Σ(x − x̄)(y − ȳ) and S_xx = Σ(x − x̄)².
- Slope: b = S_xy ÷ S_xx.
- Intercept: a = ȳ − b × x̄. The line always passes through (x̄, ȳ).
Example: height (in) and weight (lb) for 7 people
| Height (x) | 60 | 62 | 65 | 68 | 70 | 72 | 75 |
|---|---|---|---|---|---|---|---|
| Weight (y) | 115 | 120 | 135 | 150 | 160 | 170 | 185 |
x̄ = 67.43, ȳ = 147.86, S_xy = 841.43 and S_xx = 175.71, so b = 4.789 and a = 147.86 − 4.789 × 67.43 = −175.03.
ŷ = −175.03 + 4.789x. Each extra inch goes with about 4.8 lb more. The predicted weight at 66 in is −175.03 + 4.789 × 66 = 141.0 lb.
How to calculate the correlation coefficient
Pearson’s r measures how close the points are to a straight line:
r = S_xy ÷ √(S_xx × S_yy)
It runs from −1 (perfect downhill line) through 0 (no linear pattern) to +1 (perfect uphill line). For the example, S_yy = 4,042.86, so r = 841.43 ÷ √(175.71 × 4,042.86) = 0.998.
| |r| | Common description | | --- | --- | | 0.8 to 1.0 | Strong | | 0.5 to 0.8 | Moderate | | 0.3 to 0.5 | Weak | | Below 0.3 | Very weak or none |
These cut-offs are rules of thumb and vary by field. Correlation doesn’t show that x causes y.
How to calculate r squared
r² = r × r, the share of the variation in y that the line explains. In the example r² = 0.9966: height accounts for 99.7% of the variation in weight in this small, tidy sample. Equivalently, r² = 1 − SS_residual ÷ SS_total.
The calculator also reports the standard errors of the slope and intercept and a t-test of whether the slope differs from zero (the p-value). For other tests, see the p-value calculator.
How to calculate a residual
Residual = observed y − predicted ŷ. The person who is 60 in and 115 lb has a predicted weight of −175.03 + 4.789 × 60 = 112.3 lb, so the residual is 115 − 112.3 = +2.7 lb: they’re 2.7 lb above the line. Residuals always add up to zero for a least-squares line.
Plot the residuals against x: a random scatter means a line fits; a curve or a funnel shape means it doesn’t, and another model may be better.
Other regression types
| Type | Equation | Fitted by |
|---|---|---|
| Exponential | y = a·e^(bx) | Linear regression of ln y on x (y > 0) |
| Logarithmic | y = a + b·ln x | Linear regression of y on ln x (x > 0) |
| Power | y = a·x^b | Linear regression of ln y on ln x (x, y > 0) |
| Quadratic | y = ax² + bx + c | Least squares on x², x and 1 |
Exponential fits suit growth and decay, such as compound growth or bacteria counts. The R² shown for these is measured on the original y values, so it can differ slightly from a TI-84, which reports r for the transformed data.
Frequently asked questions
Can I use the line to predict outside my data?
Extrapolating beyond the range of x values you have is risky: the relationship may not stay straight. The height example says nothing reliable about a 90-inch adult or a child.
What does the slope mean?
The change in predicted y for each one-unit increase in x, in y’s units.
How do I find the slope of a line through two points?
For just two points, use the slope calculator. Regression is for many points that don’t all lie on one line.