Linear Regression Calculator

Enter equal numbers of X and Y values to calculate the best fit line.


Add this Calculator to Your Site

What is Linear Regression?

Linear regression is a statistical method used to model the relationship between a dependent variable (Y) and one independent variable (X) by fitting a linear equation to observed data. The goal is to find the line that best fits the data points.

The Regression Equation

y = mx + b

Where:
m = slope (rate of change)
b = y-intercept (value of y when x = 0)

Least Squares Formulas

The slope and y-intercept are calculated using the least squares method:

Slope (m) = (n * sum(xy) - sum(x) * sum(y)) / (n * sum(x^2) - (sum(x))^2)

Y-intercept (b) = (sum(y) - m * sum(x)) / n

Or equivalently:
b = y_mean - m * x_mean

How to Calculate Linear Regression

  1. Calculate sums: Find sum(x), sum(y), sum(xy), sum(x^2)
  2. Calculate slope (m): Use the slope formula
  3. Calculate y-intercept (b): Use the intercept formula
  4. Write equation: y = mx + b
  5. Make predictions: Substitute x values to predict y

Example Calculation

For data points: (1, 2), (2, 4), (3, 5), (4, 4), (5, 5)

n = 5
sum(x) = 15, sum(y) = 20
sum(xy) = 66, sum(x^2) = 55
x_mean = 3, y_mean = 4

Slope (m) = (5*66 - 15*20) / (5*55 - 225)
         = (330 - 300) / (275 - 225)
         = 30 / 50 = 0.6

Y-intercept (b) = 4 - 0.6*3 = 4 - 1.8 = 2.2

Equation: y = 0.6x + 2.2

Prediction: When x = 6
y = 0.6(6) + 2.2 = 3.6 + 2.2 = 5.8

Interpreting the Results

Slope (m)

  • Positive slope: Y increases as X increases
  • Negative slope: Y decreases as X increases
  • Slope = 0: No relationship between X and Y
  • The slope indicates how much Y changes for each unit increase in X

Y-Intercept (b)

  • The value of Y when X equals zero
  • May or may not have practical meaning depending on context

R-Squared (Coefficient of Determination)

R-squared measures how well the regression line fits the data. Values range from 0 to 1, where 1 indicates a perfect fit.

Applications of Linear Regression

Business

Sales forecasting, demand prediction, cost analysis.

Science

Analyzing experimental data, predicting outcomes.

Economics

Modeling economic relationships, trend analysis.

Engineering

Calibration curves, performance prediction.

Other Calculators