Principal Component Analysis (PCA) Workshop
Published:

Adapted from my oral presentation at Epic Bio BDS Learning Seminar (December 8, 2022).
Supervised vs. Unsupervised Learning
Supervised learning attempts to model relationships between p features X1, X2, …, Xp with a response variable Y based on n observations. Examples include regression and classification models.
Unsupervised learning instead explores relationships between the p features in the absence of any response variable (no prediction). This is an important part of exploratory data analysis (EDA).
Two common techniques in unsupervised learning are:
- Principal Component Analysis (PCA)
- Clustering techniques (hierarchical clustering, k-means clustering, etc.)
Source: An Introduction to Statistical Learning (James, Witten, Hastie, and Tibshirani, 2013)
What is PCA?
Principal Component Analysis (PCA) is a widely used unsupervised learning technique for the analysis of high-dimensional data.
High-dimensional data are data with many more features (p) than observations (n). High-throughput sequencing libraries (e.g. RNA-seq) are a classic example of high-dimensional data (p = 20,000 genes; n = 10 libraries).
PCA is a technique that allows us to summarize data with a large set of features with a smaller number of representative features that explain most of the variability present in the original data by computing principal components.
Principal components are the directions in feature space along which the original data are highly variable.
The most common applications of PCA are:
- Visualization of high-dimensional data
- Feature generation through dimensionality reduction
Source: An Introduction to Statistical Learning (James, Witten, Hastie, and Tibshirani, 2013)
Why PCA? Visualizing High-Dimensional Data
We can easily visualize 150 datapoints with 2 features using a 2D scatterplot. Increasing dimensionality to 3 features, we can still use a 3D scatterplot. But what about 4 features? Or p » 4 features? Since we live and evolved in 3 spatial dimensions, it becomes exceedingly challenging to spatially visualize high-dimensional data with p » 4.

With such high-dimensional data, one natural question is: are all features/dimensions equally informative?
There often exist redundant features that are highly correlative with other features – they don’t actually add much new information.
One natural way of visualizing high-dimensional data is to find a low-dimensional representation of the data that captures most of the information present. In fact, the manifold hypothesis states that high-dimensional datasets that occur in the real world lie along low-dimensional latent manifolds.
Workshop: Exploring the Iris Dataset in R
The built-in R iris dataset is a classic tutorial for data exploration (often used in introductiory workshops teaching R). It contains 150 observations of 3 flower species (setosa, versicolor, virginica) with 4 features: Sepal Length, Sepal Width, Petal Length, and Petal Width. here we’ll use it to explore a simple case of applying Principal Component Analysis.

Since there are 4 features, we can’t directly visualize the global properties of these 150 observations in a single plot. How can we answer questions like which flower species are the most similar?
Exploring pairwise relationships
Well, since there are only 4 features, we can explore pairs of variables and plot each possible pairwise combination in 2D. For example, maybe we hypothesize there exists a relationship between width and length of sepals and petals respectively:
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(size = 2, alpha = 0.7) +
labs(x = "Sepal Length", y = "Sepal Width", title = "Sepal Length vs Width") +
theme_minimal()
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
geom_point(size = 2, alpha = 0.7) +
labs(x = "Petal Length", y = "Petal Width", title = "Petal Length vs Width") +
theme_minimal()


We can see a relationship between petal length and width but not much structure in the sepal measurements. However, we haven’t learned anything about which species are globally most similar. Let’s now add color by species to the plot:
ggplot(iris, aes(x = Petal.Length, y = Petal.Width, color = Species)) +
geom_point(size = 2, alpha = 0.7) +
labs(x = "Petal Length", y = "Petal Width", title = "Petal Length vs Width") +
theme_minimal()

By coloring the points by species, we can start to see that versicolor and virginica might be more closely related. But this is only plotting 2 of the 6 possible feature relationships, so it doesn’t capture all information in our data.
Plotting all pairwise relationships
There are nice statistical functions that automate this process for us (e.g. ggpairs from the GGally package) and plot relationships between all features:
library(GGally)
ggpairs(iris, columns = 1:4, aes(color = Species, alpha = 0.5)) + theme_minimal()

