-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
310 lines (221 loc) · 6.04 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
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
---
output: github_document
title: ""
author: ""
date: ""
---
```{r,echo=FALSE}
knitr::opts_chunk$set(comment='.', message=FALSE,
fig.path="inst/maintenance/img/README-")
```
# mrgsolvetk
A toolkit to be used with `mrgsolve`
# Installation
```{r, eval = FALSE}
library(devtools)
install_github("mrgsolve/mrgsolvetk", ref = "mrgoptim")
```
# Examples
- [Sensitivity Analyses](#sensitivity-analyses)
- [Parameter Optimization](#maximum-likelihood-parameter-optimization)
```{r warning=FALSE}
library(ggplot2)
library(dplyr)
library(mrgsolve)
library(mrgsolvetk)
theme_set(theme_bw())
mod <- mread_cache("pk1cmt",modlib())
mod <- ev(mod, amt=100) %>% Req(CP) %>% update(end = 48, delta = 0.25)
param(mod)
```
## Sensitivity analyses
### `sens_unif`
- Draw parameters from uniform distribution based on current parameter values
- `lower` and `upper` scale the parameter value to provide `a` and `b` arguments to `runif`
```{r}
out <-
mod %>%
select(CL,VC,KA1) %>%
sens_unif(.n=10, lower=0.2, upper=3)
out
sens_plot(out, CP)
```
We can also make a univariate version of this
```{r}
mod %>%
select(CL,VC,KA1) %>%
sens_unif(.n=10, lower=0.2, upper=3, univariate = TRUE) %>%
sens_plot(CP, split = TRUE)
```
### `sens_norm`
- Draw parameters from (log) normal distribution based on current parameter
values and `%CV`
```{r}
mod %>%
select(CL,VC) %>%
sens_norm(.n=10, cv=30) %>%
sens_plot(CP)
```
### `sens_seq`
- Give a sequence for one or more parameters
```{r}
mod %>% sens_seq(CL = seq(2,12,2), VC = seq(30,100,10)) %>% sens_plot(CP)
```
### `sens_range`
- Create sets of parameters equally-spaced between two bounds
```{r}
mod %>%
select(CL,VC) %>%
sens_range(.n = 5, .factor = 4) %>%
sens_plot(CP, split = TRUE)
```
or
```{r}
mod %>%
sens_range(CL = c(0.5, 1.5), VC = c(10,40), .n = 5) %>%
sens_plot(CP)
```
### `sens_grid`
- Like `sens_seq` but performs all combinations
```{r}
mod %>% sens_grid(CL = seq(1,10,1), VC = seq(20,40,5)) %>% sens_plot(CP)
```
### `sens_covset`
- Use `dmutate` to generate random variates for each parameter
```{r}
cov1 <- dmutate::covset(CL ~ runif(1,3.5), VC[0,] ~ rnorm(50,25))
cov1
```
```{r}
out <- mod %>% sens_covset(cov1)
```
```{r}
out
distinct(out,ID,CL,VC)
```
## Maximum Likelihood Parameter Optimization
### `mrgoptim`
This example shows a simultaneous fit of PK and PD data from five dose levels.
#### Data structure
The data to be fit is an mrgsolve dataset. Required columns for fitting are:
- ID
- time
- evid
- cmt
- amt
- dv
```{r}
data <- read.csv("inst/maintenance/data/optim-example.csv")
head(data)
```
Plot the data to get an idea of the profiles to be fit. cmt 1 is plasma concentration
data and cmt 2 is PD data
```{r warning=FALSE}
ggplot(data, aes(x = time, y = dv, color = as.factor(ID))) +
geom_point() +
geom_line() +
facet_wrap("cmt") +
guides(color = FALSE)
```
The following model will be fit to these data:
- PK: 2 compartment model
- PD: Inhibitory Emax model with baseline
- Proportional error models for both PK and PD
```{r}
code<-"
$PROB 2 cmt PK Model, Emax PD model
$PARAM
CL=10
VC = 20
VP = 20
Q=20
Emax = 60
BL = 50
EC50 = 10
gamma =1
sigma1 = 0.1
sigma2 = 0.1
$CMT X1 X2
$ODE
dxdt_X1 = -(Q+CL)/VC*X1+Q/VP*X2;
dxdt_X2 = Q/VC*X1-Q/VP*X2;
$TABLE
capture PK = X1/VC;
capture varPK = (PK*sigma1)*(PK*sigma1);
capture PD = BL-(pow(PK,gamma)*Emax)/(pow(PK,gamma)+pow(EC50,gamma));
capture varPD = (PD*sigma2)*(PD*sigma2);
capture ipred = NAN;
capture var = NAN;
if(self.cmt == 1) {
ipred = PK;
var = varPK;
}
if(self.cmt == 2) {
ipred = PD;
var = varPD;
}"
mod <- mcode("2cmtPK-Emax", code)
```
Here, the predicted plasma concentrations, response, and variances were captured in
the **PK**, **PD**, **varPK**, and **varPD** outputs, respectively. Predictions
and variances are consolidated to a single column each. If cmt == 1 the predicted
output, `ipred`, will be PK and prediction variance, `var`, varPK. If cmt == 2 the
predicted output will be PD and prediction variance varPD.
Let's check how the initial parameter values fit the data.
```{r warning=FALSE}
out <- mod %>%
data_set(data) %>%
carry.out(cmt, dv) %>%
obsonly() %>%
mrgsim() %>%
as.data.frame()
ggplot(filter(out, cmt == 1), aes(x = time, y = ipred, color = as.factor(ID))) +
geom_line() +
geom_point(aes(y = dv)) +
guides(color = FALSE)
ggplot(filter(out, cmt == 2), aes(x = time, y = ipred, color = as.factor(ID))) +
geom_line() +
geom_point(aes(y = dv)) +
guides(color = FALSE)
```
Not terrible, should be good enough for initial estimates.
Now let's use `mrgoptim` to optimize the parameters and return parameter values and precision.
Use the `output`, and `var` arguments to specify which columns in the model code correspond to the predicted values and variances. Specify which system parameters to optimize with the `prms` argument and variance parameters with the `v_prms` arguments.
```{r echo=T, results='hide'}
fit <- mod %>%
data_set(data) %>%
mrgoptim(output = "ipred",
var = "var",
prms = c("CL",
"VC",
"VP",
"Q",
"Emax",
"BL",
"EC50",
"gamma"),
v_prms = c("sigma1", "sigma2"),
method = "newuoa")
```
The function returns a list with some information about the optimization, the final objective function value (-LL), final parameter estimates, covariance and correlation matrices, CV percent, and output dataset.
```{r}
print(fit)
```
Lets check how the optimized parameters fit the data.
```{r warning=FALSE}
out_fit <- mod %>%
param(fit$par) %>%
carry.out(cmt, dv) %>%
data_set(data) %>%
obsonly() %>%
mrgsim() %>%
as.data.frame()
ggplot(filter(out_fit, cmt == 1), aes(x = time, y = ipred, color = as.factor(ID))) +
geom_line() +
geom_point(aes(y = dv)) +
guides(color = FALSE)
ggplot(filter(out_fit, cmt == 2), aes(x = time, y = ipred, color = as.factor(ID))) +
geom_line() +
geom_point(aes(y = dv)) +
guides(color = FALSE)
```