Linear Regression Equation: Formula & How to Use
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 computes the equation of a simple linear regression line (y = a + bx) from your paired data. You enter the x and y values, and it returns the slope, intercept, and the regression equation, ready for making predictions.
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 predictor).
- a is the y-intercept (the value of y when x = 0).
- b is the slope (the change in y for a one-unit change in x).
The slope b is calculated using the least squares method:
b = Σ((xi – x̄)(yi – ȳ)) / Σ((xi – x̄)²)
Or equivalently:
b = (nΣxy – ΣxΣy) / (nΣx² – (Σx)²)
Then the intercept is:
a = ȳ – b·x̄
Here, x̄ and ȳ are the means of x and y, and n is the number of data points.
Worked Example
Using the default data: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5.
- Calculate sums: Σx = 15, Σy = 20, Σxy = (1×2)+(2×4)+(3×5)+(4×4)+(5×5) = 2+8+15+16+25 = 66, Σx² = 1+4+9+16+25 = 55.
- n = 5.
- Slope b = (5×66 – 15×20) / (5×55 – 15²) = (330 – 300) / (275 – 225) = 30/50 = 0.6.
- Means: x̄ = 15/5 = 3, ȳ = 20/5 = 4.
- Intercept a = 4 – 0.6×3 = 4 – 1.8 = 2.2.
The regression equation is y = 2.2 + 0.6x. For x = 6, the predicted y is 2.2 + 3.6 = 5.8.
How Linear Regression Works
Linear regression finds the line that minimizes the sum of the squared vertical distances between the data points and the line. This is called the “least squares” criterion. The slope and intercept are chosen so that the line is as close as possible to all points, balancing errors above and below.
The line always passes through the point (x̄, ȳ). This is a useful check: if your calculated line does not pass through the mean point, you have made an arithmetic error.
Linear regression assumes a straight-line relationship. It does not prove causation; it only describes association. Even a strong correlation does not mean x causes y.
How to Choose the Right Data for Regression
Simple linear regression works best when:
- The relationship between x and y appears roughly linear (check a scatter plot).
- You have at least two distinct x values (otherwise the slope is undefined).
- The data are paired: each x has one corresponding y.
- There are no extreme outliers that could pull the line.
If the relationship is curved, consider polynomial regression or transforming the data. If you have multiple predictors, use multiple linear regression instead.
Common Mistakes and Pitfalls
- Extrapolating beyond the data range: Predictions outside the observed x range are risky because the linear trend may not continue.
- Confusing correlation with causation: A strong regression fit does not imply x causes y.
- Ignoring outliers: A single extreme point can drastically change the slope.
- Using too few points: With only two points, the line is exact but meaningless.
- Rounding too early: Keep full precision during calculations; round only at the end.
Interpreting the Results
The slope b tells you the average change in y for each one-unit increase in x. For example, if x is hours studied and y is test score, a slope of 5 means each additional hour is associated with a 5-point higher score on average. The intercept a is the predicted y when x = 0, which may not be meaningful if x=0 is outside the observed range.
The regression equation is used for prediction: plug in a new x value to get the predicted y. Remember that predictions are averages; actual values will vary around the line.
Limitations of the Formula
The least squares method is sensitive to outliers and assumes homoscedasticity (constant variance of errors) and independence of observations. It also assumes that x is measured without error, which is rarely true in practice. For small samples, the estimates are less stable. The formula gives the best-fitting line for the data you have, but it does not guarantee that the line is useful or accurate for new data.
Frequently Asked Questions
What is the difference between linear regression and correlation?
Correlation (r) measures the strength and direction of a linear relationship, ranging from -1 to 1. Regression gives an equation that predicts y from x. Correlation is symmetric (x vs y is same as y vs x), while regression is not: the line changes if you swap x and y.
How do I know if my regression line is a good fit?
Look at R², the coefficient of determination. It tells you the proportion of variance in y explained by x. An R² close to 1 means the line fits well, but a low R² does not necessarily mean the model is useless—it may just mean there is a lot of unexplained variation.
Can I use linear regression for time series data?
Yes, but be careful. Time series data often have autocorrelation (errors are not independent), which violates regression assumptions. You may need to account for trends or seasonality first.
What does the y-intercept mean if x never equals 0?
The intercept is still the mathematical value of y when x=0, but it may be meaningless in context. For example, predicting weight from height, a height of 0 is impossible, so the intercept has no practical interpretation.
How many data points do I need for a reliable regression?
There is no strict minimum, but at least 10-20 points is often recommended for stable estimates. With fewer points, the slope and intercept have large uncertainty.
What if my data has missing values?
Most regression methods require complete pairs. You can either exclude rows with missing values or use imputation, but be aware that imputation can introduce bias.
Related
Formula used
list x | X values | 1, 2, 3, 4, 5 list y | Y values | 2, 4, 5, 4, 5 out slope | Slope (b) | - | (n*sum(x*y) - sum(x)*sum(y)) / (n*sum(x^2) - (sum(x))^2) | 4 out intercept | Intercept (a) | - | mean(y) - slope * mean(x) | 4 out equation | Regression equation | - | "y = " + intercept + " + " + slope + "x" | 0
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.