R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
119: Principal Component Analysis in R
I've always found that the hardest part of dealing with high-dimensional data isn't the math—it's the visualization. When you have two variables, you have a scatter plot. Three variables, and you can maybe get away with a 3D plot if you're feeling fancy. But once you hit four or five variables, you're essentially flying blind. That's where Principal Component Analysis (PCA) comes in. It's basically a way of asking R, "If I had to squash all this data down into two dimensions without losing the 'soul' of the dataset, how would I do it?"
The Curse of Too Many Columns
Let's take a look at the built-in USArrests dataset. It tracks crime statistics across different US states. It's small, but it has four variables: Murder, Assault, UrbanPop, and Rape. I want to see if there are any natural groupings of states, but I can't plot four axes.
# Let's see what we're dealing with
data(USArrests)
head(USArrests)
pairs(USArrests)
The pairs() plot gives us a matrix of scatter plots. It's useful, but it's a lot of ocular noise. I can see some correlations—like Murder and Assault usually move together—but I can't see the "big picture" of the states as single points in space.
Trying a Naive Compression
I'll use prcomp(), which is the standard workhorse for PCA in R. I'll just throw the data in and see what happens.
# First attempt at PCA
pca_result <- prcomp(USArrests)
summary(pca_result)
Wait. Look at the importance section of that summary. The first principal component (PC1) is explaining almost 99% of the variance. That seems... suspicious. Usually, it's not that clean. Why is PC1 dominating everything?
The Scaling Trap
I suspect it's a units problem. If you look at USArrests, the Assault column has values in the hundreds, while Murder is in the single digits. PCA works by looking at variance. Because Assault has a much larger numerical range, R thinks it's the most "important" variable simply because its numbers are bigger. It's essentially ignoring the other columns because their variance is tiny by comparison.
This is a classic mistake. Whenever you're doing PCA, you almost always need to scale your data so that every variable has a mean of 0 and a standard deviation of 1. Luckily, prcomp has a built-in argument for this.
# Re-running with scaling enabled
pca_scaled <- prcomp(USArrests, scale = TRUE)
summary(pca_scaled)
That looks much more realistic. Now PC1 explains about 62% of the variance, and PC2 explains about 23%. Together, they cover 85% of the "story" the data is telling, but we've reduced our dimensions from four down to two.
Mapping the Noise
Now that we've squeezed the data, I want to see it. The biplot() function is the best way to do this because it shows us both the samples (the states) and the variables (the original columns) as vectors.
biplot(pca_scaled)
When you look at this plot, notice how the arrows for Murder, Assault, and Rape are all pointing in roughly the same direction (to the right). This tells me those three variables are highly correlated—they are essentially measuring the same "thing" (violent crime). UrbanPop is pointing in a different direction, meaning it doesn't track the same way as the crime stats.
If a state is far to the right on the x-axis (PC1), it's high in all those violent crime categories. If it's high on the y-axis (PC2), it likely has a higher urban population relative to its crime rates. We've successfully turned a 4D problem into a 2D map.
📋 Practical Task
Dimensionality Reduction of the Motor Trend Car Road Tests
Using the built-in mtcars dataset, perform a Principal Component Analysis to identify which cars are most similar based on their performance and design specifications.
- Run a PCA on the
mtcarsdataset, ensuring that the data is centered and scaled. - Generate a summary of the PCA to determine how many principal components are required to explain at least 80% of the total variance.
- Create a biplot of the results.
- Identify which variables are most strongly correlated with each other based on the direction of the vectors in your biplot.
There are no comments for now.