ThisCalculator

Everyday

Linear Regression Formula Calculator

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 calculator works out the slope and intercept of a simple linear regression line from a set of paired (x, y) values. You enter the x and y data points, and it returns the equation y = a + bx, along with the correlation coefficient and the predicted y for a given x. The calculation uses the ordinary least squares method, which finds the line that minimizes the sum of squared vertical distances between the data points and the line.

Formula

The linear regression equation is written as:

y = a + bx

Where:

  • y is the dependent variable (what you are predicting).
  • x is the independent variable (the input).
  • b is the slope, the change in y for a one-unit change in x.
  • a is the intercept, the value of y when x = 0.

The slope is calculated as:

b = (n Σxy – Σx Σy) / (n Σx² – (Σx)²)

The intercept is:

a = (Σy – b Σx) / n

Where n is the number of data points, Σxy is the sum of the products of each x and y pair, Σx is the sum of x values, Σy is the sum of y values, and Σx² is the sum of squared x values.

Worked Example

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

  1. n = 5
  2. Σx = 1 + 2 + 3 + 4 + 5 = 15
  3. Σy = 2 + 3 + 5 + 4 + 6 = 20
  4. Σxy = (1×2) + (2×3) + (3×5) + (4×4) + (5×6) = 2 + 6 + 15 + 16 + 30 = 69
  5. Σx² = 1 + 4 + 9 + 16 + 25 = 55
  6. Slope b = (5×69 – 15×20) / (5×55 – 15²) = (345 – 300) / (275 – 225) = 45 / 50 = 0.9
  7. Intercept a = (20 – 0.9×15) / 5 = (20 – 13.5) / 5 = 6.5 / 5 = 1.3

So the regression line is y = 1.3 + 0.9x. For x = 6, predicted y = 1.3 + 0.9×6 = 1.3 + 5.4 = 6.7. The calculator shows slope 0.9, intercept 1.3, and predicted y 6.7, matching the manual calculation.

When to Use Linear Regression

Linear regression is appropriate when you suspect a straight-line relationship between two continuous variables. Common examples include:

  • Predicting sales based on advertising spend.
  • Estimating a person’s weight from height.
  • Forecasting temperature from time of day.
  • Analyzing the relationship between study hours and exam scores.

It works best when the data points roughly follow a line, the spread around the line is consistent (homoscedasticity), and there are no extreme outliers. If the relationship is curved, a linear model will be misleading.

How to Read the Results

The slope tells you the direction and strength of the relationship. A positive slope means y increases as x increases; a negative slope means y decreases as x increases. The intercept is the predicted value when x is zero—this may not be meaningful if zero is outside the range of your data.

The correlation coefficient (r) ranges from –1 to 1. Values close to 1 or –1 indicate a strong linear relationship; values near 0 indicate a weak one. However, r only measures linear association, not causation, and it can be sensitive to outliers.

The predicted y for a given x is only reliable within the range of the original x data. Extrapolating far beyond that range is risky because the linear trend may not continue.

Common Mistakes and Pitfalls

  • Mixing up x and y: The independent variable (x) goes on the horizontal axis, and the dependent variable (y) on the vertical. Swapping them changes the equation.
  • Using non-linear data: If the scatter plot shows a curve, a straight line will give poor predictions.
  • Ignoring outliers: A single extreme point can pull the line significantly. Always plot the data first.
  • Rounding too early: Keep full precision during calculations; round only the final answer.
  • Assuming causation: A strong correlation does not prove that x causes y.

Limitations of the Formula

The least squares method assumes that the errors (residuals) are independent, have constant variance, and are normally distributed. In practice, these assumptions are often violated. For example, if the spread of y increases as x increases (heteroscedasticity), the standard errors of the coefficients become unreliable.

The formula also cannot handle missing data—each pair must be complete. It only fits a straight line; if the true relationship is quadratic or exponential, you need polynomial or nonlinear regression. Additionally, the intercept and slope are sensitive to outliers, so robust regression methods may be preferred in those cases.

Frequently Asked Questions

What is the difference between slope and intercept?

The slope (b) tells you how much y changes on average when x increases by one unit. The intercept (a) is the predicted y when x equals zero. For example, in y = 1.3 + 0.9x, the slope 0.9 means each additional unit of x adds 0.9 to y; the intercept 1.3 is the y-value when x is 0.

Can I use linear regression for binary data?

No, linear regression assumes a continuous dependent variable. If your y is binary (0 or 1), use logistic regression instead. Linear regression could produce predictions outside the 0–1 range and the assumptions about errors are violated.

What does R-squared mean?

R-squared is the square of the correlation coefficient (r). It represents the proportion of variance in y that is explained by x. For example, if r = 0.8, then r² = 0.64, meaning 64% of the variability in y is explained by the linear relationship with x. The remaining 36% is due to other factors.

How many data points do I need?

Technically, you need at least two points to draw a line, but that gives a perfect fit with no information about reliability. For meaningful results, aim for at least 10–20 pairs. More points help reduce the influence of random variation and give more stable estimates.

What if my data has multiple x values for the same y?

That is fine. Each pair (x, y) is treated as one observation. The formula uses all pairs equally. If you have repeated x values, the regression line will still be calculated correctly, but you might want to check for non-linearity by plotting the means of y for each x.

How do I handle outliers?

First, check if the outlier is a data entry error. If it is a legitimate value, consider using a robust regression method that downweights outliers, or remove it only if you have a clear justification. The least squares line is very sensitive to outliers, so they can drastically change the slope and intercept.

Related

Formula used
in x | X values (comma separated) | - | 1, 2, 3, 4, 5 | 0
in y | Y values (comma separated) | - | 2, 3, 5, 4, 6 | 0
out slope | Slope (b) | - | (count(x)*sum(x*y)-sum(x)*sum(y))/(count(x)*sum(x^2)-sum(x)^2) | 4
out intercept | Intercept (a) | - | (sum(y)-slope*sum(x))/count(x) | 4
out r | Correlation (r) | - | (count(x)*sum(x*y)-sum(x)*sum(y))/sqrt((count(x)*sum(x^2)-sum(x)^2)*(count(x)*sum(y^2)-sum(y)^2)) | 4
out pred | Predicted y at x = 6 | - | intercept + slope*6 | 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.