Book contents

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 𝐗\mathbf{X} (centred and standardised), PCA involves computing the covariance matrix 𝐒=1n−1𝐗⊤𝐗\mathbf{S} = \frac{1}{n-1}\mathbf{X}^\top \mathbf{X} and its eigenvalues λ1≥λ2≥⋯\lambda_1 \geq \lambda_2 \geq \cdots and eigenvectors 𝐞1,…\mathbf{e}_1,\dots. The first principal component is 𝐳1=𝐗𝐞1\mathbf{z}_1 = \mathbf{X}\mathbf{e}_1, and so on. The proportion of variance explained by the kkth component is λk/∑iλi\lambda_k/\sum_i \lambda_i.

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:

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

  1. 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.

  2. Use k–means to cluster the iris data into three clusters. Compare cluster assignments to species labels. Try different values of KK and compute the silhouette score.

  3. Apply hierarchical clustering to the same data with different linkage methods. Produce dendrograms and comment on differences.

  4. Discuss the limitations of PCA and clustering. When might you prefer supervised methods such as discriminant analysis?

References