andrew-edwards / EDMsimulate

An R package for simulating fish populations for empirical dynamic modeling
1 stars 1 forks source link

Need to get example working in `fit_models()` #18

Open andrew-edwards opened 12 months ago

andrew-edwards commented 12 months ago

Finding it hard to test fit_models() without doing parallel stuff, but it should be able to work independently (I would think?). Putting a browser() in kind of gets ignored when calling it from sim_and_fit_realisations(), so having a working example would really help me!

Realised that line 120 has realisation <- dplyr::pull(all_sims['m'][1,]) but no m has been defined within the function. [See ** below - figured out a solution]

Trying to get an example working, this is what I'm using (also copied into example of function, but easier to paste from here):

simulated_5 <- salmon_sim()
R_switch_manual <- "R_t"
edm_args_manual <- list(lags = list(R_switch = 0, S_t = 0:3),
                                first_difference = TRUE,
                                centre_and_scale = TRUE)
mve_args_manual <- list(lags = list(R_t = 0:1, S_t = 0:3))
larkin_args_manual = list(run_stan = FALSE,
                           prior_mean_alpha = 2,
                           prior_mean_beta = -rep(1,4),
                           prior_mean_sigma = 0.5,
                           prior_sd_alpha = 0.5,
                           prior_sd_beta = rep(0.25,4),
                           prior_sd_sigma = 0.25)
ricker_args_manual = list(run_stan = FALSE,
                           prior_mean_alpha = 2,
                           prior_mean_beta = -1,
                           prior_mean_sigma = 0.5,
                           prior_sd_alpha = 0.5,
                           prior_sd_beta = 0.25,
                           prior_sd_sigma = 0.25)
res_fit_models_5 <- fit_models(simulated_5,
                                R_switch = R_switch_manual,
                T = nrow(simulated_5),
                                mve_args = mve_args_manual,
                                pbsEDM_args = edm_args_manual,
                                larkin_args = larkin_args_manual,
                                ricker_args = ricker_args_manual)
andrew-edwards commented 12 months ago

Also, do we need T in the call for fit_models - or can we assume it is always the number of rows in the data? Not exactly sure what would happen when they disagree.

andrew-edwards commented 12 months ago

** Realised that the call to fit_models for the parallel code is

  outputs <- parallel::parLapply(cl, all_sims, function(x) {
    fit_models(x,
               res_realisations = res_realisations,
               R_switch = R_switch,
               T = T,
               pbsEDM_args = pbsEDM_args,
               mve_args = mve_args,
               larkin_args = larkin_args,
               ricker_args = ricker_args)
  }

which includes all_sims, then fit_models uses the mth value. I was going to say that it would seem clearer to do:

  outputs <- parallel::parLapply(cl, all_sims[m], function(x) {
    ......

(i.e. all_sims[m]) and then change fit_models, but now I'm not sure how the parallel stuff would get split up. But reading the code, it looks as though m would equal M (its last value from an earlier loop) at this stage of sim_and_fit_realisations().

Bit confused, and feel I'm missing something subtle/important about the parallelisation. Maybe to do with why you changed m to realisations somewhere also.

carrieholt commented 12 months ago

I renamed to the input to fit_models() to 'sim' instead of 'all_sims', and I removed [[m]] from inside the function and in the call to fti_models() in sim_and_fit_realistions.R (see commented out lines). It all runs for me, and gives the same answer when do_parallel is true and false.

andrew-edwards commented 12 months ago

Awesome, all works. I added a test to compare parallel and not. All tests pass locally for me now.