Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Residual Diagnostics for Pseudobulk Models

Differential Expression With Pseudobulk and Mixed Models · Tutorial 504 of 700

Residual Diagnostics for Pseudobulk Models

Use deviance and randomized quantile residuals to identify structured discrepancies between observed pseudobulk counts and a fitted count model.

Advanced 11 min read

What You'll Learn

  • Define deviance residuals for count models and interpret their signs and magnitudes.
  • Calculate randomized quantile residuals from a discrete fitted distribution.
  • Explain why correctly specified randomized quantile residuals are approximately standard normal.
  • Use residual patterns against predictors, fitted values, and sample structure to detect systematic lack of fit.
  • Distinguish conditional from marginal residuals when a model includes donor random effects.
  • Recognize when residual plots are warning signals rather than formal tests or proof of a particular model failure.

Why Examine Residuals in Pseudobulk Models?

A gene-level pseudobulk model relates a sample’s count for a gene to its condition and other sample-level predictors, often while accounting for library size and repeated observations from donors. A fitted model can produce an estimated effect even when its assumptions are imperfect. Residual diagnostics ask a different question: does the model reproduce the observed counts without leaving a systematic pattern unexplained?

For a single gene, residuals are summaries of the discrepancy between each observed count and what the fitted model predicts. A residual that is positive or negative is not, by itself, evidence of a problem. The concern is a repeated pattern—for example, residuals mostly positive in one condition, or a trend with fitted values or sample order. Such patterns can indicate that the mean structure, dependence structure, or assumed count distribution does not adequately represent the data. Residuals flag a mismatch; they generally do not identify its cause on their own.

The focus here is on deviance residuals and randomized quantile residuals. Both compare observations with fitted count distributions, but they put those discrepancies on different scales. The introductory material on pseudobulk count models provides the setting: observations are gene counts for biological samples, and a model specifies their distribution conditional on predictors, offsets, and any modeled donor effects.

Model and Residual Definitions

For a fixed-effect count model, let \(Y_i\) be the observed count for sample \(i\), and let \(\widehat{\mu}_i\) be its fitted mean. A negative binomial model is commonly written with variance \(\mu_i+\alpha\mu_i^2\), where \(\alpha\) is a dispersion parameter. A Poisson model is the special case with variance \(\mu_i\). The model also specifies how the mean depends on predictors and any offset, such as a library-size adjustment.

The interpretations below require that the chosen count distribution and mean structure adequately describe the observations, with the specified offset and dependence structure. In an ordinary generalized linear model, observations are assumed independent conditional on their predictors. For repeated pseudobulk samples, independence may instead be assumed conditional on modeled donor effects. Diagnostics are approximate in practice because model parameters are estimated from the same data.

Definition: For an observation \(y_i\), its deviance contribution is \(d_i=2\{\ell_i(y_i;y_i)-\ell_i(y_i;\widehat{\mu}_i)\}\), where \(\ell_i(y_i;\mu)\) is the observation’s log-likelihood under the specified count family and dispersion, and the first term is the saturated-model log-likelihood for that observation. The deviance residual is \(r_{D,i}=\operatorname{sign}(y_i-\widehat{\mu}_i)\sqrt{d_i}\). It is positive when the observed count exceeds its fitted mean and negative when it falls below it.

The saturated model gives each observation its own fitted mean, so it can reproduce that observation exactly. The deviance measures the log-likelihood loss when the fitted model replaces those separate means. In a Poisson model, the residual has the explicit form below, using the convention that \(y_i\log(y_i/\widehat{\mu}_i)=0\) when \(y_i=0\).

$$ r_{D,i} = \operatorname{sign}(y_i-\widehat{\mu}_i) \sqrt{ 2\left[ y_i\log\left(\frac{y_i}{\widehat{\mu}_i}\right) -(y_i-\widehat{\mu}_i) \right] } $$

For a negative binomial model, use the same likelihood-ratio definition with the negative binomial log-likelihood; the Poisson shortcut above does not apply. Deviance residuals are useful for spotting whether observations tend to fall above or below their predictions. Their scale, however, does not make them exactly standard normal, especially for small or discrete counts.

Randomized quantile residuals instead use the entire fitted cumulative distribution. Let \(F_i(y)=P(Y_i\leq y)\) be the fitted cumulative distribution for observation \(i\). Draw \(V_i\) independently from a uniform distribution on \((0,1)\), and define:

