ff_predict: Predicting deforestation
The ff_predict
function is designed to make predictions using a trained XGBoost model from the ForestForesight algorithm. It can handle both loaded models and model file paths, applies the model to test data, and can optionally create a predicted raster. The function also calculates performance metrics if ground truth data is provided.
Exercises
Basic Usage:
Make predictions using a trained model and test data.
library(ForestForesight)
# Assume we have a trained model and prepared test data
model <- ... # Trained model from ff_train
test_data <- ff_prep(...) # Prepare test data
predictions <- ff_predict(
model = model,
test_matrix = test_data$data_matrix,
verbose = TRUE
)
print(head(predictions$predictions))
Using a Saved Model File:
Load a model from a file and make predictions.
predictions <- ff_predict(
model = "path/to/my_deforestation_model.model",
test_matrix = test_data$data_matrix,
verbose = TRUE
)
print(summary(predictions$predictions))
Adjusting the Threshold:
Experiment with different probability thresholds for binary classification.
predictions <- ff_predict(
model = model,
test_matrix = test_data$data_matrix,
threshold = 0.7,
verbose = TRUE
)
print(table(predictions$predicted_raster[] > 0.7))
Performance Evaluation:
Use ground truth data to evaluate model performance.
predictions_with_metrics <- ff_predict(
model = model,
test_matrix = test_data$data_matrix,
groundtruth = test_data$groundtruthraster,
verbose = TRUE
)
print(predictions_with_metrics$precision)
print(predictions_with_metrics$recall)
print(predictions_with_metrics$F0.5)
Creating a Predicted Raster:
Generate a spatial raster of predictions.
predictions_with_raster <- ff_predict(
model = model,
test_matrix = test_data$data_matrix,
indices = test_data$testindices,
templateraster = test_data$groundtruthraster,
verbose = TRUE
)
# Plot the predicted raster
plot(predictions_with_raster$predicted_raster, main = "Deforestation Prediction")
Generating Probability Maps:
Create a raster of deforestation probabilities instead of binary classification.
probability_predictions <- ff_predict(
model = model,
test_matrix = test_data$data_matrix,
indices = test_data$testindices,
templateraster = test_data$groundtruthraster,
certainty = TRUE,
verbose = TRUE
)
# Plot the probability map
plot(probability_predictions$predicted_raster, main = "Deforestation Probability")
These exercises will help you understand how to use the ff_predict
function to make predictions with trained ForestForesight models, evaluate model performance, and create spatial predictions. Remember to replace the placeholder data (like model
and test_data
) with actual trained models and prepared data from your ForestForesight workflow.