Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Estimating a Smooth Mean-Variance Trend

Highly Variable Genes and Feature Selection · Tutorial 112 of 700

Estimating a Smooth Mean-Variance Trend

Use a smooth mean–variability curve to distinguish genes with unusually high dispersion from genes whose variability is expected at their expression level.

Intermediate 12 min read

What You'll Learn

  • Define the gene-level mean and dispersion used to fit a trend.
  • Fit and interpret a smooth curve on the log-mean, log-dispersion scale.
  • Calculate residuals that measure variability relative to the fitted expectation.
  • Recognize assumptions, diagnostic warning signs, and smoothing limitations.
  • Distinguish excess variability from high expression and from differential expression.

Why Estimate a Mean–Variance Trend?

Genes with higher average expression often have larger observed variability simply because they are more highly expressed. Ranking genes by variance alone can therefore favor abundant genes, even when their variability is ordinary for that expression level. A smooth mean–variability trend provides a reference: it estimates the typical variability associated with each gene’s average expression, so that unusually variable genes can be identified by their departures from that reference.

The goal is descriptive feature ranking, not a hypothesis test. A large positive departure says that a gene is more variable across the observed cells than the fitted trend predicts at its mean expression. It does not, by itself, identify the source of that variability or show that the gene is differentially expressed.

Gene-Level Summaries and the Trend

Let \(Y_{ig}\) denote the normalized expression of gene \(g\) in cell \(i\), for \(n\) cells. Assume that the same set of cells and the same expression scale are used to summarize the genes being compared. Define the gene’s average expression as \(\bar{Y}_g\), and its sample variance across cells as \(s_g^2\). Following the mean-adjusted dispersion convention, define \(D_g=s_g^2/\bar{Y}_g\) when \(\bar{Y}_g>0\). Both summaries are calculated across cells for each gene.

Definition: A smooth mean–dispersion trend is a fitted function describing how the typical dispersion \(D_g\) changes with mean expression \(\bar{Y}_g\). A gene’s trend residual compares its observed dispersion with the fitted value at its own mean. A positive residual indicates dispersion above the fitted expectation; a negative residual indicates dispersion below it.

A common choice is to work on logarithmic scales. Set \(x_g=\log(\bar{Y}_g)\) and \(z_g=\log(D_g)\) for positive means and dispersions, then fit a smooth function \(f\) so that \(z_g\) is approximately \(f(x_g)\) plus a residual. The fitted dispersion on the original scale is \(\widehat{D}_g=\exp\{\widehat{f}(x_g)\}\). A log residual is

$$ r_g=\log(D_g)-\widehat{f}\{\log(\bar{Y}_g)\} =\log\left(\frac{D_g}{\widehat{D}_g}\right). $$

Thus, the residual has a relative interpretation: \(\exp(r_g)=D_g/\widehat{D}_g\) is the observed dispersion divided by the fitted dispersion. For example, \(r_g=\log(1.5)\) means the observed dispersion is 1.5 times the fitted value. Log residuals treat proportional differences symmetrically: being twice the trend corresponds to \(\log(2)\), while being half the trend corresponds to \(\log(0.5)=-\log(2)\).

This transformation requires positive values. If some estimated dispersions are zero, a defined alternative is \(z_g=\log(D_g+\delta)\), where \(\delta>0\) is a small, fixed offset chosen on the dispersion scale before fitting. The resulting residual compares \(D_g+\delta\) with the fitted value on that shifted scale; it is not exactly a log ratio of the unshifted dispersions. The offset and its effect on rankings should be reported and checked. Genes with zero mean cannot be placed on the log-mean scale and should not be included in this fit.

Fitting a Smooth Curve

A local polynomial smoother, such as LOESS, estimates the trend from nearby genes rather than imposing one global straight line. For a target location \(x_0\), it assigns each gene a weight that typically decreases with its distance from \(x_0\), then fits a low-degree polynomial to the weighted observations \((x_g,z_g)\). A local linear fit can adapt its slope across the expression range. The neighborhood width, often called the span or bandwidth, controls smoothness: a wider neighborhood gives a steadier but less flexible trend, while a narrow one follows local fluctuations more closely.

