Tutorials › Multi-Omics Data Integration › Residual Diagnostics for NMF Fits

Non-Negative Matrix Factorization · Tutorial 210 of 597

Residual Diagnostics for NMF Fits

Use signed and aggregated NMF residuals to locate structured lack of fit, while accounting for scale, preprocessing, and the loss used to fit the factors.

Advanced 12 min read

What You'll Learn

  • Define signed residuals for a single NMF matrix and for multiple omics blocks
  • Calculate feature-wise and sample-wise residual summaries and interpret their units
  • Compare modality fit using scale-aware errors rather than raw sums alone
  • Use residual patterns and dependence summaries to flag structure that remains
  • Distinguish descriptive diagnostics from inferential tests and identify key limitations

Why Inspect NMF Residuals?

A low NMF objective does not by itself show that a reconstruction captures the relevant structure in every feature, sample, or omics modality. A total error can conceal a group of poorly reconstructed features, one unusual sample, or a consistent pattern left in a particular block. Residual diagnostics make these patterns visible. They help assess where a fitted reconstruction is inadequate; they do not establish that the model is correct or that a residual pattern has a particular biological cause.

For an omics block, let \(X^{(m)}\) be a nonnegative matrix with \(p_m\) features in rows and \(n\) aligned samples in columns. A rank-\(K\) NMF reconstruction is \(\widehat X^{(m)}=W^{(m)}H\), where \(W^{(m)}\) is a nonnegative feature-by-component matrix and \(H\) is a nonnegative component-by-sample matrix. In a joint fit, the shared \(H\) links samples across blocks, although some models may also include block-specific terms. The diagnostic object is the difference between the observed matrix and the reconstruction on the scale being examined.

Definition: The signed residual for feature \(i\), sample \(j\), and block \(m\) is \(E^{(m)}_{ij}=X^{(m)}_{ij}-\widehat X^{(m)}_{ij}\). A positive residual means the observed value exceeds its fitted reconstruction; a negative residual means the reconstruction exceeds the observed value. The residual has the same units as the matrix values.

These are descriptive residuals, not automatically standardized errors with a known reference distribution. Their interpretation depends on the data scale, preprocessing, missingness pattern, and objective used to fit the model. If squared-error loss was used, squared residuals directly contribute to the fitting objective. With another beta-divergence loss, signed differences can still be inspected, but they are not the loss contributions and may not reflect the fitting criterion well.

Feature-Wise and Sample-Wise Summaries

A residual table is often too large to inspect cell by cell. For a complete, equally weighted matrix fitted with squared-error loss, a feature-wise sum of squared residuals identifies features contributing most to the total error. Dividing by the number of observed samples gives a mean squared residual; its square root is a root mean squared residual (RMSE), in the original units. Sample-wise summaries are defined in the same way across features.

$$ Q^{(m)}_i=\sum_{j=1}^{n}\left(E^{(m)}_{ij}\right)^2,\qquad \operatorname{RMSE}^{(m)}_i=\sqrt{\frac{Q^{(m)}_i}{n_i}} $$
$$ Q^{(m)}_j=\sum_{i=1}^{p_m}\left(E^{(m)}_{ij}\right)^2,\qquad \operatorname{RMSE}^{(m)}_j=\sqrt{\frac{Q^{(m)}_j}{p_{mj}}} $$

Here \(n_i\) is the number of observed entries for feature \(i\), and \(p_{mj}\) is the number observed for sample \(j\). For complete matrices these are \(n\) and \(p_m\). With missing entries, calculate summaries only over observed entries and report the denominators; otherwise samples or features with more missing values can appear artificially well fitted. A sum of squares measures total error and grows with the number of observations, while an RMSE is an average-scale summary. Neither removes the effect of feature-specific variability or measurement noise.

Worked Example: Locating a Feature Pattern and a Poorly Fit Sample

Suppose a hypothetical block has observed values and fitted values, in the same measurement units, as follows. Rows are features and columns are samples.

$$ X=\begin{pmatrix} 10&12&8&10\\ 4&6&2&4\\ 3&3&3&3 \end{pmatrix}, \qquad \widehat X=\begin{pmatrix} 9&11&9&11\\ 4&5&3&4\\ 2&2&2&2 \end{pmatrix} $$

Subtracting the fitted matrix gives

$$ E=X-\widehat X= \begin{pmatrix} 1&1&-1&-1\\ 0&1&-1&0\\ 1&1&1&1 \end{pmatrix}. $$

