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

# Make sure ff_environment has been run, otherwise assign ff_folder yourself
# to the folder you used for ff_sync
ff_folder <- "/path/to/your/forestforesight/data"Sys.getenv("FF_FOLDER")
country_code <- "PERSUR"
train_start <- "2023-06-01"
train_end <- "2023-1207-3101"
validation_start <- "2023-0608-01"
validation_end <- "2023-1208-01"
prediction_date <- "2024-01-01"

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

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

# Step 4: Run the ff_run function

prediction_result <- ff_run(
  country = country_code,
  prediction_dates = prediction_date,
  ff_folder = ff_folder,
  train_dates = train_dates,
  validation_dates = TRUE,
  ff_train_parameters = list("nrounds" = validation_dates25),
  model_save_path = model_save_path,
  predictions_save_path_predictions =  predictions_save_path,
  accuracy_csvoutput_path =  accuracy_csv_path,
  importance_csvoutput_path = importance_csv_path,
  verbose = TRUE,
  autoscale_sample = TRUEFALSE
)

# Step 5: Plot the prediction result
if
(!is.na(prediction_result)) {
  plot(prediction_resultresult$predictions, main = "Deforestation Prediction for PeruLaos")
}

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

# Optional: Load and print the first few lines of the accuracy CSV
if (file.exists(accuracy_csv_path))
{

 accuracy_data <- read.csv(accuracy_csv_path)
  prediction_result$accuracy
ff_cat("\nFirstFirst 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:

...