Tutorials › High-Dimensional Genomic Data Analysis › Residual Variance and Residual Degrees of Freedom

Linear Models for Expression Microarrays · Tutorial 389 of 795

Residual Variance and Residual Degrees of Freedom

Learn how to calculate residual variance for each gene, why its denominator is set by design rank, and when the estimate is unavailable or unreliable.

Intermediate 10 min read

What You'll Learn

  • Write the gene-wise linear model and distinguish its residual variance estimate from the error variance.
  • Calculate residual sums of squares and residual degrees of freedom from fitted values.
  • Explain why the design-matrix rank, rather than its number of columns, determines residual degrees of freedom.
  • Derive the unbiasedness and, under normal errors, the sampling distribution of the residual variance estimate.
  • Recognize saturated or rank-deficient designs and diagnose when residual variance estimates are unreliable.

Why Residual Variance Depends on the Design

A gene-wise linear model separates systematic expression differences, described by the design, from remaining sample-to-sample variation. The residual variance estimate summarizes that remaining variation for a particular gene. It also depends on how much information the fit used: estimating more independent model directions leaves fewer residual degrees of freedom.

This distinction matters in expression analysis because each gene has its own observed residual variation, while the sample design is shared across genes. The fitted values and residual sum of squares vary by gene; if all genes use the same design and observations, the residual degrees of freedom are the same. The earlier material on contrasts describes questions about model coefficients; here the focus is how the fit supplies a residual variance estimate and how its degrees of freedom are determined.

