knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
dir.create(file.path(tempdir(), "sims")) knitr::opts_knit$set(root.dir = file.path(tempdir(), "sims"))
simcases is an R package to simplify the conduct of simulation studies across multiple cases/scenarios.
To install the latest release version from CRAN
install.packages("simcases")
To install the latest development version from GitHub
# install.packages("remotes") remotes::install_github("audrey-b/simcases")
First, define the likelihood, constants, parameters and other characteristics of the models to use for the simulations.
library(simcases) lik = "for(i in 1:10){ a[i] ~ dnorm(mu, 1/sigma^2)}" const = list(mu=0) sigma1 = list(sigma=1) sigma2 = list(sigma=2) all = ".*" a = "a"
Specify the models to use. The first row is a header and the following rows each describe a model.
models_sims = "code constants parameters monitor lik const sigma1 a lik const sigma2 a lik const sigma1 all lik const sigma2 all"
Simulate data. The results are written to files.
set.seed(10) smc_simulate(models = models_sims, nsims = 3, exists = NA, ask = FALSE)
Analyse data according to specific cases (scenarios).
prior <- "sigma ~ dunif(0, 6)" sigma <- "sigma" models_analysis <- "code code.add monitor lik prior sigma lik prior sigma" cases <- "sims analyse 1 1 2 1 3 2 4 2" smc_analyse(models = models_analysis, cases = cases, mode = simanalyse::sma_set_mode("quick"))
Evaluate the performance of the models across the cases (scenarios)
smc_evaluate(cases, monitor="sigma")
Have a look at the files created.
files <- list.files(getwd(), recursive=TRUE, all.files=TRUE) print(files)
Load one file.
readRDS(file.path(getwd(), files[3]))
When a large number of models is used, it can be more convenient to specify models as data frames to facilitate query and manipulation. The example above may be reproduced as follows.
models <- tibble::tribble( ~parameters, ~monitor, "sigma1", "a", "sigma2", "all" ) models <- tidyr::expand(models, parameters, monitor) models$code <- "lik" models$constants <- "const" models set.seed(10) smc_simulate(models = models, nsims = 3, fun = identity, exists = NA, ask = FALSE)
Please report any issues.
Pull requests are always welcome.
Please note that this project is released with a Contributor Code of Conduct. By contributing, you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.