Tutorials › Regression Analysis (Undergraduate) › The Slope Formula from Centered Data

Fitting a Simple Linear Regression · Tutorial 78 of 991

The Slope Formula from Centered Data

Learn why the least-squares slope is the sum of centered cross-products divided by the sum of centered predictor squares, and how that equals sample covariance over sample variance.

Beginner 9 min read

What You'll Learn

  • Write the least-squares slope formula using centered predictor and response values.
  • Understand why minimizing squared errors gives this centered-data formula.
  • Relate the slope to sample covariance and sample variance.
  • Calculate and interpret slopes with the units of the variables.
  • Recognize why dividing covariance by predictor variance gives the slope, not correlation.

Why Center the Data?

A fitted slope describes how the fitted response changes as the predictor changes. Its calculation depends on how the observed \(x\)- and \(y\)-values vary together. Subtracting each variable’s sample mean makes that variation visible: centered values show whether each observation lies above or below its own variable’s mean. The least-squares slope can be written directly in terms of these centered values.

This formula is useful both as a way to understand the slope and as a check on calculations. It also connects regression to two familiar summaries of paired data: sample covariance, which measures joint variation, and sample variance, which measures variation in the predictor.

Definition: For \(n\) paired observations \((x_i,y_i)\), let \(\bar{x}\) and \(\bar{y}\) be the sample means. The centered predictor and response values for observation \(i\) are \(x_i-\bar{x}\) and \(y_i-\bar{y}\). If the predictor values are not all identical, the least-squares slope is $$ b_1=\frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})}{\sum_{i=1}^{n}(x_i-\bar{x})^2}. $$

The Fitting Setup and the Slope Formula

In simple linear regression, the working representation relates a response \(y_i\) to a predictor \(x_i\) through a line and an error term, \(y_i=\beta_0+\beta_1x_i+\varepsilon_i\). For the observed data, the fitted line is written \(\hat{y}_i=b_0+b_1x_i\). The least-squares rule chooses \(b_0\) and \(b_1\) to minimize the sum of squared differences between observed and fitted responses.

The centered slope formula is a result about this least-squares calculation. To use it, we require paired quantitative observations, a well-defined sample mean for each variable, and variation in the predictor, so that \(\sum (x_i-\bar{x})^2>0\). The calculation itself does not require normally distributed errors, constant error variance, or independent errors. Such probability assumptions matter for some inferential procedures, not for this descriptive formula.

The least-squares line passes through the data centroid \((\bar{x},\bar{y})\). Thus, once the slope is specified, the fitted line can be written in centered form as \(\hat{y}=\bar{y}+b_1(x-\bar{x})\). For an observed \(x_i\), its fitted value is \(\bar{y}+b_1(x_i-\bar{x})\). The squared-error objective as a function of the slope is therefore

$$ Q(b_1)=\sum_{i=1}^{n}\left[(y_i-\bar{y})-b_1(x_i-\bar{x})\right]^2. $$

This expression gives a direct derivation of the centered formula. Define \(S_{xx}=\sum (x_i-\bar{x})^2\) and \(S_{xy}=\sum (x_i-\bar{x})(y_i-\bar{y})\). Expanding the squared terms gives

$$ Q(b_1)=\sum_{i=1}^{n}(y_i-\bar{y})^2-2b_1S_{xy}+b_1^2S_{xx}. $$

When \(S_{xx}>0\), complete the square in \(b_1\):

$$ Q(b_1)=S_{xx}\left(b_1-\frac{S_{xy}}{S_{xx}}\right)^2 +\sum_{i=1}^{n}(y_i-\bar{y})^2-\frac{S_{xy}^2}{S_{xx}}. $$

The last two terms do not depend on \(b_1\). The squared term is nonnegative and is smallest, reaching zero, when \(b_1=S_{xy}/S_{xx}\). Because \(S_{xx}>0\), this is the unique minimizing slope. Substituting the definitions of \(S_{xy}\) and \(S_{xx}\) gives the centered-data formula.

Connection to Sample Covariance and Variance

The numerator of the slope formula is the centered cross-product sum, and the denominator is the centered sum of squares for the predictor. Sample covariance and sample variance use those same sums, each divided by \(n-1\).

Definition: The sample covariance of \(x\) and \(y\) is \(s_{xy}=\frac{1}{n-1}\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})\). The sample variance of the predictor is \(s_x^2=\frac{1}{n-1}\sum_{i=1}^{n}(x_i-\bar{x})^2\). When \(s_x^2>0\), the least-squares slope can equivalently be written \(b_1=s_{xy}/s_x^2\).

The \(n-1\) factors cancel in the ratio: \(s_{xy}/s_x^2=[S_{xy}/(n-1)]/[S_{xx}/(n-1)]=S_{xy}/S_{xx}\). This is why the slope can be calculated either from centered sums or from sample covariance divided by sample variance. It is important that the denominator is the variance of the predictor, not its standard deviation.

Units provide a useful check. If \(x\) is measured in hours and \(y\) in points, then each cross-product has units of point-hours, while each squared centered predictor value has units of hours squared. Their ratio has units of points per hour. The covariance-to-variance expression has the same units. A slope is therefore not unitless; its units are response units per predictor unit.

Worked Example: Study Time and Quiz Score

Suppose, for a hypothetical set of four students, study time \(x\) is recorded in hours and quiz score \(y\) in points. The observed pairs are \((1,52)\), \((2,55)\), \((3,61)\), and \((4,64)\). Find the least-squares slope using centered data.

First calculate the means: \(\bar{x}=(1+2+3+4)/4=2.5\) hours and \(\bar{y}=(52+55+61+64)/4=58\) points. The centered values and their products are:

