Principal component analysis

Week 2 — Principal component analysis

Quantitative biology and integrative omics

About this lecture

You have measured thousands of genes across hundreds of samples. You cannot plot a cloud of points in 20,000 dimensions. PCA finds a handful of new axes that capture most of the variation, so you can see the structure in two dimensions.

  • The covariance matrix of the data is symmetric, so (from Eigenvalues & eigenvectors) its eigenvectors are orthogonal and its eigenvalues are real and non-negative.
  • The leading eigenvector points along the direction of maximum variance — the first principal component (PC).
  • Scores = where each sample lands when we project it onto a PC. Loadings = the eigenvector entries = how much each gene contributes.
  • We centre (and sometimes scale) the data first, then rotate the axes to line up with the data cloud.

This is a concept day. We build PCA from the eigen-decomposition of a covariance matrix. The R function prcomp that does it all in one call comes in the next unit (PCA in R).

The biology problem

A typical RNA-seq experiment gives an expression matrix: samples (rows) × genes (columns).

sample GENE1 GENE2 GENE3 … GENE20000
tumour_01 8.2 0.1 5.4 … 2.0
tumour_02 7.9 0.0 5.1 … 2.3
normal_01 2.1 6.7 5.3 … 1.9
…
  • Each sample is a point in 20,000-dimensional space. We cannot look at it.
  • But the genes are not independent — they are co-regulated in programmes (cell-cycle genes rise together, immune genes rise together…).
  • So the real structure lives in far fewer dimensions than 20,000. PCA finds them.

PCA combines genes instead of plotting them one by one

You could plot GENE1 against GENE2, then GENE1 against GENE3, …

  • With 20,000 genes there are 20{,}000 \times 19{,}999 / 2 \approx 200 million pairs of genes, one scatter plot each. Hopeless.
  • Any single gene is noisy and captures only a sliver of the biology.
  • The interesting signal — “these samples separate into two groups” — is usually spread across many genes at once, not visible in any single pair.

PCA builds new composite variables (mixtures of many genes) chosen so that the first few show the most variation. We look at those instead.

Recall: the covariance matrix

Variance measures spread of one variable; covariance measures how two vary together.

For p genes, the covariance matrix C is p \times p:

C_{ij} = \text{cov}(\text{gene}_i, \text{gene}_j), \qquad C_{ii} = \text{var}(\text{gene}_i)

  • Diagonal = each gene’s own variance. Off-diagonal = co-variation between gene pairs.
  • C is symmetric because \text{cov}(g_i, g_j) = \text{cov}(g_j, g_i).

From Eigenvalues & eigenvectors, a symmetric matrix has:

  • real, non-negative eigenvalues (they are variances — they cannot be negative), and
  • orthogonal eigenvectors — a clean set of perpendicular axes.

Those eigenvectors are the principal components.

A tiny worked example: two genes

Keep it to 2 genes and 5 samples so we can see everything by hand.

sample GENE_A GENE_B
s1 1 5
s2 3 3
s3 4 6
s4 5 7
s5 7 9
  • Plot the 5 points with GENE_A on the x-axis, GENE_B on the y-axis.
  • The cloud is a tilted, elongated sausage — the two genes rise together (positive covariance).
  • PCA will find the long axis of that sausage.

Step 1: centre the data

PCA describes variation around the mean, so we first subtract each gene’s mean (centring).

Means: \bar{A} = (1+3+4+5+7)/5 = 4, \quad \bar{B} = (5+3+6+7+9)/5 = 6.

Centred data (subtract the column mean from each column):

sample A - \bar A B - \bar B
s1 -3 -1
s2 -1 -3
s3 0 0
s4 1 1
s5 3 3

Now the cloud is centred on the origin. Each column sums to 0.

Step 2: the covariance matrix

With centred columns, the covariance uses \dfrac{1}{n-1}\sum (\text{centred}_i)(\text{centred}_j) (here n-1 = 4).

