From a Centered Matrix to Cell Coordinates
Principal component analysis (PCA) turns a matrix of measurements into new axes that summarize directions of variation. In single-cell RNA sequencing, a row can represent a cell and a column a gene. Once the matrix has been centered as required for PCA, the calculation has a concrete sequence: form a covariance matrix, find its principal directions, and project each cell onto those directions. This tutorial carries out that sequence for a small hypothetical example.
Assume the entries below are quantitative expression values already on a suitable common scale for this illustration. They are invented solely to make the arithmetic transparent; they are not real sequencing data. The introductory material on centering cells and genes explains the centering convention. Here we use gene-wise centering across cells and focus on the resulting PCA calculation.
The eigenvectors are chosen orthonormally, so each has length one and different component directions are perpendicular. Collecting the first two directions as columns of \(Q_2\), the score matrix is \(Z_2=XQ_2\). A score is a coordinate in the new axis system; it is not a raw expression measurement for any one gene.
Worked Calculation: Centering and Covariance
Worked Example: Center a Four-Cell Matrix
Suppose four hypothetical cells have the following values for two genes, \(G_1\) and \(G_2\).
| Cell | \(G_1\) | \(G_2\) |
|---|---|---|
| A | 2 | 2 |
| B | -1 | 1 |
| C | 1 | -1 |
| D | -2 | -2 |
The mean for \(G_1\) is \((2-1+1-2)/4=0\). The mean for \(G_2\) is \((2+1-1-2)/4=0\). Thus this particular matrix is already centered, and subtracting the gene means leaves it unchanged:
The entries in each column sum to zero, as a centered matrix should. With \(n=4\), the sample covariance matrix is
For instance, the off-diagonal sum is \(4-1-1+4=6\), which becomes \(6/3=2\) after division by \(n-1\). The matrix is symmetric, and its diagonal entries are the sample variances of the two centered gene columns.
Finding the Principal Directions
For a symmetric covariance matrix, the principal directions are its unit eigenvectors. The eigenvalues identify the variance along their corresponding directions. In this example, the eigenvalues can be found from the characteristic equation:
Solving gives \(\lambda_1=16/3\) and \(\lambda_2=4/3\). For the first eigenvalue, the two rows of \(S-\lambda_1 I\) are proportional to \((-2,2)\), so an eigenvector is proportional to \((1,1)^\mathsf{T}\). For the second, an eigenvector is proportional to \((1,-1)^\mathsf{T}\). Normalizing both gives
These directions are perpendicular: \(q_1^\mathsf{T}q_2=(1-1)/2=0\), and each has squared length one. A direct check confirms the eigenvector calculations:
The order is by decreasing eigenvalue, so \(q_1\) is the first principal direction and \(q_2\) is the second. Reversing the sign of either eigenvector would describe the same axis and produce scores with the opposite sign; it would not change the PCA geometry.
Worked Example: Verify the First Eigenpair
Use the covariance matrix from the example and the proposed first direction \(q_1=(1,1)^\mathsf{T}/\sqrt{2}\). The eigenvector equation requires \(Sq_1=\lambda_1q_1\).
Multiplying with the exact entries gives
Therefore \(q_1\) is an eigenvector with eigenvalue \(16/3\). This exact check is preferable to mixing rounded matrix entries with an exact eigenvalue: if decimals are used at an intermediate step, the resulting product should be reported at a precision consistent with those rounded inputs.
Projecting Cells Onto the Two Axes
A cell’s score on an axis is its centered expression vector dotted with that axis’s unit direction. For the matrix above, \(Q_2\) has columns \(q_1\) and \(q_2\), so \(Z_2=XQ_2\). The two score coordinates for cell A, whose row is \((2,2)\), are
The same dot products for all four cells give the following coordinates. The decimal values are rounded to three significant figures; the exact forms show the underlying arithmetic.
| Cell | First-axis score | Second-axis score |
|---|---|---|
| A | \(2\sqrt{2}\approx 2.83\) | 0 |
| B | 0 | \(-\sqrt{2}\approx -1.41\) |
| C | 0 | \(\sqrt{2}\approx 1.41\) |
| D | \(-2\sqrt{2}\approx -2.83\) | 0 |
Worked Example: Calculate and Interpret a Cell’s Coordinates
For cell B, the centered gene values are \((-1,1)\). Its first-axis score is
Its second-axis score is
Thus cell B lies at \((0,-1.41)\), to the negative side of the second axis and at the origin of the first-axis coordinate. In this toy matrix the first direction combines the two gene values, while the second contrasts them. A score describes the cell’s position relative to the centered data and the chosen axis; it does not mean that the cell has negative expression. The axes are signed coordinate directions, and their signs are arbitrary.
Why the Eigenvalues Match Score Variances
The connection between the covariance calculation and the cell coordinates can be checked directly. For a unit direction \(q\), the vector of cell scores is \(Xq\). Its sample variance is
If \(q\) is an eigenvector with eigenvalue \(\lambda\), then \(q^\mathsf{T}Sq=q^\mathsf{T}(\lambda q)=\lambda\), because \(q^\mathsf{T}q=1\). Therefore the sample variance of the scores on that axis is \(\lambda\). This verifies, for the example, that the first-axis score variance is \(16/3\) and the second-axis score variance is \(4/3\). This calculation explains what the eigenvalues quantify; deciding how many components to retain is a separate question.
Here there are two genes and two orthonormal component directions, so retaining both axes reconstructs every centered row exactly. For cell B, its coordinates are \((0,-\sqrt{2})\); converting these coordinates back to the gene axes gives
which is the original centered row for B. The same identity holds for each cell because the two directions form an orthonormal basis of the two-gene coordinate space. If only the first direction were retained, this exact reconstruction would generally not hold; discarded directions contribute reconstruction error.
Hand Calculation Checks and Alternatives
A useful way to audit a small PCA calculation is to check each stage before interpreting the coordinates. The checks below are algebraic; PCA as a descriptive calculation does not require the cell values to be normally distributed. If the goal is to generalize conclusions to a population of cells, the sampling design and dependence among cells need separate consideration.
Each gene column of \(X\) should sum to zero, up to numerical rounding. Nonzero column means can indicate that the intended centering step was omitted or performed on the wrong dimension.
The matrix \(X^\mathsf{T}X/(n-1)\) should be symmetric. Its diagonal entries should be nonnegative, and the divisor should match the stated covariance convention.
Verify \(q_k^\mathsf{T}q_k=1\), \(q_j^\mathsf{T}q_k=0\) for different directions, and \(Sq_k=\lambda_kq_k\). Then calculate scores using the same centered rows and the stated direction vectors.
A common mistake is to interpret a score of zero on one component as evidence that the cell has no expression for the genes involved. That is incorrect: zero means the cell’s centered row has zero projection on that direction. For example, cell B has a first-axis score of zero because its centered values \((-1,1)\) cancel when projected onto the equal-weight direction; its expression values themselves are not zero.
For a small matrix, eigendecomposition of the covariance matrix makes the directions and their eigenvalues explicit. An equivalent route is the singular value decomposition \(X=U\Sigma V^\mathsf{T}\): the columns of \(V\) are the gene-space directions, and the cell scores are \(U\Sigma=XV\). The eigenvalues of \(S\) equal the squared singular values divided by \(n-1\). This equivalence can be useful computationally, but it does not change the meaning of the scores. Forming a covariance matrix is convenient by hand; for large or numerically challenging matrices, directly computing singular vectors can avoid some numerical issues associated with forming \(X^\mathsf{T}X\).
These calculations depend on using the matrix intended for PCA: the rows must be cells, the columns must be the chosen features, and centering must be applied consistently. A mismatch in row and column orientation can produce coordinates for genes instead of cells. Also, dividing by \(n\) rather than \(n-1\) rescales the covariance eigenvalues but leaves the eigenvectors and projected scores unchanged. Mixing those conventions can therefore produce plausible-looking directions alongside inconsistent reported variance values.
Check Your Understanding
Try these questions using the calculations and definitions above. Solutions follow the questions.
- For cell C, calculate its first- and second-axis scores from its row \((1,-1)\).
- Why is the first direction \(q_1\) assigned the larger eigenvalue in this example?
- What does the first-axis score of zero for cell B mean, and what does it not mean?
- If the covariance matrix were computed with divisor \(n\) instead of \(n-1\), what would happen to its eigenvectors and eigenvalues?
- What two basic checks can identify a mistake in a proposed principal direction or a centered matrix?
Solutions. (1) For cell C, \(z_{C1}=(1-1)/\sqrt{2}=0\), and \(z_{C2}=(1-(-1))/\sqrt{2}=\sqrt{2}\approx1.41\). (2) Its eigenvalue \(16/3\) is greater than \(4/3\), so its direction has greater score variance and is ordered first. (3) The centered row is perpendicular to the first direction, so its projection there is zero; this does not mean either gene’s expression value is zero. (4) The eigenvectors stay the same because the new covariance matrix is a positive scalar multiple of the old one; each eigenvalue is multiplied by \((n-1)/n=3/4\). (5) Check that centered columns sum to zero, and check that each proposed direction has unit length and satisfies the eigenvector equation \(Sq=\lambda q\).