For feature 1, \(Q_1=1^2+1^2+(-1)^2+(-1)^2=4\), so its RMSE is \(\sqrt{4/4}=1\) measurement unit. For feature 2, \(Q_2=0^2+1^2+(-1)^2+0^2=2\), and its RMSE is \(\sqrt{2/4}\approx0.7071\) units. Feature 3 has \(Q_3=1+1+1+1=4\) and RMSE \(1\) unit. Its residuals are all positive: the reconstruction is below the observed value by one unit in every sample. This repeated direction is a potentially important feature-level pattern, not merely a large error total.

For sample 2, the squared residuals are \(1^2+1^2+1^2=3\), so its RMSE across three features is \(\sqrt{3/3}=1\) unit. Samples 1 and 4 each have a squared-error sum of \(1+0+1=2\), giving RMSE \(\sqrt{2/3}\approx0.8165\) units. Sample 3 also has sum \(1+1+1=3\), hence RMSE \(1\). Thus samples 2 and 3 have the largest sample-wise RMSE here, but the feature-wise pattern in row 3 occurs in every sample. Neither summary alone describes the whole residual structure.

Comparing Omics Blocks and Examining Residual Dependence

Raw residual sums of squares should not be used alone to decide which modality fits worse. Their size depends on the block's scale and number of entries. One descriptive comparison is the relative Frobenius error, the square root of the residual sum of squares divided by the observed sum of squares. It is dimensionless, but it can be unstable when the observed sum of squares is close to zero and is not a substitute for checking the preprocessing and scientific meaning of each block.

$$ \operatorname{RelErr}^{(m)} =\sqrt{\frac{\sum_{i,j\ {\rm observed}}\left(E^{(m)}_{ij}\right)^2} {\sum_{i,j\ {\rm observed}}\left(X^{(m)}_{ij}\right)^2}} $$

Worked Example: Equal Absolute Errors, Unequal Relative Errors

Consider two hypothetical blocks, each with two features and three samples. In block A, let \(X^{(A)}\) have rows \((10,12,8)\) and \((5,5,5)\); let its reconstruction have rows \((9,11,9)\) and \((5,5,5)\). The residual rows are \((1,1,-1)\) and \((0,0,0)\), so the residual sum of squares is \(1+1+1=3\). The observed sum of squares is \(100+144+64+25+25+25=383\), giving \(\operatorname{RelErr}^{(A)}=\sqrt{3/383}\approx0.08848\).

In block B, let the observed rows be \((2,2,2)\) and \((1,1,1)\), and let the reconstruction rows be \((1,1,1)\) and \((1,1,1)\). The residual rows are \((1,1,1)\) and \((0,0,0)\), again giving a residual sum of squares of \(3\). The observed sum of squares is \(4+4+4+1+1+1=15\), so \(\operatorname{RelErr}^{(B)}=\sqrt{3/15}=\sqrt{0.2}\approx0.4472\). The absolute errors are equal, but the relative errors differ substantially. This signals that block B has greater error relative to its observed energy; it does not, by itself, prove that block B is less scientifically useful or should receive a different weight.

Residual dependence can reveal coordinated structure the factorization has not captured. For example, residuals for two features can move together across samples, or two modality residual profiles can align across matched samples. A simple descriptive measure is cosine similarity, the residual dot product divided by the product of Euclidean lengths. It measures directional alignment without centering; it is not Pearson correlation and does not test independence. Pairwise residual summaries are most interpretable when the features or samples are meaningfully matched and the scales have been considered.

$$ c(e_a,e_b)=\frac{\sum_j e_{aj}e_{bj}} {\sqrt{\sum_j e_{aj}^2}\sqrt{\sum_j e_{bj}^2}} $$

Worked Example: Residual Profiles Align Across Two Blocks

For a hypothetical five-sample comparison, suppose two modality residual profiles are \(e_1=(1,2,-1,-2,0)\) and \(e_2=(1,4,-2,-4,\sqrt{3})\). Their dot product is \(1(1)+2(4)+(-1)(-2)+(-2)(-4)+0(\sqrt{3})=1+8+2+8+0=19\). The squared lengths are \(1+4+1+4+0=10\) and \(1+16+4+16+3=40\). Therefore,

$$ c(e_1,e_2)=\frac{19}{\sqrt{10\cdot40}}=\frac{19}{20}=0.9500. $$

The profiles are strongly aligned in direction: samples with positive residuals in the first profile tend to have positive residuals in the second, and likewise for negative residuals. This can motivate investigation of shared structure left unexplained by the fitted reconstruction. It is not evidence that one modality causes errors in the other, and it is not a formal significance test. In particular, cosine similarity uses uncentered profiles and should not be reported as a correlation coefficient.

