Author: Yu Gao(gaoyu19920914@gmail.com)
Date: 2026-06-01

# BetaStability with vegan datasets

#### Yu Gao(gaoyu19920914@gmail.com)

#### 2026-06-01

# BetaStability with vegan datasets

This vignette demonstrates the BetaStability package using `linearPred` with multiple datasets (BCI, dune, and mite) from the vegan package.

## Installation

First, install the package from GitHub:

    # Install from GitHub
    # install.packages("devtools")
    # devtools::install_github("gaoyu19920914/betaStability")

    # OR install from BioConductor (in the future when it's available)
    # if (!requireNamespace("BiocManager", quietly = TRUE))
    #     install.packages("BiocManager")
    # BiocManager::install("betaStability")

## Loading Required Packages

Load the BetaStability package and the vegan package for test data:

    library(betaStability)
    library(vegan)
    library(ggplot2)
    data("BCI", "BCI.env", "mite", "mite.env", "dune", "dune.env")

## Demonstrating General Applicability with linearPred

    df_prepare <- function(df) {
        # Remove columns where all elements are the same
        df <- df[, vapply(df, function(x) length(unique(x)) > 1, logical(1))]

        # Convert columns to integer if they contain different types of strings
        for (col in names(df)) {
            if (is.character(df[[col]]) && length(unique(df[[col]])) > 1) {
                df[[col]] <- as.integer(as.factor(df[[col]]))
            }
        }
        return(df)
    }

### BCI Dataset with linearPred

Calculate stability for BCI dataset using `linearPred`:

    # Calculate stability with linearPred
    stability_BCI_linear <- betaStability(
        comtable = BCI,
        envmeta = BCI.env[, c("Precipitation", "Elevation", "EnvHet")],
        method = "linearPred"
    )

    # Inspect the result
    head(stability_BCI_linear)
    #>   stability_Linear
    #> 1      0.053679327
    #> 2      0.118797973
    #> 3      0.052407524
    #> 4      0.087186422
    #> 5     -0.034849722
    #> 6      0.001645529
    length(stability_BCI_linear)
    #> [1] 1

### Dune Dataset with linearPred

Load the `dune` (community data) and `dune.env` (environmental metadata) datasets from the vegan package, then calculate stability:

    data(dune)
    data(dune.env)

    # Inspect the data
    head(dune)
    #>   Achimill Agrostol Airaprae Alopgeni Anthodor Bellpere Bromhord Chenalbu
    #> 1        1        0        0        0        0        0        0        0
    #> 2        3        0        0        2        0        3        4        0
    #> 3        0        4        0        7        0        2        0        0
    #> 4        0        8        0        2        0        2        3        0
    #> 5        2        0        0        0        4        2        2        0
    #> 6        2        0        0        0        3        0        0        0
    #>   Cirsarve Comapalu Eleopalu Elymrepe Empenigr Hyporadi Juncarti Juncbufo
    #> 1        0        0        0        4        0        0        0        0
    #> 2        0        0        0        4        0        0        0        0
    #> 3        0        0        0        4        0        0        0        0
    #> 4        2        0        0        4        0        0        0        0
    #> 5        0        0        0        4        0        0        0        0
    #> 6        0        0        0        0        0        0        0        0
    #>   Lolipere Planlanc Poaprat Poatriv Ranuflam Rumeacet Sagiproc Salirepe
    #> 1        7        0       4       2        0        0        0        0
    #> 2        5        0       4       7        0        0        0        0
    #> 3        6        0       5       6        0        0        0        0
    #> 4        5        0       4       5        0        0        5        0
    #> 5        2        5       2       6        0        5        0        0
    #> 6        6        5       3       4        0        6        0        0
    #>   Scorautu Trifprat Trifrepe Vicilath Bracruta Callcusp
    #> 1        0        0        0        0        0        0
    #> 2        5        0        5        0        0        0
    #> 3        2        0        2        0        2        0
    #> 4        2        0        1        0        2        0
    #> 5        3        2        2        0        2        0
    #> 6        3        5        5        0        6        0
    head(dune.env)
    #>    A1 Moisture Management      Use Manure
    #> 1 2.8        1         SF Haypastu      4
    #> 2 3.5        1         BF Haypastu      2
    #> 3 4.3        2         SF Haypastu      4
    #> 4 4.2        2         SF Haypastu      4
    #> 5 6.3        1         HF Hayfield      2
    #> 6 4.3        1         HF Haypastu      2

    # Dimensions of the datasets
    cat("Dimensions of dune:", dim(dune), "\n")
    #> Dimensions of dune: 20 30
    cat("Dimensions of dune.env:", dim(dune.env), "\n")
    #> Dimensions of dune.env: 20 5

    # Process environmental data using df_prepare
    dune.env_processed <- df_prepare(dune.env)

    # Calculate stability with linearPred
    stability_dune_linear <- betaStability(
        comtable = dune,
        envmeta = dune.env_processed,
        method = "linearPred"
    )
    #> Warning in dist(BBmisc::normalize(envmeta, method = "range", margin = 2), : NAs
    #> introduced by coercion

    # Inspect the result
    head(stability_dune_linear)
    #>   stability_Linear
    #> 1      -0.10941041
    #> 2       0.06295745
    #> 3       0.20765436
    #> 4       0.15511150
    #> 5       0.06873168
    #> 6       0.06454557
    length(stability_dune_linear)
    #> [1] 1

