Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Centering Cells and Genes for PCA

Dimension Reduction and Embeddings · Tutorial 202 of 700

Centering Cells and Genes for PCA

Learn how to center each gene across cells, form the covariance matrix used for PCA, and recognize when omitting centering changes the analysis.

Intermediate 11 min read

What You'll Learn

  • Distinguish centering each gene across cells from centering each cell across genes.
  • Derive the sample covariance matrix from the centered expression matrix.
  • Work out how a toy expression matrix gives different leading directions with and without centering.
  • Check centered data and covariance calculations for common numerical or conceptual problems.
  • Compare covariance-based PCA with uncentered second-moment analysis and alternative centering choices.

Why Centering Matters Before PCA

A gene-expression matrix contains both differences among cells and the average expression level of each gene. PCA based on covariance describes variation around those gene averages. If the matrix is not centered first, the calculation also reflects the genes’ mean levels, so the leading direction can describe overall expression magnitude rather than the main pattern of cell-to-cell variation.

Let \(X\) be an \(n \times p\) matrix, with one cell per row and one gene per column. Its entry \(x_{ij}\) is the expression value for cell \(i\) and gene \(j\), after whatever transformation has been chosen for the analysis. This tutorial concerns centering those columns across cells. It does not prescribe a transformation or gene-scaling rule.

Definition: The mean expression of gene \(j\) across the \(n\) cells is \(\bar{x}_j = \frac{1}{n}\sum_{i=1}^{n}x_{ij}\). The gene-centered value is \(x_{ij}^{(c)}=x_{ij}-\bar{x}_j\). The centered matrix \(X_c\) has entries \(x_{ij}^{(c)}\), so each column of \(X_c\) has mean zero.

The expression “center the genes” can be confusing: it means subtract each gene’s mean across cells, not subtract each cell’s mean across genes. Column centering preserves each cell’s position relative to the gene-specific average. Row centering instead changes each cell by subtracting its average across genes; these are different operations and generally produce different results.

Centering is a data transformation, not a claim that every gene has zero biological expression. A centered value of zero means expression equals that gene’s mean in the analyzed cells. A negative centered value means it is below that mean.

From Centered Expression to Covariance

For two genes \(j\) and \(k\), their sample covariance across cells is the average product of their deviations from their respective means, using \(n-1\) in the denominator. Positive covariance indicates that cells above the mean for one gene tend also to be above the mean for the other; negative covariance indicates an opposing pattern.

Definition: The sample covariance matrix of the \(p\) genes is the \(p \times p\) matrix \(S\) with entries \(s_{jk}=\frac{1}{n-1}\sum_{i=1}^{n}(x_{ij}-\bar{x}_j)(x_{ik}-\bar{x}_k)\). In matrix form, \(S=\frac{1}{n-1}X_c^\mathsf{T}X_c\). Its diagonal entries are gene sample variances; its off-diagonal entries are pairwise sample covariances.

The factor \(n-1\) gives the usual sample covariance and makes each variance an unbiased estimator of its population counterpart when cells are independent observations from a population with finite variance. PCA can also be computed from a covariance matrix using a different common divisor, such as \(n\); that changes eigenvalue scale but not the eigenvector directions. The choice of divisor should be stated when reporting numerical eigenvalues.

The matrix identity follows directly from matrix multiplication. The \((j,k)\) entry of \(X_c^\mathsf{T}X_c\) is the dot product of centered gene columns \(j\) and \(k\), which is the sum of their paired deviations. Dividing that sum by \(n-1\) gives the sample covariance.

$$ (X_c^\mathsf{T}X_c)_{jk} =\sum_{i=1}^{n}x_{ij}^{(c)}x_{ik}^{(c)} =\sum_{i=1}^{n}(x_{ij}-\bar{x}_j)(x_{ik}-\bar{x}_k), \qquad S=\frac{1}{n-1}X_c^\mathsf{T}X_c. $$

Equivalently, define the centering matrix \(H=I_n-\frac{1}{n}\mathbf{1}\mathbf{1}^\mathsf{T}\), where \(I_n\) is the \(n \times n\) identity matrix and \(\mathbf{1}\) is the length-\(n\) vector of ones. Then \(X_c=HX\): multiplication by \(H\) subtracts each column mean. Since \(H^\mathsf{T}=H\) and \(H^2=H\), the covariance can also be written \(S=\frac{1}{n-1}X^\mathsf{T}HX\).

