-
Notifications
You must be signed in to change notification settings - Fork 1
/
NMsim-ReuseSimSubjects.Rmd
133 lines (109 loc) · 4.23 KB
/
NMsim-ReuseSimSubjects.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
---
title: "Reuse simulated subjects"
output:
rmarkdown::html_vignette:
toc: true
Suggests: markdown
VignetteBuilder: knitr
vignette: >
%\VignetteIndexEntry{ParameterUncertainty}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
header-includes:
- \usepackage{ae}
---
```{r,include = FALSE}
library(data.table)
library(NMdata)
library(NMsim)
library(fst)
##knitr::opts_chunk$set(dev = "cairo_pdf")
knitr::opts_chunk$set(
collapse = TRUE
,comment = "#>"
,fig.width=7
,cache=FALSE
)
## NMdataConf(dir.psn="/opt/psn")
## this changes data.table syntax. I think we can do without.
## knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60), tidy=TRUE)
run.simuls <- FALSE
```
Simulating new subjects is a very common task, and this is even the
default simulation method in `NMsim` (because Nonmem does that with in
a `$SIMULATION` step). However, sometimes one may want to perform
multiple simulations using the same _simulated_ subjects. So we want
to simulate new subjects, but only once, and then we want to be able
to reuse those subjects. This can be wanted for at least these
reasons:
* Compare simulations of different scenarios with multiple subjects
where ensuring that the difference in results isn't caused by
differences in simulated subjects (less important the more subjects
that are used in the simulations)
* Ensure reproducibility of results. If the simulation input data is
modified by say adding a simulation scenario in between two existing
ones - or even just a sample time, the random number generator will
be in different state when sampling the subjects the next time it
runs. Suddenly, an estimated prediction interval comes out a little
different.
`NMsim` has a way to do this. It includes a function, `simPopEtas()`, to generate a
simulated `.phi` file which is a Nonmem-native format for storing
emperical Bayes estimates (essentially individual parameters). It does
so by reading and simulating from the estimated `OMEGA` matrix
(random-effect variance-covariance matrix - a covariance step is not
needed to get this). The simulation method called `NMsim_known()` can
then be used to simulate subjects as stored in this file. See
[`NMsim-known.html`](https://philipdelff.github.io/NMsim/articles/NMsim-known.html)
for more information on `NMsim_known`.
### What is a subject?
What is refered to here by "subject" is really a combination of `ETA`s. Covariates must be handled by the user in the simulation input dataset. This is also discussed in [`NMsim-known.html`](https://philipdelff.github.io/NMsim/articles/NMsim-known.html).
## Example
```{r}
file.project <- function(...)file.path(system.file("examples",package="NMsim"),...)
file.mod <- file.project("nonmem/xgxr032.mod")
```
Let's simulate 10,000 ETA combinations and store them in a file called `xgxr032_simEtas.phi`.
```{r}
NMsim:::simPopEtas(file.mod=file.mod,
N=1e4,
seed=238861,
file.phi="xgxr032_simEtas.phi"
)
```
We'll just use the simulation data set created in [`NMsim-DataCreate.html`](https://philipdelff.github.io/NMsim/articles/NMsim-DataCreate.html)
```{r}
dat.sim <- read_fst(path="simulate-results/dat_sim.fst",as.data.table=TRUE)
```
And now we can use `NMsim_known()`.
```{r}
dat.sim.multiple <- egdt(dat.sim[,!("ID")],data.table(ID=c(1,4,89)))
setorder(dat.sim.multiple,ID,TIME,EVID)
```
```{r,include=FALSE,eval=FALSE}
library(devtools)
unloadNamespace("NMsim")
unloadNamespace("NMdata")
load_all("~/wdirs/NMdata")
load_all("~/wdirs/NMsim")
```
```{r,eval=FALSE}
simres <- NMsim(
file.mod=file.mod,
data=dat.sim.multiple,
method.sim=NMsim_known,
file.phi="xgxr032_simEtas.phi",
name.sim="reuseSubjs",
table.vars="PRED IPRED",
path.nonmem="/opt/NONMEM/nm75/run/nmfe75",
dir.sims="simulate-tmp",
dir.res="simulate-results"
)
```
### Simulate the same simulated subjects on multiple regimens
```{r}
dat.sim.multiple.regs <- lapply(c(100,300,600),function(dose1){
transform(dat.sim.multiple,AMT=AMT/300*dose1,trt=sprintf("%d mg then %d mg QD",dose1,dose1/2))
}) |> rbindlist()
dat.sim.multiple.regs[,REC:=.I]
```
## Note on between-occasion variability (supported)