Definition: The randomized quantile residual is \(r_{Q,i}=\Phi^{-1}(U_i)\), where \(U_i=F_i(y_i-1)+V_iP(Y_i=y_i)\), and \(\Phi^{-1}\) is the standard normal quantile function. For \(y_i=0\), define \(F_i(-1)=0\). The random draw spreads the observation across its fitted probability interval, avoiding the tied residual values that arise from a discrete count distribution.

Why Randomization Gives a Normal Reference Scale

If the model’s distribution is correct and its parameters are known, the randomized probability \(U_i\) is uniform on \((0,1)\). To see why, the event \(Y_i=y\) has probability \(P(Y_i=y)\). Conditional on that event, \(U_i\) is uniform across the interval from \(F_i(y-1)\) to \(F_i(y)\), whose length is exactly \(P(Y_i=y)\). Thus, across all possible counts, those intervals fill \((0,1)\) in proportion to their lengths, giving a uniform distribution. Applying the standard normal quantile function to a uniform variable gives a standard normal variable.

$$ P(U_i\leq u)=u,\quad 0<u<1, \qquad \Phi^{-1}(U_i)\sim N(0,1) $$

This is a reference result, not a guarantee that residuals calculated from fitted models will be exactly independent standard normal variables. Estimated parameters make the reference approximate, and repeated samples can remain dependent if the donor structure is not handled correctly. A normal quantile plot can help assess whether the residual distribution is broadly compatible with the reference, but plots against predictors and sample structure are needed to look for systematic patterns.

Worked Example: Calculating a Poisson Deviance Residual

Suppose a hypothetical gene count is \(y=8\) in one pseudobulk sample, and the fitted Poisson model predicts \(\widehat{\mu}=5\). Assume the Poisson model and its fitted mean are being used only for this illustration.

Calculation. Substitute into the Poisson deviance-residual formula:

$$ r_D = \sqrt{2\left[8\log(8/5)-(8-5)\right]} = \sqrt{2\left[8(0.4700036)-3\right]} = \sqrt{1.520058} \approx 1.233 $$

The sign is positive because \(8>5\). The observation is therefore above its fitted mean, with a deviance residual of about 1.233 on the signed deviance scale. This does not mean that the count is 1.233 units above the mean; the residual is a likelihood-based discrepancy, not a count difference. For a negative binomial model, the corresponding calculation must use its own log-likelihood.

Worked Example: Calculating a Randomized Quantile Residual

For a hypothetical Poisson observation, suppose \(Y=0\) and the fitted mean is \(\widehat{\mu}=1\). For this example, take the random draw to be \(V=0.5\). The probability of zero is \(e^{-1}\approx0.3678794\), and \(F(-1)=0\).

Calculation. First calculate the randomized probability, then transform it to the normal scale:

$$ U=0+0.5(0.3678794)=0.1839397, \qquad r_Q=\Phi^{-1}(0.1839397)\approx -0.900 $$

The residual is negative because a zero count falls in the lower part of the fitted distribution in this particular randomization. A different draw of \(V\) would give a different residual within the interval corresponding to \(Y=0\). Repeating this calculation does not change the observed count or fitted model; it changes only the randomized position assigned within that count’s probability interval.

Looking for Structure in Residuals

Plot residuals against fitted means and against predictors included in the model, such as condition or time. Also consider sample order and donor grouping when they are relevant to the design. A cloud without a clear trend is broadly reassuring, while runs of residuals on one side of zero, curvature, or repeated donor-specific patterns deserve investigation. A normal quantile plot is most directly interpretable for randomized quantile residuals; it does not replace plots that preserve the design structure.

The following example illustrates a condition-related pattern. It deliberately fits a model with one common mean to hypothetical counts from two conditions. The resulting residual pattern is evidence that the fitted model has left a systematic condition difference unexplained; it is not a formal test of a condition effect.

Worked Example: Detecting a Condition Pattern

For one hypothetical gene, eight independent pseudobulk samples have counts \(4,5,5,6\) in condition A and \(8,9,9,10\) in condition B. Suppose a Poisson model with a single common mean is fitted. The pooled fitted mean is \((4+5+5+6+8+9+9+10)/8=56/8=7\).

Calculate representative residuals. For the condition A count \(y=4\):