### Mite Dataset with linearPred

Load the `mite` (community data) and `mite.env` (environmental metadata) datasets from the vegan package, then calculate stability:

    data(mite)
    data(mite.env)

    # Inspect the data
    head(mite)
    #>   Brachy PHTH HPAV RARD SSTR Protopl MEGR MPRO TVIE HMIN HMIN2 NPRA TVEL ONOV
    #> 1     17    5    5    3    2       1    4    2    2    1     4    1   17    4
    #> 2      2    7   16    0    6       0    4    2    0    0     1    3   21   27
    #> 3      4    3    1    1    2       0    3    0    0    0     6    3   20   17
    #> 4     23    7   10    2    2       0    4    0    1    2    10    0   18   47
    #> 5      5    8   13    9    0      13    0    0    0    3    14    3   32   43
    #> 6     19    7    5    9    3       2    3    0    0   20    16    2   13   38
    #>   SUCT LCIL Oribatl1 Ceratoz1 PWIL Galumna1 Stgncrs2 HRUF Trhypch1 PPEL NCOR
    #> 1    9   50        3        1    1        8        0    0        0    0    0
    #> 2   12  138        6        0    1        3        9    1        1    1    2
    #> 3   10   89        3        0    2        1        8    0        3    0    2
    #> 4   17  108       10        1    0        1        2    1        2    1    3
    #> 5   27    5        1        0    5        2        1    0        1    0    0
    #> 6   39    3        5        0    1        1        8    0        4    0    1
    #>   SLAT FSET Lepidzts Eupelops Miniglmn LRUG PLAG2 Ceratoz3 Oppiminu Trimalc2
    #> 1    0    0        0        0        0    0     0        0        0        0
    #> 2    2    2        1        0        0    0     0        0        0        0
    #> 3    0    8        0        0        0    0     0        0        0        0
    #> 4    2   12        0        0        0    0     0        0        0        0
    #> 5    0   12        2        0        0    0     0        0        0        0
    #> 6    0   10        0        0        0    0     0        0        0        0
    head(mite.env)
    #>   SubsDens WatrCont Substrate Shrub    Topo
    #> 1    39.18   350.15   Sphagn1   Few Hummock
    #> 2    54.99   434.81    Litter   Few Hummock
    #> 3    46.07   371.72 Interface   Few Hummock
    #> 4    48.19   360.50   Sphagn1   Few Hummock
    #> 5    23.55   204.13   Sphagn1   Few Hummock
    #> 6    57.32   311.55   Sphagn1   Few Hummock

    # Dimensions of the datasets
    cat("Dimensions of mite:", dim(mite), "\n")
    #> Dimensions of mite: 70 35
    cat("Dimensions of mite.env:", dim(mite.env), "\n")
    #> Dimensions of mite.env: 70 5

    # Process environmental data using df_prepare
    mite.env_processed <- df_prepare(mite.env)

    # Calculate stability with linearPred
    stability_mite_linear <- betaStability(
        comtable = mite,
        envmeta = mite.env_processed,
        method = "linearPred"
    )
    #> Warning in dist(BBmisc::normalize(envmeta, method = "range", margin = 2), : NAs
    #> introduced by coercion

    # Inspect the result
    head(stability_mite_linear)
    #>   stability_Linear
    #> 1     0.0246162099
    #> 2    -0.0047508295
    #> 3    -0.0001991863
    #> 4     0.0161315321
    #> 5     0.0528486489
    #> 6     0.0533943609
    length(stability_mite_linear)
    #> [1] 1

