Linear Regression Line 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 line calculator finds the line of best fit (y = a + bx) for a set of paired data points. You enter the x and y values, and it returns the slope (b), the intercept (a), and the correlation coefficient (r) so you can see how well the line fits. The calculator uses the ordinary least squares method, the standard approach taught in statistics courses.
Formula
The linear regression line is written as:
y = a + b·x
Where:
- y is the dependent variable (the value you want to predict).
- x is the independent variable (the input).
- b is the slope, calculated as: b = [n·Σ(xy) − Σx·Σy] / [n·Σ(x²) − (Σx)²]
- a is the intercept, calculated as: a = (Σy − b·Σx) / n
- n is the number of data points.
Σ means “sum of.” For example, Σ(xy) means multiply each x by its paired y, then add those products together. The slope tells you how much y changes for a one-unit increase in x. The intercept is the predicted y when x = 0.
Worked Example
Using the default data: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5.
- Count: 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
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 line is y = 2.2 + 0.6x. For x = 3, the predicted y is 2.2 + 0.6×3 = 4.0. The actual y at x=3 is 5, so the residual is 1.0.
Correlation r = (5×66 − 15×20) / √[(5×55 − 225) × (5×86 − 400)] = 30 / √(50 × 30) = 30 / √1500 ≈ 0.7746. This indicates a moderate positive relationship.
When to Use Linear Regression
Use linear regression when you have two continuous variables and you believe one depends on the other in a roughly straight-line fashion. Common examples include:
- Predicting sales from advertising spend.
- Estimating a person’s weight from their height.
- Forecasting temperature from altitude.
It works best when the relationship is actually linear and there are no obvious curves or outliers. If the scatter plot shows a U-shape or a fan pattern, a straight line may mislead you.
How to Read the Results
The slope (b) is the most important number. A slope of 0.6 means that for every 1-unit increase in x, y increases by 0.6 units on average. The intercept (a) is the predicted y when x is zero, but only interpret it if x=0 is within the range of your data – otherwise it’s just a mathematical anchor.
The correlation coefficient (r) ranges from −1 to 1. Values close to 1 or −1 indicate a strong linear relationship; values near 0 mean little to no linear association. But r does not tell you the slope – a dataset can have r = 0.9 with a tiny slope if the data are tightly clustered.
Always plot your data. A single outlier can drastically change the line, and Anscombe’s quartet (four datasets with nearly identical regression lines) shows why visualization matters.
Common Mistakes and Limitations
- Mixing up x and y: The regression of y on x is not the same as x on y. The calculator assumes you want to predict y from x.
- Using the line outside the data range: Extrapolation beyond your x values is risky; the relationship may not hold.
- Rounding too early: Keep full precision in intermediate steps, then round at the end. Rounding the slope before computing the intercept can change the answer.
- Ignoring non-linearity: If the true relationship is curved, a straight line will give biased predictions.
- Assuming causation: Correlation does not imply causation. A strong r does not mean x causes y.
Also note that the least squares method is sensitive to outliers. One unusually large or small value can pull the line toward it. If you have outliers, consider robust regression or remove the point if it’s an error.
Alternatives to Simple Linear Regression
If your data doesn’t fit a straight line, consider:
- Polynomial regression – adds squared or cubed terms to model curves.
- Multiple linear regression – uses two or more independent variables.
- Logarithmic or exponential transformation – makes the relationship linear by transforming one variable.
- Nonparametric methods – like LOESS, which don’t assume a specific form.
For most introductory statistics needs, simple linear regression is the right starting point.
Frequently Asked Questions
What is the difference between slope and correlation?
The slope (b) tells you the steepness of the line – how many units y changes per one unit of x. The correlation (r) tells you the strength and direction of the linear relationship, independent of the units. A line can have a steep slope but a weak correlation if the points are widely scattered.
Can I use linear regression with only two points?
Yes, but the line will pass exactly through both points, and the correlation will be either 1 or −1. With two points you have no information about variability, so the line is not statistically meaningful for prediction.
What does R² mean?
R² is the square of the correlation coefficient (r²). It represents the proportion of variance in y that is explained by x. For example, r = 0.7746 gives R² ≈ 0.60, meaning 60% of the variation in y is explained by the linear relationship with x.
How do I know if my linear regression is good?
Check the residual plot (differences between actual and predicted y). If the residuals are randomly scattered around zero with no pattern, the linear model is appropriate. Also check R² – higher is better, but a high R² doesn’t guarantee the model is correct.
What if my data has different units?
The calculator treats numbers as plain numbers. If your x is in meters and y is in seconds, the slope will be in seconds per meter. That’s fine as long as you interpret the units correctly. For example, a slope of 0.6 seconds per meter means each additional meter adds 0.6 seconds.
Can I use this calculator for time series forecasting?
Yes, if you set x as time (e.g., 1, 2, 3, …) and y as the observed value, the regression line gives a simple linear trend forecast. However, time series often have autocorrelation and seasonality, so more advanced methods like ARIMA may be better.
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 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
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.