How the Linear Regression Calculator Works
Simple Linear Regression fits a straight line of best fit (ŷ = b₁x + b₀) through paired observations of an independent variable X and a dependent variable Y using the method of Ordinary Least Squares (OLS). This method minimizes the sum of squared vertical residuals ∑(yᵢ − ŷᵢ)². Our Linear Regression Calculator computes the slope (b₁), y-intercept (b₀), full regression equation, Pearson correlation (r), coefficient of determination (R²), and predicted output Ŷ for any target X value.
How to Use This Calculator
- Independent Variable X Values: Enter at least two numerical values for the predictor variable
X(comma, space, or line separated). - Dependent Variable Y Values: Enter the matching numerical values for the response variable
Yin the same order. - Optional X Value for Prediction (Ŷ): Enter a specific value of
Xto evaluate the fitted regression equation at that point.
Ordinary Least Squares (OLS) Regression Formulas
Mean X̄ = (∑ xᵢ) / n , Mean Ȳ = (∑ yᵢ) / n
S_xx = ∑ (xᵢ − X̄)² , S_yy = ∑ (yᵢ − Ȳ)² , S_xy = ∑ (xᵢ − X̄)(yᵢ − Ȳ)
Regression Slope (b₁) = S_xy / S_xx
Regression Intercept (b₀) = Ȳ − (b₁ × X̄)
Regression Line Equation : ŷ = b₁x + b₀
Predicted Value at X = x* : ŷ* = (b₁ × x*) + b₀
Verified Calculation Example
Fit a linear regression model to X = [1, 2, 3, 4, 5, 6] and Y = [2.5, 3.8, 5.2, 6.9, 8.1, 9.6] and predict Ŷ at X = 8:
| Metric | Formula | Verified Output |
|---|---|---|
| Regression Slope (b₁) | S_xy / S_xx = 25.05 / 17.5 | 1.431429 |
| Regression Intercept (b₀) | 6.016667 − (1.431429 × 3.5) | 1.006667 |
| Linear Regression Equation | ŷ = b₁x + b₀ | ŷ = 1.4314x + 1.0067 |
| Correlation & R-Squared | r and R² | r = 0.9993 | R² = 0.9985 |
| Predicted Ŷ at X = 8 | 1.431429 × 8 + 1.006667 | 12.458095 |
Key Assumptions & Common Mistakes to Avoid
- Linearity & Homoscedasticity: OLS regression assumes the true relationship between
XandYis linear and that residual variance is roughly constant acrossX. - Extrapolating Far Beyond the Data Range: Predicting
ŶatX = 1,000from data observed only betweenX = 1andX = 6can be unreliable if the linear trend does not hold outside the observed domain.
Frequently Asked Questions
Why is regression undefined when all X values are identical?
If all X values are the same, S_xx equals zero, meaning the points form a vertical line with an infinite/undefined slope.
How is the regression slope related to the correlation coefficient?
The OLS slope b₁ equals r × (s_y / s_x), where r is the Pearson correlation coefficient and s_y and s_x are the sample standard deviations of Y and X.