How to use the SLOPE function
What is the SLOPE function?
The SLOPE function calculates the slope of the linear regression line through coordinates.
Table of Contents
1. Introduction
What is a linear regression line?
A linear regression line is a straight line fit to data to model the linear relationship between two variables. The line of best fit minimizes the distance from all data points and allows predicting one variable from the other.
What is the line equation?
The linear equation has the following form: y = mx + b
y - dependent data points
x - independent data points
m - slope
b - where the line intercepts the y axis
The image above shows the following equation: y = 2x+5
What is a negative SLOPE value?
The linear equation has the following form: y = mx + b
If m < 0 which means m is negative and the graph slopes downwards, this is demonstrated in the image above.
If m > 0 then it is positive and the graph slopes upwards.
Related functions
Excel Function and Arguments | Description |
---|---|
LINEST(known_y's, [known_x's], [const], [stats]) | Returns statistics for a linear trend line fit |
INTERCEPT(known_y's, known_x's) | Returns y-intercept of linear regression line |
FORECAST.LINEAR(x, known_y's, known_x's) | Predicts y value on linear trend line for given x |
TREND(known_y's, [known_x's], [new_x's], [const]) | Returns predicted y values for linear trend model |
SLOPE(known_y's, known_x's) | Returns slope of linear regression line |
What is the difference between the INTERCEPT function and the SLOPE function?
The INTERCEPT function calculates the b coefficient meaning where the linear equation crosses the y-axis whereas the SLOPE function calculates the m coefficient which is a measure of the slope.
y = mx + b
What if the slope is 0 (zero)?
The image above demonstrates the linear function y = 0x +5 which is the same as y = 5 This function describes a perfectly horizontal line on an xy chart.
This means that a linear function y = mx + b with a slope coefficient equal to 0 (zero) is horizontal.
How to calculate a regression line?
A regression line is based on the method of least squares, which aims to find the line that best fits a set of data points by minimizing the sum of the squared differences between the actual data points and the corresponding points on the line.
We discussed the straight line equation above:
y = mx + c.
- c is where the line cuts the y-axis
- m is the slope of the line
To find the values of m and c that best fit the data we use the following formulas:
Slope (m): m = Σ((x - xmean)(y - ymean)) / Σ((x - xmean)2)
Σ represents the sum of the values
xmean and ymean are the means (averages) of the x and y values respectively.
Y-intercept (c): c = ymean - m * xmean
2. Syntax
SLOPE(known_y's, known_x's)
known_y's | Required. An array or cell reference to dependent data points. |
known_x's | Required. An array or cell reference to independent data points. |
3. Example 1
Determine the slope of a linear regression line for the following data?
Y Values: 10, 30, 50, 60, 90
X Values: 5, 23, 11, 56, 45
The arguments are:
- known_y's = B3:B7
- known_x's = C3:C7
Formula in cell B10:
The SLOPE function returns approx. 1.033 for the calculated regressions line that has the best fit based on the given data points. The slope is larger than 0 (zero) meaning it slopes upwards. The SLOPE function includes zeros but text, logical values and empty cells are ignored.
The image above shows the data points given as blue dots in the chart, the black line represents the calculated regression line. The SLOPE value is calculated like this:
SLOPE function:
Σ((x-x̄)(y-ȳ))/ Σ(x-x̄)2
x̄ is the mean for all x values.
ȳ is the mean based on all y values.
The arithmetic mean for the following y values: 10, 30, 50, 60, and 90 are 10+ 30 + 50 + 60 + 90 = 240. 240/5 = 48.
The arithmetic mean for the following x values: 5, 23, 11, 56, and 45 are 5+23+11+56+45 = 140. 140/5 = 28.
Calculate x-x̄
5-28=-23
23-28=-5
11-28=-17
56-28=28
45-28=17
Calculate y-ȳ
10-48=-38
30-48=-18
50-48=2
60-48=12
90-48=42
Calculate (x-x̄)(y-ȳ)
-23*-38=874
-5*-18=90
-17*2=-34
28*12=336
17*42=714
Calculate Σ((x-x̄)(y-ȳ))
874+90+-34+336+714=1980
Calculate Σ(x-x̄)2
(-23)2+(-5)2+(-17)2+(28)2+(17)2=529+25+289+784+289=1916
Calculate Σ((x-x̄)(y-ȳ))/ Σ(x-x̄)2
1980/1916 = approx. 1.033
4. Example 2
Estimate the fixed cost component based on the following production data?
Output (units): 1000, 1200, 1400, 1600, 1800
Total Cost ($): 19600, 22450, 25130, 25340, 29670
The image above shows the data in cell range B23:C27, the x y scatter chart above shows the data points. The formula in cell E3 calculates the SLOPE value based on the x and y values in B23:C27.
Formula in cell E17:
The intercept value is where the regression line intersects the y-axis. The regression line is calculated using the INTERCEPT and SLOPE function, the black line shown in the chart above represents the regression line.
SLOPE function: Σ((x-x̄)(y-ȳ))/ Σ(x-x̄)2
x̄ = AVERAGE(B23:B27) equals 1400
ȳ = AVERAGE(C23:C27) equals 24438
Calculate x-x̄
1000-1400=-400
1200-1400=-200
1400-1400=0
1600-1400=200
1800-1400=400
Calculate y-ȳ
19600-24438=-4838
22450-24438=-1988
25130-24438=692
25340-24438=902
29670-24438=5232
Calculate (x-x̄)(y-ȳ)
-400*-4838=1935200
-200*-1988=397600
0*692=0
200*902=180400
400*5232=2092800
Calculate Σ((x-x̄)(y-ȳ))
1935200+397600+0+180400+2092800=4606000
Calculate Σ(x-x̄)2
-400^2=160000
-200^2=40000
0^2=0
200^2=40000
400^2=160000
160000+40000+0+40000+160000=400000
Calculate Σ((x-x̄)(y-ȳ))/ Σ(x-x̄)2
4606000/400000=11.515
This value matches the calculated value in cell C17.
5. Why is the function not working?
The SLOPE function returns
- #N/A! error if there are a different number of values in known_y's and known_x's.
The image above shows the SLOPE function with two cell references B3:B7 and C3:C8 which are not identical in size meaning there are more values in C3:C8 than in B3:B7. The SLOPE function requires the same number of values in both cell references or arrays.
5.1 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
5.2 The formula returns an unwanted value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a hunch that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue..
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference B3:B7 and C3:C8 converted to hard-coded value using the F9 key. Comparing the arrays shows that the number of values does not match. The SLOPE function expects the same number of items in both arrays. We have found what is wrong with the formula using the F9 key.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
6. How is the function calculated?
Here is the math formula behind the SLOPE function which calculates linear regression:
Here is the text representation. SLOPE function = Σ(x - x̄)(y - ȳ)/Σ(x - x̄)2
x̄ - arithmetic mean of x coordinates
ȳ - arithmetic mean of y coordinates
The numerator Σ(x - x̄)(y - ȳ):
- x̄ is the mean of all x values
- ȳ is the mean of all y values
- (x - x̄) is the deviation of each x value from its mean
- (y - ȳ) is the deviation of each y value from its mean
- We multiply these deviations and sum them all up (Σ)
The denominator Σ(x - x̄)²:
- Takes each x deviation (x - x̄)
- Squares it (x - x̄)2
- Sums up all these squared deviations Σ(x - x̄)2
This formula measures how much y changes in relation to x accounting for how the variables move together (numerator) relative to the spread of x values (denominator). If x and y tend to move together in the same direction, the slope will be positive. If they move in opposite directions, the slope will be negative. The larger the magnitude of the slope, the steeper the relationship. This is also known as the least squares regression slope.
'SLOPE' function examples
The following article has a formula that contains the SLOPE function.
Have you ever tried to build a formula to calculate discounts based on price? The VLOOKUP function is much easier […]
Functions in 'Statistical' category
The SLOPE function function is one of 73 functions in the 'Statistical' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form