Versions Compared

Key

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

...

Code Block
library(ForestForesight)

# Step 1: Set up the parameters

# Make sure you have DATA_FOLDER in config.yml in your working directoryff_environment has been run, otherwise assign ff_folder yourself
# to the folder you used for ff_sync
ff_folder <- Sys.getenv("DATAFF_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 <- paste0(ff_folder, "peru_deforestation_model.model")
predictions_save_path <- paste0(ff_folder, "peru_deforestation_prediction.tif")
accuracy_csv_path <- paste0(ff_folder, "peru_deforestation_accuracy.csv")
importance_csv_path <- 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 = TRUE,
  ff_train_parameters = list("nrounds" = 25),
  model_save_path = model_save_path,
  predictions_save_path =  predictions_save_path,
  accuracy_output_path =  accuracy_csv_path,
  importance_output_path = importance_csv_path,
  verbose = TRUE,
  autoscale_sample = FALSE
)

# Step 5: Plot the prediction result

plot(prediction_result$predictions, main = "Deforestation Prediction for Laos")


# Step 6: Print a summary of the results
ff_cat("Prediction completed. Results saved to disk:")
ff_cat("Model:", model_save_path)
ff_cat("Prediction raster:", predictions_save_path)
ff_cat("Accuracy CSV:", accuracy_csv_path)
ff_cat("Feature importance CSV:", importance_csv_path)
# Optional: Load and print the first few lines of the accuracy CSV


accuracy_data <- prediction_result$accuracy
ff_cat("\nFirstFirst few lines of accuracy data:")
print(head(accuracy_data))

...