-
Notifications
You must be signed in to change notification settings - Fork 1
/
getting-started-nmbayes.Rmd
343 lines (270 loc) · 10.2 KB
/
getting-started-nmbayes.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
---
title: "Getting Started with bbr.bayes and NONMEM Bayes"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Getting Started with bbr.bayes and NONMEM Bayes}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
# Introduction
This vignette takes you through some basic scenarios for modeling with
NONMEM Bayes using `bbr.bayes`, introducing you to its standard
workflow and functionality.
```{r, include = FALSE}
# NOTE: if running chunks interactively we need to load the package first
# because renv isolation prevents us from finding a bbr.bayes installation
if (interactive() || (isTRUE(getOption("knitr.in.progress") && Sys.getenv("CI") != "true"))) {
devtools::load_all()
}
```
```{r, results = "hide", message = FALSE, warning = FALSE}
library(bbr)
library(bbr.bayes)
library(dplyr)
```
`bbr.bayes` is an extension to `{bbr}` which focuses on traceable and
reproducible modeling using Bayesian methods. Note that you will also
need to load `bbr` to use most `bbr.bayes` functionality.
The `bbr` ["Getting Started with bbr and NONMEM"][gsb] vignette
provides an introduction to modeling with NONMEM using `bbr`. This
vignette covers modeling in NONMEM using Bayesian estimation methods,
highlighting ways NONMEM Bayes models in `bbr.bayes` extend or are
different from NONMEM models in `bbr`.
See the ["Getting Started with bbr.bayes and Stan"][gss] vignette for
an introduction to modeling with [Stan](https://mc-stan.org/).
```{r, include = FALSE}
options(
"bbr.bbi_exe_path" = read_bbi_path(),
"bbr.verbose" = FALSE
)
# Call normalizePath() to match bbr's handling (which matters if
# tempdir(), e.g., includes a symlink). See commit 13c2053 for more info.
TMP_DIR <- normalizePath(withr::local_tempdir("nmbayes-vignette-"))
REF_DIR <- system.file(
"model", "nonmem",
package = "bbr.bayes", mustWork = TRUE
)
MODEL_DIR <- file.path(TMP_DIR, "model", "nonmem", "bayes")
fs::dir_create(MODEL_DIR)
DATA_DIR <- file.path(TMP_DIR, "extdata")
fs::dir_create(DATA_DIR)
fs::file_copy(
system.file(
"extdata", "analysis3.csv",
package = "bbr.bayes", mustWork = TRUE
),
DATA_DIR
)
```
# Installing and configuring bbi
[bbi][] is required to work with NONMEM Bayes models. Below is a
high-level overview, but please see the
["Installing bbi" section][gsbi] of `bbr`'s "Getting Started with bbr
and NONMEM" vignette for detailed instructions.
First, point the `bbr.bbi_exe_path` option to a path where `bbi` is or
should be installed. This should be done for each R session, so you
likely want to include it in your project's `.Rprofile`.
```{r, eval = FALSE}
options("bbr.bbi_exe_path" = file.path(getwd(), "bin", "bbi"))
```
Then, if `bbi` is not already installed at the above location, call
`use_bbi()` to install it.
```{r, eval = FALSE}
use_bbi()
```
Finally, use `bbi_init()` to initialize a directory in which you plan
to store models.
```{r, eval = FALSE}
MODEL_DIR <- "model/nonmem"
bbi_init(
.dir = MODEL_DIR,
.nonmem_dir = "/opt/NONMEM",
.nonmem_version = "nm75"
)
```
This needs to be done only once for a given directory.
# Creating a NONMEM Bayes model object
As in `bbr`, the `bbr.bayes` workflow is organized around the concept
of [model objects][gsbm]. These are S3 objects which you will pass to
`bbr` and `bbr.bayes` functions that submit, summarize, or manipulate
models. A model object points to a path on disk; the set of files
that define a model is derived from this path.
A NONMEM Bayes model is similar to the "regular" NONMEM model from
`bbr`, but it has a more complex structure underneath that is used to
represent each MCMC chain. See the [NONMEM Bayes models in`bbr`][nb]
reference page for more details.
## Creating a model object *de novo*
A NONMEM bbr.bayes can be created by calling `new_model()` with the
path to your control stream _without the file extension_, in the same
way you would create a standard NONMEM model with `bbr`, as described
in the ["Getting Started with bbr and NONMEM"][gsb] vignette. To tell
`new_model()` to create a NONMEM Bayes model, pass "nmbayes" as its
`.model_type` argument.
```{r, include = FALSE}
fs::file_copy(
file.path(REF_DIR, "bayes", "1100.ctl"),
file.path(MODEL_DIR, "1000.ctl")
)
```
```{r}
mod1000 <- new_model(file.path(MODEL_DIR, 1000), .model_type = "nmbayes")
mod1000$model_type
```
## Creating a model object from an existing model
While you can create a fresh NONMEM Bayes model with `new_model()`,
more commonly you will create a new NONMEM Bayes model from an
existing model, either a regular NONMEM model (`bbi_nonmem_model`) or
a NONMEM Bayes model (`bbi_nmbayes_model`).
### From a regular NONMEM model
```{r, include = FALSE}
copy_model_from(
read_model(
system.file(
"model", "nonmem", "basic", "1",
package = "bbr.bayes", mustWork = TRUE
)
),
file.path(MODEL_DIR, "100")
) %>% replace_all_tags(c("two-compartment + absorption", "FOCE"))
```
To create a NONMEM Bayes model from a regular NONMEM model, use
`copy_model_as_nmbayes()`.
```{r}
mod100 <- read_model(file.path(MODEL_DIR, 100))
mod1100 <- copy_model_as_nmbayes(mod100, 1100, .inherit_tags = TRUE) %>%
update_model_id() %>%
replace_tag("FOCE", "BAYES")
mod100$model_type
mod1100$model_type
```
That will copy over the control stream and YAML , like
`copy_model_from()`, and then switch the model type to "nmbayes". In
addition, it will replace the estimation methods with two estimation
records that serve as a template for defining a NONMEM Bayes run.
```{r, echo = FALSE, message = FALSE, comment = ""}
glue::glue_data_safe(
list(model_id = get_model_id(mod1100)),
bbr.bayes:::NMBAYES_HELP,
.trim = FALSE
)
```
### From a NONMEM Bayes model
To create a NONMEM Bayes from another one, use `copy_model_from()`,
just as you would in `bbr` to [copy and iterate on a model][gsbc].
```{r}
mod1101 <- copy_model_from(mod1100) %>%
update_model_id()
# Edit control stream...
mod1101 <- add_notes(mod1101, "Switched from foo to bar")
```
# Submitting a model
To execute a model, pass it to [`submit_model()`][].
```{r, eval = FALSE}
submit_model(mod1100)
```
Underneath the [`submit_model()`][] method of `bbi_nmbayes_model`
objects creates several sub-models, one for generating the initial
values and one for each MCMC chain.
```{r, include = FALSE}
fs::dir_copy(
file.path(REF_DIR, "bayes", "1100"),
file.path(MODEL_DIR, "1100"),
overwrite = TRUE
)
mod1100 <- read_model(file.path(MODEL_DIR, "1100"))
```
```{r}
dir(get_output_dir(mod1100))
```
You shouldn't often need to directly access these sub-models because
`bbr.bayes` implements custom methods and functions to work with them
behind the scenes. For example, to monitor running models, you can
use `tail_lst()` and `tail_output()` in the same way as you would with
regular NONMEM models. They will print the result for each chain.
# Summarizing a model
## `read_fit_model()`
Once a model finishes running, an important entry point to summarizing
the result is the `?posterior::draws_array` object returned by
`read_fit_model()`.
```{r}
fit <- read_fit_model(mod1100)
dim(fit)
# Note that real models should use more chains and iterations!
posterior::niterations(fit)
posterior::nchains(fit)
posterior::nvariables(fit)
```
`{posterior}` provides various functions for inspecting and
summarizing posterior draws. For example, you can use
`posterior::summarize_draws()` to generate a table of summary
statistics and diagnostics:
```{r}
posterior::subset_draws(fit, "THETA") %>%
posterior::summarize_draws()
```
## `nm_join_bayes()`
`nm_join_bayes()` and `nm_join_bayes_quick()` are functions for
joining model output summaries to the input data, in a similar way
that `bbr::nm_join` does for non-Bayesian NONMEM models.
`nm_join_bayes()` selects a subset of the posterior samples. It uses
these as input to a user-defined [mrgsolve][] model to simulate
`EPRED` and `IPRED` and then summarizes the simulated values in the
result. In addition, it feeds the simulated `EPRED` values to
[npde][] to calculate EWRES and NPDE values.
`nm_join_bayes_quick()` does _not_ simulate quantities from the
posterior samples. It instead summarizes the reported values from the
table files. As the name suggests, this is useful for taking an
initial look at the results, though simulation-based values should be
used for more reliable estimates.
```{r}
nm_join_bayes_quick(mod1100) %>%
select(NUM, TIME, EPRED, IPRED, NPDE, EWRES)
```
## Accessing other information
If you need access to output files in the chain submodels, you can use
the `chain_paths()` helper.
```{r}
lst_files <- chain_paths(mod1100, extension = ".lst")
tab_files <- chain_paths(
mod1100,
name = get_model_id(mod1100), extension = ".tab"
)
fs::path_rel(lst_files, get_output_dir(mod1100))
file.size(lst_files)
fs::path_rel(tab_files, get_output_dir(mod1100))
file.size(tab_files)
```
## `model_summary()`
We have **not yet implemented** `model_summary()` for
`bbi_nmbayes_model` objects. This is a work in progress, but the
`?posterior::draws_array` object returned by `read_fit_model()` has
[various methods][ps] for summarizing the outputs.
# Run log and friends
`run_log()`, `config_log()`, and `summary_log()` will all find and
list NONMEM Bayes models.
```{r}
run_log(MODEL_DIR) %>%
select(run, model_type, tags, notes) %>%
collapse_to_string(tags, notes)
```
Note, however, that `summary_log()` does not currently include include
any useful summary statistics or diagnostics for NONMEM Bayes models.
This will be revisited once `model_summary()` is implemented (see
above).
[`submit_model()`]: ../reference/nmbayes_submit_model.html
[bbi]: https://github.com/metrumresearchgroup/bbi
[gsb]: https://metrumresearchgroup.github.io/bbr/articles/getting-started.html
[gsbc]: https://metrumresearchgroup.github.io/bbr/articles/getting-started.html#iteration
[gsbi]: https://metrumresearchgroup.github.io/bbr/articles/getting-started.html#installing-bbi
[gsbm]: https://metrumresearchgroup.github.io/bbr/articles/getting-started.html#create-model-object
[gss]: ./getting-started-stan.html
[mrgsolve]: https://mrgsolve.org/
[nb]: ../reference/bbr_nmbayes.html
[npde]: https://cran.r-project.org/web/packages/npde/index.html
[ps]: https://mc-stan.org/posterior/reference/index.html#summarizing-and-diagnosing-draws-objects