\text{var}(A) = \tfrac{1}{4}\big[(-3)^2 + (-1)^2 + 0^2 + 1^2 + 3^2\big] = \tfrac{1}{4}(9+1+0+1+9) = \tfrac{20}{4} = 5 \text{var}(B) = \tfrac{1}{4}\big[(-1)^2+(-3)^2+0^2+1^2+3^2\big] = \tfrac{1}{4}(1+9+0+1+9) = \tfrac{20}{4} = 5 \text{cov}(A,B) = \tfrac{1}{4}\big[(-3)(-1)+(-1)(-3)+0+1\cdot1+3\cdot3\big] = \tfrac{1}{4}(3+3+0+1+9) = \tfrac{16}{4} = 4

C = \begin{pmatrix} 5 & 4 \\ 4 & 5 \end{pmatrix}

Symmetric, as promised. The large positive off-diagonal says the two genes strongly co-vary.

Step 3: the eigenvalues of C

An eigenvector \mathbf{v} satisfies C\mathbf{v} = \lambda\mathbf{v}, i.e. (C - \lambda I)\,\mathbf{v} = \mathbf{0} with \mathbf{v} \neq \mathbf{0}.

That is only possible if C - \lambda I has no inverse, so its determinant is 0:

C - \lambda I = \begin{pmatrix} 5 - \lambda & 4 \\ 4 & 5 - \lambda \end{pmatrix}, \qquad \det(C - \lambda I) = (5 - \lambda)(5 - \lambda) - 4 \cdot 4 = 0

Solve it:

(5 - \lambda)^2 = 16 \quad\Rightarrow\quad 5 - \lambda = 4 \ \text{ or } \ 5 - \lambda = -4 \quad\Rightarrow\quad \lambda = 1 \ \text{ or } \ \lambda = 9

  • The eigenvalues are 9 and 1. Each is the variance along its PC, so PC1 (λ = 9) carries most of the spread.
  • Check: they add up to 5 + 5 = 10, the total variance (the diagonal of C).

Step 3: eigenvectors = principal components

For each eigenvalue, solve (C - \lambda I)\,\mathbf{v} = \mathbf{0} for \mathbf{v} = (x, y):

λ = 9: \begin{pmatrix} -4 & 4 \\ 4 & -4 \end{pmatrix}\begin{pmatrix} x \\ y \end{pmatrix} = \mathbf{0} gives -4x + 4y = 0, so y = x: direction (1, 1).

λ = 1: \begin{pmatrix} 4 & 4 \\ 4 & 4 \end{pmatrix}\begin{pmatrix} x \\ y \end{pmatrix} = \mathbf{0} gives 4x + 4y = 0, so y = -x: direction (1, -1).

Check: C\begin{pmatrix} 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 9 \\ 9 \end{pmatrix} = 9\begin{pmatrix} 1 \\ 1 \end{pmatrix} ✓

Length 1: (1, 1) has length \sqrt{2} \approx 1.41, so PC1 = (0.71,\ 0.71) and PC2 = (0.71,\ -0.71).

  • PC1 lies along the long axis of the cloud; PC2 is perpendicular.
  • In R, eigen(C) does all of this in one call.

Principal components are new axes

The two eigenvectors form a new, rotated coordinate system:

  • PC1: the direction of greatest variance (biggest eigenvalue).
  • PC2: orthogonal to PC1, the direction of the next greatest variance.
  • In general, PCk is orthogonal to all earlier PCs and captures the largest remaining variance.

We have not thrown anything away yet — we have only rotated the axes to line up with the data cloud. Same points, new description.

The PCs are perpendicular to each other (dot product 0) and each has length 1.

A score is a sample’s position along a PC

A score is the coordinate of a sample along a PC — its projection onto that PC direction (dot product with the unit eigenvector).

For a centred sample \mathbf{x} and unit PC direction \mathbf{u}:

\text{score} = \mathbf{x} \cdot \mathbf{u} = (\text{centred A}) \cdot (\text{PC1 weight for A}) + (\text{centred B}) \cdot (\text{PC1 weight for B})

