9 Exercise
9.1 Exercise: Building and Interpreting a MaxEnt Model
In this exercise, you will build a MaxEnt model to predict the distribution of a species using occurrence data and environmental predictors provided by the dismo
package.
9.1.1 Objectives
- Load and explore occurrence data and environmental predictors.
- Fit a MaxEnt model to predict the species distribution.
- Generate and visualize a habitat suitability map.
- Evaluate the model using AUC and interpret the results.
9.1.2 Step 1: Load Required Libraries
Load the necessary libraries for building and visualizing the MaxEnt model.
# Load required libraries
library(dismo) # For MaxEnt modeling
library(raster) # For spatial raster data
library(maps) # For base map visualization
9.1.3 Step 2: Load Occurrence Data
The dismo
package includes occurrence data for Bradypus variegatus (a species of sloth). Your task is to:
- Load the data.
- Extract latitude and longitude columns.
- Visualize the occurrence points on a world map.
# Load occurrence data
occurrence_file <- system.file("ex", "bradypus.csv", package = "dismo")
occurrences <- read.csv(occurrence_file)
# Keep only longitude and latitude columns
occurrences <- occurrences[, 2:3]
colnames(occurrences) <- c("lon", "lat")
# Visualize occurrences on a map
map("world", col = "gray90", fill = TRUE, bg = "lightblue", lwd = 0.5)
points(occurrences, col = "red", pch = 20, cex = 0.8)
9.1.4 Step 3: Load Environmental Predictors
Environmental predictors (e.g., temperature, precipitation) are provided as raster layers in the dismo
package. Your task is to:
- Load and stack the predictors.
- Visualize one layer to understand the data.
# Load and stack environmental predictors
path <- system.file("ex", package = "dismo")
predictor_files <- list.files(path, pattern = "grd$", full.names = TRUE)
predictors <- stack(predictor_files)
# Visualize one predictor layer
plot(predictors[[1]], main = "Environmental Predictor: Layer 1")
9.1.5 Step 4: Fit a MaxEnt Model
Use the maxent
function to build a MaxEnt model using the occurrence data and environmental predictors.
# Fit the MaxEnt model
maxent_model <- maxent(predictors, occurrences)
# View model summary
print(maxent_model)
9.1.6 Step 5: Generate a Habitat Suitability Map
Use the fitted model to predict habitat suitability across the study area and visualize the output.
# Predict habitat suitability
suitability_map <- predict(maxent_model, predictors)
# Visualize the suitability map
plot(suitability_map, main = "MaxEnt Habitat Suitability Map")
map("world", add = TRUE, col = "gray", lwd = 0.5)
9.1.7 Step 6: Evaluate the Model
Evaluate the model’s performance using AUC.
- Extract suitability values at occurrence points.
- Generate random background points and extract their suitability values.
- Combine the predictions and calculate the AUC.
# Extract predictions for presence and background points
presence_values <- extract(suitability_map, occurrences)
background_points <- randomPoints(predictors, 500) # Generate 500 background points
background_values <- extract(suitability_map, background_points)
# Combine predictions
labels <- c(rep(1, length(presence_values)), rep(0, length(background_values)))
predictions <- c(presence_values, background_values)
# Evaluate using AUC
library(pROC)
roc_curve <- roc(labels, predictions)
plot(roc_curve, main = "ROC Curve for MaxEnt Model")
auc_value <- auc(roc_curve)
print(paste("AUC:", auc_value))
9.2 Questions
- What do the high-suitability areas on the map represent? How do they align with your knowledge of Bradypus variegatus habitat?
- What does the AUC value tell you about the model’s performance?
- What steps could you take to improve this model (e.g., addressing sampling bias or selecting different predictors)?