steps-dev / steps

steps: Spatially- and Temporally- Explicit Population Simulator
19 stars 7 forks source link

Error in stats::rmultinom(1, sum(population), population) : invalid second argument 'size' In addition: Warning message: In stats::rmultinom(1, sum(population), population) : NAs introduced by coercion to integer range #18

Closed geryan closed 5 years ago

geryan commented 5 years ago

Error appears over longer runs with demographic stochasticity and fast dispersal

egk_mat_hf <- matrix(c(0.00,0.00,1.00,
                       0.50,0.00,0.00,
                       0.00,0.85,0.85),
                     nrow = 3,
                     ncol = 3,
                     byrow = TRUE)
colnames(egk_mat_hf) <- rownames(egk_mat_hf) <- c('juvenile','subadult','adult')

egk_landscape <- landscape(population = egk_pop,
                           suitability = NULL,
                           carrying_capacity = NULL)

egk_pop_dynamics_fd_hf <- population_dynamics(change = growth(transition_matrix = egk_mat_hf),
                                              dispersal = fast_dispersal(
                                                dispersal_kernel = exponential_dispersal_kernel(
                                                  distance_decay = 1000)),
                                              modification = NULL,
                                              density_dependence = NULL)

egk_results <- simulation(landscape = egk_landscape,
                          population_dynamics = egk_pop_dynamics_fd_hf,
                          habitat_dynamics = NULL,
                          timesteps = 50,
                          replicates = 1,
                          verbose = FALSE) # no problem NB shorter timeframe

egk_results <- simulation(landscape = egk_landscape,
                          population_dynamics = egk_pop_dynamics_fd_hf,
                          habitat_dynamics = NULL,
                          timesteps = 100,
                          replicates = 1,
                          verbose = FALSE,
                          demo_stochasticity = "none") # no problem NB demo stochasticity off

egk_results <- simulation(landscape = egk_landscape,
                          population_dynamics = egk_pop_dynamics_fd_hf,
                          habitat_dynamics = NULL,
                          timesteps = 100,
                          replicates = 1,
                          verbose = FALSE) #generates error
geryan commented 5 years ago

Related error:

Error in if (any(pop_new[!missing] > 0)) { : 
  missing value where TRUE/FALSE needed

This error appears instead when a low value for fecundity is used. E.g.:

egk_mat_lf <- matrix(c(0.00,0.00,0.10,
                       0.50,0.00,0.00,
                       0.00,0.85,0.85),
                     nrow = 3,
                     ncol = 3,
                     byrow = TRUE)

egk_pop_dynamics_fd_lf <- population_dynamics(change = growth(transition_matrix = egk_mat_lf),
                                              dispersal = fast_dispersal(
                                                dispersal_kernel = exponential_dispersal_kernel(
                                                  distance_decay = 1000)),
                                              modification = NULL,
                                              density_dependence = NULL)

egk_landscape <- landscape(population = egk_pop,
                           suitability = NULL,
                           carrying_capacity = NULL)

egk_results <- simulation(landscape = egk_landscape,
                          population_dynamics = egk_pop_dynamics_fd_lf,
                          habitat_dynamics = NULL,
                          timesteps = 100,
                          replicates = 1,
                          verbose = FALSE)
cvisintin commented 5 years ago

Both of these errors are coming from the round_pop() function because rmultinom() can only handle integers up to a certain size.

geryan commented 5 years ago

Possible workaround if populations size is too large:

chunksize <- 1000000
chunks <-  population/ chunksize
rowSums(rmultinom(chunks, population, probs)))

https://stackoverflow.com/a/43718966

Would need something to not land on decimals of course.

cvisintin commented 5 years ago

We added a wrapper function that deals with large integers