Skip to content
Snippets Groups Projects
run_analysis.R 3.66 KiB
Newer Older
thierrychambert's avatar
thierrychambert committed
rm(list = ls(all.names = TRUE))
graphics.off()
library(popbio)
library(magrittr)
thierrychambert's avatar
thierrychambert committed
## Libraries
library(eolpop)

## Inputs
thierrychambert's avatar
thierrychambert committed

thierrychambert's avatar
thierrychambert committed

carrying_capacity = 1e8


#(4.8/100)*sum(N000[-1])
#(0.7/100)*sum(N000[-1])
fatalities_mean = c(0, 27.5) #c(0, 20, 12, 2)
fatalities_se = c(0, 4) #c(0, 6.5, 2.5, 0.2)


survivals <- c(0.47, 0.67, 0.67)
fecundities <- c(0, 0.30, 1.16)

pop_growth_mean = 1.12
# lambda( build_Leslie(s = survivals, f = fecundities) )
pop_growth_se = 0.01
model_demo = NULL # M2_noDD_WithDemoStoch #M1_noDD_noDemoStoch #M4_WithDD_WithDemoStoch #M3_WithDD_noDemoStoch #
thierrychambert's avatar
thierrychambert committed
time_horzion = 30
fatal_constant = "h"
pop_size_type = "Npair"
thierrychambert's avatar
thierrychambert committed

if(length(fatalities_mean) > 2) cumulated_impacts = TRUE else cumulated_impacts = FALSE
onset_time = onset_year - min(onset_year) + 1
onset_time = c(min(onset_time), onset_time)
thierrychambert's avatar
thierrychambert committed

N000 <- pop_vector(pop_size = pop_size_mean, pop_size_type = pop_size_type, s = survivals, f = fecundities)
sum(N000)

# Define K
theta = 1
K = pop_vector(pop_size = carrying_capacity, pop_size_type = pop_size_type, s = survivals, f = fecundities) %>% sum
K

# Define theoretical rMAX for the species
rMAX_species <- rMAX_spp(surv = tail(survivals,1), afr = min(which(fecundities != 0)))
rMAX_species

##  Avoid unrealistic scenarios
pop_growth_mean <- min(1 + rMAX_species, pop_growth_mean)
pop_growth_mean

thierrychambert's avatar
thierrychambert committed
lambda( build_Leslie(s = survivals, f = fecundities) )
thierrychambert's avatar
thierrychambert committed
##--------------------------------------------
thierrychambert's avatar
thierrychambert committed
##--------------------------------------------
# Calibrate vital rates to match the the desired lambda
inits <- init_calib(s = survivals, f = fecundities, lam0 = pop_growth_mean)
vr_calibrated <- calibrate_params(inits = inits, f = fecundities, s = survivals, lam0 = pop_growth_mean)
s_calibrated <- head(vr_calibrated, length(survivals))
f_calibrated <- tail(vr_calibrated, length(fecundities))
thierrychambert's avatar
thierrychambert committed

lambda( build_Leslie(s = s_calibrated, f = f_calibrated) )
thierrychambert's avatar
thierrychambert committed
##==============================================================================
##                         Analyses (simulations)                             ==
##==============================================================================
thierrychambert's avatar
thierrychambert committed
system.time(
thierrychambert's avatar
thierrychambert committed
run0 <- run_simul(nsim = nsim,
                            cumulated_impacts = cumulated_impacts,

                            fatalities_mean = fatalities_mean,
                            fatalities_se = fatalities_se,
                            onset_time = onset_time,

                            pop_size_mean = pop_size_mean,
                            pop_size_se = pop_size_se,
                            pop_size_type = pop_size_type,

                            pop_growth_mean = pop_growth_mean,
                            pop_growth_se = pop_growth_se,

                            survivals = s_calibrated,
                            fecundities = f_calibrated,

                            carrying_capacity = carrying_capacity,
                            theta = theta,
                            rMAX_species = rMAX_species,

                            model_demo = NULL,
                            time_horzion = time_horzion,
                            coeff_var_environ = coeff_var_environ,
                            fatal_constant = fatal_constant)
thierrychambert's avatar
thierrychambert committed
)
thierrychambert's avatar
thierrychambert committed

thierrychambert's avatar
thierrychambert committed
#####################################################
names(run0)
plot_traj(N, xlab = "Annee", ylab = "Taille de population (totale)")
thierrychambert's avatar
thierrychambert committed

get_metrics(N = out$N)$scenario$impact[time_horzion, ,-1] %>% round(.,2)

res = get_metrics(N = out$N, cumulated_impacts = cumulated_impacts)