Derivation, Diagnostics, and Limits

The feature-wise and sample-wise squared-error summaries are two ways to partition the same total squared error. For a complete matrix, finite sums can be regrouped by rows or columns:

$$ \sum_{i=1}^{p_m}\sum_{j=1}^{n}\left(E^{(m)}_{ij}\right)^2 =\sum_{i=1}^{p_m}Q^{(m)}_i =\sum_{j=1}^{n}Q^{(m)}_j. $$

This identity is useful as a calculation check. It also explains why a single total cannot reveal where error is concentrated: the total remains unchanged whether its contributions are spread across all entries or concentrated in one feature or sample.

A practical review combines summaries with the signed residual matrix. Plot residuals against fitted values, inspect feature and sample summaries, and look for contiguous or repeated patterns when features have a meaningful order. In a joint fit, examine each block separately before interpreting a pooled summary. Patterns to investigate include:

  • Feature-wise bias: residuals for one feature repeatedly have the same sign, suggesting a systematic mismatch for that feature.
  • Sample-wise concentration: a sample has unusually large residuals across many features, which may reflect unusual biology, data quality, or preprocessing rather than a single cause.
  • Structured dependence: groups of residuals move together, or an ordered feature region shows a pattern, suggesting structure not represented by the current reconstruction.
  • Block-specific error: one modality has a larger scale-aware error or a distinctive residual pattern, which may indicate a modality-specific mismatch or an unsuitable comparison scale.

Before assigning meaning to any pattern, check that samples are aligned correctly across blocks, residuals are calculated on the intended scale, feature transformations are appropriate, and missing entries are excluded consistently. For count matrices, a raw difference can be hard to interpret when variability depends on the mean; the diagnostic should be considered alongside the count objective and the data's scale. If the fit used a non-squared beta-divergence, inspect the corresponding objective contributions as well as signed differences. If sparse outlier corrections were included, clarify whether the diagnostic residual is computed before or after those corrections: the two answer different questions about the factor reconstruction and the model's full fitted representation.

Residuals are not independent observations simply because they are displayed in a matrix. The factors were estimated from the data, which constrains the residuals and can induce dependence. Large residuals are not automatically outliers, and small residuals do not guarantee a good model: a high-dimensional reconstruction can fit noise, while a large residual can reflect a legitimate feature with a different scale. Residual diagnostics are descriptive checks, not a replacement for assessing rank, preprocessing, or scientific plausibility.

Common Mistake and Alternatives

Common mistake: “The two blocks have the same residual sum of squares, so they fit equally well.” This ignores differences in scale and observed magnitude. Compare scale-aware summaries, report the units and denominators, and inspect each block's signed patterns. Relative error helps with magnitude comparisons, while feature-wise or sample-wise summaries locate the source of error; none is universally sufficient.

Other useful views answer different questions. Absolute residuals emphasize magnitude but hide whether the model over- or under-reconstructs. Squared residuals emphasize large deviations and match a squared-error objective. Relative residuals can compare scale but become unstable near zero. For a non-squared fitting objective, its per-entry divergence contributions are closer to the criterion actually optimized, but they do not provide signed direction. These diagnostics complement one another; they should not be treated as interchangeable rankings.

Check Your Understanding

Try each question, then compare your work with the solutions.

  1. A feature has residuals \((2,-1,1)\) across three observed samples. Find its sum of squared residuals and RMSE, including units if each matrix entry is measured in counts.
  2. A sample has residuals \((1,-2,2)\) across three observed features. Which feature has the largest signed residual, and what is the sample-wise RMSE?
  3. Why can equal residual sums of squares in two omics blocks fail to imply equal relative fit?
  4. What does a positive residual mean, and why is it not by itself evidence that a biological process is increased?

Solutions. (1) The sum is \(2^2+(-1)^2+1^2=6\) counts squared. The RMSE is \(\sqrt{6/3}=\sqrt{2}\approx1.414\) counts. (2) The largest signed residual is \(2\), tied between the second and third listed features. The sample-wise RMSE is \(\sqrt{(1+4+4)/3}=\sqrt{3}\approx1.732\) in the matrix's measurement units. (3) The observed sums of squares and block scales may differ; dividing each residual sum by its own observed sum of squares can yield different relative errors. (4) A positive residual means the observed value exceeds its fitted reconstruction. It is a discrepancy on the chosen data scale, not a causal or biological interpretation; preprocessing, noise, and model misspecification can all contribute.

Clinical Biostats