AI summary by TwinMind
StatQuest: MDS and PCoA in R
StatQuest with Josh Starmer
7m 44s
Introduction and Setup
- Host: Josh Dharma, Stack Quest tutorial
- Topic: Multi-dimensional scaling (MDS) and Principal Coordinate Analysis (PCOa) in R
- Key concept: Classical/metric MDS = PCOa (same technique)
- Code available on Stack Quest website (link in description)
- Load ggplot2 library for visualization
Fake Data Generation
- Same dataset used in previous PCA Stack Quest tutorial
- Matrix structure:
- 10 columns (samples)
- 100 rows (gene measurements)
- First 5 columns: WT (wild-type) samples
- Last 5 columns: KO (knockout) samples
- Gene names: "gene one", "gene two", etc.
- head() function shows first 6 rows/genes for all 10 samples
PCA Comparison Analysis
- Performed PCA on dataset for comparison purposes
- Steps covered briefly (detailed in previous PCA tutorial)
- PCA plot results:
- Wild-type samples: left side of graph
- Knockout samples: right side of graph
- PC1 (x-axis): accounts for 91% of variation
- PC2 (y-axis): accounts for 2.7% of variation
- Most differences between wild-type and knockout samples
MDS/PCOa Implementation - Euclidean Distance
Step 1: Create Distance Matrix
- Use dist() function
- Transpose matrix (samples as rows)
- Center and scale measurements for each gene (now columns)
- Specify euclidean distance metric
- Note: dist() function has 6 different distance metrics available
Step 2: Perform Multi-dimensional Scaling
- Use cmdscale() function
- cmdscale = Classical Multi-dimensional Scaling
- Parameters:
- Return eigenvalues (for variation calculations)
- Can return doubly centered matrix (both rows and columns centered)
- Note: Could use eigen() function instead, but cmdscale() more practical
Step 3: Calculate Variation
- Calculate variation each axis accounts for using eigenvalues
Step 4: Format and Visualize
- Format data for ggplot
- Create fancy graph with ggplot
MDS Results - Euclidean Distance
- Wild-type samples: left side
- Knockout samples: right side
- X-axis: 91% of variation
- Y-axis: 2.7% of variation
- Results identical to PCA graph (because euclidean metric used)
MDS Implementation - Custom Distance Metric
- Alternative metric: Average of absolute value of log fold change
- Used by edgeR when calling plotMDS function
- Process:
- Calculate log2 values of gene measurements
- Create custom distance matrix (not built into dist() function)
- Calculate average of absolute values of log fold changes
- Create empty matrix, fill with calculated values
- Matrix is symmetrical, only calculate lower triangle
Custom Matrix Processing
- Convert homemade matrix to true distance matrix
- True distance matrix only needs bottom triangle computed
- Use cmdscale() on new distance matrix
- Same steps for variation calculation and ggplot formatting
Comparison of Different Distance Metrics
- Two MDS plots compared:
- Euclidean distance
- Average absolute value of log fold change
- Results similar but not identical
- New graph (custom metric):
- X-axis accounts for 99.2% variation (vs 91% with euclidean)
- Higher variation capture with custom metric
Conclusion
- Tutorial completion
- Subscribe request for more Stack Quest content
- Comment section for additional tutorial suggestions
