forked from audrey-b/simcases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
148 lines (118 loc) · 4.27 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
```{r setup, include = FALSE}
dir.create(file.path(tempdir(), "sims"))
knitr::opts_knit$set(root.dir = file.path(tempdir(), "sims"))
```
# simcases
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![Travis build Status](https://www.travis-ci.com/audrey-b/simcases.svg?token=LCuTqqVUfUECxm1xTQLb&branch=master)](https://www.travis-ci.com/audrey-b/simcases)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/51yh2k5lkd9h7ey3/branch/master?svg=true)](https://ci.appveyor.com/project/audrey-b/simcases/branch/master)
[![Codecov test coverage](https://codecov.io/gh/audrey-b/simcases/branch/master/graph/badge.svg)](https://codecov.io/gh/audrey-b/simcases?branch=master)
[![License: GPL3](https://img.shields.io/badge/License-GPL3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.html)
[![CRAN status](https://www.r-pkg.org/badges/version/simcases)](https://cran.r-project.org/package=simcases)
![CRAN downloads](http://cranlogs.r-pkg.org/badges/simcases)
<!-- badges: end -->
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](https://cran.r-project.org)
```r
install.packages("simcases")
```
To install the latest development version from [GitHub](https://github.com/audrey-b/simcases)
```r
# 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.
```{r}
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.
```{r}
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.
```{r}
set.seed(10)
smc_simulate(models = models_sims,
nsims = 3,
exists = NA,
ask = FALSE)
```
Analyse data according to specific cases (scenarios).
```{r}
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)
```{r}
smc_evaluate(cases, monitor="sigma")
```
Have a look at the files created.
```{r}
files <- list.files(getwd(), recursive=TRUE, all.files=TRUE)
print(files)
```
Load one file.
```{r}
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.
```{r}
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](https://github.com/audrey-b/simcases/issues).
[Pull requests](https://github.com/audrey-b/simcases/pulls) are always welcome.
Please note that this project is released with a [Contributor Code of Conduct](https://github.com/audrey-b/simcases/blob/master/CODE_OF_CONDUCT.md).
By contributing, you agree to abide by its terms.