Linear Regression Calculator
Find the line of best fit y = mx + b for any paired dataset using least squares. Calculates slope, intercept, R², and lets you predict Y from X.
Results
Linear Regression (Least Squares)
Linear regression finds the line y = mx + b that minimizes the sum of squared residuals — the squared vertical distances between each observed y and the predicted ŷ. This is called the ordinary least squares (OLS) method.
Formulas
Slope: m = Σ(xᵢ−x̄)(yᵢ−ȳ) / Σ(xᵢ−x̄)²
Intercept: b = ȳ − m × x̄
R² = 1 − SSres/SStot, where SSres = Σ(yᵢ−ŷᵢ)² and SStot = Σ(yᵢ−ȳ)²
Assumptions
For valid inference, linear regression assumes: linearity (the true relationship is linear), independence of residuals, homoscedasticity (constant variance of residuals), and normality of residuals. Always plot your data and residuals to check these.
Worked Example
Study hours (X) vs. exam scores (Y): (1,52), (2,58), (3,64), (4,70), (5,76). Calculate the means: x̄=3, ȳ=64. Compute slope: m = Σ(xᵢ−x̄)(yᵢ−ȳ) / Σ(xᵢ−x̄)² = 40/10 = 6. Intercept: b = 64 − 6×3 = 46. The regression equation is ŷ = 6x + 46.
Prediction: for a student studying 6 hours, predicted score = 6(6)+46 = 82. R² = 1.0 in this perfect example, meaning study hours explains 100% of the score variation.
Interpreting R² Values
| R² Value | Interpretation | Common In |
|---|---|---|
| 0.90 – 1.00 | Excellent fit | Controlled lab experiments |
| 0.70 – 0.89 | Good fit | Physical measurements |
| 0.50 – 0.69 | Moderate fit | Social science studies |
| 0.20 – 0.49 | Weak fit | Behavioral research |
| 0.00 – 0.19 | Very weak | Complex human behavior |
R² values must be interpreted in context. In medicine or social sciences, R²=0.40 may be quite useful, while in engineering R²=0.95 might still be considered insufficient for a critical application.
Frequently Asked Questions
The line of best fit minimizes the sum of squared vertical distances (residuals) between each data point and the line. This OLS approach guarantees the unique line that best represents the data linearly.
A negative slope (m < 0) means Y decreases as X increases — a negative linear relationship. For example, as temperature increases, heating bill decreases.
R² is the proportion of variance in Y explained by the linear model. R²=0.85 means 85% of Y's variability is captured by the regression line; the remaining 15% is unexplained by X alone.
Plug the new X value into ŷ = mx + b. If m=2, b=1, predict for x=5: ŷ = 2(5)+1 = 11. Predictions are most reliable within the range of your original data (interpolation). Extrapolating beyond the data range can be unreliable.
R (the correlation coefficient) measures the strength and direction of the linear relationship between X and Y, ranging from −1 to +1. R² is simply R squared, representing the proportion of variance explained. A correlation of R=0.8 gives R²=0.64, meaning 64% of Y's variance is explained by X. R can be negative (indicating a downward slope), while R² is always between 0 and 1.
Yes — that becomes multiple linear regression: ŷ = b₀ + b₁x₁ + b₂x₂ + ... Each coefficient shows the effect of one predictor while holding others constant. However, multiple regression requires more data and the interpretation becomes more complex. Adding too many predictors relative to data points leads to overfitting.
A residual is the difference between an observed Y value and the predicted ŷ value: e = y − ŷ. Analyzing residuals is critical for checking regression assumptions. If residuals show a pattern (curved, fan-shaped), the linear model may be misspecified. Ideally, residuals should be randomly scattered around zero with no systematic pattern.