knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) par(mar = c(1, 1, 1, 1)) set.seed(429) suppressPackageStartupMessages(library(bSims))
We recommend exploring the simulation settings
interactively in the shiny apps
using run_app("bsimsH")
app for the homogeneous habitat case and
the run_app("bsimsHER")
app for the stratified habitat case.
The apps represent the simulation layers as tabs,
the last tab presenting the settings that can be copied onto the
clipboard and pasted into the R session or code.
In simple situations, comparing results from a few different settings
might be enough.
Let us consider the following simple comparison: we want to see how much of an effect does roads have when the only effect is that the road stratum is unsuitable. Otherwise there are no behavioral or detectability effects of the road.
library(bSims) tint <- c(2, 4, 6, 8, 10) rint <- c(0.5, 1, 1.5, 2, Inf) # unlimited ## no road b1 <- bsims_all( road = 0, density = c(1, 1, 0), tint = tint, rint = rint) ## road b2 <- bsims_all( road = 0.5, density = c(1, 1, 0), tint = tint, rint = rint) b1 b2
The bsims_all
function accepts all the arguments we discussed
before for the simulation layers. Unspecified arguments
will be taken to be the default value.
However, bsims_all
does not evaluate these arguments,
but it creates a closure with the settings.
Realizations can be drawn as:
b1$new() b2$new()
Run multiple realizations is done as:
B <- 25 # number of runs bb1 <- b1$replicate(B) bb2 <- b2$replicate(B)
The replicate function takes an argument for the
number of replicates (B
) and returns a list of transcript objects
with B elements.
The cl
argument can be used to parallelize the work,
it can be a numeric value on Unix/Linux/OSX, or a cluster object on any OS.
The recover = TRUE
argument allows to run simulations with error
catching.
Simulated objects returned by bsims_all
will contain different
realizations and all the conditionally independent layers.
Use a customized layered approach if former layers are meant to be kept
identical across runs.
In more complex situations the shiny apps will help identifying
corner cases that are used to define a gradient of settings
for single or multiple simulation options.
Let us consider the following scenario:
we would like to evaluate how the estimates are changing with
increasing road width. We will use the expand_list
function
which creates a list from all combinations of the supplied inputs.
Note that we need to wrap vectors inside list()
to avoid
interpreting those as values to iterate over.
s <- expand_list( road = c(0, 0.5, 1), density = list(c(1, 1, 0)), tint = list(tint), rint = list(rint)) str(s)
We now can use this list of settings to run simulations for each. The following illustrates the use of multiple cores:
b <- lapply(s, bsims_all) nc <- 4 # number of cores to use library(parallel) cl <- makeCluster(nc) bb <- lapply(b, function(z) z$replicate(B, cl=cl)) stopCluster(cl)
In some cases, we want to evaluate crossed effects of multiple settings. For example, road width and spatial pattern (random vs. clustered):
s <- expand_list( road = c(0, 0.5), xy_fun = list( NULL, function(d) exp(-d^2/1^2) + 0.5*(1-exp(-d^2/4^2))), density = list(c(1, 1, 0)), tint = list(tint), rint = list(rint)) str(s)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.