Justification: The \(i\)th entry of \(H\) applied to a column \(x_{\cdot j}\) is \(x_{ij}-\bar{x}_j\), because \(H x_{\cdot j}=x_{\cdot j}-\mathbf{1}(\mathbf{1}^\mathsf{T}x_{\cdot j}/n)\). Thus \(HX=X_c\). Substituting this into the covariance formula gives \(X_c^\mathsf{T}X_c=X^\mathsf{T}H^\mathsf{T}HX=X^\mathsf{T}HX\).

This covariance matrix is symmetric and positive semidefinite: for any length-\(p\) vector \(a\), \(a^\mathsf{T}Sa=\frac{1}{n-1}\lVert X_ca\rVert^2\geq 0\). These properties are useful checks on a computed matrix. Because the centered columns sum to zero, the rank of \(S\) is at most \(\min(p,n-1)\); having more genes than cells therefore does not make all covariance directions independently estimable.

Worked Calculations

Worked Example: Centering Each Gene Across Cells

Suppose a hypothetical transformed-expression matrix for three cells and two genes is

$$ X= \begin{pmatrix} 2&1\\ 4&3\\ 6&5 \end{pmatrix}. $$

The column means are \(\bar{x}_1=(2+4+6)/3=4\) and \(\bar{x}_2=(1+3+5)/3=3\). Subtract these means from their respective columns:

$$ X_c= \begin{pmatrix} 2-4&1-3\\ 4-4&3-3\\ 6-4&5-3 \end{pmatrix} = \begin{pmatrix} -2&-2\\ 0&0\\ 2&2 \end{pmatrix}. $$

Each centered column sums to zero. The sample variance of gene 1 is \(((-2)^2+0^2+2^2)/(3-1)=8/2=4\). The sample covariance of genes 1 and 2 is \(((-2)(-2)+0(0)+2(2))/2=8/2=4\). Thus \(S=\begin{pmatrix}4&4\\4&4\end{pmatrix}\), in squared expression units. Here the genes have identical centered patterns, so the covariance is positive and equals each gene’s variance.

Worked Example: Centering Changes the Leading Direction

Consider this hypothetical expression matrix for two cells and two genes:

$$ X= \begin{pmatrix} 1&3\\ 3&1 \end{pmatrix}. $$

Both gene means are \(2\), so column centering gives \(X_c=\begin{pmatrix}-1&1\\1&-1\end{pmatrix}\). Using the sample covariance denominator \(n-1=1\),

$$ S=X_c^\mathsf{T}X_c = \begin{pmatrix} (-1)^2+1^2&(-1)(1)+(1)(-1)\\ (-1)(1)+(1)(-1)&1^2+(-1)^2 \end{pmatrix} = \begin{pmatrix} 2&-2\\ -2&2 \end{pmatrix}. $$

The centered covariance has leading direction proportional to \((1,-1)^\mathsf{T}\): the two genes vary in opposite directions across these cells. Indeed, multiplying \(S\) by \((1,-1)^\mathsf{T}\) gives \(4(1,-1)^\mathsf{T}\), while multiplying it by \((1,1)^\mathsf{T}\) gives the zero vector.

Now omit centering and use the uncentered second-moment matrix \(X^\mathsf{T}X/n\):

$$ \frac{X^\mathsf{T}X}{2} = \frac{1}{2} \begin{pmatrix} 10&6\\ 6&10 \end{pmatrix} = \begin{pmatrix} 5&3\\ 3&5 \end{pmatrix}. $$

This matrix has leading direction proportional to \((1,1)^\mathsf{T}\), with eigenvalue \(8\); its direction \((1,-1)^\mathsf{T}\) has eigenvalue \(2\). Thus omitting centering reverses which gene combination is leading in this example. The uncentered matrix is not the sample covariance: it includes the genes’ nonzero means as well as their variation. The result illustrates why a leading direction from an uncentered calculation need not describe the dominant pattern of variation around gene means.

Worked Example: Column Centering Is Not Row Centering

For a second hypothetical matrix, let three cells have expression values \(X=\begin{pmatrix}10&1\\12&2\\14&3\end{pmatrix}\). The gene means are \(12\) and \(2\). Centering each gene across cells gives

$$ X_c= \begin{pmatrix} -2&-1\\ 0&0\\ 2&1 \end{pmatrix}, \qquad S=\frac{1}{2} \begin{pmatrix} 8&4\\ 4&2 \end{pmatrix} = \begin{pmatrix} 4&2\\ 2&1 \end{pmatrix}. $$

