Linear Regression Equation Calculator & Formula
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 linear regression calculator works out the equation of the line of best fit (y = a + bx) from your paired x and y data, and shows the slope, intercept, and correlation coefficient. You enter your data as two lists of numbers, and the calculator returns the regression coefficients plus R² so you can see how well the line fits. The page below explains the formulas, gives a worked example, and covers common mistakes and limitations.
Formula
The linear regression equation is written as:
y = a + b·x
where:
- y is the dependent variable (the value you are trying to predict).
- x is the independent variable (the input or predictor).
- b is the slope – the change in y for a one-unit change in x.
- a is the y-intercept – the predicted y when x = 0.
The slope is calculated with the least squares method:
b = [ n·Σ(xy) – Σx·Σy ] / [ n·Σ(x²) – (Σx)² ]
and the intercept is:
a = (Σy – b·Σx) / n
where n is the number of data points, Σ means “sum of”, and xy is the product of each x and y pair. The correlation coefficient r (and its square, R²) measures the strength and direction of the linear relationship. R² is the proportion of variance in y explained by x.
Worked Example
Take the default data: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5. Here n = 5.
- Compute the sums: Σx = 15, Σy = 20, Σxy = (1×2 + 2×4 + 3×5 + 4×4 + 5×5) = 2 + 8 + 15 + 16 + 25 = 66.
- Compute Σx² = 1 + 4 + 9 + 16 + 25 = 55, and Σy² = 4 + 16 + 25 + 16 + 25 = 86.
- Slope b = (5×66 – 15×20) / (5×55 – 15²) = (330 – 300) / (275 – 225) = 30 / 50 = 0.6.
- Intercept a = (20 – 0.6×15) / 5 = (20 – 9) / 5 = 11 / 5 = 2.2.
- So the regression equation is y = 2.2 + 0.6x.
- R² = [ (5×66 – 15×20) / sqrt((5×55 – 225)(5×86 – 400)) ]² = [30 / sqrt(50×30)]² = (30 / sqrt(1500))² = (30/38.7298)² ≈ 0.6² = 0.36.
The calculator gives slope = 0.6, intercept = 2.2, and R² = 0.36, matching the hand calculation.
When to Use Linear Regression
Use linear regression when you have two continuous variables and you want to:
- Predict a value of y from a known x (e.g., predicting exam score from study hours).
- Quantify the strength of a linear relationship (via R²).
- Identify the trend or slope between two variables (e.g., sales vs. advertising spend).
It is appropriate only when the relationship is roughly linear – that is, the data points scatter around a straight line. If the scatter plot shows a curve, linear regression will give a misleading line. Also, the data should be independent and the residuals (the differences between observed and predicted y) should be roughly normally distributed for reliable inference, though the equation itself can still be computed.
How to Read the Results
The slope (b) tells you the direction and steepness: a positive slope means y increases as x increases; a negative slope means the opposite. The intercept (a) is the predicted y when x = 0, but only interpret it if x=0 is within the range of your data – otherwise it is just a mathematical artifact. R² ranges from 0 to 1: a value of 0.36 means 36% of the variation in y is explained by x. The remaining 64% is due to other factors or random variation.
Always check the scatter plot, not just R². A high R² can still hide a curved pattern, and a low R² does not mean the relationship is absent – it may be non-linear.
Common Mistakes and Limitations
- Unit mix-ups: The slope and intercept depend on the units of x and y. If you change units (e.g., from meters to centimeters), the coefficients change, but the underlying relationship does not.
- Rounding too early: Keep full precision during calculations, then round the final answer. Rounding intermediate sums can produce noticeably different coefficients.
- Extrapolation: Using the regression line to predict y for x values far outside your data range is risky. The linear trend may not continue.
- Outliers: A single extreme point can pull the line toward it, changing the slope and intercept dramatically. Check for outliers before trusting the equation.
- Correlation ≠ causation: A strong linear relationship does not mean x causes y. There may be a lurking variable.
Alternatives to Simple Linear Regression
If your data is not linear, consider:
- Polynomial regression – fits a curve (e.g., y = a + bx + cx²).
- Multiple linear regression – uses several independent variables (y = a + b₁x₁ + b₂x₂ + …).
- Logarithmic or exponential transformation – sometimes taking log of x or y makes the relationship linear.
For a quick check, you can also compute the correlation coefficient alone, which gives r but not the equation.
Frequently Asked Questions
What is the difference between linear regression and correlation?
Correlation (r) measures the strength and direction of a linear relationship, but it does not give a prediction equation. Linear regression provides the equation y = a + bx, which allows you to predict y from x. Correlation is symmetric (x vs y is the same as y vs x), while regression is not – the regression line of y on x is different from the line of x on y.
How do I interpret the slope in real terms?
The slope b means that for every one-unit increase in x, y increases by b units on average. For example, if x is study hours and y is test score, a slope of 0.6 means each additional hour of study is associated with a 0.6-point higher test score, on average.
What if my data has only two points?
With two points, the regression line passes exactly through both, and R² will be 1 (or -1 for a negative slope). That is a perfect fit, but it is not meaningful – you need more points to estimate the relationship reliably.
Can I use linear regression for binary (yes/no) data?
Not directly. Linear regression assumes a continuous outcome. For a binary outcome, use logistic regression instead. However, you can use a binary independent variable (e.g., gender coded as 0/1) in linear regression – that is equivalent to comparing group means.
Why does my calculator give a different equation than Excel?
Different tools may round intermediate values differently, or you might be using the wrong variable order (x vs y). Also, some calculators use population vs sample formulas for variance, but for regression coefficients the formula is standard. Check that you entered the data correctly and that you are reading the slope and intercept in the same order.
Related
Formula used
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) | - | (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 r2 | R-squared | - | ( (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)) )^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.