Principal component analysis (PCA) explained, with an example

Updated

Principal component analysis (PCA) replaces a set of correlated variables with a few new variables, the principal components, that are uncorrelated and capture most of the variation. Each component is a weighted sum of the original variables. When five quality tests on a material all move together because of one or two underlying causes, PCA shows those causes as one or two components, so you can watch two numbers instead of five.

When to use it

  • You measure many correlated characteristics on each part, lot or batch and want to see the main patterns.
  • You want to reduce many variables to a few before plotting, clustering or regression.
  • You want to find unusual lots that look normal on each variable alone but odd in combination.
  • You want inputs for a multivariate control chart, such as a T² chart on component scores.

How it works

  1. Standardize. When the variables are in different units (g/10 min, MPa, %), PCA works on the correlation matrix R, which is the covariance matrix of the standardized variables. Use the covariance matrix only when all variables share the same unit and scale.
  2. Eigen-decomposition. Solve R·v = λ·v. Each eigenvector v holds the coefficients of one component; its eigenvalue λ is the variance of that component. With p standardized variables, the eigenvalues sum to p.
  3. Proportion of variation. Component k explains λₖ / p of the total variation.
  4. Scores. Each observation gets a score on each component:
PCₖ = vₖ₁·z₁ + vₖ₂·z₂ + … + vₖₚ·zₚ

where z are the standardized values. The loading of a variable on a component, v·√λ, is its correlation with that component.

In NumPy the core is one line: w, V = np.linalg.eigh(R), then sort the eigenvalues from largest to smallest.

The sign of an eigenvector is arbitrary: multiplying a whole component by −1 gives the same solution, so different programs may show opposite signs.

Example

A compounding plant tests 30 lots of a polypropylene resin for melt flow index (MFI), density, tensile strength, elongation at break and notched impact strength. The correlation matrix of the five tests:

MFIDensityTensileElongationImpact
MFI1−0.01−0.27−0.54−0.62
Density−0.0110.77−0.66−0.51
Tensile−0.270.771−0.46−0.36
Elongation−0.54−0.66−0.4610.86
Impact−0.62−0.51−0.360.861

The eigenvalues of this matrix:

ComponentEigenvalueProportionCumulative
PC12.9470.5890.589
PC21.5710.3140.904
PC30.2220.0440.948
PC40.1490.0300.978
PC50.1110.0221.000
Scree plot of the resin test correlation matrix0123EigenvaluePC1PC2PC3PC4PC5
Scree plot of the resin test correlation matrix

Two components explain 90.4% of the variation; the other three are small. Their coefficients (eigenvectors):

VariablePC1PC2
MFI0.2630.679
Density0.469−0.378
Tensile0.375−0.559
Elongation−0.549−0.138
Impact−0.518−0.255
  • PC1 is high for dense, strong lots with low elongation and low impact: a stiff-versus-tough axis, consistent with lot-to-lot differences in crystallinity.
  • PC2 is high for lots with a high melt flow and low tensile strength: a flow axis, consistent with differences in molecular weight.

Plotting each lot's scores on the two components shows the lots in one picture:

Scores of 30 resin lots on the first two components-2024PC2 (flow)-2.50.02.55.0PC1 (stiff vs tough)
Scores of 30 resin lots on the first two components

Two lots stand apart. Lot 10 (PC1 = 5.59) is the stiffest and most brittle: density 0.9106 g/cm³, elongation 318% and impact 2.92 kJ/m², against averages of 0.9051, 449% and 4.48. Lot 7 (PC2 = 4.29) has the highest flow, MFI 15.7 g/10 min against an average of 12.0, with low tensile strength. Both deserve a call to the supplier.

How to read the results

  • Decide how many components to keep. Common rules: keep components with eigenvalues above 1 (for a correlation matrix), look for the elbow in the scree plot, or keep enough to explain 80 to 90% of the variation. Here all three point to two.
  • Name components from their large coefficients. Look for variables with large coefficients of the same or opposite sign and ask what physical cause would move them that way.
  • Use the score plot to find unusual observations. Points far from the center in any direction combine values that are rare together.
  • Remember components are not causes. They are directions of joint variation; the physical meaning is your interpretation.

Common mistakes

  • Running PCA on the covariance matrix with mixed units. Elongation, in hundreds of percent, would dominate density, in thousandths.
  • Over-reading small components. The last components often capture measurement noise.
  • Treating signs as meaningful across programs. A component and its negative are the same solution.
  • Using too few observations. With fewer observations than about five per variable, the correlation matrix and its eigenvectors are unstable.

Frequently asked questions

What is the difference between PCA and factor analysis?

PCA rewrites the data exactly as uncorrelated components and keeps the ones that carry most of the variance. Factor analysis assumes a model with a few hidden factors plus unique error for each variable, and usually rotates the solution to make it easier to interpret.

Should I use the correlation or covariance matrix?

Use the correlation matrix when the variables have different units or very different spreads, which is the usual case in quality data. Use the covariance matrix when all variables are the same kind of measurement on the same scale, such as several diameters in mm.

What is a loading?

The loading of a variable on a component is the correlation between them, the eigenvector coefficient times √λ. Here density has a loading of 0.469 × √2.947 = 0.80 on PC1.

Can PCA be used for process monitoring?

Yes. Scores on the first few components can be charted with a T² chart, and the distance of each observation from the component space (the squared prediction error) flags new kinds of variation.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab