Last updated: 2019-09-09

Checks: 7 0

Knit directory: W_shredder/

This reproducible R Markdown analysis was created with workflowr (version 1.4.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20180716) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    Proc_B_manuscript/.DS_Store
    Ignored:    code/.DS_Store
    Ignored:    data/
    Ignored:    figures/.DS_Store
    Ignored:    output/

Untracked files:
    Untracked:  .gitignore

Unstaged changes:
    Modified:   figures/fig_4_inset.pdf

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
Rmd 12cf147 lukeholman 2019-09-09 almost done
html 9f1c407 lukeholman 2019-09-09 Build site.
Rmd 3041bd8 lukeholman 2019-09-09 First commit
html 3041bd8 lukeholman 2019-09-09 First commit

Load functions from analysis/model_functions.Rmd

source_rmd <- function(file){
  options(knitr.duplicate.label = "allow")
  tempR <- tempfile(tmpdir = ".", fileext = ".R")
  on.exit(unlink(tempR))
  knitr::purl(file, output = tempR, quiet = TRUE)
  source(tempR, local = globalenv())
}

source_rmd("analysis/model_functions.Rmd")
custom_functions <- ls()

Define the parameter space

Define the first parameter space

Here, we define the possible parameter ranges, which we will sample from using Latin hypercube sampling. For this first parameter space, the parameter ranges span most/all of the sensible range.

parameter_ranges_1 <- data.frame(
  release_size = c(10, 100),
  release_strategy = c(0, 1),         # binary variable: local or global release
  W_shredding_rate = c(0.4, 1),       # p-shred in the paper
  Z_conversion_rate = c(0, 1),        # p-conv in the paper
  Zr_creation_rate = c(0, 0.1),       # p-nhej in the paper
  Zr_mutation_rate = c(0.0, 0.00001), # mu-Z
  Wr_mutation_rate = c(0.0, 0.00001), # mu-W
  cost_Zdrive_female = c(0, 0.6),     # Cost of Z* to female fecundity
  cost_Zdrive_male   = c(0, 0.6),     # Cost of Z* to male mating success
  male_migration_prob = c(0.001, 0.5),
  female_migration_prob = c(0.001, 0.5),
  migration_type = c(0, 1), # binary variable: do migrants move to next door patch, or a random patch anywhere in the world?
  n_patches = c(2, 50), # integer number of patches
  max_fecundity = c(10, 1000), # r in the paper
  softness = c(0, 1), # psi in the paper
  male_weighting = c(0.1, 1.9), # delta in the paper
  density_dependence_shape = c(0.1, 1.9), # alpha in the paper
  initial_A = c(0, 1),
  initial_B = c(0, 1)
)

Define the second parameter space

For this second parameter space, we assume that the W-shredding rate (\(p_{shred}\)) is 1, while all other parameter ranges span most/all of the sensible range, same as before.

parameter_ranges_2 <- data.frame(
  release_size = c(10, 100),
  release_strategy = c(0, 1),         # binary variable: local or global release
  W_shredding_rate = c(0, 1),         # p-shred in the paper
  Z_conversion_rate = c(0, 1),        # p-conv in the paper
  Zr_creation_rate = c(0, 0.1),       # p-nhej in the paper
  Zr_mutation_rate = c(0.0, 0.00001), # mu-Z
  Wr_mutation_rate = c(0.0, 0.00001), # mu-W
  cost_Zdrive_female = c(0, 0.6),     # Cost of Z* to female fecundity
  cost_Zdrive_male   = c(0, 0.6),     # Cost of Z* to male mating success
  male_migration_prob = c(0.001, 0.5),
  female_migration_prob = c(0.001, 0.5),
  migration_type = c(0, 1), # binary variable: do migrants move to next door patch, or a random patch anywhere in the world?
  n_patches = c(2, 50), # integer number of patches
  max_fecundity = c(10, 1000), # r in the paper
  softness = c(0, 1), # psi in the paper
  male_weighting = c(0.1, 1.9), # delta in the paper
  density_dependence_shape = c(0.1, 1.9), # alpha in the paper
  initial_A = c(0, 1),
  initial_B = c(0, 1)
) %>% mutate(W_shredding_rate = 1)

Define the third parameter space

For this third parameter space, we assume that females carrying the W-shredder are sterile, so cost_Zdrive_female (\(c_f\)) is 1. The W-shredding rate (\(p_{shred}\)), and the initial frequency of the shredding resistance allele A, is also fixed (this doesn’t affect the model since \(Z^*\) females don’t breed). All other parameter ranges span most/all of the sensible range, same as before.

parameter_ranges_3 <- data.frame(
  release_size = c(10, 100),
  release_strategy = c(0, 1),         # binary variable: local or global release
  W_shredding_rate = c(0, 1),         # p-shred in the paper
  Z_conversion_rate = c(0, 1),        # p-conv in the paper
  Zr_creation_rate = c(0, 0.1),       # p-nhej in the paper
  Zr_mutation_rate = c(0.0, 0.00001), # mu-Z
  Wr_mutation_rate = c(0.0, 0.00001), # mu-W
  cost_Zdrive_female = c(0, 1),       # Cost of Z* to female fecundity
  cost_Zdrive_male   = c(0, 0.6),     # Cost of Z* to male mating success
  male_migration_prob = c(0.001, 0.5),
  female_migration_prob = c(0.001, 0.5),
  migration_type = c(0, 1), # binary variable: do migrants move to next door patch, or a random patch anywhere in the world?
  n_patches = c(2, 50), # integer number of patches
  max_fecundity = c(10, 1000), # r in the paper
  softness = c(0, 1), # psi in the paper
  male_weighting = c(0.1, 1.9), # delta in the paper
  density_dependence_shape = c(0.1, 1.9), # alpha in the paper
  initial_A = c(0, 1),
  initial_B = c(0, 1)
) %>% mutate(W_shredding_rate = 0,
             initial_A = 0,
             cost_Zdrive_female = 1)