### Visualizing Results

Visualize the stability results for all datasets using `linearPred`:

    p_mite <- plotStability(stability_mite_linear)
    p_mite

    p_dune <- plotStability(stability_dune_linear)
    p_dune

    p_BCI <- plotStability(stability_BCI_linear)
    p_BCI

## Summary

General applicability across different datasets: Using the simple and fast `linearPred` method, we successfully calculated stability for three different datasets (BCI, dune, and mite) representing different ecological systems. This demonstrates the versatility of the BetaStability package across various types of ecological data.

    print(sessionInfo())
    #> R version 4.5.3 (2026-03-11 ucrt)
    #> Platform: x86_64-w64-mingw32/x64
    #> Running under: Windows 11 x64 (build 26200)
    #>
    #> Matrix products: default
    #>   LAPACK version 3.12.1
    #>
    #> locale:
    #> [1] LC_COLLATE=C
    #> [2] LC_CTYPE=English_United States.utf8
    #> [3] LC_MONETARY=English_United States.utf8
    #> [4] LC_NUMERIC=C
    #> [5] LC_TIME=English_United States.utf8
    #>
    #> time zone: Asia/Shanghai
    #> tzcode source: internal
    #>
    #> attached base packages:
    #> [1] stats     graphics  grDevices utils     datasets  methods   base
    #>
    #> other attached packages:
    #> [1] ggplot2_4.0.2       vegan_2.7-3         permute_0.9-10
    #> [4] betaStability_0.0.4
    #>
    #> loaded via a namespace (and not attached):
    #>  [1] sass_0.4.10          generics_0.1.4       shape_1.4.6.1
    #>  [4] stringi_1.8.7        lattice_0.22-9       usedist_0.4.0
    #>  [7] digest_0.6.39        magrittr_2.0.4       rioja_1.0-7
    #> [10] evaluate_1.0.5       grid_4.5.3           RColorBrewer_1.1-3
    #> [13] BBmisc_1.13.1        iterators_1.0.14     fastmap_1.2.0
    #> [16] xgboost_3.2.1.1      glmnet_4.1-10        Matrix_1.7-5
    #> [19] foreach_1.5.2        doParallel_1.0.17    plyr_1.8.9
    #> [22] jsonlite_2.0.0       backports_1.5.0      survival_3.8-6
    #> [25] mgcv_1.9-4           scales_1.4.0         pbapply_1.7-4
    #> [28] codetools_0.2-20     jquerylib_0.1.4      cli_3.6.5
    #> [31] rlang_1.1.7          splines_4.5.3        withr_3.0.2
    #> [34] cachem_1.1.0         yaml_2.3.12          otel_0.2.0
    #> [37] tools_4.5.3          parallel_4.5.3       reshape2_1.4.5
    #> [40] checkmate_2.3.4      dplyr_1.2.0          gdm_1.6.0-7
    #> [43] vctrs_0.7.2          R6_2.6.1             lifecycle_1.0.5
    #> [46] randomForest_4.7-1.2 stringr_1.6.0        MASS_7.3-65
    #> [49] cluster_2.1.8.2      pkgconfig_2.0.3      pillar_1.11.1
    #> [52] bslib_0.10.0         gtable_0.3.6         data.table_1.18.2.1
    #> [55] glue_1.8.0           Rcpp_1.1.1           xfun_0.57
    #> [58] tibble_3.3.1         tidyselect_1.2.1     rstudioapi_0.18.0
    #> [61] knitr_1.51           farver_2.1.2         nlme_3.1-169
    #> [64] htmltools_0.5.9      labeling_0.4.3       rmarkdown_2.31
    #> [67] compiler_4.5.3       S7_0.2.1