AfricaBirdData / BIRDIE

code and documentation related to the South Africa Biodiversity Data Pipeline for Wetlands and Waterbirds (BIRDIE) project
https://africabirddata.github.io/BIRDIE/
Other
3 stars 0 forks source link

Object 'year_sel' not found when running distribution pipeline for one species locally #15

Open anemone19 opened 6 months ago

anemone19 commented 6 months ago

Here is the setup and model fitting code:

library(BIRDIE)

det_mods <- list(det_mod1 = c("(1|obs_id)", "log_hours", "prcp", "tdiff", "cwac"),
                 det_mod2 = c("(1|obs_id)", "(1|site_id)", "log_hours", "prcp", "tdiff", "cwac"))

# Configure pipeline
config <- configPipeline(year = 2023,
                         dur = 1,
                         module = "dst",
                         occ_mod = c("log_dist_coast", "elev", "log_hum.km2", "wetcon",
                                     "watrec", "watext", "log_watext", "watrec:watext",
                                     "ndvi", "prcp", "tdiff"),
                         det_mod = det_mods$det_mod2,
                         fixed_vars = c("Pentad", "lon", "lat", "watocc_ever", "wetext_2018","wetcon_2018",
                                        "dist_coast", "elev"),
                         package = "spOccupancy",
                         data_dir = "analysis/data",
                         out_dir = "analysis/output",
                         server = FALSE)

test <- ppl_run_pipe_dst1(sp_code = config$species[3],
                          year = config$year, # this is the year models will run for
                          config = config,
                          steps = c("data","fit","diagnose","summary"),
                          force_gee_dwld = FALSE,
                          monitor_gee = TRUE,
                          force_site_visit = TRUE,
                          force_abap_dwld = FALSE,
                          spatial = FALSE,
                          print_fitting = TRUE)

For species which more than 22 pentads in 2023, i.e. spcode = 6, the following error is produced:

Screenshot 2024-03-11 at 12 07 07

Although not where the error is triggered (as suggested by the traceback) - the object 'year_sel' is first created and subsequently removed in lines 38 to 42 of the ppl_run_pipe_dst1 script:

        year_sel <- year
        n_pentads <- occu_data$visit %>% dplyr::count(year, Pentad, 
            obs) %>% dplyr::filter(year == year_sel, obs == 1) %>% 
            nrow()
        rm(year_sel)

Following the traceback. In the fit step 'year_sel' is an argument for the function ppl_fit_occu_model function:

        fit_out <- ppl_fit_occu_model(sp_code, year_sel = year, 
            config, varargs_dst1$spatial, ...)

Then the function 'fitSpOccu' does not pick up the year_sel argument:


    # Fit model
    if(config$package == "spOccupancy"){
        fitSpOccu(site_data_year, visit_data_year, config, sp_code, spatial, sp_sites, year_sel)
    } else if(config$package == "occuR"){
        fitOccuR(site_data_year, visit_data_year, config, spatial, sp_sites, verbose = varargs$print_fitting)
    }
anemone19 commented 6 months ago

This is resolved by modifying the function fitSpOccu by adding an argument explicitly for year_sel called year (for example) and then calling the function as:

    # Fit model
    if(config$package == "spOccupancy"){
        fitSpOccu(site_data_year, visit_data_year, config, sp_code, spatial, sp_sites, year = year_sel)
    } else if(config$package == "occuR"){
        fitOccuR(site_data_year, visit_data_year, config, spatial, sp_sites, verbose = varargs$print_fitting)
    }

Not sure why inputting year_sel as allowed (as an additional argument) by the function does not work.