3  Forest communities: beetles, birds and plants

# Load required packages
# Data manipulation
library(here)
library(readxl)
library(tidyverse)
library(DT)
# Modeling
library(mrIML)
library(tidymodels)
library(future)
library(finetune)
library(flashlight)
# Plotting
library(igraph)
library(ggnetwork)
library(cowplot)
library(patchwork)

# Load custom functions
source(here("R-functions", "select_variables.r"))
source(here("R-functions", "plot_network.r"))

# Set seed
set.seed(123)

# Set up parallel processing
n_cores <- parallel::detectCores()
plan("multicore", workers = 2)

# Set data directory
data_dir <- here("case-studies", "02-forest-communities", "data", "raw")

This case study demonstrates MrIML 2.0 on a larger dataset with multiple taxonomic groups across a forest successional gradient. We analyze 144 plots established across forest age categories:

At each plot, we recorded ground-active beetles, plants, and birds, along with environmental covariates including coarse wood debris, leaf litter depth, and soil variables (pH, conductivity).

3.1 Data Preparation

raw_predictors <- read_excel(
  file.path(data_dir, "Site_covariates_decision_making_4.xlsx"),
  sheet = 4
) %>%
  column_to_rownames(var = "plot_code") %>%
  rename_all(~ gsub(" ", "_", .)) %>%
  dplyr::select(-plot_code_T_D, -Transect)

datatable(raw_predictors)
raw_data_beetles <- read_excel(
  file.path(data_dir, "all_taxa_all_sp_mat_sp.xlsx"),
  sheet = "beetles_all",
) %>%
  dplyr::select(-Site, -Distance, -Transect, -Age) %>%
  rename_all(~ gsub(" ", "_", .)) %>%
  column_to_rownames(var = "Plot") %>%
  mutate_all(~ ifelse(. > 0, 1, .))

datatable(raw_data_beetles)
raw_data_birds <- read_excel(
  file.path(data_dir, "all_taxa_all_sp_mat_sp.xlsx"),
  sheet = "birds_all"
) %>%
  dplyr::select(-Site, -Distance, -Transect, -Age) %>%
  rename_all(~ gsub(" ", "_", .)) %>%
  column_to_rownames(var = "Plot") %>%
  mutate_all(~ ifelse(. > 0, 1, .))

datatable(raw_data_birds)
raw_data_plants <- read_excel(
  file.path(data_dir, "all_taxa_all_sp_mat_sp.xlsx"),
  sheet = "plants_all"
) %>%
  dplyr::select(-Site, -Distance, -Transect, -Age) %>%
  rename_all(~ gsub(" ", "_", .)) %>%
  column_to_rownames(var = "Plot") %>%
  mutate_all(~ ifelse(. > 0, 1, .))

datatable(raw_data_plants)

3.2 Model

This analysis uses a random forrest model.

model_rf <- rand_forest(
  trees = 1000,
  mode = "classification",
  mtry = tune(),
  min_n = tune()
) %>%
  set_engine("randomForest")

3.3 Beetle Community Analysis

Preparing the X and Y matrix for mrIML.

# Filter rare and common species (20-70% prevalence)
Y_beetles <- filterRareCommon(raw_data_beetles, lower = 0.2, higher = 0.7)

# Prepare environmental predictors
X_beetles <- raw_predictors %>%
  dplyr::select(
    -Beetles_S_Mat_Indval2,
    -Beetle_S_Total,
    -dist_regrowth,
    -Age,
    -Beetles_Mat_Ratio,
    -Birds_S_Mat_EFL,
    -Birds_S_Total,
    -Birds_Mat_Ratio,
    -slope_f_Jayne,
    -aspect_f_Jayne
  )

# Convert categorical variables
X_beetles$Site <- as.factor(X_beetles$Site)
X_beetles$geol_Jayne <- as.factor(X_beetles$geol_Jayne)

# Variable selection
var_sel_beetles <- select_variables(Y = Y_beetles, X = X_beetles)
X_beetles <- var_sel_beetles[[2]]

# Remove site for model comparison
X_beetles$Site <- NULL

# Co-occurrence matrix
X1_beetles <- Y_beetles

3.3.1 Beetle Model Configuration and Training

