H. Sherry Zhang Department of Statistics and Data Sciences The University of Texas at Austin
Fall 2026
Learning objectives
Understand what problem dimension reduction solves, and what principal components (PCs) are
Perform principal component analysis (PCA) in R:
prepare the data: remove missing values and scale the variables (prcomp(scale = TRUE))
compute PCA with prcomp() and read its output: standard deviations, rotation (loadings), center/scale
choose the number of PCs with a scree plot (factoextra::fviz_eig())
get the projected data with broom::augment() and plot the first two PCs
interpret the PCs with a biplot (factoextra::fviz_pca_biplot())
Motivation: dimension reduction
Hand shadow puppets: a 3D hand is projected onto a 2D wall as a shadow.
From a good angle, the shadow still looks like a rabbit: most of the information is kept.
From a bad angle, it’s just a blob.
Principal component analysis (PCA)
PCA is a linear dimension reduction method: it finds a set of new variables, the principal components (PCs), that capture as much of the variation in the data as possible.
Mathematically, PCA solves an optimization problem: maximize the variance of the projected data, subject to some constraints.
(Optional, see the Appendix) The solution is given by the singular value decomposition (SVD) of the centered data matrix \(X\).
In R, prcomp() does all of this for you.
library(tidyverse)as_tibble(penguins)
# A tibble: 344 × 8
species island bill_len bill_dep flipper_len body_mass sex year
<fct> <fct> <dbl> <dbl> <int> <int> <fct> <int>
1 Adelie Torgersen 39.1 18.7 181 3750 male 2007
2 Adelie Torgersen 39.5 17.4 186 3800 female 2007
3 Adelie Torgersen 40.3 18 195 3250 female 2007
4 Adelie Torgersen NA NA NA NA <NA> 2007
5 Adelie Torgersen 36.7 19.3 193 3450 female 2007
# ℹ 339 more rows
PCA pre-processing: missing values
Do you think missing values will be a problem for PCA?
The fviz_eig() function draws the scree plot in one command.
factoextra::fviz_eig(res_pca, addlabels =TRUE)
This plot is called a scree plot - used to tell the number of principal components to retain. Choose the number at the “elbow” point: k = 2 here.
Variable plot: how variables contribute to PC1 and PC2
factoextra::fviz_pca_var(res_pca)
Biplot: observations and variables together
factoextra::fviz_pca_biplot(res_pca)
Interpret the first two PCs
PC1 contrasts overall size (flipper length, body mass, bill length) with bill depth: Gentoo penguins are larger with shallower bills, so they have high PC1 scores.
PC2 is driven mostly by bill depth and bill length: Chinstrap penguins have much longer bills than Adelie, so they have lower PC2 scores.
Use Lagrange multipliers to solve the constrained optimization problem:
\[ L(\phi_1, \lambda) = \phi_1^T X^T X \phi_1 - \lambda (\phi_1^T \phi_1 - 1)\] Take the derivative and set it to zero gives \[X^TX\phi_1 = \lambda \phi_1\]
This is the eigenvalue equation for the matrix \(X^TX\).
Recognize \(X^TX\) is related the covariance matrix: \(S = \frac{1}{n-1}X^TX\), the first principal component (\(\phi_1\)) is the eigenvector of \(X^TX\) corresponding to the largest eigenvalue.
Computing the PCs: SVD or eigen-decomposition
The first principal component (\(\phi_1\)) is the eigenvector of \(X^TX\) corresponding to the largest eigenvalue. This can be obtained through:
Singular value decomposition (SVD) of \(X\) (always apply):
\[X = UDV^T\]
Eigen-decomposition of \(X^TX = (n-1) S\) (since \(X^TX\) is square):
\[X^TX = V D^2 V^T\]
where
\(X\) is a \(n \times p\) matrix
\(U\) is a \(n \times n\) orthogonal matrix (\(U^TU = 1\))
\(D\) is a \(n \times p\) diagonal matrix with non-negative elements (square root of the eigenvalues of \(X^TX\) arranged in descending order)
\(V\) is a \(p \times p\) orthogonal matrix (eigenvectors: \(V^TV = 1\))
The second principal component (PC2)
The second principal component (PC2): \(Z_2 = \phi_{12}X_1 + \phi_{22}X_2 + \dots + \phi_{p2}X_p\)
The second principal component is the linear combination of \(X_1, X_2, \dots, X_p\) that has the largest variance out of all linear combinations that are uncorrelated with \(Z_1\).
subject to “\(Z_2\) being uncorrelated with \(Z_1\)”.
This is equivalent to constraining \(\phi_2\) to be orthogonal to \(\phi_1\), hence
\[\max_{\phi_{12}, \dots, \phi_{p2}} \text{Var}(Z_2) = \max_{\phi_{12}, \dots, \phi_{p2}} \frac{1}{n}\sum_{i = 1}^n (\sum_{j = 1}^p \phi_{j2} x_{ij})^2 \] subject to the normalization constraint \(\sum _{i=1}^{p} \phi_{i2}^2 = 1\) and the orthogonality constraint \(\sum_{i=1}^{p} \phi_{i1}\phi_{i2} = 0\).