Principal component analysis

Principal component analysis

Principal component analysis condenses many correlated variables into a few uncorrelated components that capture most of the variation in a dataset.

What it is

Principal component analysis (PCA) is a multivariate technique that takes a dataset with many measured variables, such as absorbance at hundreds of wavelengths or dozens of process readings, and re-expresses it with a smaller number of new variables called principal components (PCs). Each PC is a weighted combination of the original variables. The first captures as much of the variation as possible, the second as much of the remainder as possible, and so on, with all PCs uncorrelated with one another.

PCA answers the question: what are the main patterns of variation in my data, and which samples and variables drive them? It is used in spectroscopy and chemometrics, genomics, image compression, process monitoring, sensory science and as a pre-processing step for machine learning.

Fundamentals

The data matrix

The data are arranged as a matrix X with n rows (samples or observations) and p columns (variables). Each column is mean-centered, meaning its average is subtracted, so that PCA describes variation about the mean. If the variables have different units or very different ranges, each column is also divided by its standard deviation. This is called autoscaling or standardization, and it is equivalent to running PCA on the correlation matrix instead of the covariance matrix.

Finding the components

For centered data the covariance matrix is

C = XTX / (n − 1)

a p × p matrix whose diagonal holds the variance of each variable and whose off-diagonal entries hold the covariances between pairs of variables. XT is the transpose of X. The principal components are the eigenvectors of C:

C vk = λk vk

Here vk is the k-th eigenvector, called the loading vector: a unit-length set of weights, one per original variable. λk is the matching eigenvalue, equal to the variance of the data along that direction. The components are sorted so that λ1 ≥ λ2 ≥ …, and the fraction of total variance explained by PC k is λk / Σλ. Software usually computes the same result through the singular value decomposition (SVD) of X, which is numerically more stable.

Scores and loadings

Projecting the data onto the loadings gives the scores:

T = X P

T is the n × A matrix of scores (the coordinates of each sample on each retained PC), P is the p × A matrix of loadings, and A is the number of components kept. Loadings tell you how much each original variable contributes to a PC; scores tell you where each sample lies. The data can be approximately rebuilt as X ≈ T PT, with a residual matrix E holding whatever the retained PCs do not capture.

The geometric picture

Think of each sample as a point in p-dimensional space. PC1 is the line through the cloud along which the points are most spread out; PC2 is perpendicular to it, along the next-largest spread. Plotting samples on the first two or three PCs gives a low-dimensional view that preserves as much spread as possible.

PCA is linear and unsupervised: it uses only X, never class labels or a response, so the direction of greatest variance is not necessarily the one most relevant to your question. The sign of each PC is arbitrary.

Basic operation

PCA runs in a few lines in R (prcomp), Python (scikit-learn’s PCA), MATLAB or dedicated chemometrics packages. Most of your decisions concern preparing the data.

  1. Assemble the data matrix. Put samples in rows and variables in columns, with the same variables measured for every sample. Keep sample information (group, batch, date) in a separate table so you can color plots by it later.
  2. Handle missing values and gross errors. Standard PCA needs a complete matrix, so remove or impute missing entries. Check for transcription errors or failed measurements that would dominate the variance.
  3. Pre-process. Always mean-center. Autoscale when variables have different units or ranges, for example temperature in °C alongside pressure in kPa. For spectra recorded on a common scale, centering alone is often used, sometimes after baseline correction or normalization.
  4. Fit the model. The software returns eigenvalues (or explained variance per component), loadings and scores.
  5. Choose how many components to keep. Look for an “elbow” in the scree plot, check the cumulative variance explained, or use cross-validation, which tests how well the model predicts left-out data.
  6. Look at the output. A scores plot (PC1 against PC2, one point per sample) shows clusters, trends and outliers. A loadings plot shows which variables drive each PC. A biplot overlays the two.
  7. Check diagnostics. Hotelling’s T2 measures how far a sample sits from the center within the model, and the Q residual measures how far it sits from the model plane; both flag unusual samples.
  8. Apply to new data. Pre-process new samples with the original means and standard deviations, then project them onto the stored loadings.

Good practice

  • Decide on scaling deliberately. Without autoscaling, variables with large numerical ranges dominate PC1 regardless of their importance.
  • Reuse the original centering and scaling values for new data; recalculating them shifts the model.
  • A large share of variance is not the same as relevance; a small PC can carry the signal you care about.
  • One extreme outlier can define a whole component. Investigate it and justify any removal.
  • Do not read meaning into the sign of a PC; a component and its negative are equivalent.
  • With very few samples, apparent patterns may just be noise.

Key terms

  • Principal component — a new variable formed as a weighted linear combination of the original variables.
  • Loading — the weight of an original variable in a principal component.
  • Score — the coordinate of a sample along a principal component.
  • Eigenvalue — the variance captured by a principal component.
  • Explained variance — the share of total variance captured by one component or a set of them.
  • Autoscaling — centering each variable and dividing it by its standard deviation.
  • Scree plot — a plot of eigenvalue against component number, used to decide how many components to keep.

Video coming soon

About five minutes on Principal component analysis: how it works in detail, how the data is acquired, and how it is analyzed.

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping