Linear Regression Calculator
Calculate the best-fit line for your data points.
Quick Examples
Linear Regression
Linear regression finds the best-fitting straight line through a set of data points by minimizing the sum of squared residuals.
Regression Equation
Where m is the slope and b is the y-intercept.
Formulas
b = ȳ - m·x̄
Interpreting Results
- Slope (m): Change in Y for each unit increase in X
- R (correlation): Strength and direction of linear relationship (-1 to 1)
- R²: Proportion of variance in Y explained by X (0 to 1)
Correlation Guidelines
| |r| Value | Interpretation |
|---|---|
| 0.90 - 1.00 | Very strong |
| 0.70 - 0.90 | Strong |
| 0.50 - 0.70 | Moderate |
| 0.30 - 0.50 | Weak |
| 0.00 - 0.30 | Very weak/none |
What Is Linear Regression?
Linear regression finds the straight line that best summarizes a scatterplot — the line ŷ = mx + b that sits closest to all the points at once. "Closest" has a precise meaning: the least-squares line is the one that minimizes the total of the squared vertical distances (residuals) between each data point and the line. Squaring punishes large misses more than small ones, which pulls the line toward a fair compromise across the whole dataset.
Step-by-Step Worked Example
Five students report study hours (x) and exam scores (y): (1, 55), (2, 58), (3, 65), (4, 70), (5, 74).
- Step 1 — Means: x̄ = 15/5 = 3; ŵ = 322/5 = 64.4.
- Step 2 — Deviations: Sxx = (−2)² + (−1)² + 0² + 1² + 2² = 10. Sxy = (−2)(−9.4) + (−1)(−6.4) + (0)(0.6) + (1)(5.6) + (2)(9.6) = 18.8 + 6.4 + 0 + 5.6 + 19.2 = 50.
- Step 3 — Slope: m = Sxy/Sxx = 50/10 = 5. Each extra study hour goes with 5 extra score points.
- Step 4 — Intercept: b = ŵ − m·x̄ = 64.4 − 5×3 = 49.4.
- Step 5 — Equation: ŷ = 5x + 49.4. The line always passes through (x̄, ŵ) = (3, 64.4) — a useful check.
- Step 6 — Fit quality: r = Sxy/√(Sxx·Syy) = 50/√(10×253.2) ≈ 0.9937, so R² ≈ 0.987 — the line explains about 98.7% of the variation in scores.
A negative-slope example
Gaming hours vs grades: (1, 92), (2, 88), (3, 83), (4, 79), (5, 73). The same steps give m = −4.7, b = 97.1, so ŷ = −4.7x + 97.1, with r ≈ −0.998. The negative slope means more gaming goes with lower grades; |r| near 1 means the trend is tight.
Making Predictions — and Their Limits
Plug an x into the equation to predict y. For 6 study hours: ŷ = 5×6 + 49.4 = 79.4 — a predicted exam score of about 79.
Residuals
A residual is the gap between an actual y and the line's prediction: residual = y − ŷ. For the first student: ŷ = 5×1 + 49.4 = 54.4, so the residual is 55 − 54.4 = 0.6 — the student scored 0.6 points above the line. Least-squares regression chooses the line that makes the sum of squared residuals as small as possible.
Where Linear Regression Is Used
- Business: forecasting sales from advertising spend.
- Science: calibrating instruments — converting a sensor reading into a temperature.
- Medicine: estimating the effect of dosage on response.
- Sports: predicting performance from training volume.
Common Mistakes
- Confusing slope and correlation: the slope has units (score points per hour); r is unit-free and always between −1 and 1.
- Assuming causation: a tight regression line shows association, not that x causes y.
- Extrapolating blindly: the line is only trustworthy near the observed x values.
- Ignoring curvature: if the scatterplot curves, a straight line will mislead no matter how good R² looks.
- Reading too much into R² from tiny samples: five points can produce a flattering R² by luck.
Quick Answers
What is the difference between interpolation and extrapolation? Interpolation predicts within the observed x range and is usually safe; extrapolation predicts beyond it and should be treated with suspicion.
Does the regression line always pass through (x̄, ŵ)? Yes — for ordinary least-squares regression this is a mathematical guarantee.
What does a slope of zero mean? That changes in x are not associated with changes in y — the best-fit line is flat.
Why Squares? The Least-Squares Idea
Why minimize squared misses instead of plain absolute misses? Squaring penalizes large errors disproportionately — being off by 4 costs 16, not 4 — so the line cannot ignore a badly-missed point to fit the rest perfectly. It also yields a unique line with a clean closed-form formula; the absolute-deviation alternative has no such formula and must be solved iteratively. The trade-off: least squares is sensitive to outliers, which is why checking residuals matters.
Reading R²: A Rough Guide
| R² | Reading |
|---|---|
| 0.90 – 1.00 | Excellent fit; the line captures nearly all variation |
| 0.70 – 0.90 | Good fit; useful for prediction |
| 0.50 – 0.70 | Moderate; the trend is real but noisy |
| Below 0.50 | Weak; the line explains less than half the variation |
These bands are context-dependent: in physics an R² of 0.95 may be routine, while in social science 0.40 can be a publishable finding.
Assumptions Behind the Line
The least-squares line is only trustworthy when its assumptions roughly hold:
- Linearity: the true relationship is approximately straight — check the scatterplot, not just R².
- Independence: each observation stands on its own; time-series data often violates this.
- Constant spread: the scatter around the line should look similar across the x range (fanning-out is a warning sign).
- Roughly normal residuals: for small samples, the misses should not be wildly lopsided.
Correlation vs Regression
Correlation and regression are siblings, not twins. Correlation (r) is symmetric and unit-free: the correlation of x with y equals that of y with x. Regression is directional and carries units: regressing y on x gives a line for predicting y, and swapping the variables gives a different line. Use correlation to describe a relationship; use regression to predict from it.