$$ r_D = -\sqrt{2\left[4\log(4/7)-(4-7)\right]} = -\sqrt{2\left[4(-0.5596158)+3\right]} \approx -1.234 $$

For a condition B count \(y=9\):

$$ r_D = \sqrt{2\left[9\log(9/7)-(9-7)\right]} = \sqrt{2\left[9(0.2513144)-2\right]} \approx 0.724 $$

All four counts in A are below 7 and all four counts in B are above 7, so the residuals divide by condition rather than fluctuating around zero without a pattern. This is a warning that the common-mean model does not capture the observed condition structure. It does not establish that condition caused the difference, nor does it determine whether another feature of the model also needs attention.

Repeated Measures, Comparisons, and Limitations

For models with donor random effects, the distribution used to calculate a residual matters. A conditional residual evaluates an observation conditional on a specified donor effect, often an estimated effect. A marginal predictive distribution averages over the random-effect distribution. These answer different diagnostic questions: the conditional view examines discrepancies after accounting for donor effects, while the marginal view asks whether predictions for new observations from the population of donors are adequate. Treating conditional residuals as independent standard normal values can be misleading when the donor effects were estimated from the same data.

Deviance and randomized quantile residuals also differ from the Pearson residual, which divides the difference between the observed and fitted counts by a model-based standard deviation. Pearson residuals can be convenient for a quick standardized discrepancy, but they do not directly use the likelihood-ratio scale or transform the fitted discrete distribution to a continuous reference scale. No one residual plot is a complete diagnostic procedure: use residuals that match the fitted model and inspect them in ways that preserve its design.

These diagnostics have limits. A small number of pseudobulk samples makes patterns difficult to judge, and a visually striking residual may arise by chance. Parameter estimation, repeated observations, and randomization affect the reference distribution. Residuals alone cannot validate a model, prove a particular alternative, or establish a biological explanation. In particular, do not infer a specific variance or zero-generating mechanism from a residual pattern alone; those questions require targeted checks of the distributional assumptions.

Key takeaway: Use residuals to ask whether discrepancies are systematic. Check their signs and distribution, then look for patterns against fitted values, predictors, and the sample structure. Treat a pattern as a prompt for model checking, not as a diagnosis of its cause.

Common Mistakes and Practical Checks

  • Reading a residual as a count difference. A deviance residual is a signed likelihood-based quantity, not \(y_i-\widehat{\mu}_i\). Report the observed and fitted counts separately when the count-scale discrepancy matters.
  • Expecting every residual to be standard normal. The standard normal reference for randomized quantile residuals assumes the fitted distribution is correct and known. In a fitted, repeated-measures model, it is approximate and may need to account for dependence.
  • Ignoring which distribution generated the residual. The Poisson deviance formula is not the negative binomial formula. Use the likelihood for the family actually fitted.
  • Treating one unusual value as proof of model failure. Residual diagnostics seek repeated structure. Check the full set of samples and relevant predictors before drawing conclusions.
1
Match the residual to the model.
Use the fitted count family and account for offsets and modeled donor dependence.
2
Inspect the distribution and the design.
For randomized quantile residuals, use a normal quantile plot as a reference and examine patterns against fitted values, predictors, sample order, and donor structure.
3
Describe the signal cautiously.
State which residual pattern is present, then investigate possible explanations with diagnostics suited to those assumptions. Do not treat the pattern alone as proof of a cause.

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. A Poisson model has \(y=3\) and \(\widehat{\mu}=5\). What is the sign of its deviance residual, and why?
  2. For a correctly specified discrete model with known parameters, why is the randomized probability \(U\) uniform?
  3. What does a repeated run of positive residuals in one condition suggest, and what does it not prove?
  4. Why can conditional residuals from a donor random-intercept model be inappropriate to treat as independent standard normal observations?

Solutions. (1) The sign is negative because \(3-5<0\), by the definition \(r_D=\operatorname{sign}(y-\widehat{\mu})\sqrt{d}\). (2) Given each count, randomization spreads \(U\) uniformly over an interval whose length equals that count’s probability; the intervals together partition \((0,1)\), so \(P(U\leq u)=u\). (3) The run suggests that the fitted model may leave condition-related structure unexplained. It does not prove a causal condition effect or identify the model component responsible. (4) The donor effects are estimated, and observations from the same donor share modeled dependence; conditioning on estimated effects does not make those residuals independent standard normal variables.

Clinical Biostats