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

Lifecycle: experimental Travis build Status AppVeyor build status Codecov test coverage License: GPL3 CRAN status CRAN downloads

simcases is an R package to simplify the conduct of simulation studies across multiple cases/scenarios.

Installation

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")

Demonstration

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]))

Additional features

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)

Contribution

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.



audrey-b/simcases documentation built on July 22, 2020, 6:20 p.m.