AI summary by TwinMind
StatQuest: PCA in R
StatQuest with Josh Starmer
8m 57s
Introduction and Setup
- Tutorial by Josh Stormer from StatQuest, UNC Chapel Hill Genetics Department
- Covers PCA implementation in R with code provided in video description
- Topics: prcomp function usage, PCA plotting (base graphics + ggplot2), variation analysis, loading scores interpretation
Fake Dataset Generation
- Created matrix with 10 samples, 100 genes per sample
- Sample naming convention:
- First 5 samples: WT (wild-type, normal samples)
- Last 5 samples: KO (knockout, missing gene samples)
- Gene naming: Gene1 through Gene100
- Used Poisson distribution for fake read counts (acknowledged negative binomial more appropriate)
- Data structure: samples as columns, genes as rows
PCA Implementation with prcomp
- Key requirement: prcomp expects samples as rows, genes as columns
- Must transpose matrix using T() function to convert from genes-as-rows format
- Without transposition: would show gene relationships instead of sample relationships
- prcomp returns 3 components:
- X: principal components for plotting
- sdev: standard deviations
- rotation: loading scores
Basic PCA Plotting
- Used first two columns of X for 2D plot (PC1 and PC2)
- 10 samples = 10 total principal components available
- PC1: accounts for most variation in original data
- PC2: accounts for second most variation
- Alternative: sometimes use PC2 vs PC3
- Base graphics plot results: 5 samples on each side of graph (clear clustering)
Variation Analysis
- Calculate variation using square of sdev values
- Convert to percentages for meaningful interpretation
- Visualization using barplot function
- Results: PC1 accounts for ~92% of total variation
- High PC1 variation indicates strong difference between clusters
Advanced ggplot2 Visualization
- Data formatting: create data frame with sample IDs, X/Y coordinates
- ggplot components:
- geom_text: plot labels instead of dots
- xlab/ylab: axis labels with variation percentages using paste function
- theme_bw: white background (optional styling)
- ggtitle: graph title
- Enhanced plot shows variation percentages on axes, labeled samples
Loading Scores Analysis
- Loading scores stored in rotation component
- Focus on PC1 loading scores (92% variation)
- Interpretation:
- Large negative values: push samples left
- Large positive values: push samples right
- Analysis workflow:
- Use abs() function for magnitude-based sorting
- Sort magnitudes high to low
- Extract top 10 genes with largest loading score magnitudes
- Identify positive loading scores (push KO samples right)
- Identify negative loading scores (push WT samples left)