This works great for 4 features – we get a nice summary of how the different features relate to one another, requiring $\binom{4}{2}$ scatterplots. However, the number of plots needed grows combinatorially with features (e.g. 10 features requires 45 plots, 100 features requires 4,950 plots).
PCA to the rescue
What we need is a statistical tool that captures as much global information as possible about the relationship between these 4 features and allows us to visualize them in 2D.
This is exactly what PCA does. Below we compute the PCA of this 4-dimensional dataset and visualize it in R.
iris_pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)
pca_df <- data.frame(
PC1 = iris_pca$x[, 1],
PC2 = iris_pca$x[, 2],
Species = iris$Species
)
ggplot(pca_df, aes(x = PC1, y = PC2, color = Species)) +
geom_point(size = 2, alpha = 0.7) +
labs(
x = sprintf("PC1 (%.1f%% variance)", summary(iris_pca)$importance[2, 1] * 100),
y = sprintf("PC2 (%.1f%% variance)", summary(iris_pca)$importance[2, 2] * 100),
title = "PCA of Iris Dataset"
) +
theme_minimal()

We now have a 2-dimensional projection of a 4-dimensional feature space. The PCA clearly shows that setosa is well-separated from the other two species, and that versicolor and virginica overlap considerably – confirming that they are the most similar pair.
How Does PCA Work?
PCA attempts to find a low-dimensional representation of the data that captures as much information about the features as possible .
The idea is that n observations live in p-dimensional space, but not all of these dimensions are equally informative. PCA distills these p dimensions down to L dimensions where the new dimensions are linear combinations of the originals constructed to capture as much possible variation in the n observations as possible.
Finding the principal components
For a dataset of n observations with p features, the first principal component Z1 is the normalized linear combination of features:
Z1 = φ11X1 + φ21X2 + … + φp1Xp
that has the largest variance. The coefficients φ11, φ21, …, φp1 are called the loadings of the first principal component.
Source: An Introduction to Statistical Learning (James, Witten, Hastie, and Tibshirani, 2013)
In the example above, we are looking at population size and ad spending for 100 cities (n = 100, p = 2). The first principal component Z1 = 0.839X1 + 0.544X2 is along the solid green line (the direction of largest variance). The second principal component Z2 is the linear combination that has maximal variance out of all linear combinations that are uncorrelated with Z1: Z2 = 0.544X1 - 0.839X2 (the dotted blue line, perpendicular to PC1).
Proportion of variance explained
Each principal component is constructed to maximize variability explained, but each successive principal component explains less and less of the total variance. This can be visualized as a scree plot:
var_explained <- summary(iris_pca)$importance[2, ] * 100
scree_df <- data.frame(PC = paste0("PC", 1:4), Variance = var_explained)
scree_df$PC <- factor(scree_df$PC, levels = scree_df$PC)
ggplot(scree_df, aes(x = PC, y = Variance)) +
geom_col(fill = "steelblue") +
geom_line(aes(group = 1), linewidth = 1) +
geom_point(size = 3) +
labs(x = "Principal Component", y = "% Variance Explained",
title = "Scree Plot - Iris PCA") +
theme_minimal()

For the iris dataset, PC1 explains 73.0% and PC2 explains 22.9% of the total variance – together they capture 95.9% of the total variance, providing a very accurate 2D representation of the 4D data.
Geometric interpretation
Statistically, principal component loading vectors are directions in feature space along which data varies the most. Geometrically, principal components provide low-dimensional surfaces that are closest to the datapoints.
Source: An Introduction to Statistical Learning (James, Witten, Hastie, and Tibshirani, 2013)
Computing the first two principal components defines the plane closest to our data (minimizing the Euclidean squared distance). When the original data are projected onto this plane, we retain the global features that distinguish the groups and are able to visualize them in a lower dimension.
Source: An Introduction to Statistical Learning (James, Witten, Hastie, and Tibshirani, 2013) —
Mathematical Walkthrough: Student Grades Example
Now we’ll go step-by-step through a hands-on example on how to compute principal components from a small dataset of the academic grades of 5 students in 3 classes.
Adapted from the example given in The Mathematics Behind Principal Component Analysis
Goal: Transform a given dataset X of dimension p to an alternative dataset Y of smaller dimension k.
The steps are:
- Organize data into matrix X of dimensions n × p
- Compute the mean for each dimension p
- Calculate the covariance matrix of the whole dataset
- Compute the eigenvalues and corresponding eigenvectors of the covariance matrix
- Sort eigenvectors by decreasing eigenvalues and select k eigenvectors to form a p × k matrix W
- Use the eigenvector matrix W to transform original data and project onto the new basis
Step 1: Organize the data
Let’s calculate PCA for a simple dataset about the grades of 5 students in 3 classes: Math, English, and Art.