\(i\)\(x_i-\bar{x}\)\(y_i-\bar{y}\)Product\((x_i-\bar{x})^2\)
1-1.5-692.25
2-0.5-31.50.25
30.531.50.25
41.5692.25

The cross-product sum is \(S_{xy}=9+1.5+1.5+9=21\) point-hours. The predictor sum of squares is \(S_{xx}=2.25+0.25+0.25+2.25=5\) hours squared. Therefore,

$$ b_1=\frac{S_{xy}}{S_{xx}}=\frac{21}{5}=4.2\text{ points per hour}. $$

As a check, the sample covariance is \(s_{xy}=21/(4-1)=7\) point-hours, and the sample variance of study time is \(s_x^2=5/(4-1)=1.6667\) hours squared. Their ratio is \(7/1.6667\approx4.2\) points per hour. The positive slope indicates that the fitted score increases by 4.2 points for each additional hour of study time; it does not establish that study time caused the difference.

Worked Example: A Negative Slope

For a hypothetical set of four days, let \(x\) be outdoor temperature in degrees and \(y\) be heating use in units. The observed pairs are \((10,30)\), \((12,25)\), \((14,24)\), and \((16,17)\). Calculate the slope from centered values.

The means are \(\bar{x}=(10+12+14+16)/4=13\) degrees and \(\bar{y}=(30+25+24+17)/4=24\) units. The centered predictor values are \(-3,-1,1,3\); the centered response values are \(6,1,0,-7\). Their cross-products are \(-18,-1,0,-21\), which sum to \(S_{xy}=-40\) degree-units. The squared centered predictor values are \(9,1,1,9\), which sum to \(S_{xx}=20\) degrees squared.

$$ b_1=\frac{-40}{20}=-2\text{ heating-use units per degree}. $$

The slope is negative: the fitted heating use decreases by 2 units for each one-degree increase in temperature. The covariance check gives \(s_{xy}=-40/3=-13.333\) degree-units and \(s_x^2=20/3=6.667\) degrees squared. Their ratio is \((-13.333)/6.667=-2\). The sign follows from the centered products: observations with temperatures below the mean tend to have heating use above its mean, and those above the temperature mean tend to have heating use below its mean.

Worked Example: A Slope From Five Paired Measurements

Suppose a hypothetical set of five plants has water supplied \(x\) in liters and height increase \(y\) in centimeters. The pairs are \((2,8)\), \((4,7)\), \((5,9)\), \((7,11)\), and \((12,15)\). Find the slope and verify the covariance relationship.

The predictor mean is \(\bar{x}=30/5=6\) liters, and the response mean is \(\bar{y}=50/5=10\) centimeters. The centered predictor values are \(-4,-2,-1,1,6\), and the centered response values are \(-2,-3,-1,1,5\). Multiplying corresponding deviations gives \(8,6,1,1,30\), so \(S_{xy}=46\) liter-centimeters. Squaring the centered predictor values gives \(16,4,1,1,36\), so \(S_{xx}=58\) liters squared.

$$ b_1=\frac{46}{58}\approx0.7931\text{ centimeters per liter}. $$

Here \(n-1=4\). Thus \(s_{xy}=46/4=11.5\) liter-centimeters and \(s_x^2=58/4=14.5\) liters squared, giving \(s_{xy}/s_x^2=11.5/14.5\approx0.7931\) centimeters per liter. The same slope results because both sample summaries divide their centered sums by the same number.

Reading the Formula Carefully

The numerator records whether centered \(x\) and centered \(y\) values tend to have matching or opposite signs, and how large their products are. When observations with above-average \(x\) tend also to have above-average \(y\), and below-average values tend to occur together, the numerator tends to be positive. When above-average \(x\) tends to accompany below-average \(y\), it tends to be negative. The denominator is always positive when \(x\) varies, so the sign of the slope is the sign of the centered cross-product sum.

The denominator also explains why the predictor must vary. If every \(x_i=\bar{x}\), then \(S_{xx}=0\), and the formula would involve division by zero. In that case the observed data contain no different predictor values from which to estimate a rate of change. This is not a slope of zero; the least-squares slope is not uniquely determined from those predictor values.

The slope is also not the correlation. Correlation divides covariance by the product of the sample standard deviations of both variables, producing a unitless measure. The regression slope divides covariance only by the predictor variance, preserving response-per-predictor units. A large or small numerical slope can depend on measurement units, whereas correlation does not.

Common Mistake: Using the Wrong Denominator

A frequent error is to divide the centered cross-product sum by the predictor standard deviation, or to divide covariance by the predictor standard deviation. For the first worked example, the denominator in the slope formula is \(S_{xx}=5\) hours squared, not \(\sqrt{5}\) hours. Likewise, the covariance form is \(s_{xy}/s_x^2\), not \(s_{xy}/s_x\). The distinction matters both numerically and dimensionally: covariance divided by standard deviation has units of response times predictor, divided by predictor, or response units, rather than response per predictor unit. Always use the predictor variance in the covariance ratio.

Key takeaway: The least-squares slope is the centered cross-product sum divided by the predictor’s centered sum of squares. Equivalently, it is sample covariance divided by sample variance of the predictor. The formula requires variation in \(x\), and the resulting slope has units of \(y\) per unit of \(x\).

Check Your Understanding

Use centered deviations and the definitions in this tutorial to answer the questions.

  1. In the slope formula, what does the numerator summarize about the paired observations?
  2. Why does dividing sample covariance by sample variance give the same result as dividing the centered sums?
  3. If \(S_{xy}<0\) and \(S_{xx}>0\), what is the sign of the least-squares slope, and why?
  4. Suppose \(s_{xy}=12\) meter-seconds and \(s_x^2=3\) seconds squared. Calculate the slope and state its units.
  5. Why can the slope not be calculated from this formula when every observed predictor value is identical?