# Model 1: Environment only
yhats_beetles_env <- mrIMLpredicts(
  X = X_beetles,
  Y = Y_beetles,
  Model = model_rf,
  balance_data = 'no',
  tune_grid_size = 5,
  prop = 0.6,
  k = 5,
  racing = TRUE
)
Setting dummy = TRUE because X contains factors.
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
→ A | warning: No control observations were detected in `truth` with control level '1'.
There were issues with some computations   A: x1
There were issues with some computations   A: x2
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
→ A | warning: No control observations were detected in `truth` with control level '1'.
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
# Model 2: Co-occurrence only
yhats_beetles_cooccur <- mrIMLpredicts(
  Y = Y_beetles,
  X1 = X1_beetles,
  Model = model_rf,
  balance_data = 'no',
  tune_grid_size = 5,
  prop = 0.6,
  k = 5,
  racing = TRUE
)
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
# Model 3: Combined
yhats_beetles_combined <- mrIMLpredicts(
  X = X_beetles,
  Y = Y_beetles,
  X1 = X1_beetles,
  Model = model_rf,
  balance_data = 'no',
  tune_grid_size = 5,
  prop = 0.6,
  k = 5,
  racing = TRUE
)
Setting dummy = TRUE because X contains factors.
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"

3.3.2 Beetle Performance Assessment

# Evaluate model performance
ModelPerf_beetles_env <- mrIMLperformance(yhats_beetles_env)
ModelPerf_beetles_cooccur <- mrIMLperformance(yhats_beetles_cooccur)
ModelPerf_beetles_combined <- mrIMLperformance(yhats_beetles_combined)

# Performance comparison
perf_comparison_beetles <- mrPerformancePlot(
  ModelPerf1 = ModelPerf_beetles_combined,
  ModelPerf2 = ModelPerf_beetles_env,
  mode = "classification"
)

perf_comparison_beetles$performance_plot +
  labs(title = "Beetle Community Model Performance")

# Summary table
beetle_performance <- data.frame(
  Model = c("Environment only", "Co-occurrence only", "Combined"),
  MCC = c(
    ModelPerf_beetles_env[[2]],
    ModelPerf_beetles_cooccur[[2]],
    ModelPerf_beetles_combined[[2]]
  )
)
beetle_performance

3.3.3 Beetle Variable Importance and Associations

# Bootstrap analysis
bs_beetles <- mrBootstrap(yhats_beetles_combined, num_bootstrap = 100)
# Variable importance
vi_beetles <- mrVip(
  yhats_beetles_combined,
  mrBootstrap_obj = bs_beetles,
  threshold = 0.5,
  global_top_var = 10
)
Warning: NAs produced when calculating MCC. This is common when there is a
class imbalance. Substituting NA values with zero.
vi_beetles[[3]] +
  labs(title = "Beetle Community Variable Importance")

# Co-occurrence network
assoc_net_beetles <- mrCoOccurNet(bs_beetles)
plot_network(
  assoc_net_beetles,
  strength_threash = 0.1,
  network_title = "Beetle Co-occurrence Network"
)
Warning in plot_network(assoc_net_beetles, strength_threash = 0.1,
network_title = "Beetle Co-occurrence Network"): No effects above the specified
threashold of 0.1
NULL

3.3.4 Beetle interactions

interactions_rf <- yhats_beetles_combined %>%
  mrInteractions(num_bootstrap = 10)

interactions_rf[[1]]

3.4 Bird community analysis

# Prepare bird data (similar structure to beetles)
Y_birds <- filterRareCommon(raw_data_birds, lower = 0.2, higher = 0.7)

# Use same environmental predictors but select relevant variables
# Prepare environmental predictors
X_birds <- raw_predictors %>%
  dplyr::select(
    -Beetles_S_Mat_Indval2,
    -Beetle_S_Total,
    -dist_regrowth,
    -Age,
    -Beetles_Mat_Ratio,
    -Birds_S_Mat_EFL,
    -Birds_S_Total,
    -Birds_Mat_Ratio,
    -slope_f_Jayne,
    -aspect_f_Jayne
  )

# Convert categorical variables
X_birds$Site <- as.factor(X_birds$Site)
X_birds$geol_Jayne <- as.factor(X_birds$geol_Jayne)

