Auxiliary functions

The simulationTimes function generates lists of time series that can be specified as simulation time and time points to keep in simulated time.

Time <- simulationTimes(t_start = 0, t_end = 100, t_step = 0.5, 
                           t_store = 100)

Time$t_sys[Time$t_index]

Time$t_sys contains all time points, and Time$t_index stores the selected index for time points. Therefore, Time$t_sys[Time$t_index] returns the selected time points. Note that the tail would be trimmed when the interval between start and end cannot be divided exactly by step length with no remainder.

Generating multiple simulations at once

params <- list(
    n_species = 10,
    n_resources = 5,
    E = randomE(
        n_species = 10, n_resources = 5,
        mean_consumption = 1, mean_production = 3
    ),
    x0 = rep(0.001, 10),
    resources = rep(1000, 5),
    monod_constant = matrix(rbeta(10 * 5, 10, 10), nrow = 10, ncol = 5),
    inflow_rate = .5,
    outflow_rate = .5,
    migration_p = 0,
    stochastic = TRUE,
    t_start = 0,
    t_end = 20,
    t_store = 100,
    growth_rates = rep(2, 10),
    norm = FALSE
)

###  restructure the parameter list
param_iter_new <-replicate(8, list(list(x0 = runif(n = 10, min = 0.01, max = 0.02))))

CRMSimus_single_param_new <- list()
CRMSimus_single_param_new <- lapply(
  param_iter_new,
  function(pars){
    do.call(
      what = simulateConsumerResource,
      args = modifyList(params, val = pars)
    )
  }
)
  1. demo to iterate combinations of parameters e.g. 9 different x0 and 9 different growth_rates
param_iter2_new <- replicate(9, list(list(x0 = runif(n = 10, min = 0.01, max = 0.02),
                                          growth_rates = runif(n=10, min = 0.5, max = 1))))

CRMSimus_double_param_new <- list()
CRMSimus_double_param_new <- lapply(
  param_iter2_new,
  function(pars){
    do.call(
      what = simulateConsumerResource,
      args = modifyList(crm_params, val = pars)
    )
  }
)
## 3 demo multiple instances
CRMsimus_multiple_instances_new <- lapply(
  seq_len(5),
  function(pars){
    do.call(
      what = simulateConsumerResource,
      args = crm_params
    )
  }
)
## 4&5 demo to iterate multiple instances
CRMSimus_single_param_multiple_instances_new <- lapply(
  rep(param_iter_new, each = 3), 
  # or rep(param_iter_new, times = 3), depending on how you'd like to organize the data
  function(pars){
    do.call(
      what = simulateConsumerResource,
      args = modifyList(crm_params, val = pars)
    )
  }
)

CRMSimus_single_param_multiple_instances_new <- lapply(
  rep(param_iter2_new, each = 4),
  function(pars){
    do.call(
      what = simulateConsumerResource,
      args = modifyList(crm_params, val = pars)
    )
  }
)

It is also possible to incorporate phylogenetic tree information into TreeSummarizedExperiment objects. For further details, see:

library(TreeSummarizedExperiment)
help("TreeSummarizedExperiment-constructor", package = TreeSummarizedExperiment)
hubbellmodelRates <- simulateHubbellRates(x0 = c(0,5,10),
    migration_p = 0.1, metacommunity_probability = NULL, k_events = 1, 
    growth_rates = NULL, norm = FALSE, t_end=100)
A_normal <- powerlawA(n_species = 4, alpha = 3)
glvmodel <- simulateGLV(n_species = 4, A = A_normal, t_start = 0, 
    t_store = 100, t_end=100, stochastic = FALSE, norm = FALSE)


microbiome/miaSim documentation built on July 22, 2024, 4:58 p.m.