Linear Regression Calculator Excel: How to Do It
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.
You can perform linear regression in Excel using the built-in LINEST function, the Data Analysis Toolpak, or by adding a trendline to a scatter chart. This article explains each method step by step, what the output means, and how to avoid common mistakes.
Formula
The linear regression equation is y = mx + b, where:
- y = dependent variable (what you are predicting)
- x = independent variable (the predictor)
- m = slope (change in y per unit change in x)
- b = y-intercept (value of y when x = 0)
The slope and intercept are calculated using the least squares method, which minimizes the sum of the squared vertical distances between the data points and the regression line. The formulas are:
m = [n·Σ(xy) − Σx·Σy] / [n·Σ(x²) − (Σx)²]
b = [Σy − m·Σx] / n
where n is the number of data points, Σ denotes summation, and x and y are the individual data values.
Worked Example
Using the default values from the calculator above: X = 1, Y = 2 (just two points). With n = 2:
- Σx = 1, Σy = 2, Σxy = 1×2 = 2, Σx² = 1² = 1, Σy² = 2² = 4
- m = [2×2 − 1×2] / [2×1 − 1²] = (4 − 2) / (2 − 1) = 2 / 1 = 2.0000
- b = [2 − 2×1] / 2 = (2 − 2) / 2 = 0 / 2 = 0.0000
- R² = [2×2 − 1×2]² / [(2×1 − 1²)(2×4 − 2²)] = (2)² / [(1)(8 − 4)] = 4 / 4 = 1.0000
So the regression line is y = 2x + 0, and R² = 1 (perfect fit, because two points always define a line).
How to Use Excel’s Built-in Functions
Excel offers several ways to compute linear regression without any add-ins:
- SLOPE(known_y’s, known_x’s) – returns the slope m.
- INTERCEPT(known_y’s, known_x’s) – returns the intercept b.
- RSQ(known_y’s, known_x’s) – returns R².
- LINEST(known_y’s, known_x’s, TRUE, TRUE) – returns an array with slope, intercept, and other statistics. Select a 2×5 range, enter the formula, and press Ctrl+Shift+Enter to see all values.
For example, if your y values are in A2:A10 and x values in B2:B10, type =SLOPE(A2:A10, B2:B10) to get the slope.
Using the Data Analysis Toolpak
The Toolpak is an Excel add-in that provides a full regression output. To enable it:
- Go to File → Options → Add-ins.
- Select Analysis Toolpak and click Go.
- Check Analysis Toolpak and click OK.
- Now go to Data → Data Analysis → Regression.
- Set the Input Y Range and Input X Range (including headers if you check “Labels”).
- Choose an output location (e.g., New Worksheet Ply).
- Click OK. The output includes coefficients, standard errors, R², and ANOVA table.
Adding a Trendline to a Chart
If you have a scatter plot, you can add a regression line visually:
- Select your data and insert a Scatter chart.
- Click on any data point to select the series.
- Right-click and choose Add Trendline.
- Choose Linear.
- Check Display Equation on chart and Display R-squared value on chart.
The equation shown is the same y = mx + b from the least squares method.
Common Mistakes and How to Avoid Them
- Swapping x and y – The slope and intercept change if you swap the variables. Always put the independent variable as x.
- Non-numeric data – Excel ignores text and blank cells in SLOPE and INTERCEPT, but if your range includes headers, exclude them from the function arguments.
- Not using Ctrl+Shift+Enter for LINEST – LINEST is an array function; if you just press Enter, you only get the first value.
- Interpreting R² as a percentage – R² is a proportion between 0 and 1; multiply by 100 to get a percentage.
- Extrapolating beyond your data range – The regression line is only valid within the range of x values you observed.
Limitations of Linear Regression
Linear regression assumes a linear relationship between x and y. It is sensitive to outliers, which can heavily influence the slope. It also assumes that the residuals (differences between observed and predicted y) are normally distributed and have constant variance. If your data has a curved pattern, consider polynomial regression or transforming the variables.
Frequently Asked Questions
How do I get the p-value for the slope in Excel?
Use the Data Analysis Toolpak regression output. It gives you the p-value for the slope in the “P-value” column. Alternatively, you can use the T.DIST.2T function with the t-statistic and degrees of freedom, but the Toolpak is simpler.
What does R-squared mean in Excel regression?
R² (coefficient of determination) tells you the proportion of variance in y that is explained by x. For example, R² = 0.8 means 80% of the variation in y is explained by the model. It ranges from 0 to 1, with higher values indicating a better fit.
Why does my trendline equation not match the SLOPE function?
If you have blank cells or text in your data range, the trendline and SLOPE may use different data. Ensure both use the same ranges and that there are no hidden rows or non-numeric entries.
Can I do linear regression in Excel without the Toolpak?
Yes, you can use the SLOPE, INTERCEPT, and RSQ functions, or add a trendline to a chart. These are built-in and do not require any add-ins.
How do I forecast a value using linear regression in Excel?
Use the FORECAST.LINEAR(x, known_y's, known_x's) function. For example, =FORECAST.LINEAR(10, A2:A10, B2:B10) predicts the y value for x=10.
Related
Formula used
in x | X values | - | 1 | 0 in y | Y values | - | 2 | 0 out slope | Slope (m) | - | (count(x)*sum(x*y)-sum(x)*sum(y))/(count(x)*sum(x^2)-sum(x)^2) | 4 out intercept | Intercept (b) | - | (sum(y)-slope*sum(x))/count(x) | 4 out r2 | R-squared | - | (count(x)*sum(x*y)-sum(x)*sum(y))^2/((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.