# Remove site for model comparison
X_birds$Site <- NULL
var_sel_birds <- select_variables(Y = Y_birds, X = X_birds)
X_birds <- var_sel_birds[[2]]
X1_birds <- Y_birds

# Train models
yhats_birds_env <- mrIMLpredicts(
  X = X_birds,
  Y = Y_birds,
  Model = model_rf,
  balance_data = 'no',
  tune_grid_size = 5,
  prop = 0.6,
  k = 5,
  racing = TRUE
)
Setting dummy = TRUE because X contains factors.
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
yhats_birds_cooccur <- mrIMLpredicts(
  Y = Y_birds,
  X1 = X1_birds,
  Model = model_rf,
  balance_data = 'no',
  tune_grid_size = 5,
  prop = 0.6,
  k = 5,
  racing = TRUE
)
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
yhats_birds_combined <- mrIMLpredicts(
  X = X_birds,
  Y = Y_birds,
  X1 = X1_birds,
  Model = model_rf,
  balance_data = 'no',
  tune_grid_size = 5,
  prop = 0.6,
  k = 5,
  racing = TRUE
)
Setting dummy = TRUE because X contains factors.
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
→ A | warning: No control observations were detected in `truth` with control level '1'.
There were issues with some computations   A: x1
There were issues with some computations   A: x2
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
# Performance assessment
ModelPerf_birds_env <- mrIMLperformance(yhats_birds_env)
ModelPerf_birds_cooccur <- mrIMLperformance(yhats_birds_cooccur)
Warning: NAs produced when calculating MCC. This is common when there is a
class imbalance. Substituting NA values with zero.
ModelPerf_birds_combined <- mrIMLperformance(yhats_birds_combined)
Warning: NAs produced when calculating MCC. This is common when there is a
class imbalance. Substituting NA values with zero.
# Performance comparison
perf_comparison_birds <- mrPerformancePlot(
  ModelPerf1 = ModelPerf_birds_combined,
  ModelPerf2 = ModelPerf_birds_env,
  mode = "classification"
)
# Plot performance
perf_comparison_birds$performance_plot +
  labs(title = "Bird Community Model Performance")
Warning: The following aesthetics were dropped during statistical transformation: label.
ℹ This can happen when ggplot fails to infer the correct grouping structure in
  the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
  variable into a factor?

# Performance summary table
bird_performance <- data.frame(
  Model = c("Environment only", "Co-occurrence only", "Combined"),
  MCC = c(
    ModelPerf_birds_env[[2]],
    ModelPerf_birds_cooccur[[2]],
    ModelPerf_birds_combined[[2]]
  )
)
bird_performance
               Model        MCC
1   Environment only 0.20896812
2 Co-occurrence only 0.02983434
3           Combined 0.17430102
# Bootstrap analysis
bs_birds <- mrBootstrap(yhats_birds_combined, num_bootstrap = 100)

# Variable importance
vi_birds <- mrVip(
  yhats_birds_combined,
  mrBootstrap_obj = bs_birds,
  threshold = 0.5,
  global_top_var = 10
)
Warning: NAs produced when calculating MCC. This is common when there is a
class imbalance. Substituting NA values with zero.
# Variable importance plot
vi_birds[[3]] +
  labs(title = "Bird Community Variable Importance")

# Co-occurrence network
assoc_net_birds <- mrCoOccurNet(bs_birds)
plot_network(
  assoc_net_birds,
  strength_threash = 0.1,
  network_title = "Bird Co-occurrence Network"
)
Warning in plot_network(assoc_net_birds, strength_threash = 0.1, network_title
= "Bird Co-occurrence Network"): No effects above the specified threashold of
0.1
NULL

3.5 Plant community analysis

# Prepare Plant data (similar structure to beetles)
Y_plants <- filterRareCommon(raw_data_plants, lower = 0.2, higher = 0.7)

# Use same environmental predictors but select relevant variables
# Prepare environmental predictors
X_plants <- raw_predictors %>%
  dplyr::select(
    -Beetles_S_Mat_Indval2,
    -Beetle_S_Total,
    -dist_regrowth,
    -Age,
    -Beetles_Mat_Ratio,
    -Birds_S_Mat_EFL,
    -Birds_S_Total,
    -Birds_Mat_Ratio,
    -slope_f_Jayne,
    -aspect_f_Jayne,
    -contains("Bryo"),
    -contains("Plant"),
    -contains("Moss"),
    -Vegetation_cover
  ) %>%
  rename_with(~ gsub("Jayne", "", .), contains("Jayne"))