Step 2: Compute the mean for each dimension
We remove the labels from our dataframe and get a numerical matrix A with 3 dimensions, then compute the mean of each:


Step 3: Calculate the covariance matrix
We compute the covariance of two variables using the standard formula:

This yields the following covariance matrix:

Key observations about the covariance matrix:
- The diagonal entries (blue) show the variance of each test
- Covariance between math and other subjects is positive – higher math scores are correlated with higher scores in other subjects
- Covariance between english and art is zero – there is no correlation between english and art scores
Step 4: Compute eigenvalues and eigenvectors
Now comes the fun part! Thinking about eigenvectors/eigenvalues may sound intimidating because the name seems complicated but they are in fact deeply intuitive features of a vector space. When you understand how they work and what they represent, you’ll find that they are some of the most beautiful representations in linear algebra.
Intuitively, an eigenvector is a vector whose direction remains unchanged when a linear transformation is applied to it.
Definition: Let A be a square matrix, v a vector, and λ a scalar that satisfies Av = λv, then λ is an eigenvalue associated with eigenvector v of A.
In practice, we compute the eigenvalues of A by solving for roots of the characteristic equation det(A - λI) = 0.



Solving the characteristic equation gives three eigenvalues:

Having calculated the eigenvalues, we plug each into the equation (A - λI)v = 0 to calculate their respective eigenvectors:

Step 5: Sort eigenvectors and form matrix W
We now have 3 distinct eigenvalues, each with their associated eigenvector. The greater the eigenvalue, the more variance explained by that principal component.

To reduce our 3-dimensional data to 2-dimensional PC space, we construct the matrix W from the two eigenvectors with the largest eigenvalues.
Step 6: Project onto the new basis
We use the eigenvector matrix W (3×2) to transform our original data A (5×3) onto 2-dimensional PC space:
Let’s now compute this using R so we can visualize the result:
grades <- matrix(c(90, 60, 90,
90, 90, 30,
60, 60, 60,
60, 60, 90,
30, 30, 30), nrow = 5, byrow = TRUE)
colnames(grades) <- c("Math", "English", "Art")
students <- c("Student 1", "Student 2", "Student 3", "Student 4", "Student 5")
grades_pca <- prcomp(grades, center = TRUE, scale. = FALSE)
proj_df <- data.frame(
PC1 = grades_pca$x[, 1],
PC2 = grades_pca$x[, 2],
Student = students
)
ggplot(proj_df, aes(x = PC1, y = PC2, label = Student)) +
geom_point(size = 3, color = "steelblue") +
geom_text(vjust = -1, size = 3.5) +
labs(
x = sprintf("PC1 (%.1f%% variance)", summary(grades_pca)$importance[2, 1] * 100),
y = sprintf("PC2 (%.1f%% variance)", summary(grades_pca)$importance[2, 2] * 100),
title = "PCA Projection of Student Grades"
) +
theme_minimal()

PC1 captures 57.5% and PC2 captures 39.7% of the variance, together accounting for 97.2% of the information in the original 3D data.
PCA in Scientific Applications
PCA is an extremely useful and widely used tool for visualizing high-dimensional biological data. Below I’ll share two examples from literature.
ATAC-seq data (~100K features)
In my main thesis project from grad school, I investigated how the chromatin accessibility landscape of different sub-populations of the neural stem cell lineage changed with age in mice.

To do this, I generated 2-3 ATAC-seq (Assay for Transposase-Accessible Chromatin using sequencing) libraries for each young vs old cell populaton to measure genome-wide chromatin accessibility (yielding >100,000 different genomic sites with chromatin accessibility values per library). One of the first statistical visualizations I performed on these datasets was PCA to get a sense of the global similarities and differences between the overall chromatin ladnscape of different celltypes and ages. As you can see below, this global PCA shows distinct clustering of endothelial cells away from the NSC lineage along PC1 and then of proliferating NSCs (aNSCs and NPCs) from non-proliferating subpopulations of this lineage (astrocytes and qNSCs) along PC2.

