idem-lab / map-ir-pipeline

Prototype demonstration of stacked generalisation method used in https://journals.plos.org/plosbiology/article?id=10.1371/journal.pbio.3000633#sec010
3 stars 1 forks source link

Ensuring raster covariate data is in the right shape/names/dimensions for `spatial_prediction()` #63

Open njtierney opened 7 months ago

njtierney commented 7 months ago

When doing the spatial_prediction() step the following larger steps happen:

This is the part I'm not 100% sure on. The values we want to provide here for this raster are currently specified as start year, month, end year, end month, and also sets insecticide_id to be 1. The model later fits each insecticide separately, BUT this data is used again in fitting. I'm uncertain the below is right - and I'm not sure how we should tackle giving/specifying the columns or variables/values appropriately.

spatial_prediction <- function(covariate_rasters = raster_covariates,
                               training_data = ir_data_subset,
                               level_zero_models = model_list,
                               inla_mesh_setup = gp_inla_setup) {

  ir_data_subset_spatial_covariates <- join_rasters_to_mosquito_data(
    rasters = covariate_rasters,
    mosquito_data = training_data
  )

  chosen_year <- max(as.integer(training_data$start_year))

  rasters_as_data <- raster_to_df(covariate_rasters)

  rasters_w_basic_info <- rasters_as_data %>%
    mutate(
      start_year = chosen_year,
      start_month = 1,
      end_month = 12,
      end_year = chosen_year,
      int = 1,
      # dummy to be changed later?
      insecticide_id = 1,
      .before = everything()
    )

  # predict out for each raster in rasters_for_inner_loop
  # full set of map data (environmental covariates and coords)
  # in this final step we take a set of rasters, pull out coords and env
  # covariates for each pixel, and use stacked generalisation to predict to
  # all of them, then put predicted IR values back in a raster of predictions.
  spatial_predictions <- inner_loop(
        data = ir_data_subset_spatial_covariates,
        new_data = rasters_w_basic_info,
        level_zero_models = level_zero_models,
        level_one_model_setup = inla_mesh_setup
      )

  spatial_predictions

}