# Convert categorical variables
X_plants$Site <- as.factor(X_plants$Site)
X_plants$geol_ <- as.factor(X_plants$geol_)

# Remove site for model comparison
X_plants$Site <- NULL
var_sel_plants <- select_variables(Y = Y_plants, X = X_plants)
X_plants <- var_sel_plants[[2]]
X1_plants <- Y_plants

# Train models
yhats_plants_env <- mrIMLpredicts(
  X = X_plants,
  Y = Y_plants,
  Model = model_rf,
  balance_data = 'no',
  tune_grid_size = 5,
  prop = 0.6,
  k = 5,
  racing = TRUE
)
Setting dummy = TRUE because X contains factors.
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
→ A | warning: No control observations were detected in `truth` with control level '1'.
There were issues with some computations   A: x1
There were issues with some computations   A: x10
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
→ A | warning: No control observations were detected in `truth` with control level '1'.
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
yhats_plants_cooccur <- mrIMLpredicts(
  Y = Y_plants,
  X1 = X1_plants,
  Model = model_rf,
  balance_data = 'no',
  tune_grid_size = 5,
  prop = 0.6,
  k = 5,
  racing = TRUE
)
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
yhats_plants_combined <- mrIMLpredicts(
  X = X_plants,
  Y = Y_plants,
  X1 = X1_plants,
  Model = model_rf,
  balance_data = 'no',
  tune_grid_size = 5,
  prop = 0.6,
  k = 5,
  racing = TRUE
)
Setting dummy = TRUE because X contains factors.
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
→ A | warning: No control observations were detected in `truth` with control level '1'.
There were issues with some computations   A: x1
There were issues with some computations   A: x10
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
i Creating pre-processing data to finalize 1 unknown parameter: "mtry"
# Performance assessment
ModelPerf_plants_env <- mrIMLperformance(yhats_plants_env)
Warning: NAs produced when calculating MCC. This is common when there is a
class imbalance. Substituting NA values with zero.
ModelPerf_plants_cooccur <- mrIMLperformance(yhats_plants_cooccur)
Warning: NAs produced when calculating MCC. This is common when there is a
class imbalance. Substituting NA values with zero.
ModelPerf_plants_combined <- mrIMLperformance(yhats_plants_combined)
Warning: NAs produced when calculating MCC. This is common when there is a
class imbalance. Substituting NA values with zero.
# Performance comparison
perf_comparison_plants <- mrPerformancePlot(
  ModelPerf1 = ModelPerf_plants_combined,
  ModelPerf2 = ModelPerf_plants_env,
  mode = "classification"
)
# Plot performance
perf_comparison_plants$performance_plot +
  labs(title = "Bird Community Model Performance")
Warning: The following aesthetics were dropped during statistical transformation: label.
ℹ This can happen when ggplot fails to infer the correct grouping structure in
  the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
  variable into a factor?

# Performance summary table
plant_performance <- data.frame(
  Model = c("Environment only", "Co-occurrence only", "Combined"),
  MCC = c(
    ModelPerf_plants_env[[2]],
    ModelPerf_plants_cooccur[[2]],
    ModelPerf_plants_combined[[2]]
  )
)
plant_performance
               Model       MCC
1   Environment only 0.4644385
2 Co-occurrence only 0.3957885
3           Combined 0.4860622
# Bootstrap analysis
bs_plants <- mrBootstrap(
  yhats_plants_combined,
  num_bootstrap = 100
)

# Variable importance
vi_plants <- mrVip(
  yhats_plants_combined,
  mrBootstrap_obj = bs_plants,
  threshold = 0.5,
  global_top_var = 10
)
Warning: NAs produced when calculating MCC. This is common when there is a
class imbalance. Substituting NA values with zero.
# Variable importance plot
vi_plants[[3]] +
  labs(title = "Bird Community Variable Importance")

# Co-occurrence network
assoc_net_plants <- mrCoOccurNet(bs_plants)
plot_network(
  assoc_net_plants,
  strength_threash = 0.1,
  network_title = "Plant Co-occurrence Network",
  nmds_layout = TRUE
)