Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

For all the functionalities of every function please go to the manual in R to get the latest info, or type help({name of function}) directly in the Rstudio terminal to get the help of every function.

ExerciseExercise 

In this exercise, you'll use the ff_run function to predict deforestation in Peru. You'll create training data, validation data, train a model, make predictions, and analyze the results. 

r
Code Block
language
library(ForestForesight) 

# Step 1: Set up the parameters parameters 

# Make sure you have DATA_FOLDER in config.yml in your working directory 
ff_folder <- "/path/to/your/forestforesight/data"Sys.getenv("DATA_FOLDER") 
country_code <- "PER" 
train_start <- "2023-06-01" 
train_end <- "2023-12-31" 
validation_start <- "2023-06-01" 
validation_end <- "2023-12-01" 
prediction_date <- "2024-01-01" 

# Step 2: Create date ranges for training and validationvalidation 
train_dates <- ForestForesight::daterange(train_start, train_end) 
validation_dates <- ForestForesight::daterange(validation_start, validation_end) 

# Step 3: Set up file paths for saving resultsresults 
model_save_path <- paste0(ff_folder, "/path/to/save/peru_deforestation_model.model"
predictions_save_path <- paste0(ff_folder, "/path/to/save/peru_deforestation_prediction.tif")
accuracy_csv_path <- "/path/to/save/paste0(ff_folder, "peru_deforestation_accuracy.csv"
importance_csv_path <- paste(ff_folder, "/path/to/save/peru_feature_importance.csv"

# Step 4: Run the ff_run function 
function
prediction_result <- ff_run( 
   country = country_code, 
   prediction_dates = prediction_date, 
   ff_folder = ff_folder, 
   train_dates = train_dates, 
   validation_dates = validation_dates, 
   save_path = model_save_path, 
   save_path_predictions = predictions_save_path, 
   accuracy_csv = accuracy_csv_path, 
   importance_csv = importance_csv_path, 
   verbose = TRUE, 
   autoscale_sample = TRUETRUE 
) 

# Step 5: Plot the prediction resultresult 
if (!is.na(prediction_result)) { 
   plot(prediction_result, main = "Deforestation Prediction for Peru") 
} 

# Step 6: Print a summary of the resultsresults 
cat("Prediction completed. Results saved to disk:\n") 
cat("Model:", model_save_path, "\n") 
cat("Prediction raster:", predictions_save_path, "\n")
cat("Accuracy CSV:", accuracy_csv_path, "\n") 
cat("Feature importance CSV:", importance_csv_path, "\n")

# Optional: Load and print the first few lines of the accuracy CSVCSV 

if (file.exists(accuracy_csv_path)) { 
   accuracy_data <- read.csv(accuracy_csv_path)
   cat("\nFirst few lines of accuracy data:\n")
   print(head(accuracy_data)) 

} 

For more information about the config file please refer to the configuration page here Open-Source Contribution .  

This exercise does the following:

...