Linear Regression Calculator: 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.
A linear regression calculator works out the line of best fit (y = a + bx) for a set of paired data points, giving you the slope, intercept, and usually the correlation coefficient (r) and R². You enter your x and y values, and the calculator returns the equation of the straight line that minimizes the sum of squared vertical distances between the points and the line. This line lets you summarize the relationship between two variables and make predictions for new x values.
Formula
The linear regression equation is written as:
y = a + b·x
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 using the least squares method:
b = [n·Σ(xy) – Σx·Σy] / [n·Σ(x²) – (Σx)²]
And the intercept:
a = (Σy – b·Σx) / n
Here, n is the number of data pairs, Σ means “sum of”, and x² means each x value squared. The formula minimizes the sum of the squared vertical distances between each point and the fitted line, which is why it is called “least squares”.
Worked Example
Using the default values from the calculator above: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5.
First, compute the sums:
- n = 5
- Σx = 1+2+3+4+5 = 15
- Σy = 2+4+5+4+5 = 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
- Σy² = 4+16+25+16+25 = 86
Now plug into the slope formula:
b = [5×66 – 15×20] / [5×55 – 15²] = (330 – 300) / (275 – 225) = 30 / 50 = 0.6
Then the intercept:
a = (20 – 0.6×15) / 5 = (20 – 9) / 5 = 11 / 5 = 2.2
So the regression line is y = 2.2 + 0.6x.
For the correlation coefficient:
r = (5×66 – 15×20) / √[(5×55 – 15²) × (5×86 – 20²)] = 30 / √(50 × 30) = 30 / √1500 ≈ 30 / 38.7298 ≈ 0.7746
R² = 0.7746² ≈ 0.6. The calculator rounds to 4 decimal places, so you get slope 0.6, intercept 2.2, r = 0.7746, and R² = 0.6.
When to Use Linear Regression
Use linear regression when you have two continuous variables and you want to know if a straight line can describe their relationship. Common examples:
- Predicting a student’s exam score based on hours studied.
- Estimating house price based on square footage.
- Forecasting sales based on advertising spend.
- Examining the relationship between temperature and ice cream sales.
Before using it, check that the relationship is roughly linear – a scatterplot helps. If the points curve or fan out, a straight line may not be appropriate. Linear regression also assumes that the residuals (the vertical distances from the line) are roughly normally distributed and have constant spread, though for simple prediction these assumptions matter less.
How to Read the Results
The slope (b) 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. In the example, slope 0.6 means that for each additional unit of x, y increases by 0.6 on average.
The intercept (a) is the predicted y when x is zero. Sometimes this value has a practical meaning, sometimes not – if x=0 is outside your data range, the intercept is just a mathematical artifact.
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. In the example, r = 0.7746 suggests a moderately strong positive correlation.
R² (the coefficient of determination) is the proportion of variance in y explained by x. An R² of 0.6 means 60% of the variation in y is explained by the linear relationship with x. The remaining 40% is due to other factors or random variation.
Common Mistakes and Misconceptions
One common mistake is using the regression line to predict far outside the range of your data – this is called extrapolation and can be wildly inaccurate. For example, if your x data ranges from 1 to 5, predicting at x=50 is risky because the relationship may not stay linear.
Another mistake is confusing correlation with causation. A strong r does not prove that x causes y – both could be influenced by a third variable, or the relationship could be coincidental.
Also, be careful with units. If x is in hours and y is in dollars, the slope is in dollars per hour. If you change units (e.g., minutes instead of hours), the slope changes accordingly. Always keep track of units when interpreting results.
Finally, remember that linear regression is sensitive to outliers. A single extreme point can pull the line toward it, changing the slope and intercept dramatically. Always look at a scatterplot before trusting the numbers.
Alternatives to Simple Linear Regression
If your data is not linear, you have several options:
- Polynomial regression – fits a curve (e.g., y = a + bx + cx²) to capture curvature.
- Multiple linear regression – uses two or more independent variables (e.g., y = a + b₁x₁ + b₂x₂).
- Logarithmic or exponential transformation – transform x or y to make the relationship linear.
- Non-parametric methods like LOESS – do not assume a specific functional form.
For simple forecasting, sometimes a moving average or exponential smoothing works better than regression if the relationship is not linear. But for many basic analyses, simple linear regression is a solid, interpretable starting point.
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 you a prediction equation. Linear regression gives you the actual line (slope and intercept) so you can predict y from x. Correlation is a single number; regression is an equation.
Can I use linear regression if my data is not normally distributed?
Strictly, the residuals (errors) should be normally distributed for hypothesis tests and confidence intervals. But for simply fitting a line and making predictions, the normality assumption is less critical. However, if your data has extreme skew or outliers, the line may be misleading.
What does R² mean in simple terms?
R² tells you how much of the variation in y is explained by x. An R² of 0.8 means 80% of the variability in y is accounted for by the linear relationship with x. The other 20% is unexplained, due to other factors or random noise.
How many data points do I need for linear regression?
Technically, you need at least two points to draw a line, but that gives you a perfect fit with no way to assess reliability. For meaningful results, aim for at least 10-20 pairs. More data gives more stable estimates and better detection of violations of assumptions.
What if my intercept is negative or zero?
That is perfectly fine. A negative intercept means the line crosses the y-axis below zero. A zero intercept means the line passes through the origin (0,0). It all depends on your data – there is no rule that the intercept must be positive.
Can I use the regression equation to predict y for any x?
Only within the range of x values you observed (interpolation). Extrapolating beyond that range is risky because the linear relationship may not hold. The further you go outside the data, the less reliable the prediction.
Related
Formula used
in x | X values | - | 1, 2, 3, 4, 5 | 0 in y | Y values | - | 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 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 r2 | R-squared | - | r^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.