Sample s1 = (-3, -1), PC1 = (0.71, 0.71): score = (-3)(0.71) + (-1)(0.71) \approx -2.83.

  • The PC1 score tells you where the sample sits along the long axis.
  • Samples with similar biology get similar scores; different groups spread apart along PC1.
  • Plotting PC1 score vs PC2 score for all samples is the PCA plot you see in papers.

Loadings: how much each gene contributes

The loadings of a PC are just the entries of its eigenvector — one weight per gene.

For PC1 = (0.71, 0.71):

\text{PC1 score} = 0.71 \cdot (\text{gene A}) + 0.71 \cdot (\text{gene B}).

  • A gene with a large loading (positive or negative) drives that PC.
  • A gene with a loading near zero barely matters for that PC.
  • Loadings tell the biological story: “PC1 is high in the immune-gene programme”, read off from which genes load heavily.

Scores describe samples; loadings describe genes. Two sides of the same eigenvector.

Each eigenvalue gives its PC’s share of the variance

Each eigenvalue is the variance along its PC. The fraction of total variance captured by PCk is

\text{variance explained by PC}_k = \frac{\lambda_k}{\lambda_1 + \lambda_2 + \cdots + \lambda_p}.

For our example (\lambda_1 = 9, \lambda_2 = 1, total = 10):

\text{PC1}: \frac{9}{10} = 90\%, \qquad \text{PC2}: \frac{1}{10} = 10\%.

  • PC1 alone captures 90% of the variation — a 1-D summary keeps most of the signal.
  • The sum of all eigenvalues equals the total variance (the trace of C: 5+5=10) — so the fractions add to 100%.

Try it: rotate the axis

Optional Step 0: scaling

Centring is always done. Scaling (dividing each gene by its standard deviation) is a choice.

  • Without scaling, a gene measured on a large numeric range dominates the variance — and therefore PC1 — just because of its units, not its biology.
  • With scaling, every gene has variance 1, so all genes get an equal say. (This uses the correlation matrix instead of the covariance matrix.)

When to scale:

  • Variables on different units (gene expression + a pH + a temperature)? Scale.
  • All variables already comparable (all log-expression)? Often leave unscaled.

A PCA plot shows which samples are alike

A PCA plot is a scatter of samples: PC1 score on x, PC2 score on y.

  • Clusters of points = groups of samples with similar overall expression (e.g. tumour vs normal).
  • Separation along PC1 = the biggest source of variation splits those groups.
  • Outliers = possible failed libraries, contamination, or a genuinely unusual sample — worth checking.
  • A PC that separates by processing date, not biology, reveals a batch effect.

PCA is unsupervised: it never saw the labels. If the biology falls out anyway, the signal is strong.

What PCA does and does not do

Does:

  • Find orthogonal axes of decreasing variance — a compact, ranked summary.
  • Give an honest first look at structure, outliers, and batch effects.
  • Reduce thousands of genes to a few interpretable components.

Does not:

  • Use sample labels (it is unsupervised — it optimises variance, not class separation).
  • Guarantee PC1 is the biologically interesting axis — the largest variance might be a batch effect.
  • Capture non-linear structure — PCs are straight-line combinations of genes.

Summary

Concept Key fact
Goal rotate axes to line up with the data cloud; keep the high-variance ones
Covariance matrix C symmetric p\times p; diagonal = variances, off-diagonal = covariances
Principal components eigenvectors of C — orthogonal, ranked by variance
Eigenvalue \lambda_k variance captured along PCk
Centre first subtract each gene’s mean (always)
Scale? divide by SD when units differ (correlation vs covariance matrix)
Score sample projected onto a PC: \mathbf{x}\cdot\mathbf{u} (describes samples)
Loading eigenvector entry = gene’s weight in the PC (describes genes)
Variance explained \lambda_k / \sum_j \lambda_j; sums to 100% (denominator = trace)
Unsupervised never uses labels; PC1 = most variance, not necessarily the biology

Next (PCA in R): prcomp does centring, scaling, and the eigen-decomposition in one call — you will read its scores, loadings, and variance explained.