Definition: For gene \(g\), let \(y_g\) be the vector of its expression measurements from \(n\) samples, and let \(X\) be the \(n \times q\) design matrix. The linear model is \(y_g=X\beta_g+\epsilon_g\). The least-squares fitted vector is \(\hat{y}_g\), the residual vector is \(e_g=y_g-\hat{y}_g\), and the residual sum of squares is \(\mathrm{RSS}_g=e_g'e_g\). If \(r=\operatorname{rank}(X)\) and \(n>r\), the residual degrees of freedom are \(df_{\mathrm{res}}=n-r\), and the residual variance estimate is \(s_g^2=\mathrm{RSS}_g/(n-r)\).

The entries of \(y_g\), \(\hat{y}_g\), and \(e_g\) are expression values, often on a transformed scale such as log base 2. Accordingly, \(s_g^2\) is in squared expression units. Its square root \(s_g\) is in expression units and describes the estimated residual standard deviation. The residual variance is not the variance of the fitted values or the total variation in the observations: it is the variation left after fitting the specified design.

Model Conditions and the Least-Squares Residuals

For a gene-wise model with a common residual variance, assume the errors have mean zero and covariance matrix \(\sigma_g^2 I\). This means the observations have equal error variance and uncorrelated errors under the model. For exact normal-theory results, additionally assume \(\epsilon_g\sim N(0,\sigma_g^2 I)\). The normality assumption is not needed to define the least-squares fit or calculate its residual sum of squares.

When \(X\) has full column rank, the least-squares coefficient estimate is \(\hat{\beta}_g=(X'X)^{-1}X'y_g\), and the fitted values are \(\hat{y}_g=X\hat{\beta}_g\). If columns of \(X\) are redundant, this coefficient formula using an ordinary inverse does not apply. Least-squares fitted values are still defined as the projection of \(y_g\) onto the column space of \(X\), and the rank \(r\) counts the independent directions in that space.

The residuals are orthogonal to the fitted model space: \(X'e_g=0\). Thus, when \(X\) is full rank, fitting its \(q\) independent columns imposes \(q\) independent constraints on the \(n\) residuals. More generally there are \(r\) independent constraints, even if the design contains more than \(r\) columns. The residuals therefore lie in a space of dimension \(n-r\).

Deriving the Residual Variance Estimate

Let \(P_X\) denote the projection matrix onto the column space of \(X\), and let \(M_X=I-P_X\) denote the projection onto the residual space. These matrices are symmetric and idempotent, and \(P_X\) has rank \(r\). The residual vector is \(e_g=M_Xy_g\). Since \(M_XX=0\), the model component vanishes from the residuals:

$$ \mathrm{RSS}_g=e_g'e_g =y_g'M_Xy_g =\epsilon_g'M_X\epsilon_g. $$

Under the equal-variance, uncorrelated-error assumption, the expected residual sum of squares is obtained by taking the trace of the residual projection:

$$ E(\mathrm{RSS}_g) =E(\epsilon_g'M_X\epsilon_g) =\sigma_g^2\operatorname{tr}(M_X) =\sigma_g^2(n-r). $$

The final equality follows because \(M_X\) has \(n-r\) eigenvalues equal to 1 and \(r\) equal to 0, so its trace is \(n-r\). Dividing by this expectation factor gives \(E(s_g^2)=\sigma_g^2\). Thus \(s_g^2=\mathrm{RSS}_g/(n-r)\) is unbiased under the stated covariance assumptions. Using \(n\) or \(n-q\) in the denominator instead would generally not produce this unbiased estimate.

Under the additional normality assumption, rotate the errors into an orthonormal basis whose first \(r\) directions span the model space and whose remaining \(n-r\) directions span the residual space. In those residual directions, the standardized errors are independent standard normal random variables, and the residual sum of squares is their squared sum multiplied by \(\sigma_g^2\). Therefore:

$$ \frac{\mathrm{RSS}_g}{\sigma_g^2}\sim\chi^2_{n-r}, \qquad \frac{(n-r)s_g^2}{\sigma_g^2}\sim\chi^2_{n-r}. $$

This exact distribution relies on normal, independent errors with a common variance. Without normality, the formula for \(s_g^2\) still defines the least-squares residual variance estimate, and it remains unbiased under the stated covariance assumption, but the chi-squared distribution need not hold.

Worked Example: Calculating a Gene’s Residual Variance

Suppose a hypothetical gene has log base 2 expression values \(1.8, 2.8, 4.8, 3.8, 5.8\) in five samples. A model includes an intercept and a numeric predictor \(x=0,1,2,3,4\). The fitted line is \(\hat{y}=2+0.9x\). The design has two independent columns, so \(n=5\), \(r=2\), and \(df_{\mathrm{res}}=5-2=3\).

Applying the fitted line to each predictor value gives fitted values \(2.0, 2.9, 3.8, 4.7, 5.6\). Subtracting these from the observed values gives residuals \(-0.2,-0.1,1.0,-0.9,0.2\). Their squared sum is:

$$ \mathrm{RSS}_g=(-0.2)^2+(-0.1)^2+(1.0)^2+(-0.9)^2+(0.2)^2 =0.04+0.01+1.00+0.81+0.04=1.90. $$

The residual variance estimate is \(s_g^2=1.90/3=0.6333\) squared log base 2 expression units. Its square root is \(s_g\approx0.7958\) log base 2 expression units. The calculation uses the three residual degrees of freedom, not all five observations, because two independent directions were used to fit the intercept and slope.

How Design Rank Determines Residual Information

The rank of \(X\) is the number of linearly independent columns, or equivalently the dimension of the model space. With a full-rank design, this equals the number of columns \(q\). With redundant columns, rank is smaller than \(q\); the fitted values remain identifiable as a projection, but some individual coefficients cannot be uniquely determined. In either case, the residual degrees of freedom are \(n-r\).

For a group-means design with \(k\) represented groups and an intercept-style group parameterization, the model space has rank \(k\). If there are \(n\) observations in total, residual degrees of freedom are \(n-k\): within-group deviations remain after estimating the group means. This count depends on the observations and the independent model directions, not on the number of genes.

Worked Example: Residual Variance in a Two-Group Design

Consider hypothetical log-expression values \(5,6,7\) for three samples in group A and \(8,9,10\) for three samples in group B. The model has one fitted mean per group. There are \(n=6\) observations, and the two group-indicator columns are independent, so \(r=2\) and \(df_{\mathrm{res}}=6-2=4\).

The fitted value for each A sample is its group mean, \((5+6+7)/3=6\), and the fitted value for each B sample is \((8+9+10)/3=9\). The within-group residuals are \(-1,0,1\) in each group. Therefore:

$$ \mathrm{RSS}_g=[(-1)^2+0^2+1^2]+[(-1)^2+0^2+1^2]=2+2=4, \qquad s_g^2=\frac{4}{4}=1. $$

The residual variance estimate is \(1\) squared log-expression units, and the residual standard deviation is \(1\) log-expression unit. The four residual degrees of freedom correspond to the two within-group deviations left after fitting the mean in each group.

Worked Example: Redundant Columns Do Not Use Extra Degrees of Freedom

Suppose four hypothetical samples have expression values \(4,5\) in a control group and \(7,8\) in a treated group. A design matrix is formed with an intercept, a treatment indicator, and a second copy of that treatment indicator. It has three columns but rank \(r=2\), because the two indicator columns are identical. Thus \(df_{\mathrm{res}}=4-2=2\), not \(4-3=1\).

The least-squares fitted values are the group means \(4.5,4.5,7.5,7.5\). The residuals are \(-0.5,0.5,-0.5,0.5\), giving:

$$ \mathrm{RSS}_g=4(0.5^2)=1, \qquad s_g^2=\frac{1}{2}=0.5000. $$

The duplicate column makes the coefficient representation nonunique: different coefficient pairs can give the same treatment effect. It does not add a new fitted direction, change the fitted values, or consume another residual degree of freedom.

When There Is Too Little Residual Information

If \(n=r\), the design is saturated: its model space fills the observation space. Least squares can then fit every possible response vector exactly, leaving \(\mathrm{RSS}_g=0\) and \(df_{\mathrm{res}}=0\). The quotient \(\mathrm{RSS}_g/df_{\mathrm{res}}\) is undefined, so the model provides no residual variance estimate. A zero residual sum of squares in this setting is not evidence that the measurement process has no variability; it is a consequence of having no residual directions.

Even when \(df_{\mathrm{res}}>0\), a very small value means the variance estimate is based on little residual information and can be unstable. Under the normal model, the chi-squared distribution above makes this variability explicit: with few degrees of freedom, the residual sum of squares can fluctuate substantially around its expectation. The number of genes does not add residual degrees of freedom to a gene-specific fit; each gene’s \(s_g^2\) is calculated from that gene’s sample residuals.

Rank also reflects the actual design and observations used. If an intended group has no samples, its effect cannot be estimated from that dataset as a distinct direction. If samples are removed, \(n\) may decrease; if the retained design loses independent columns, \(r\) may also decrease. Recalculate the rank and the residual degrees of freedom for the data actually fitted rather than assuming that the planned column count still applies.

Diagnostics, Alternatives, and Common Mistakes

Useful checks begin with the fit itself. Verify that the reported residual degrees of freedom equal \(n-\operatorname{rank}(X)\), inspect the design for duplicate or dependent columns, and confirm that the residuals are orthogonal to the model columns up to numerical precision. Residual plots can reveal structure left unexplained by the model, such as a trend across a predictor or markedly different spread across groups. Such patterns call into question whether a common-variance linear error model is adequate. A residual plot cannot establish normality or independence by itself, and technical or biological dependence must be considered from the study design.

The ordinary gene-wise estimate uses only that gene’s residual sum of squares and divides by its residual degrees of freedom. This is directly interpretable but can be unstable when the residual degrees of freedom are small. Other approaches can model unequal variances, dependence, or share information across genes, but they require additional assumptions and are not the same calculation as the ordinary residual variance estimate. In particular, borrowing information across genes does not change the rank-based residual degrees of freedom of this least-squares fit.

  • Using the number of columns instead of rank. The wrong reasoning is that every written column costs one degree of freedom. The correction is \(df_{\mathrm{res}}=n-r\); redundant columns do not add independent fitted directions.
  • Dividing by \(n\). This treats the fitted model as if it used no information. Under the stated assumptions, divide by \(n-r\) to obtain the unbiased residual variance estimate.
  • Calling a saturated fit’s zero RSS a zero variance estimate. With \(n=r\), the residual variance formula has a zero denominator and is undefined, regardless of the fitted RSS.
  • Interpreting residual variance as total expression variability. It measures variation around the fitted model, in squared expression units; variation explained by the model is not part of the residual sum of squares.
Key takeaway: For each gene, calculate the residual sum of squares from its least-squares fit and divide by \(n-\operatorname{rank}(X)\), provided this number is positive. The residual degrees of freedom count independent directions left after fitting the design.

Check Your Understanding

Answer each question, then compare with the brief solutions.

  1. A gene has \(n=8\) observations and a design matrix with six columns but rank four. What are its residual degrees of freedom?
  2. A hypothetical fit has residual sum of squares \(6.0\) and \(df_{\mathrm{res}}=3\). Calculate its residual variance estimate and state its units if expression is measured in log base 2 units.
  3. Why does adding a duplicate column to a design not reduce the residual degrees of freedom by one?
  4. What happens to the ordinary residual variance estimate when \(n=r\), and why?

Solutions. (1) \(df_{\mathrm{res}}=8-4=4\); use rank, not the six-column count. (2) \(s_g^2=6.0/3=2.0\) squared log base 2 expression units. (3) A duplicate column adds no independent direction, so the design rank and the dimension of the residual space are unchanged. (4) The residual degrees of freedom are zero, so \(\mathrm{RSS}_g/df_{\mathrm{res}}\) is undefined; a saturated fit supplies no residual directions for estimating variance.

Clinical Biostats