BetaStability with vegan datasets

knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>"
)

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)
length(stability_BCI_linear)

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)
head(dune.env)

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

# 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"
)

# Inspect the result
head(stability_dune_linear)
length(stability_dune_linear)

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)
head(mite.env)

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

# 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"
)

# Inspect the result
head(stability_mite_linear)
length(stability_mite_linear)

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())


Try the betaStability package in your browser

Any scripts or data that you put into this service are public.

betaStability documentation built on June 5, 2026, 5:08 p.m.