Skip to contents

Creates boxplots comparing model performance metrics across training, testing, and full datasets from spatial cross-validation performed by rf_evaluate(). Displays distributions of R-squared, RMSE, and other metrics across all spatial folds.

Usage

plot_evaluation(
  model,
  fill.color = viridis::viridis(3, option = "F", alpha = 0.8, direction = -1),
  line.color = "gray30",
  verbose = TRUE,
  notch = TRUE
)

Arguments

model

Model fitted with rf_evaluate(). Must be of class "rf_evaluate".

fill.color

Character vector with three colors (one for each model type: Testing, Training, Full) or a function that generates a color palette. Accepts hexadecimal codes (e.g., c("#440154FF", "#21908CFF", "#FDE725FF")) or palette functions (e.g., viridis::viridis(3)). Default: viridis::viridis(3, option = "F", alpha = 0.8, direction = -1).

line.color

Character string specifying the color of boxplot borders. Default: "gray30".

verbose

Logical. If TRUE, prints the plot to the graphics device. Default: TRUE.

notch

Logical. If TRUE, displays notched boxplots where notches represent approximate 95% confidence intervals around the median. Non-overlapping notches suggest significant differences between medians. Default: TRUE.

Value

ggplot object that can be further customized or saved. The plot displays boxplots of performance metrics (R-squared, RMSE, NRMSE, pseudo R-squared, or AUC depending on model type) across spatial folds, faceted by metric.

Details

This function visualizes the distribution of performance metrics across spatial folds, with separate boxplots for three model variants:

  • Testing: Performance on spatially independent testing folds (most reliable estimate of generalization)

  • Training: Performance on training folds (typically optimistic)

  • Full: Performance on the complete dataset (reference baseline)

Interpreting the plot:

The boxplots show the distribution of each metric across all spatial folds. Ideally:

  • Testing performance should be reasonably close to training performance (indicates good generalization)

  • Large gaps between training and testing suggest overfitting

  • Low variance across folds indicates stable, consistent model performance

  • High variance suggests performance depends strongly on spatial location

The plot includes a title showing the number of spatial folds used in the evaluation.

Available metrics:

Displayed metrics depend on the response variable type:

  • Continuous response: R-squared, RMSE (Root Mean Squared Error), NRMSE (Normalized RMSE)

  • Binary response: AUC (Area Under ROC Curve), pseudo R-squared

Examples

if(interactive()){

data(plants_rf, plants_xy)

# Perform spatial cross-validation
plants_rf <- rf_evaluate(
  model = plants_rf,
  xy = plants_xy,
  repetitions = 5,
  n.cores = 1
)

# Visualize evaluation results
plot_evaluation(plants_rf)

# Without notches for simpler boxplots
plot_evaluation(plants_rf, notch = FALSE)

# Custom colors
plot_evaluation(
  plants_rf,
  fill.color = c("#E64B35FF", "#4DBBD5FF", "#00A087FF")
)

# Print summary statistics
print_evaluation(plants_rf)

# Extract evaluation data for custom analysis
evaluation_data <- get_evaluation(plants_rf)
head(evaluation_data)

}