Why Derive the Slope?
A least-squares regression line is chosen to make the squared prediction errors as small as possible. The fitted slope is not an arbitrary feature of a graph: it is the value that minimizes a specific function of the observed pairs. Deriving it shows why the slope depends on how \(x\) and \(y\) vary together and on how much the explanatory variable \(x\) varies on its own.
Here, \(x_i\) is the explanatory-variable value and \(y_i\) is the response-variable value for observation \(i\), for \(i=1,\ldots,n\). Write a candidate line as \(\hat{y}=a+bx\), where \(a\) is its intercept and \(b\) its slope. The residual for observation \(i\) is \(y_i-(a+bx_i)\).
This is a mathematical optimization of the observed data. To carry it out, assume the data consist of paired numerical values and that all values are finite. A unique least-squares slope also requires that the observed \(x\)-values are not all identical. No probability model, normal distribution, or independence assumption is needed just to derive or calculate this descriptive slope.
Deriving the Slope
Because both the intercept and slope can affect the sum of squared errors, first account for the intercept. For a fixed value of \(b\), differentiate \(Q(a,b)\) with respect to \(a\) and set the derivative equal to zero:
Rearranging this equation gives the intercept that minimizes the sum of squared errors for that fixed slope:
where \(\bar{x}\) and \(\bar{y}\) are the sample means of the explanatory and response values. Substituting this intercept into each residual gives
Thus, after choosing the best intercept for each \(b\), the sum of squared errors is a function of the slope alone. Define \(S_{xx}=\sum_{i=1}^{n}(x_i-\bar{x})^2\), \(S_{xy}=\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})\), and \(S_{yy}=\sum_{i=1}^{n}(y_i-\bar{y})^2\). Expanding the squared residuals gives
Differentiate with respect to \(b\). At a minimum the derivative is zero, so \(2bS_{xx}-2S_{xy}=0\). If \(S_{xx}>0\), solving gives the least-squares slope. The expression below also verifies that it is the unique minimum: completing the square shows that \(Q(b)\) is a positive multiple of a squared distance from this slope, plus a constant.
If all the \(x_i\) values are identical, then \(S_{xx}=0\), and the formula cannot be used. In that case, changing the slope can be offset by changing the intercept, so the data do not determine a unique slope. There is no variation in the explanatory variable with which to compare changes in the response.
Covariance and Variation in \(x\)
The numerator \(S_{xy}\) measures how the two variables deviate from their respective means together. It is positive when observations with above-average \(x\) tend to have above-average \(y\), and negative when above-average \(x\) tends to accompany below-average \(y\). The denominator \(S_{xx}\) measures the total squared deviations of the explanatory values from their mean.
The sample covariance and sample variance of \(x\) use the same divisor, \(n-1\):
Consequently, their common divisor cancels in the slope formula. This gives a compact interpretation: the least-squares slope is the sample covariance of \(x\) and \(y\), divided by the sample variance of \(x\).
The numerator has units of \(x\) times units of \(y\), while the denominator has units of \(x\) squared. Their ratio therefore has units of \(y\) per unit of \(x\), as a slope should. The sign of the slope matches the sign of the covariance because \(S_{xx}\) is positive. A positive slope means the fitted response increases as \(x\) increases; a negative slope means it decreases.
The slope is directional: it describes the fitted response \(y\) in relation to the explanatory variable \(x\). Reversing the roles of \(x\) and \(y\) changes which variable appears in the denominator, and generally produces a different slope. This formula also agrees with the correlation-based expression \(b_1=r(s_y/s_x)\), since \(s_{xy}=rs_xs_y\) and \(s_x^2\) is in the denominator.
Worked Example: A Positive Slope from Paired Data
Suppose a hypothetical class records study time \(x\), in hours, and quiz score \(y\), in points, for three students. The pairs are \((1,2)\), \((2,3)\), and \((3,5)\). Find the least-squares slope.
The means are \(\bar{x}=(1+2+3)/3=2\) hours and \(\bar{y}=(2+3+5)/3=10/3\) points. The centered products and squared \(x\)-deviations are
Substitute these totals into the derived formula:
The fitted score increases by 1.5 points for each additional hour of study. This describes the slope of the fitted line for these hypothetical observations; it does not establish that studying caused the score difference.
Worked Example: A Negative Slope
Suppose a hypothetical set of locations has outdoor temperatures \(x\), in degrees Celsius, of 1, 2, and 4, with corresponding heating-energy use \(y\) of 9, 7, and 4 kilowatt-hours. Calculate and interpret the least-squares slope.
The means are \(\bar{x}=7/3\) degrees Celsius and \(\bar{y}=20/3\) kilowatt-hours. The centered values are \(x_i-\bar{x}=-4/3,-1/3,5/3\) and \(y_i-\bar{y}=7/3,1/3,-8/3\). Therefore,
Then
The negative sign reflects that higher temperatures in these data tend to accompany lower heating-energy use. The fitted energy use decreases by about 1.643 kilowatt-hours per degree Celsius of increase in temperature.
Worked Example: Connecting the Slope to Covariance
For another hypothetical data set, let explanatory values be \(x=2,4,6,8\), and response values be \(y=5,8,7,12\). Find the slope using both the centered sums and the covariance-to-variance form.
The means are \(\bar{x}=5\) and \(\bar{y}=8\). The centered values are \(-3,-1,1,3\) for \(x\) and \(-3,0,-1,4\) for \(y\). Thus,
The least-squares slope is \(b_1=20/20=1\) response unit per explanatory-variable unit. There are \(n=4\) pairs, so the sample covariance is \(s_{xy}=20/(4-1)=20/3\), and the sample variance of \(x\) is \(s_x^2=20/(4-1)=20/3\). Their ratio gives the same answer:
This verifies that the divisor in the sample covariance and variance does not affect their ratio. The slope’s units still depend on the units used for \(x\) and \(y\), even though these example values have a numerical ratio of one.
Common Mistake: Using the Wrong Variable in the Denominator
A common error is to divide the covariance by the variance of the response variable and call the result the slope for predicting \(y\) from \(x\). The correct formula has \(x\)-variation in the denominator: \(b_1=s_{xy}/s_x^2\). The explanatory variable is the quantity whose variation is used to describe the change in the fitted response. If the goal instead is to predict \(x\) from \(y\), the roles reverse and the denominator becomes \(s_y^2\); that is a different regression line.
Another tempting mistake is to think that the denominator’s divisor \(n-1\) should remain in the slope. It appears in both the sample covariance and sample variance, so it cancels exactly. Either use the centered sums directly or use the ratio \(s_{xy}/s_x^2\); both yield the same slope.
Check Your Understanding
Answer each question using the derivation and examples above.
- What does \(S_{xy}\) measure about paired deviations from the means?
- Why must the observed \(x\)-values not all be identical for the usual least-squares slope formula to give a unique value?
- If \(S_{xy}=-12\) and \(S_{xx}=6\), what is the least-squares slope, and what does its sign indicate?
- Why does using sample covariance and sample variance give the same slope as using the centered sums?
- What changes in the slope formula if the goal is to predict \(x\) from \(y\) instead of predicting \(y\) from \(x\)?