📉 Linear Regression Calculator
Paste x, y data — get the line of best fit, r and r², a slope test, predictions and residuals, with a scatterplot.
Regression formulas
- Sxx = Σ(x − x̄)², Syy = Σ(y − ȳ)², Sxy = Σ(x − x̄)(y − ȳ)
- Slope b₁ = Sxy ÷ Sxx · intercept b₀ = ȳ − b₁x̄ · line ŷ = b₀ + b₁x
- Correlation r = Sxy ÷ √(Sxx · Syy) · coefficient of determination r²
- Slope test: s = √(SSE ÷ (n − 2)), SE(b₁) = s ÷ √Sxx, t = b₁ ÷ SE(b₁), df = n − 2
Worked example
Hours studied (x) and exam score (y) for 7 students: (2, 65), (3, 68), (4, 70), (5, 74), (6, 75), (7, 81), (8, 85).
| Quantity | Work | Value |
|---|---|---|
| Means | x̄ = 35 ÷ 7, ȳ = 518 ÷ 7 | 5 and 74 |
| Sxx, Sxy, Syy | sums of products of deviations | 28, 91, 304 |
| Slope | 91 ÷ 28 | 3.25 points per hour |
| Intercept | 74 − 3.25 × 5 | 57.75 |
| r and r² | 91 ÷ √(28 × 304) | r = 0.986, r² = 0.973 |
| Prediction at 6 hours | 57.75 + 3.25 × 6 | 77.25 |
The slope’s standard error is 0.243, so t = 13.39 with 5 df and p < 0.0001. Press “Try an example” above to see the scatterplot. Concepts: Correlation and regression.
Commands: Excel / Sheets =SLOPE(y, x), =INTERCEPT(y, x), =CORREL(x, y), =RSQ(y, x), =LINEST(y, x, TRUE, TRUE); TI-84 STAT → CALC → 8:LinReg(a+bx) (turn on DiagnosticOn for r).
Questions and answers
How is the line of best fit calculated?
Least squares: the slope is b₁ = Sxy ÷ Sxx = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², and the intercept is b₀ = ȳ − b₁x̄. This line makes the sum of squared residuals as small as possible, and it always passes through the point (x̄, ȳ).
What is the difference between r and r²?
r (from −1 to 1) is the correlation: its sign gives the direction and its size the strength of the linear relationship. r² (from 0 to 1) is the share of the variation in y explained by the line — r = 0.8 means r² = 0.64, so 64% is explained.
What is a residual?
Observed y minus predicted ŷ. Positive residuals are above the line, negative ones below. A residual plot with no pattern means a straight line fits; a curve in the residuals means it does not.
Is the slope significant?
The calculator tests H₀: slope = 0 with t = b₁ ÷ SE(b₁) and df = n − 2. A small p-value (below 0.05) means the data give evidence of a linear relationship. It still does not show cause and effect.
Can I predict for any x?
Only inside the range of your x data. Predicting outside it (extrapolation) assumes the straight-line pattern continues, which is often false — the calculator warns you when you do this.
Does correlation mean causation?
No. Two variables can move together because of a third variable (a lurking or confounding variable) or by chance. Only a randomized experiment can show cause. See the lesson on correlation and regression.