Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Exercise: Predicting Deforestation in Peru

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.

library(ForestForesight)

# Step 1: Set up the parameters
ff_folder <- "/path/to/your/forestforesight/data"
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 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/peru_deforestation_model.model"
predictions_save_path <- "/path/to/save/peru_deforestation_prediction.tif"
accuracy_csv_path <- "/path/to/save/peru_deforestation_accuracy.csv"
importance_csv_path <- "/path/to/save/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 = 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 = TRUE
)

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

# Step 6: Print a summary of the results
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 CSV
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))
}

This exercise does the following:

  1. Sets up the necessary parameters, including date ranges for training and validation.

  2. Specifies file paths for saving the model, predictions, accuracy metrics, and feature importance.

  3. Calls the ff_run function with the specified parameters.

  4. Plots the prediction result if available.

  5. Prints a summary of where the results are saved.

  6. Optionally loads and displays the first few lines of the accuracy CSV file.

To complete this exercise:

  1. Make sure you have the ForestForesight package and its dependencies installed.

  2. Replace the /path/to/your/forestforesight/data with the actual path to your ForestForesight data folder.

  3. Adjust the file paths for saving results as needed.

  4. Run the script and observe the output, including the plot and the saved files.

  5. Review the contents of the saved CSV files to understand the accuracy metrics and feature importance.

  • No labels

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.