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

  1. Independent Variable X Values: Enter at least two numerical values for the predictor variable X (comma, space, or line separated).
  2. Dependent Variable Y Values: Enter the matching numerical values for the response variable Y in the same order.
  3. Optional X Value for Prediction (Ŷ): Enter a specific value of X to 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:

MetricFormulaVerified Output
Regression Slope (b₁)S_xy / S_xx = 25.05 / 17.51.431429
Regression Intercept (b₀)6.016667 − (1.431429 × 3.5)1.006667
Linear Regression Equationŷ = b₁x + b₀ŷ = 1.4314x + 1.0067
Correlation & R-Squaredr and R²r = 0.9993 | R² = 0.9985
Predicted Ŷ at X = 81.431429 × 8 + 1.00666712.458095

Key Assumptions & Common Mistakes to Avoid

  • Linearity & Homoscedasticity: OLS regression assumes the true relationship between X and Y is linear and that residual variance is roughly constant across X.
  • Extrapolating Far Beyond the Data Range: Predicting Ŷ at X = 1,000 from data observed only between X = 1 and X = 6 can 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.

Related calculators