class: center, middle, inverse, title-slide .title[ # Seeing Groups, Not Just Gradients: ] .subtitle[ ## Supervised Class Differentiation to Better Interpret Ecological Diversity ] .author[ ###
Nicholas Spyrison, PhD
spyrison@gmail.com
github.com/nspyrison/betaspace ] .date[ ### 2026-07-03 ] --- # Terminology - Mixed audiences; terms written in Biology and verbally bridge back to Statistics |Term |Alias |Dimension notation | |:---------------|:------------------------------|:-------------------------| |Sample |Site, observation, row |n | |Variable |Colunm, feature, metric |p | |Metadata |Context- or demographic- data |n rows | |Abundance table |Data |n x p | |Distance (*) |Dissimilarity |n x n', where n' <= n (*) | |Ordination |Dimension reduction, embedding |n x d, where d <= n' <= n | .footnote[ (*) Some Biological distances do not meet the triangle inequality, causing some negative eigenvalues, negative eigenvalues are dropped typically ] --- # Ecological Diversity - **Alpha diversity, α (1),** richness and evenness within a sample - Typically choose 2-3 of 7 metrics and visualized separately; **we illustrate the benefit of viewing these metrics together** - **Beta diversity, β (1),** compositional differences between sample - Typically 10s-1000s of components visualized as the first 2 components; **we illustrate group-based ordination method, CAP, and visualizing more than 2 components** - Beyond the scope: - Gamma diversity, γ (1), total species diversity in a landscape, `γ = α × β` - Zeta diversity, ζ Hui, C. *et al* 2014, the degree of overlap in the type of taxa present between a set of observed samples, overly sensitive to selection and order of samples .footnote[ (1) R. H. Whittaker (1960) Vegetation of the Siskiyou Mountains, Oregon and California. Ecological Monographs, 30, 279–338. doi:10.2307/1943563 ] --- # Alpha Diversity, α - Richness describes the number of unique taxa - Evenness describes the distribution of taxa abundances - Diversity is a measure of both richness and evenness |Metric |Class |Description | |:----------|:---------|:--------------------------------------------------------------------------| |Observed |Richness |Count of observed taxa. Sensitive to rare taxa | |Shannon |Diversity |Eveness and richness, log-based, range [0, log(n_tax)] | |Simpson |Evenness |Prob two randomly selected individuals are different species, range [0, 1] | |InvSimpson |Evenness |Inverse of Simpson (1 / D), More intuitive scale | |Fisher |Diversity |Parametric index balancing richness and evenness, assumes log-series | |Chao1 |Richness |Accounts for unseen rare species, better for undersampled data | |ACE |Richness |Abundance-based Chao estimator, weights by abundance | --- # The Data Gut microbiome data from Jie et al, 2017 (3). Compare and contrast the gut microbiome in individuals with Artherosclerotic CardioVascular Disease (ACVD, "heart disease") compared to healthy individuals in a Chinese cohort. - Abundance Table (2): 370 samp x 2397 taxa (relative abundances) <br>`phyloseq::otu_table(physeq)` - Metadata (2): 370 samp x 9 variables (Heart Disease, Age, Sex, BMI, Triglycerides, Cholesterols) <br>`phyloseq::sample_data(physeq)` - Alpha diversity: 370 samp x 4 within-site measures of richness, evenness, and diversity <br>`phyloseq::estimate_richness(physeq, measures = c("Observed", "Shannon", "InvSimpson", "Fisher"))` - Beta diversity, PCoA on Bray-Curtis distance: 370 samp x 211 components (*) <br>`phyloseq::ordinate(physeq, method = "PCoA", distance = "bray")` - Beta diversity, CAP on Bray-Curtis distance: 370 samp x 212 components (*) <br>`phyloseq::ordinate(physeq, method = "CAP", distance = "bray")` .footnote[ (2) Jie, Z., Xia, H., Zhong, S. L., Feng, Q., Li, S., Liang, S., ... & Kristiansen, K. (2017). The gut microbiome in atherosclerotic cardiovascular disease. Nature communications, 8(1), 845. <br>(*) Bray-Cutis dissimilarity does not meet the triangle inequality, about 43% of the components to have negative eigenvalues and are dropped ] --- # Alpha Diversity -- Univariate .pull-left[ Typically a 2-4 metrics are used with univariate visualization ```` phyloseq::estimate_richness(...) %>% as_tibble(rownames = "SampleID") %>% left_join(metadata) %>% pivot_longer() %>% ggplot() + ... ```` ] .pull-right[ <img src="BetaSpaces_Presentation_files/figure-html/unnamed-chunk-5-1.png" alt="" width="2100" height="70%" style="display: block; margin: auto;" /> ] --- # Alpha Diversity -- Univariate Suggestions .pull-left[ 1. Consider `geom_violin()` instead of `geom_boxplot()` 1. Use `geom_jitter()` 1. Map `shape` to the same variable as `color` (if available) 1. Consider adding arrows showing domain preference, espesially for inverted scales ] .pull-right[ <img src="BetaSpaces_Presentation_files/figure-html/unnamed-chunk-6-1.png" alt="" width="2100" height="70%" style="display: block; margin: auto;" /> ] --- # Alpha Diversity -- Multivariate .pull-left[ - More information when pairing metrics: - **Visualize where evenness and richness disagree** - More take-aways than the last two figures - Scatterplot matrix(3) (aka SPLOM, pairs plot), `GGally::ggpairs()` - 2d Scatter plot with tooltips on sample/site & metadata with `plotly::ggplotly()` .footnote[ (3) Hartigan, J. A. (1975). Clustering algorithms. <br>John Wiley & Sons, Inc.. ] ] .pull-right[ <img src="BetaSpaces_Presentation_files/figure-html/unnamed-chunk-7-1.png" alt="" width="2100" height="70%" style="display: block; margin: auto;" /> ] --- # Ordination, Linear - Of a distance matrix, there is a large space to visualize - Principal Component Analysis (PCA, 4, 1901) - Principal Coordinate Analysis (PCoA, 5, 1966, aka cMDS, NMDS, PO) - Linear Discriminant Analysis (LDA, 6, 1936) - Canonical Analysis of Principal coordinates (CAP, 7, 2003) - ~**PCA is to PCoA as LDA is to CAP** |Goal |Data Method |Distance Method |Year Diff |Supervision | |:---------------------------|:-----------|:---------------|:---------|:------------| |Maximize variance explained |PCA, 1901 |PCoA, 1966 |65 Years |Unsupervised | |Maximize group separation |LDA, 1936 |CAP, 2003 |67 Years |Supervised | .footnote[ (4) Pearson, K. (1901). "On Lines and Planes of Closest Fit to Systems of Points in Space." Philosophical Magazine, 2(11), 559–572. (The initial formal description). <br>(5) Gower, J. C. (1966). "Some distance properties of latent root and vector methods used in multivariate analysis." Biometrika, 53, 325–338. <br>(6) Fisher, R. A. (1936). "The use of multiple measurements in taxonomic problems." Annals of Eugenics, 7(2), 179–188. <br>(7) Anderson, M. J., & Willis, T. J. (2003). "Canonical analysis of principal coordinates: a useful method of constrained ordination for ecology." Ecology, 84(3), 511–525. ] --- # Beta Diversity -- PCoA on Bray-Curtis .pull-left[ - Viewed as PC1:PC2 - Implicitly discard 80% variance in the other 200 components - Discussion on separability of gorups or taxa abudances of individuals in unique regions - **PCoA orients directions of variance, not group separation; let's try CAP** ] .pull-right[ <img src="BetaSpaces_Presentation_files/figure-html/unnamed-chunk-9-1.png" alt="" width="2100" height="70%" style="display: block; margin: auto;" /> ] --- # Beta Diversity -- CAP on Bray-Curtis .pull-left[ - CAP component, x-axis, maximizes group separation - MDS (aka PCoA, on the remaining space); y-axis maximizes variation (of the remaining space) - However, we're still ignoring 200 components; let's try scatterplot martices ] .pull-right[ <img src="BetaSpaces_Presentation_files/figure-html/unnamed-chunk-10-1.png" alt="" width="2100" height="70%" style="display: block; margin: auto;" /> ] --- # Beta Diversity -- CAP, 4 Components .pull-left[ - Marginal density validates that CAP1 separates groups best - Including MDS2:MDS3 shows views with outlying samples that would be missing in 2D - The number of components to embed or visualize is left as an exercise for the annalist ] .pull-right[ <img src="BetaSpaces_Presentation_files/figure-html/unnamed-chunk-11-1.png" alt="" width="2100" height="70%" style="display: block; margin: auto;" /> ] --- # Beta Diversity -- PCoA vs CAP, 2 Components .pull-left[ <img src="BetaSpaces_Presentation_files/figure-html/unnamed-chunk-12-1.png" alt="" width="2100" height="70%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="BetaSpaces_Presentation_files/figure-html/unnamed-chunk-13-1.png" alt="" width="2100" height="70%" style="display: block; margin: auto;" /> ] --- # Beta Diversity -- PCoA vs CAP, 4 Components .pull-left[ <img src="BetaSpaces_Presentation_files/figure-html/unnamed-chunk-14-1.png" alt="" width="2100" height="70%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="BetaSpaces_Presentation_files/figure-html/unnamed-chunk-15-1.png" alt="" width="2100" height="70%" style="display: block; margin: auto;" /> ] --- # Summary ## Alpha Diversity - **Visualize alpha metrics together as a scatterplot matrix** - Map `shape` to the same variable as `color` (if available) - Consider adding arrows to indicate domain preferred domain directions, especially for Simpson's Index (raw) where lower is more diverse - If visualizing univariate metrics: - Use `geom_jitter()` - Consider `geom_violin()` instead of `geom_boxplot()` ## Beta Diversity - **Use CAP instead of PCoA, better allignment between method and discussion** - **Visualize more ordination dimensions as a scatterplot matrix** --- # Citations -- Methods and Data - (1) R. H. Whittaker (1960) Vegetation of the Siskiyou Mountains, Oregon and California. Ecological Monographs, 30, 279–338. doi:10.2307/1943563 - (2) Jie, Z., Xia, H., Zhong, S. L., Feng, Q., Li, S., Liang, S., ... & Kristiansen, K. (2017). The gut microbiome in atherosclerotic cardiovascular disease. Nature communications, 8(1), 845. - (3) Hartigan, J. A. (1975). Clustering algorithms. John Wiley & Sons, Inc.. - (4) Pearson, K. (1901). "On Lines and Planes of Closest Fit to Systems of Points in Space." Philosophical Magazine, 2(11), 559–572. (The initial formal description). - (5) Gower, J. C. (1966). "Some distance properties of latent root and vector methods used in multivariate analysis." Biometrika, 53, 325–338. - (6) Fisher, R. A. (1936). "The use of multiple measurements in taxonomic problems." Annals of Eugenics, 7(2), 179–188. - (7) Anderson, M. J., & Willis, T. J. (2003). "Canonical analysis of principal coordinates: a useful method of constrained ordination for ecology." Ecology, 84(3), 511–525. --- # Citations -- R Packages - `R`: R Core Team. *R: A Language and Environment for Statistical Computing*. 2026. - `vegan`: Oksanen, Jari, et al. *vegan: Community Ecology Package*. 2026. - `phyloseq`: McMurdie, Stacy, and Steven Holmes. "Discovery and Characterization of the Human Microbiome through High-Throughput 16S rRNA Gene Sequencing." *PLoS ONE*, vol. 8, no. 4, 2013, e61217. - `dplyr`: Wickham, Hadley, et al. *dplyr: A Grammar of Data Manipulation*. 2026. - `ggplot2`: Wickham, Hadley. *ggplot2: Elegant Graphics for Data Analysis*. 2016. - `knitr`: Xie, Yihui. *knitr: A General-Purpose Package for Dynamic Report Generation in R*. 2025. - `GGally`: Schloerke, Claus, et al. *GGally: Extension to 'ggplot2'*. 2025. - `plotly`: Sievert, Carson. *Interactive Web-Based Data Visualization with R, plotly, and shiny*. 2020. --- class: center, inverse count: false # Thank you <!-- ## Questions --> Nicholas Spyrison, PhD <br>spyrison@gmail.com <br>github.com/nspyrison/betaspace --- class: center, inverse count: false # Appendix --- count: false # Ecological Taxonomic Diversity - *Alpha diversity, α (1),* richness and evenness within a site/sample - Typically choose 2-3 of 7 metrics, **visualized separately** - **Beta diversity, β (1),** compositional differences between sites/samples - Typically *10s-1000s of components* (dissimilarity of taxa abundance), **visualized as 2 components of 1 or 2 ordination method** - Gamma diversity, γ (1), total species diversity in a landscape, `γ = α × β` - (Zeta diversity, ζ (2), the degree of overlap in the type of taxa present between a set of observed sites/samples, overly sensitive to selection and order of sites/samples) .footnote[ (1) R. H. Whittaker (1960) Vegetation of the Siskiyou Mountains, Oregon and California. Ecological Monographs, 30, 279–338. doi:10.2307/1943563 <br>(2) Hui, C., & McGeoch, M. A. (2014). Zeta diversity as a concept and metric that unifies incidence-based biodiversity patterns. The American Naturalist, 184(5), 684-694. ] --- count: false # Common Beta Diversity Metrics ## Bray-Curtis Dissimilarity `$$d_{BC}(X,Y) = \frac{\sum_i |X_i - Y_i|}{\sum_i (X_i + Y_i)}$$` - Range: 0 (identical) to 1 (completely different) - **Abundance-weighted**: high-abundance taxa matter more - **Ignores zeros**: doesn't penalize shared absences - **Compositional**: abundance ratios matter --- count: false # Method 1: PCoA (Technical Details) ## The Math 1. Start with distance matrix `\(\mathbf{D}\)` 2. Convert to **centered Gram matrix** `\(\mathbf{G}\)` 3. Eigenvalue decomposition: `\(\mathbf{G} = \mathbf{U}\boldsymbol{\Lambda}\mathbf{U}^T\)` 4. **Principal coordinates** = `\(\mathbf{U}\sqrt{\boldsymbol{\Lambda}}\)` ## Key Properties ✓ Works with **any distance metric** (Bray-Curtis, Aitchison, etc.) ✓ Preserves **pairwise distances** (in lower dims) ✓ **Unconstrained**: axes are not explained by metadata ✗ First axis not guaranteed to explain most variance ✗ Can have **negative eigenvalues** (metric-dependent) --- count: false # Method 2: Canonical Analysis of Principal Coordinates (CAP) ## What it does - Takes a **distance matrix** + **metadata variables** - Fits **constrained ordination** - Axes are linear combinations of metadata - Each axis explains conditional % variance --- count: false # Method 2: CAP (Technical Details) ## The Process 1. Start with distance matrix `\(\mathbf{D}\)` 2. Compute **PCoA** (axes from `\(\mathbf{D}\)`) 3. **Constrain** PCoA axes to metadata via **RDA** 4. Retain axes that maximize correlation with constraints ## Key Properties ✓ **Interpretable axes**: tied to metadata ✓ Tests whether metadata explains composition differences ✓ Can use **PERMANOVA** to test significance ✗ Requires metadata to be meaningful ✗ Axes may explain less variance than unconstrained methods --- count: false # Jie et al. 2017: Analysis Pipeline ``` 1. Load abundance data (MetaPhlAn species profiles) ↓ 2. Calculate Bray-Curtis distance matrix ↓ 3. PCoA ordination (unconstrained) ↓ 4. Visualize: PC1 vs PC2 colored by ACVD status ↓ 5. PERMANOVA: Test if ACVD explains composition ↓ 6. Beta-dispersion: Test if groups have different spread ``` --- count: false # Jie et al. 2017: Results Interpretation ## PCoA Plot (Bray-Curtis) ``` r # From ACVD_reprex.Rmd (line 515) physeq.beta <- phyloseq::ordinate( full_meta, method = 'PCoA', distance = 'bray' ) pca.plot <- ggplot(pca.df) + geom_point(aes(x = PCoA1, y = PCoA2, color = ACVD), size = 2.5) + stat_ellipse(aes(x = PCoA1, y = PCoA2, color = ACVD), level = 0.99) + xlab(paste0("PC1 (", round(percent_explained[1], 1), "%)")) ``` --- count: false # Jie et al. 2017: Beta-Dispersion ## Are groups equally "spread out"? ``` r # From ACVD_reprex.Rmd (line 575) beta_dispersion1 <- vegan::betadisper( dist_matrix, beta_metadata$ACVD ) anova_betadisper1 <- vegan::permutest( beta_dispersion1, pairwise = FALSE ) ``` **Tests**: Do healthy vs ACVD groups have equal within-group distances? - **Significant result**: Unequal dispersion - **Interpretation**: Healthy samples more/less variable in composition --- count: false # PCA vs PCoA vs CAP: Summary | Task | Best Method | Reason | |------|-------------|--------| | Quick exploration | **PCA** | Fast, intuitive, directly interpretable | | Compare with Bray-Curtis | **PCoA** | Works with ecologically meaningful distance | | Test if metadata explains beta diversity | **CAP** | Hypothesis-driven, PERMANOVA-testable | | Compositional robustness | **PCoA + Aitchison** | Handles sum-to-one constraint | | Publication-quality beta diversity plots | **PCoA** | Standard in microbiome literature |