So far, we have run models with the
spades() only has a few arguments:
- debug - .plotInitialTime - .saveInitialTime - progress - cache
What if we want to do more stuff?
What if we have a stochastic model and need many independent (Monte Carlo) runs?
experiment function does this.
?experiment # for details and examples
Generally, however, this creates problems because of the computational time required …
It is as easy as
replicates = 2 to do two replicates:
?experiment # for details # See Example 5 # ... run mySim from top of help file sims <- experiment(mySim, replicates = 2) attr(sims, "experiment")$expDesign # shows 2 reps of same expt
What does it return?
What is the
Replication is necessary but it multiplies the time it takes to run any model.
Fortunately, replication can use parallel processing:
SpaDES functions that are parallel aware:
In each case, there are two ways:
## option 1: raster::beginCluster() # make a cluster silently in background ## option 2: cl <- parallel::makeCluster() # need to explicitly pass the # cluster object into functions # using cl argument
?experiment ## see example 1 experimentParams <- list(fireSpread = list(spreadprob = list(0.2, 0.23), nFires = list(20, 10)), caribouMovement = list(N = list(100, 1000))) sims <- experiment(mySim, params = experimentParams) attr(sims, "experiment")
spades call will generate output files. Where do they go when using
?experiment ## see example 4 sims <- experiment(mySim, params = experimentParams, dirPrefix = c("expt", "simNum")) attr(sims, "experiment")$expVals # shows 8 alternative #experiment levels, 24 unique parameter values dir(outputPath(mySim))
Pick a few examples and try to understand them. (NOTE: they get more complicated as you get towards the bottom.)
experiment can take a lot of resources, but there are a few options:
Use your own computer, a local cluster, a server, cloud and more;
Access Compute Canada for free (though sometimes the queues are long):
Parameters can be estimated from:
nTrees <- 50 diamCM <- rlnorm(nTrees, meanlog = 2, sdlog = 0.7) height <- rnorm(nTrees, sqrt(diamCM)*2 + rnorm(nTrees)) glmObj <- glm(height ~ diamCM) plot(diamCM, height) summary(glmObj)
We can use these methods which give more accurate prediction:
# predict with new data predVals <- predict(glmObj, newdata = data.frame( diamCM = rlnorm(nTrees, meanlog = 2, sdlog = 0.7) ))
These have many features that can be used in simulations
SpaDES we can now fit the data and use the parameters all inside the simulation
DEoptimseems to be the best optimization package for heuristic optimization
SpaDESuses this package
There are several examples in
?POM, which we will go through:
## example 1 # What is example 1 doing? # Run it # What does the result mean?
## example 2 # What is example 2 doing? # Run it # What does the result mean?
## example 3 # What is example 3 doing? # Run it # What does the result mean?
Often we want to run a simulation with a variety of parameter values as inputs. This is sometimes called a simulation experiment. The
experiment function gives us an easy interface to this.
We will use the
establishment module that we created in exercise 4a.
library(SpaDES) module.path <- file.path(dirname(tempdir()), "modules") downloadModule("establishment", path = module.path) setPaths(modulePath = module.path) library(ggplot2) modules <- list("establishment") mySim <- simInit(modules = modules) # make sure the plotting device is clear clearPlot() spades(mySim)
experimentto run a
spadescall for each parameter set (note that
experimentis just a wrapper around
sum(mySim$establish), i.e., the number of pixels that had an establishment event.