Why the Number of Observations Is Not the Degrees of Freedom
A simple linear regression uses the observed data to estimate a line. That line has two fitted parameters: an intercept and a slope. Because the line is chosen using the same observations whose residuals are then examined, not all \(n\) residuals can vary freely. Understanding this loss of freedom explains why slope inference uses a \(t\) distribution with \(n-2\) degrees of freedom, rather than one with \(n\) degrees of freedom.
The count matters because the residuals are used to estimate the unknown spread of the regression errors. That estimate appears in the standard error of the fitted slope. The degrees of freedom therefore determine which reference distribution is appropriate; they are not just a bookkeeping convention.
Two Fitted Parameters, Two Constraints
Write the simple linear regression model as
Here \(x_i\) is the predictor value for observation \(i\), \(\beta_0\) is the population intercept, and \(\beta_1\) is the population slope. The random error \(\varepsilon_i\) represents the difference between the response and its population mean at \(x_i\). The fitted line uses the data to estimate the intercept and slope, conventionally written \(b_0\) and \(b_1\). The residual for observation \(i\) is \(e_i=y_i-(b_0+b_1x_i)\).
When the least-squares line includes an intercept, its residuals satisfy two equations:
The first says the residuals sum to zero; the second says they have zero sample cross-product with the predictor values. These properties follow from the least-squares fitting process: changing the fitted intercept or slope slightly cannot further reduce the sum of squared residuals. When the predictor varies, the two constraints are independent. Thus, although there are \(n\) residuals, only \(n-2\) of them can vary freely once the line has been fitted.
This dimension count is the basic reason for \(n-2\). It is different from a situation in which only one parameter, a mean, is estimated. Estimating a mean imposes one constraint on the residuals, leaving \(n-1\) residual degrees of freedom. A regression line estimates both an intercept and a slope, so it uses two.
Worked Example: Checking the Two Residual Constraints
Suppose a hypothetical data set has predictor values \(x=0,1,2,3,4,5\), and its fitted line is \(\hat{y}=10+2x\). The observed responses are \(11,10,15,17,16,21\). Find the residuals, check the two constraints, and count the residual degrees of freedom.
Subtract the fitted response from each observed response. In order of increasing \(x\), the residuals are \(1,-2,1,1,-2,1\). Their sum is
Their cross-product sum with the predictor values is
Both constraints hold. There are \(n=6\) observations and two fitted parameters, so the residual degrees of freedom are \(6-2=4\). The six residuals are not six independent pieces of information about error variation: they must satisfy the two equations above.
From Residual Degrees of Freedom to the \(t\) Distribution
The degrees of freedom enter slope inference through the estimate of the error standard deviation. Define the residual sum of squares as \(SSE=\sum_{i=1}^{n}e_i^2\). In simple linear regression, the usual estimate of the error variance is \(s^2=SSE/(n-2)\), and the estimate of its standard deviation is \(s=\sqrt{SSE/(n-2)}\). The slope standard error is \(SE(b_1)=s/\sqrt{S_{xx}}\), where \(S_{xx}=\sum_{i=1}^{n}(x_i-\bar{x})^2\). These formulas rely on the same \(n-2\) residual degrees of freedom.
For the exact \(t\) result, the model assumptions must be stronger than simply having a fitted line. Conditional on the observed predictor values, the errors must be independent and normally distributed with mean zero and a common variance \(\sigma^2\). The predictor values must vary, so the intercept and slope can both be estimated. Under these conditions, the fitted slope has a normal sampling distribution, while the residual-based estimate \(s\) is random. The ratio that uses \(s\), rather than the unknown \(\sigma\), follows a \(t\) distribution.
A \(t\) distribution with \(df\) degrees of freedom can be defined as the distribution of \(Z/\sqrt{U/df}\), where \(Z\) has a standard normal distribution, \(U\) has a chi-square distribution with \(df\) degrees of freedom, and \(Z\) and \(U\) are independent. For the regression slope, the relevant \(df\) is \(n-2\): the normal part comes from the slope estimate, and the chi-square part comes from the residual sum of squares.
Why the Degrees of Freedom Are \(n-2\): A Justification
The geometric argument makes the result precise. Treat the observed responses as a vector \(y\) of length \(n\). The two columns of the regression design matrix represent the intercept and predictor. If the predictor varies, those columns are linearly independent, so fitting the line projects \(y\) onto a two-dimensional space. The residual vector lies in the perpendicular space, whose dimension is \(n-2\).
Under the stated normal-error assumptions, the error vector has independent normal components with variance \(\sigma^2\). Projecting it onto the residual space gives \(n-2\) independent normal components, each with variance \(\sigma^2\). Squaring and summing those components gives the residual sum of squares. Consequently,
The slope estimate depends on the error component in the fitted two-dimensional space. The residual sum of squares depends on the error component in the perpendicular residual space. These are orthogonal projections. For a normally distributed error vector, the projections are independent, so the slope estimate and \(s^2\) are independent. Together, these facts produce the \(t\) distribution.
To see the ratio directly, let \(\beta_1\) denote the true population slope. Under the model, \((b_1-\beta_1)/(\sigma/\sqrt{S_{xx}})\) is standard normal. Also, \(s^2/\sigma^2\) is a chi-square random variable with \(n-2\) degrees of freedom divided by \(n-2\), and it is independent of that standardized slope. Replacing \(\sigma\) by \(s\) therefore gives
where \(Z\) and \(U\) have the distributions and independence stated above. This explains why slope inference uses a \(t\) distribution with \(n-2\) degrees of freedom. It is the combination of estimating two regression parameters, estimating error spread from the remaining residual variation, and the normal-error model that gives this result.
Worked Example: Calculating the Slope Standard Error with Four Observations
Consider hypothetical observations \((x,y)=(1,3),(2,4),(3,4),(4,5)\). Fit the simple linear regression line, find the residual degrees of freedom, and calculate the slope standard error.
The means are \(\bar{x}=2.5\) and \(\bar{y}=4\). The sum of squared deviations in the predictor is
The sum of cross-products is \((-1.5)(-1)+(-0.5)(0)+(0.5)(0)+(1.5)(1)=3\). Thus \(b_1=3/5=0.6\), and \(b_0=\bar{y}-b_1\bar{x}=4-(0.6)(2.5)=2.5\). The fitted line is \(\hat{y}=2.5+0.6x\).
The fitted values are \(3.1,3.7,4.3,4.9\), so the residuals are \(-0.1,0.3,-0.3,0.1\). Therefore
Here \(n=4\), so the residual degrees of freedom are \(4-2=2\). The estimated error standard deviation is \(s=\sqrt{0.20/2}=\sqrt{0.10}\approx0.3162\), and the slope standard error is \(SE(b_1)=0.3162/\sqrt{5}\approx0.1414\). Under the normal-error assumptions, slope inference for this fit uses a \(t\) distribution with \(df=2\), not \(df=4\).
Worked Example: Counting Degrees of Freedom in a Larger Sample
A hypothetical regression relates a measured response to one predictor for \(n=12\) observations. Assume the predictor varies and the model includes an intercept. How many residual degrees of freedom are available for estimating error variation?
The fit estimates two parameters, \(b_0\) and \(b_1\), so the calculation is
There are twelve residuals, but they obey the two least-squares constraints: their sum is zero, and their cross-product sum with the predictor values is zero. Thus only ten residual degrees of freedom remain. If the usual independent, constant-variance, normal-error assumptions hold, \(df=10\) is also the degrees of freedom for the \(t\) distribution used in slope inference. The count itself does not establish those assumptions; it follows from the model structure and the two fitted parameters.
When \(n-2\) Does Not Apply
The count \(n-2\) assumes the regression design has two independent columns: an intercept column and a predictor column that is not constant. If every observation has the same predictor value, the slope cannot be separated from the intercept. There is no ordinary fitted slope with the usual standard error, so it would be incorrect to mechanically assign \(n-2\) degrees of freedom to slope inference. The same principle applies more generally: residual degrees of freedom equal the number of observations minus the number of independently estimable regression parameters.
The exact \(t\) distribution also depends on the error assumptions. The degrees-of-freedom count comes from the fitting structure, but normality, independence, and common variance justify the exact distributional result. If those assumptions are not reasonable, the number \(n-2\) alone does not guarantee that the usual \(t\)-based inference is accurate.
Common Mistake
A common mistake is to say, “There are \(n\) observations, so the slope statistic has \(n\) degrees of freedom.” That reasoning overlooks that the fitted line used the observations to estimate both its intercept and slope. Its residuals satisfy two independent constraints, leaving only \(n-2\) residual degrees of freedom. Another mistake is to subtract two parameters and assume the \(t\) result always follows. The count is necessary, but the exact \(t\) distribution also relies on the regression error assumptions described above.
Check Your Understanding
Use the answers to check whether you can connect the fitted line, residual degrees of freedom, and the \(t\) distribution.
- A regression with an intercept uses \(n=15\) observations and a varying predictor. How many residual degrees of freedom are there, and which two fitted parameters account for the reduction?
- Why do the least-squares residuals satisfy both \(\sum e_i=0\) and \(\sum x_i e_i=0\)?
- What assumptions give the exact \(t_{n-2}\) distribution for the standardized slope estimate?
- If the predictor is constant for every observation, why is it not enough to calculate \(n-2\) and proceed with ordinary slope inference?
- Why does having \(n-2\) residual degrees of freedom by itself not guarantee that the usual slope \(t\) inference is accurate?