Linear Regression Calculator

Fit a line to predict Y from X.

Predictor variable

Response variable

Optional: Get predicted Y with confidence interval

Load example data:

Enter paired X and Y values for linear regression

Minimum 3 data points required

What Is Simple Linear Regression?

Simple linear regression models the relationship between two variables by fitting a straight line to the data. It allows you to predict Y (the outcome) from X (the predictor).

Question answered: "What's the best-fitting line, and how well does X predict Y?"

Examples:

  • "How do sales change with advertising spending?"
  • "Predict exam score from study hours."
  • "What's the relationship between temperature and ice cream sales?"

The Regression Equation

Y=a+bXY = a + bX

Or in alternative notation:

Y^=Ξ²0+Ξ²1X\hat{Y} = \beta_0 + \beta_1 X

Where:

  • aa (or Ξ²0\beta_0) = Y-intercept (value of Y when X = 0)
  • bb (or Ξ²1\beta_1) = slope (change in Y for one-unit increase in X)
  • Y^\hat{Y} = predicted Y value

Calculating the Coefficients

Slope:

b=βˆ‘i=1n(xiβˆ’xΛ‰)(yiβˆ’yΛ‰)βˆ‘i=1n(xiβˆ’xΛ‰)2b = \frac{\sum_{i=1}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n}(x_i - \bar{x})^2}

Or: b=Sxy/Sxx=rΓ—(sy/sx)b = S_{xy} / S_{xx} = r \times (s_y / s_x)

Intercept:

a=yΛ‰βˆ’bΓ—xΛ‰a = \bar{y} - b \times \bar{x}

The line always passes through the point (xˉ,yˉ)(\bar{x}, \bar{y}).

Example Calculation

Data: Advertising spend (X, $1000s) and sales (Y, units)

XY
2180
4220
6290
8350
10400

Summary statistics:

  • xΜ„ = 6, Θ³ = 288
  • Sxx = 40, Sxy = 1100

Slope: b = 1100 / 40 = 27.5

Intercept: a = 288 - 27.5 Γ— 6 = 123

Equation: ΕΆ = 123 + 27.5X

Interpretation: Each $1,000 increase in advertising is associated with 27.5 additional unit sales.

Interpreting the Slope

b = 27.5 means:

  • For every 1-unit increase in X, Y increases by 27.5 on average
  • The relationship is positive (Y increases as X increases)
  • This is an average effectβ€”individual observations vary

Units matter: If X is in thousands of dollars and Y is in units sold, then: "$1,000 more advertising β†’ 27.5 more units sold"

Interpreting the Intercept

a = 123 means:

  • When X = 0, the predicted Y is 123
  • Zero advertising predicts 123 unit sales

Caution: Interpret literally only if X = 0 is meaningful and within your data range.

If your data spans X = 2 to 10, the intercept is extrapolation and may not be meaningful.

Residuals and Fit

Residual = Observed - Predicted

ei=yiβˆ’y^ie_i = y_i - \hat{y}_i

XYΕΆResidual
2180178+2
4220233-13
6290288+2
8350343+7
10400398+2

Residuals should be small and randomly scattered.

R-Squared (Coefficient of Determination)

R2=proportion of variance in Y explained by XR^2 = \text{proportion of variance in Y explained by X}

R2=1βˆ’SSresidualSStotalR^2 = 1 - \frac{SS_{residual}}{SS_{total}}

Or: R2=r2R^2 = r^2 (squared correlation)

For our example: r = 0.995, so RΒ² = 0.99

Interpretation: 99% of variation in sales is explained by advertising spend.

Standard Error of the Estimate

SE=βˆ‘i=1n(yiβˆ’y^i)2nβˆ’2SE = \sqrt{\frac{\sum_{i=1}^{n}(y_i - \hat{y}_i)^2}{n-2}}

Measures typical prediction error.

For our example: SE β‰ˆ 7.9 units

Predictions are typically off by about 8 units.

Testing the Slope

Is the slope significantly different from zero?

Hβ‚€: β₁ = 0 (no relationship) H₁: β₁ β‰  0 (relationship exists)

Test statistic:

t=bSEbt = \frac{b}{SE_b}

Where SEb=SE/SxxSE_b = SE / \sqrt{S_{xx}}

If p < Ξ±, the slope is statistically significant.

Confidence Interval for Slope

CI=bΒ±tβˆ—Γ—SEbCI = b \pm t^* \times SE_b

Gives range of plausible population slopes.

Prediction

Point Prediction

For X = 7: ΕΆ = 123 + 27.5(7) = 315.5 units

Prediction Interval

Accounts for uncertainty in both the line and individual variation:

PI=Y^Β±tβˆ—Γ—SEΓ—1+1n+(Xβˆ’xΛ‰)2SxxPI = \hat{Y} \pm t^* \times SE \times \sqrt{1 + \frac{1}{n} + \frac{(X - \bar{x})^2}{S_{xx}}}

Prediction intervals are always wider than confidence intervals.

Assumptions (LINE)

Linearity

The relationship between X and Y is linear.

Check: Scatterplot, residual plot

Independence

Observations are independent.

Violation: Time series data, clustered data

Normality

Residuals are normally distributed.

Check: Histogram of residuals, QQ plot

Equal Variance (Homoscedasticity)

Residual spread is constant across X values.

Check: Residual plot (should show random scatter)

Residual Diagnostics

Good residual plot:

  • Random scatter around zero
  • No patterns or trends
  • Constant spread

Problems to detect:

  • Curvature β†’ non-linear relationship
  • Funnel shape β†’ heteroscedasticity
  • Clusters β†’ potential subgroups

Common Issues

Extrapolation

Predicting outside the range of observed X values.

If data spans X = 2 to 10, predicting at X = 20 is risky.

Outliers

Unusual points can heavily influence the line.

High leverage: Extreme X values High influence: Points that change the line substantially

Confounding

Third variables may explain the relationship.

Regression doesn't prove causation.

Applications

Business

Sales forecasting, demand modeling, pricing analysis.

Science

Calibration curves, dose-response relationships.

Economics

Consumption functions, price elasticity.

Medicine

Growth charts, treatment response modeling.

Engineering

Quality control, process optimization.

Regression vs. Correlation

AspectCorrelationRegression
PurposeMeasure relationshipPredict/explain
DirectionSymmetricX predicts Y
Outputr (single number)Equation (a, b)
InterpretationStrengthRate of change

Connection: rΒ² = RΒ² in simple regression

Reporting Results

Standard format:

"Simple linear regression revealed that advertising spending significantly predicted sales, b = 27.5, SE = 1.38, t(3) = 19.9, p < .001. The model explained 99% of the variance in sales (RΒ² = .99)."

With equation:

"The regression equation was: Sales = 123 + 27.5 Γ— Advertising, F(1, 3) = 397, p < .001, RΒ² = .99."

Historical Context

Francis Galton pioneered regression in the 1880s studying heredityβ€”specifically, why children of tall parents "regressed" toward average height.

Karl Pearson formalized the mathematics, connecting regression to correlation.

The term "regression" persists even though modern applications rarely involve "regression toward the mean" in Galton's sense.

Related Calculations

Summary

Simple linear regression fits the line Y = a + bX to model how X predicts Y. The slope (b) represents the change in Y per unit change in X; the intercept (a) is the predicted Y when X = 0. RΒ² indicates the proportion of variance explained. Test the slope for significance and check assumptions using residual plots. Report the equation, RΒ², and significance test. Remember that regression shows association, not causation, and avoid extrapolating beyond your data range.