ThisCalculator

Everyday

Linear Regression Equation: Step-by-Step Guide

Updated · Standard formula, shown below

The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.

This guide explains how to calculate a linear regression equation from a set of paired data points. You’ll learn the standard least-squares method to find the line of best fit, expressed as y = a + bx. You’ll need a basic understanding of mean, variance, and covariance, and a calculator or spreadsheet for the arithmetic.

Formula

The linear regression equation is y = a + b·x, where:

  • y is the dependent variable (what you predict)
  • x is the independent variable (the input)
  • a is the y-intercept (the predicted y when x = 0)
  • b is the slope (the change in y per one-unit change in x)

Using the least-squares method, the slope b and intercept a are calculated as:

b = [n·Σ(x·y) − Σx·Σy] / [n·Σ(x²) − (Σx)²]

a = (Σy − b·Σx) / n

Where:

  • n = number of data points
  • Σx = sum of all x values
  • Σy = sum of all y values
  • Σ(x·y) = sum of the product of each x and y pair
  • Σ(x²) = sum of each x squared

Worked Example

Using the default data: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5.

  1. Count points: n = 5.
  2. Compute sums:
    • Σx = 1+2+3+4+5 = 15
    • Σy = 2+4+5+4+5 = 20
    • Σ(x·y) = (1·2)+(2·4)+(3·5)+(4·4)+(5·5) = 2+8+15+16+25 = 66
    • Σ(x²) = 1+4+9+16+25 = 55
  3. Calculate slope b: (5·66 − 15·20) / (5·55 − 15²) = (330 − 300) / (275 − 225) = 30 / 50 = 0.6
  4. Calculate intercept a: (20 − 0.6·15) / 5 = (20 − 9) / 5 = 11 / 5 = 2.2
  5. Equation: y = 2.2 + 0.6x

For x = 3, predicted y = 2.2 + 0.6·3 = 4.0. The calculator gives slope 0.6 and intercept 2.2.

When to Use This

Use linear regression when you have paired numerical data and want to model the relationship between a predictor (x) and an outcome (y). It is appropriate when the scatter plot shows a roughly straight-line pattern. The resulting equation lets you predict y for new x values and quantify the strength of the relationship via R-squared.

How to Read the Result

The slope b tells you the average change in y for each one-unit increase in x. The intercept a is the predicted y when x = 0 (which may not be meaningful if x never equals 0 in your data). R-squared (R²) ranges from 0 to 1; higher values mean the line explains more of the variability in y. For example, R² = 0.8 means 80% of the variation in y is explained by x.

Common Mistakes

  • Mixing up x and y: The predictor (x) goes on the horizontal axis; the outcome (y) on the vertical. Swapping them changes the equation.
  • Using the wrong sums: Forgetting to square each x individually before summing, or using (Σx)² instead of Σ(x²) in the denominator.
  • Rounding too early: Rounding intermediate sums to a few decimals can change the final slope and intercept noticeably.
  • Ignoring outliers: A single extreme point can pull the line away from the majority of data.
  • Assuming causation: A strong correlation does not mean x causes y.

Tips

  • Always plot your data first to see if a straight line is reasonable.
  • Use a spreadsheet or calculator to avoid arithmetic errors.
  • Keep at least 4-6 significant digits during intermediate steps.
  • Check the residuals (differences between actual and predicted y) to see if they look random; patterns suggest a non-linear model.
  • If your x values are large, consider centering them (subtract the mean) to make the intercept more interpretable.

Troubleshooting

If your slope is wildly different from expected: Check that your x and y lists are aligned correctly (same length, paired correctly). Verify you used Σ(x·y) and Σ(x²) correctly.

If the denominator is zero: This happens when all x values are identical. Regression requires variation in x; collect more diverse data or reconsider the model.

If R-squared is negative: This can occur if you use the formula with a constant-only model or if your data doesn’t fit a line; the calculator may output a negative due to rounding. In practice, R² should be between 0 and 1.

If your predicted values are far off: The linear model may not be appropriate. Try transforming the data (e.g., log or square root) or use a non-linear model.

Frequently Asked Questions

What is the difference between correlation and regression? Correlation measures the strength and direction of a linear relationship (r), while regression gives an equation to predict y from x.

Can I use linear regression for non-linear data? No, it only models straight-line relationships. For curved patterns, consider polynomial or other non-linear models.

What does the intercept mean if x never equals 0? It is still the y-value where the line crosses the y-axis, but it may not have a practical interpretation. It’s often used only to complete the equation.

How many data points do I need? At least two are needed to draw a line, but for reliable results, aim for 10-20 or more, depending on the variability.

What if my data has multiple x values for the same y? That’s fine; each pair is a point. Regression handles repeated x values.

Why do different calculators give slightly different results? Differences usually come from rounding during intermediate steps or from using a different formula (e.g., population vs. sample variance). The least-squares method is standard, but rounding can alter the last digits.

Related

Formula used
in n | Number of points | - | 5 | 2
in x | X values (comma-separated) | - | 1,2,3,4,5 | 0
in y | Y values (comma-separated) | - | 2,4,5,4,5 | 0
out slope | Slope (b) | - | (n*sum(x*y)-sum(x)*sum(y))/(n*sum(x^2)-sum(x)^2) | 4
out intercept | Intercept (a) | - | (sum(y)-slope*sum(x))/n | 4
out r2 | R-squared | - | (n*sum(x*y)-sum(x)*sum(y))^2/((n*sum(x^2)-sum(x)^2)*(n*sum(y^2)-sum(y)^2)) | 4

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.