STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 22
Multivariate Analysis
Multivariate statistics analyses data where multiple measurements are taken on each individual. Correlations among variables require special techniques that extend univariate methods. This chapter introduces principal component analysis (PCA) and cluster analysis for exploratory multivariate analysis.
Principal Component Analysis (PCA)
PCA is a dimension–reduction technique that transforms correlated variables into a smaller set of uncorrelated variables called principal components. These components are linear combinations of the original variables and capture the maximum variance.
Computation
Given a data matrix (centred and standardised), PCA involves computing the covariance matrix and its eigenvalues and eigenvectors . The first principal component is , and so on. The proportion of variance explained by the th component is .
Example: Iris Dataset
The iris dataset contains measurements of sepal length, sepal width, petal length and petal width for 150 flowers. PCA can reduce these four variables to two components that capture most variation.
Worked example
PCA in R, Python and Julia.
data(iris)
pr <- prcomp(iris[,1:4], scale. = TRUE)
summary(pr)
plot(pr$x[,1], pr$x[,2], col=iris$Species)
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris
iris = load_iris()
X = StandardScaler().fit_transform(iris.data)
pca = PCA(n_components=2)
components = pca.fit_transform(X)
print(pca.explained_variance_ratio_)
using DataFrames, MultivariateStats, RDatasets
iris = dataset("datasets", "iris")
X = Matrix(scale(iris[:,1:4]))
pca_model = fit(PCA, X; maxoutdim=2)
components = transform(pca_model, X)
princomp_var = pca_model.eigvals / sum(pca_model.eigvals)
The first two components explain a large proportion of variance, and plotting them reveals species separation.
Cluster Analysis
Cluster analysis groups observations into clusters such that observations within a cluster are similar and observations in different clusters are different. Common methods include:
K–means: partitions data into clusters by minimising within–cluster sums of squares. Sensitive to initialisation and outliers.
Hierarchical clustering: builds a hierarchy of clusters using linkage methods (single, complete, average). Produces a dendrogram.
Example: Clustering Wine Data
The wine dataset contains chemical analysis of wines from three grape cultivars. We cluster the samples using k–means and hierarchical clustering.
Worked example
Clustering in R and Python.
library(cluster)
data(wine, package="rattle.data")
X <- scale(wine[, -1])
kmeans_fit <- kmeans(X, centers=3)
table(kmeans_fit$cluster, wine$Type)
hc <- hclust(dist(X), method="complete")
plot(hc)
from sklearn.cluster import KMeans
from scipy.cluster.hierarchy import linkage, dendrogram
import matplotlib.pyplot as plt
# X is standardized data matrix
kmeans = KMeans(n_clusters=3, random_state=0).fit(X)
print(kmeans.labels_)
Z = linkage(X, method='complete')
dendrogram(Z)
plt.show()
These methods cluster the wines; comparing clusters to actual grape cultivar labels assesses clustering performance.
Exercises
Practice
Perform PCA on a dataset of your choice (e.g., the US arrest data). How many components are needed to explain 90% of the variance? Interpret the loadings.
Use k–means to cluster the iris data into three clusters. Compare cluster assignments to species labels. Try different values of and compute the silhouette score.
Apply hierarchical clustering to the same data with different linkage methods. Produce dendrograms and comment on differences.
Discuss the limitations of PCA and clustering. When might you prefer supervised methods such as discriminant analysis?
References
Jolliffe IT, Cadima J. Principal component analysis: a review and recent developments. Phil. Trans. R. Soc. A. 2016;374(2065):20150202.
Kaufman L, Rousseeuw PJ. Finding Groups in Data: An Introduction to Cluster Analysis. Wiley; 1990.
Everitt BS, Hothorn T. An Introduction to Applied Multivariate Analysis with R. 2nd ed. Springer; 2011.