Here is the local linear calculation. Write \(u_g=x_g-x_0\), let \(w_g\) be the local weight for gene \(g\), and approximate the trend near \(x_0\) by \(a+b u_g\). The weighted least-squares criterion is \(\sum_g w_g(z_g-a-bu_g)^2\). Define \(S_k=\sum_g w_g u_g^k\) for \(k=0,1,2\), \(T_0=\sum_g w_g z_g\), and \(T_1=\sum_g w_g u_gz_g\). Setting the derivatives of the criterion with respect to \(a\) and \(b\) to zero gives the two normal equations \(S_0a+S_1b=T_0\) and \(S_1a+S_2b=T_1\). Solving them gives the fitted value at the target, which is the intercept \(a\):

$$ \widehat{f}(x_0)=a= \frac{S_2T_0-S_1T_1}{S_0S_2-S_1^2}, \qquad b=\frac{S_0T_1-S_1T_0}{S_0S_2-S_1^2}. $$

This calculation is defined when the local observations provide enough variation in \(u_g\) for \(S_0S_2-S_1^2>0\). At the edges of the observed mean range, there may be fewer useful neighbors on one side, making the estimate less stable. The smoother estimates a typical location on the transformed scale; it does not guarantee that every gene’s residual has constant variance or that residuals follow a normal distribution.

Worked Example: Calculating a Relative Dispersion Residual

Suppose a hypothetical gene has mean normalized expression \(\bar{Y}_g=4\) units and dispersion \(D_g=2.4\). The fitted smooth trend at mean expression 4 is \(\widehat{D}_g=1.5\). Calculate and interpret its log residual.

Using the log-ratio formula,

$$ r_g=\log\left(\frac{2.4}{1.5}\right) =\log(1.6)\approx 0.470. $$

The exponential of the residual is \(\exp(0.470)\approx1.60\), so the observed dispersion is about 1.60 times the fitted dispersion at this expression level. The gene is more variable than the smooth trend predicts, but this calculation alone does not provide a p-value or establish why the gene is variable.

Worked Example: A Local Linear Trend Calculation

Consider three hypothetical nearby reference genes with log means \(x_g=-1,0,1\), log dispersions \(z_g=0.2,0.4,0.8\), and local weights \(1,2,1\), respectively. Calculate the local linear trend at \(x_0=0\). To illustrate a candidate’s departure without its value influencing the fit, suppose the candidate is held out and has mean expression corresponding to \(x_0=0\) and dispersion \(D=2.5\).

Here \(u_g=x_g-x_0\), so the three \(u_g\) values are \(-1,0,1\). The weighted sums are \(S_0=1+2+1=4\), \(S_1=(-1)(1)+(0)(2)+(1)(1)=0\), and \(S_2=1(1)+0+1(1)=2\). Also, \(T_0=(1)(0.2)+(2)(0.4)+(1)(0.8)=1.8\) and \(T_1=(1)(-1)(0.2)+(2)(0)(0.4)+(1)(1)(0.8)=0.6\).

Therefore, the fitted log dispersion at \(x_0=0\) is

$$ \widehat{f}(0)=\frac{(2)(1.8)-(0)(0.6)}{(4)(2)-(0)^2} =\frac{3.6}{8}=0.45. $$

The fitted dispersion is \(\exp(0.45)\approx1.568\). For the held-out candidate, the residual is

$$ r=\log(2.5)-0.45\approx0.466, \qquad \exp(r)=\frac{2.5}{1.568}\approx1.594. $$

On this illustrative reference trend, the candidate’s dispersion is about 1.59 times the fitted value. In an ordinary analysis, the trend is generally fitted across the genes being assessed, so each gene may contribute to its own fitted value. A candidate with strong influence on a local fit can pull the trend toward itself and reduce its own residual; influence checks or leave-one-out calculations can help diagnose this issue.

Choosing and Checking the Smoother

The trend is meaningful only if the gene summaries are comparable. Before fitting, the expression values should be on a scale that makes cell-to-cell comparisons appropriate. Normalization should have addressed systematic library-size differences, and filtering should remove genes whose low mean makes their summaries too unstable for the intended comparison. Remaining batch, quality, or composition effects can still create variability that the curve will treat as part of the observed pattern.

The smoother also relies on a practical assumption: across the range being analyzed, the typical transformed dispersion changes smoothly enough for a local approximation to be useful. It does not require a particular parametric mean–variance formula, but it can be unreliable where few genes provide information, especially at the extremes of mean expression. Genes are not independent biological replicates for this purpose; they are points used to estimate a descriptive cross-gene trend. Residual size is therefore not, on its own, a calibrated measure of statistical significance.

Useful diagnostics include plotting log dispersion against log mean with the fitted curve overlaid, and plotting residuals against log mean. Look for systematic curvature in the residual plot, a trend that follows isolated extreme genes, and abrupt changes caused by sparse support at either endpoint. Compare reasonable smoother spans: if small span changes substantially reorder candidates, the ranking is sensitive to the fitting choice. A trend that looks smooth overall can still leave structured residuals, which may indicate an unsuitable expression scale, unremoved technical effects, or groups of genes with different variability patterns.

Weights can be used when there is a defensible estimate of the precision of each gene’s dispersion. In practice, that precision can be difficult to estimate, especially for sparse genes. Unweighted smoothing gives each included gene a direct role; precision weighting can reduce the influence of noisy estimates, but incorrect weights can distort the trend. The weighting rule should be specified and checked rather than chosen solely to obtain a preferred set of candidates.

Worked Example: Why Raw Dispersion Can Mislead

Two hypothetical genes have different means and dispersions. Gene A has mean expression 1 unit, observed dispersion 1.2, and fitted dispersion 0.8. Gene B has mean expression 8 units, observed dispersion 4.0, and fitted dispersion 3.0. Compare their raw dispersions and their relative departures from the trend.

Gene B has the larger raw dispersion: \(4.0>1.2\). But the log residuals are

$$ r_A=\log(1.2/0.8)=\log(1.5)\approx0.405, \qquad r_B=\log(4.0/3.0)=\log(1.333)\approx0.288. $$

Gene A is 1.5 times its fitted dispersion, while Gene B is about 1.33 times its fitted dispersion. Thus A has the larger relative excess, although B has the larger raw dispersion. This comparison does not mean A is more variable in every biological sense; it means A departs more from the fitted mean–dispersion relationship.

Alternatives, Limitations, and Common Mistakes

A parametric mean–variance model, such as a negative-binomial model for counts, specifies a particular functional form for how variability changes with the mean. It can be more efficient and interpretable when its assumptions fit the data, but misspecification can leave systematic departures. A smooth trend is more flexible and can capture shapes not specified in advance, at the cost of depending on smoothing choices and adequate gene coverage across the mean range. Ranking genes by raw variance is simpler, but does not adjust for the systematic mean–variability relationship.

The trend residual is a relative, conditional description, not a biological label. Excess dispersion may reflect genuine cell-state heterogeneity, technical artifacts, residual normalization effects, or a mixture of these. Conversely, a gene with a small residual can still have biologically important expression differences among subsets of cells. The trend does not establish causality, identify cell types, or replace downstream modeling of a specific scientific question.

  • Mistake: treating a large raw dispersion as evidence of unusually high variability. Correction: compare it with the fitted dispersion at that gene’s mean; a high value may be ordinary for a highly expressed gene.
  • Mistake: interpreting a positive residual as a significance test. Correction: it is a departure from a descriptive fitted curve. A calibrated test would require an appropriate sampling model and uncertainty assessment.
  • Mistake: choosing a very flexible smoother because it follows every point. Correction: an overly narrow neighborhood can fit noisy gene-level estimates and absorb the very departures of interest. Check residual structure and ranking sensitivity across reasonable spans.
  • Mistake: calling a highly variable gene differentially expressed. Correction: variability across cells and a difference in average expression between specified groups are distinct questions.
Key takeaway: Fit a smooth relationship between log mean expression and log dispersion, then interpret each residual as a relative departure from the expected dispersion at that mean. The residual is useful for ranking, but its reliability depends on comparable expression summaries, adequate support, and a trend that is neither too rigid nor too wiggly.

Check Your Understanding

Answer each question using the trend and residual definitions above. Solutions are included after the questions.

  1. A gene has observed dispersion 3.0 and fitted dispersion 2.0. What is its log residual, and what does the exponentiated residual mean?
  2. Why can a smoother with an extremely narrow span produce misleading residual rankings?
  3. Two genes have residuals \(\log(2)\) and \(\log(0.5)\). Interpret each relative to its fitted trend.
  4. Does a positive residual show that a gene is differentially expressed between two conditions? Explain.

Solutions. (1) \(r=\log(3.0/2.0)=\log(1.5)\approx0.405\). Its observed dispersion is 1.5 times the fitted dispersion at its mean expression. (2) A very narrow span can track noisy gene-level estimates rather than the underlying smooth pattern, making the fit sensitive to individual genes and potentially shrinking or reversing their apparent departures. (3) The first gene has twice the fitted dispersion; the second has half the fitted dispersion. (4) No. A positive residual describes dispersion above a cross-gene trend, not a difference in mean expression between specified conditions; those are separate statistical questions.

Clinical Biostats