Define the fourth parameter space

For this fourth parameter space, we assume that females carrying the W-shredder are sterile, so cost_Zdrive_female (\(c_f\)) is 1. The W-shredding rate (\(p_{shred}\)), and the initial frequency of the shredding resistance allele A, is also fixed (this doesn’t affect the model since \(Z^*\) females don’t breed). All other parameter ranges span most/all of the sensible range as for the third space, with some exceptions. All parameters that affect resistance were set to zero, to give the female-sterilising drive a better chance to work.

parameter_ranges_4 <- data.frame(
  release_size = c(10, 100),
  release_strategy = c(0, 1),         # binary variable: local or global release
  W_shredding_rate = c(0, 1),         # p-shred in the paper
  Z_conversion_rate = c(0, 1),        # p-conv in the paper
  Zr_creation_rate = c(0, 0.1),       # p-nhej in the paper
  Zr_mutation_rate = c(0.0, 0.00001), # mu-Z
  Wr_mutation_rate = c(0.0, 0.00001), # mu-W
  cost_Zdrive_female = c(0, 1),       # Cost of Z* to female fecundity
  cost_Zdrive_male   = c(0, 0.6),     # Cost of Z* to male mating success
  male_migration_prob = c(0.001, 0.5),
  female_migration_prob = c(0.001, 0.5),
  migration_type = c(0, 1), # binary variable: do migrants move to next door patch, or a random patch anywhere in the world?
  n_patches = c(2, 50), # integer number of patches
  max_fecundity = c(10, 1000), # r in the paper
  softness = c(0, 1), # psi in the paper
  male_weighting = c(0.1, 1.9), # delta in the paper
  density_dependence_shape = c(0.1, 1.9), # alpha in the paper
  initial_A = c(0, 1),
  initial_B = c(0, 1)
) %>% mutate(W_shredding_rate = 0,
             initial_A = 0,
             cost_Zdrive_female = 1,
             Zr_creation_rate = 0,    # No resistance to male gene drive this time
             Zr_mutation_rate = 0,
             initial_B = 0)

Perform Latin hypercube sampling

do_lhs <- function(parameter_ranges, n_samples){
  
  n_parameters <- ncol(parameter_ranges)
  X <- randomLHS(n_samples, n_parameters)
  
  for(i in 1:n_parameters){
    X[,i] <- parameter_ranges[1, i] + 
      (parameter_ranges[2, i] - parameter_ranges[1, i]) * X[, i]
  }
  
  colnames(X) <- colnames(parameter_ranges)
  
  # Make integers where needed, and create the binary variables
  as.data.frame(X) %>%
    mutate(n_patches = round(n_patches),
           release_size = round(release_size),
           release_strategy = ifelse(release_strategy < 0.5, "one_patch", "all_patches"),
           migration_type = ifelse(migration_type < 0.5, "local", "global"),
           initial_A = ifelse(initial_A < 0.5, 0, 0.05),
           initial_B = ifelse(initial_B < 0.5, 0, 0.05),
           cost_Wr = 0,
           cost_Zr = 0,
           cost_A = 0,
           cost_B = 0,
           carrying_capacity = 10000,
           initial_pop_size = 10000,
           initial_Zdrive = 0,
           initial_Zr = 0.00,
           initial_Wr = 0.00,
           realisations = 1, # change to e.g. 1:100 for replication
           generations = 1000,
           burn_in = 50
    )
}

n_parameter_spaces <- 10^6
print(paste("Sampling", n_parameter_spaces, "parameter spaces from a Latin hypercube..."))

set.seed(12345)
parameters <- rbind(
  do_lhs(parameter_ranges_1, 2 * n_parameter_spaces),
  do_lhs(parameter_ranges_2, n_parameter_spaces),
  do_lhs(parameter_ranges_3, n_parameter_spaces),
  do_lhs(parameter_ranges_4, n_parameter_spaces)
)

# shuffle to equalise workload across CPUs
parameters <- parameters[sample(nrow(parameters)), ] 

Launch lots of SLURM jobs to run each parameter space

print("...finished sampling. Launching SLURM job...")
chunk_size <- 1000
cpus <- 1 
sopt <- list(time = '80:00:00',  # max run time per node in hours
             mem  = '32768')     # 32GB memory per node

chunks <- split(1:nrow(parameters),
                ceiling(seq_along(1:nrow(parameters)) / chunk_size))
number_of_chunks <- length(chunks)


sjob <- slurm_apply(
  f = function(i) {
    try(do_all_parameters(parameters[chunks[[i]],], 
                          over_write = FALSE, 
                          cores = cpus,
                          wd = working_directory))
  },
  params = data.frame(i = 1:length(chunks)),
  add_objects = c("do_all_parameters", 
                  "parameters", "cpus",
                  "chunks", "number_of_chunks",
                  custom_functions),
  jobname = "W_shredder",
  nodes = number_of_chunks, 
  cpus_per_node = cpus, 
  slurm_options = sopt)

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] workflowr_1.4.0 Rcpp_1.0.2      digest_0.6.20   rprojroot_1.3-2
 [5] backports_1.1.2 git2r_0.23.0    magrittr_1.5    evaluate_0.14  
 [9] stringi_1.4.3   fs_1.3.1        whisker_0.3-2   rmarkdown_1.13 
[13] tools_3.5.1     stringr_1.4.0   glue_1.3.1.9000 xfun_0.8       
[17] yaml_2.2.0      compiler_3.5.1  htmltools_0.3.6 knitr_1.23