steps-dev / steps

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

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

Closed geryan closed 5 years ago

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)

Originally posted by @geryan in https://github.com/steps-dev/steps/issues/18#issuecomment-479731912

geryan commented 5 years ago

Still getting this issue with low fecundity - not sure if related to handling the multinomial issue #18

cvisintin commented 5 years ago

The error was generated by this line: prop_in <- sum(pop_new, na.rm = TRUE) / sum(popmat_orig, na.rm = TRUE)

When all populations were zero, it was resulting in a NaN. Fixed by adding a conditional:

if (!is.nan(prop_in)) { pop_new[!missing] <- pop_new[!missing] / prop_in }