For example, the cross-product sum is \((-2)(-1)+0(0)+(2)(1)=4\), so the sample covariance is \(4/2=2\). Instead, subtracting each cell’s row mean would produce rows \((4.5,-4.5)\), \((5,-5)\), and \((5.5,-5.5)\). This operation makes the two genes appear exactly opposite within every row, a pattern not present in the column-centered deviations. It answers a different question and is not the centering used to form the gene covariance matrix for ordinary PCA.

Assumptions, Checks, and Alternatives

The covariance calculation itself is descriptive: it requires numeric values and defined means, not normally distributed expression. For the sample covariance to estimate covariance in a broader cell population in the usual unbiased sense, cells should be independent draws from that population and have finite second moments. In single-cell data, cells may be clustered within donors, samples, or experimental batches. Then treating every cell as an independent biological replicate can overstate how much independent information the data contain. This does not prevent computing \(S\), but it affects population-level interpretation and inference.

Before interpreting PCA based on \(S\), check that the matrix contains the intended cells and genes, that the selected expression representation is appropriate, and that each column mean of \(X_c\) is approximately zero. Small discrepancies can arise from rounding. A constant gene has a zero centered column and therefore zero variance; it cannot contribute a nonzero covariance direction. Missing values need explicit handling: ordinary column means and cross-products based on different sets of observed cells can fail to produce the covariance matrix defined above.

The covariance matrix is expressed in squared units of the input expression values. Consequently, genes with larger variability on the chosen scale can contribute more strongly. Centering alone does not equalize gene variances, and it does not remove batch effects, normalize library sizes, or establish that an observed pattern is biological. Those are separate choices that should be made and evaluated in context.

ApproachWhat is calculatedKey distinction
Gene-centered covarianceCovariance of each gene’s deviations from its across-cell meanDescribes variation around gene means
Uncentered second momentProducts of raw expression values without subtracting gene meansCan reflect mean levels as well as variation
Row-centered expressionValues after subtracting each cell’s across-gene meanChanges cell profiles; it is not gene-wise centering
Correlation-based analysisCovariances after rescaling genes by their standard deviationsGives genes equal variance scale when standard deviations are nonzero

The correlation alternative is not simply a different centering convention: it also scales each gene. A constant gene cannot be scaled to unit variance, and scaling can give noisy low-variance genes substantial influence. Decisions about gene scaling therefore require their own justification. The important point here is to state whether the analysis uses centered covariance, uncentered second moments, or a rescaled matrix; these choices need not produce the same leading direction.

  • Using “mean” ambiguously. Subtracting a cell’s mean across genes is not the same as subtracting each gene’s mean across cells. State which dimension is being centered.
  • Calling an uncentered product matrix covariance. The matrix \(X^\mathsf{T}X/n\) is a second moment about zero. Centering is needed for covariance around the observed gene means.
  • Ignoring the divisor. With \(n-1\), the example’s covariance entry is \(2\); changing the divisor changes covariance values and eigenvalues. Specify the convention and recompute consistently.
  • Interpreting a component as biological by calculation alone. A covariance direction summarizes patterns in the supplied matrix. It does not by itself identify their cause or establish that preprocessing and sampling choices were adequate.
Key takeaway: With cells in rows and genes in columns, subtract each gene’s across-cell mean to form \(X_c\), then calculate \(S=X_c^\mathsf{T}X_c/(n-1)\). Centering determines whether PCA describes variation around gene means or also reflects their uncentered levels.

Check Your Understanding

Try each question, then check the brief solutions below.

  1. For \(X=\begin{pmatrix}2\\6\\10\end{pmatrix}\), what is the centered column and its sample variance?
  2. Why is \(X^\mathsf{T}X/n\) generally not the sample covariance matrix?
  3. For two centered gene columns with values \((-1,1)\) and \((1,-1)\) across two cells, calculate their sample covariance.
  4. What does a zero centered column imply about that gene in the analyzed cells?
  5. Name one assumption needed to interpret the usual sample covariance as an unbiased estimate of population covariance, and one reason it may be questionable for single-cell data.

Solutions. (1) The mean is \((2+6+10)/3=6\), so the centered column is \((-4,0,4)^\mathsf{T}\). Its sample variance is \((16+0+16)/(3-1)=16\). (2) It uses raw values rather than deviations from each gene’s mean, so nonzero gene means contribute to its entries. (3) The cross-product sum is \((-1)(1)+(1)(-1)=-2\); dividing by \(n-1=1\) gives sample covariance \(-2\). (4) The gene has the same expression value in every analyzed cell and therefore zero sample variance. (5) Cells must be independent draws with finite second moments for the usual unbiased-estimation interpretation; cells from the same donor or sample may be dependent rather than independent biological replicates.

Clinical Biostats