You may have noticed that the above PCA doesn’t differentiate young from old libraries. This is not because differences between young and old chromatin landscapes within a celltype don’t exist but instead simply because the huge differences in genome accessibility between any two celltypes overwhelms the relatively subtle chromatin changes that are seen with age.
However, we can generate another PCA restricted this time to only young vs old qNSCs and aNSCs (the main celltypes of interest in my paper):

We now see that PC1 (explaining 32.58% of variance) separates aNSCs from qNSCs and that PC3 (explaining 8.87% of variance) separates libraries generated from young vs old neurogenic niches.
Genome-wide SNP data (~500K features)
One of the most visually impressive examples of PCA that I’ve come across in the field of genomics can be found in Carlos Bustamante’s paper “Genes mirror grography within Europe” published in Nature in 2008. In this paper, they characterized genetic variation in a sample of 3,000 European individuals genotyped at over half a million variable DNA sites (single nucleotide polymorphisms (SNPs)) in the human genome. With ~500,000 SNP features per individual, the authors then performed PCA to compress this information into interpretable 2D projections:

Remarkably, the resulting 2D PCA projection clusters individual genomesw with distance metrics reminescent of geopgrahical separation. As the authors write in their abstract: “Despite low average levels of genetic differentiation among Europeans, we find a close correspondence between genetic and geographic distances; indeed, a geographical map of Europe arises naturally as an efficient two-dimensional summary of genetic variation in Europeans.”

Dimensionality Reduction: Amino Acid Descriptors
Beyond visualization, PCA is also a powerful tool for feature engineering through dimensionality reduction.

VHSE (Vectors of Hydrophobic, Steric, and Electronic properties by principal components) is a great example. VHSE is a 8-dimensional vector embedding that can be used to describe the standard 20 amino acids that amke up all proteins in an information-rich way.
To do this, the authors first gathered data on 50 physicochemical features for the 20 naturally occurring amino acids:
- 18 hydrophobic properties
- 17 steric properties
- 15 electronic properties
While this is descriptive, it’s not very concise, and many of these features are likely redundant (e.g. do we really need 18 different values to describe the hydrophobicity of an amino acid?).
The authors performed 3 separate principal component analyses, reducing dimensionality from 50 to 8:
- Hydrophobic properties: 18 → 2 (capturing 74.33% variance)
- Steric properties: 17 → 2 (capturing 78.68% variance)
- Electronic properties: 15 → 4 (capturing 77.97% variance)

So, by using PCA, they dramatically reduced the number of dimensions needed to describe amino acid properties from 50 to 8 while retaining the majority of the information.
PCA vs. t-SNE and UMAP
While this writeup is specifiocally on the toipic of PCA, I wanted to bring up two other visualization tools that have become increasingly popular in the field of genomics, especially when dealing with single cell RNA-seq datasets:
- t-SNE (t-distributed Stochastic Neighbor Embedding)
- UMAP (Uniform Manifold Approximation and Projection for Dimension Reduction)
In single-cell RNA-seq data, we typically have 10,000+ observations, each with ~5,000 dimensions. Due to the “curse of dimensionality,” this leads to an incredibly sparse and disconnected network.
PCA is one natural approach for dimensionality reduction and visualization, but it is not particularly well-suited for the highly non-linear and sparse nature of scRNA-seq data.
Both t-SNE and UMAP are neighbor graph algorithms that attempt to preserve local relationships between data – making them great at defining cell clusters and identifying heterogeneity in single-cell data.
Source: Liu et al., 2020
Key differences:
- PCA: Linear; distances in PCA plots have quantitative meaning; great for bulk data
- t-SNE: Non-linear; preserves local structure; distances between clusters are not meaningful
- UMAP: Non-linear; preserves both local and some global structure; faster than t-SNE
Benefits and Limitations of PCA
Benefits
- Reduces dimensionality of high-dimensional data through linear combinations
- Easy to compute
- Quantitative explanation of proportion of variance explained by PCs
- Distance in PCA plots has quantitative meaning (unlike t-SNE, UMAP, etc.)
Limitations
- Low interpretability of PCs (especially with a high number of features)
- By definition, dimensionality reduction using PCs is accompanied by information loss
- Requires linearly correlative features
- Sensitive to scale of features
- PCA is not robust to outliers
