Introduction

Welcome to this lesson on Principal Component Analysis (PCA), a powerful technique widely applied in data analysis and machine learning to reduce high-dimensional data into lower dimensions, effectively simplifying the dataset while still retaining the relevant information. In this lesson, we'll look at how we can prepare our data, how to apply PCA using R, how to understand the percentage of variance explained by each principal component (explained variance ratio), and finally, how to visualize the results of our PCA.

Preparing the Data

Before moving forward, let's first apply what we've learned to a dataset to standardize the data:

# Define the dataset
data <- data.frame(
  weight_lbs = c(150, 160, 155, 165, 170, 160, 158, 175, 180, 170),
  height_inches = c(68, 72, 66, 69, 71, 65, 67, 70, 73, 68),
  height_cm = c(172.72, 182.88, 167.64, 175.26, 180.34, 165.1, 170.18, 177.8, 185.42, 172.72)
)

# Standardize the data
data_scaled <- as.data.frame(scale(data))
PCA with R
The Importance of Explained Variance Ratio

An informative aspect of PCA is the explained variance ratio, which signals the proportion of the data's variance falling along the direction of each principal component. This information is key, as it tells us how much information we would lose if we ignored the less important dimensions and kept only the ones contributing most to the variance.

You can view the explained variance ratio using the summary() function:

# View explained variance ratio
summary_pca <- summary(pca_result)
print(summary_pca$importance)

The importance matrix has three rows:

  1. Standard deviation: The standard deviation of each principal component (pca_result$sdev).
  2. Proportion of Variance: The fraction of the total variance explained by each principal component.
  3. Cumulative Proportion: The cumulative variance explained up to each principal component.

To extract the proportion of variance explained by the first two principal components:

explained_variance <- summary_pca$importance[2, 1:2]
print(explained_variance)

The output will be similar to:

   PC1       PC2 
0.8400996 0.1599004 

This means that the first principal component explains about 84% of the variance, while the second principal component explains about 16% of the variance. In this case, the first principal component is the most important one, as it captures the majority of the variance in the data.

Visualizing the Results of PCA

Finally, let's visualize our PCA results via a scatter plot using ggplot2. Such a visualization can be incredibly insightful when dealing with high-dimensional data, letting us see the structure of our data in lower-dimensional space.

library(ggplot2)

# Create a data frame for plotting
plot_data <- as.data.frame(pca_scores)
colnames(plot_data) <- c("PC1", "PC2")

ggplot(plot_data, aes(x = PC1, y = PC2)) +
  geom_point(size = 3) +
  labs(
    x = "Principal Component 1",
    y = "Principal Component 2",
    title = "PCA on Measurement Dataset"
  ) +
  theme_bw() +
  theme(panel.grid = element_line())

Output:

You will see a scatter plot showing the data projected onto the first two principal components.

Lesson Summary and Practice

Congratulations! You've learned how to prepare data for PCA, how to perform PCA with R using the prcomp() function, how to interpret the explained variance ratio, and how to visualize PCA results using ggplot2. Try experimenting with different datasets and choosing different numbers of principal components to see how these choices affect the explained variance and the resulting plots. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal