ecoTolerance: A Comprehensive Tutorial

Diego F. Miranda

Universidade Federal da Bahia
dfernandes115@gmail.com

2026-02-16

Introduction

The ecoTolerance package is designed to calculate two key indices for ecological studies involving species occurrences:

  1. Road Tolerance Index (RTI)

  2. Human Footprint Tolerance Index (HFTI)

These indices enable researchers and conservationists to assess how tolerant different species are to roads and human footprint, respectively. The package is particularly useful in biodiversity and conservation biology studies, and is part of an ongoing PhD thesis focused on amphibian species in Brazil.

Why use ecoTolerance?

Features of ecoTolerance

Installation

To install the development version from your local repository, you can use:

devtools::install("path/to/ecoTolerance")

Make sure you have the required packages installed (sf, raster, dplyr, stats)

1. Quick Start Example (Toy Data)

By default, ecoTolerance runs in “Toy Mode”. It uses a small, lightweight dataset included in the package.

1.1 Load the package and Data

library(ecoTolerance)

# Sample occurrence data (in Bahia, where the Toy dataset is located)
sample_data <- data.frame(
  species   = c("SpA", "SpA", "SpB", "SpB"),
  longitude = c(-38.40, -38.41, -38.42, -38.43), 
  latitude  = c(-12.60, -12.61, -12.62, -12.63)
)

1.2 Compute Indices (Toy Mode)

The compute_indices() function uses data_type= "toy" by default.

result <- compute_indices(
  data = sample_data,
  remove_duplicates = TRUE,
  buffer_km = 1,
  ref_dist = 3.5,
  divisor = 50,
  data_type = "toy"  # Default
)

# Inspect results
head(result$indices)

1.3 Automatic Reports

To automatically save maps, plots, and CSVs for every species:

# 1) Shapefile of the study area (Optional, for context in the map)
# For this example, we create a dummy box, but you would load your shapefile.
library(sf)
study_area_sf <- st_as_sf(
    data.frame(id = 1),
    geometry = st_sfc(st_polygon(list(rbind(c(-39, -13), c(-38, -13), c(-38, -12), c(-39, -12), c(-39, -13))))),
    crs = 4326
)

# 2) Output directory
output_dir <- tempdir() # Or "C:/MyResults"

# 3) Generate all maps & plots
generate_all_reports(
  result         = result,
  area_shapefile = study_area_sf, # Can also be a path to a .shp file
  out_dir        = output_dir
)

2. Using Full Internal Data (Brazil)

To perform a real analysis covering the entire Brazilian territory, you simply need to change the data_type argument to "full".

When you run this for the first time, the package will automatically:

  1. Download the full Brazilian roads shapefile and Human Footprint raster from the Zenodo repository (~60 MB).

  2. Save these files in a persistent cache on your computer (tools::R_user_dir).

  3. Load them for analysis.

Future runs will load directly from the cache, so you only download once.

# Real occurrence data anywhere in Brazil
real_data <- data.frame(
  species   = c("Frog X", "Frog Y"),
  longitude = c(-45.32, -40.10),
  latitude  = c(-15.20, -18.50)
)

# Run with full dataset
# Note: The first time you run this, it will take a few minutes to download.
result_full <- compute_indices(
  data = real_data,
  data_type = "full",  # <--- This triggers the download/cache load
  ref_dist = 3.5
)

3. Using External Shapefiles & Rasters

If your study area is outside Brazil, or you have custom high-resolution data, you can supply your own layers using the “monkey-patch” approach.

library(ecoTolerance)

# 1) Read your own data
occ_data <- read.csv("path/to/my_occurrences.csv")

# 2) Overwrite load_roads() and load_human_footprint() in your global environment.
# IMPORTANT: They must accept arguments (like 'type'), even if you don't use them.

load_roads <- function(type="toy") {
  # Argument 'type' is ignored here, we just return our custom file
  roads_sf <- sf::st_read("path/to/my_custom_roads.shp", quiet = TRUE)
  if (sf::st_crs(roads_sf)$epsg != 4326) {
    roads_sf <- sf::st_transform(roads_sf, crs = 4326)
  }
  return(roads_sf)
}

load_human_footprint <- function(type="toy") {
  footprint_raster <- raster::raster("path/to/my_custom_footprint.tif")
  return(footprint_raster)
}

# 3) Now call compute_indices()
# It will use your custom functions instead of the package internals
result_custom <- compute_indices(
  data = occ_data
)

4. Manual Workflow

If you prefer to run step-by-step instead of using compute_indices(), here is how the package works internally.

4.1 Process Occurrences

Run process_occurrences() to remove exact duplicates and optionally filter out records closer than 1km:

processed_data <- process_occurrences(
  data            = sample_data,
  remove_duplicates = TRUE,
  buffer_km       = 1
)
  • remove_duplicates = TRUE removes identical coordinates for the same species.

  • buffer_km = 1 ensures only one record per species within a 1 km radius.

processed_data is now an sf object in EPSG:4326.

4.2 Calculating RTI and HFTI Directly

If you want to calculate RTI and HFTI in separate steps, you can do:

# 1) Load internal example layers (or full)
roads <- load_roads(type = "toy")
footprint <- load_human_footprint(type = "toy")

# 2) Calculate RTI
rti_result <- calculate_RTI(
    occurrences = processed_data, 
    roads = roads, 
    ref_dist = 3.5
)

# 3) Calculate HFTI
# Note: we use rti_result$data because it now contains the RTI values
hfti_result <- calculate_HFTI(
    occurrences = rti_result$data, 
    footprint = footprint, 
    divisor = 50, 
    buffer_m = 1000
)

# 4) Inspect the outputs
head(rti_result$RTI)   # median RTI per species
head(hfti_result$HFTI) # median HFTI per species

4.3 Plotting Individual Maps

If you want to visualize the computed indices spatially, you can do:

library(ggplot2)

# For RTI:
ggplot(hfti_result$data) +
  geom_sf(aes(color = RTI_value)) +
  scale_color_viridis_c(name = "RTI") +
  theme_minimal() +
  ggtitle("Road Tolerance Index (RTI) per Occurrence")

# For HFTI:
ggplot(hfti_result$data) +
  geom_sf(aes(color = HFTI_value)) +
  scale_color_viridis_c(name = "HFTI") +
  theme_minimal() +
  ggtitle("Human Footprint Tolerance Index (HFTI) per Occurrence")

Advanced Configuration

Conclusion

The ecoTolerance package streamlines the process of computing road and human footprint tolerance indices for ecological and conservation studies. By combining robust data processing with reproducible spatial calculations, it aids researchers in understanding species’ responses to anthropogenic disturbances.

Citation / Reference

citation("ecoTolerance")

Acknowledgments

References