Standardizing, running PCA with prcomp(), choosing how many components to keep, and reading loadings, scores, and biplots, using the iris data from lecture.
The data are R’s built-in iris, so there is no file to download.
What PCA does
PCA takes many correlated variables and rotates them into new, uncorrelated axes called principal components (PCs). PC1 points along the direction of greatest variation, PC2 the next greatest at right angles to PC1, and so on. If the first two or three PCs capture most of the variation, you can plot and analyze them instead of every variable.
Eigenvalue: the variance captured by a PC.
Loadings (eigenvectors): how much each original variable contributes to a PC.
Scores: where each observation falls on the new axes.
PCA is descriptive: there is no p-value. It assumes roughly linear relationships among the variables.
Data: four flower measurements (cm) on 150 irises of three species. Species is not used to build the PCA; we only use it to color the plot afterwards.
iris_pca <-prcomp(iris_scaled, center =FALSE, scale. =FALSE)summary(iris_pca)
Importance of components:
PC1 PC2 PC3 PC4
Standard deviation 1.7084 0.9560 0.38309 0.14393
Proportion of Variance 0.7296 0.2285 0.03669 0.00518
Cumulative Proportion 0.7296 0.9581 0.99482 1.00000
center and scale. are FALSE because scale() already did both. (Giving raw data to prcomp(..., scale. = TRUE) is identical.)
Reading the output:Proportion of Variance is each PC’s share; Cumulative Proportion adds them up.
Step 5: How many PCs to keep?
eigenvalues <- iris_pca$sdev^2eigenvalues
[1] 2.91849782 0.91403047 0.14675688 0.02071484
fviz_eig(iris_pca, addlabels =TRUE)
Common rules: keep PCs with eigenvalue > 1 (Kaiser), or keep PCs before the “elbow” in the scree plot, or keep enough to explain ~80–90%. Here PC1 and PC2 explain about 96%.
A PCA on the correlation matrix of four standardized flower measurements produced two components that together explained 96% of the variation (PC1 73%, PC2 23%). PC1 reflected overall flower size (high loadings for petal length, petal width, and sepal length); PC2 reflected sepal width. The three species separated mainly along PC1.
Report whether variables were standardized, the variance explained by each retained PC, how you chose how many to keep, what the loadings mean, and show the biplot.