Skip to content

Commit 7d6ce71

Browse files
authored
Merge pull request #185 from stan-dev/convert-odes
Added model code and data to convert-odes case study (design-doc #19)
2 parents 14d8219 + f9ea896 commit 7d6ce71

File tree

10 files changed

+1309
-11
lines changed

10 files changed

+1309
-11
lines changed

knitr/convert-odes/convert_odes.Rmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Upgrading to the new ODE interface"
33
author: "Ben Bales & Sebastian Weber"
4-
date: "26 July 2020"
4+
date: "27 July 2020"
55
output: html_document
66
---
77

@@ -294,10 +294,10 @@ transformed parameters {
294294
### Full Model
295295

296296
The full model with the new interface is
297-
[here](https://github.com/stan-dev/stat_comp_benchmarks/blob/master/benchmarks/sir/sir.stan) and the data is [here](https://github.com/stan-dev/stat_comp_benchmarks/blob/master/benchmarks/sir/sir.data.R).
297+
[here](https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/sir.stan) and the data is [here](https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/sir.data.R).
298298

299299
The full model with the old interface is
300-
[here](https://github.com/stan-dev/stat_comp_benchmarks/blob/64b6597cd17de77e91dcfb1a9977758407017d20/benchmarks/sir/sir.stan), and the data is [here](https://github.com/stan-dev/stat_comp_benchmarks/blob/64b6597cd17de77e91dcfb1a9977758407017d20/benchmarks/sir/sir.data.R).
300+
[here](https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/sir.old.stan) and the data is [here](https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/sir.old.data.R).
301301

302302
## PKPD Model
303303

@@ -401,8 +401,8 @@ transformed parameters {
401401
### Full Model
402402

403403
The full model with the new interface is
404-
[here](https://github.com/stan-dev/stat_comp_benchmarks/blob/master/benchmarks/one_comp_mm_elim_abs/one_comp_mm_elim_abs.stan) and the data is [here](https://github.com/stan-dev/stat_comp_benchmarks/blob/master/benchmarks/one_comp_mm_elim_abs/one_comp_mm_elim_abs.data.R).
404+
[here](https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/one_comp_mm_elim_abs.stan) and the data is [here](https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/one_comp_mm_elim_abs.data.R).
405405

406406
The full model with the old interface is
407-
[here](https://github.com/stan-dev/stat_comp_benchmarks/blob/64b6597cd17de77e91dcfb1a9977758407017d20/benchmarks/pkpd/one_comp_mm_elim_abs.stan) and the data is [here](https://github.com/stan-dev/stat_comp_benchmarks/blob/64b6597cd17de77e91dcfb1a9977758407017d20/benchmarks/pkpd/one_comp_mm_elim_abs.data.R).
407+
[here](https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/one_comp_mm_elim_abs.old.stan) and the data is [here](https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/one_comp_mm_elim_abs.old.data.R).
408408

knitr/convert-odes/convert_odes.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<meta name="author" content="Ben Bales &amp; Sebastian Weber" />
1313

14-
<meta name="date" content="2020-07-26" />
14+
<meta name="date" content="2020-07-27" />
1515

1616
<title>Upgrading to the new ODE interface</title>
1717

@@ -365,7 +365,7 @@
365365

366366
<h1 class="title toc-ignore">Upgrading to the new ODE interface</h1>
367367
<h4 class="author">Ben Bales &amp; Sebastian Weber</h4>
368-
<h4 class="date">26 July 2020</h4>
368+
<h4 class="date">27 July 2020</h4>
369369

370370
</div>
371371

@@ -518,8 +518,8 @@ <h3>Calling the ODE Solver</h3>
518518
</div>
519519
<div id="full-model" class="section level3">
520520
<h3>Full Model</h3>
521-
<p>The full model with the new interface is <a href="https://github.com/stan-dev/stat_comp_benchmarks/blob/master/benchmarks/sir/sir.stan">here</a> and the data is <a href="https://github.com/stan-dev/stat_comp_benchmarks/blob/master/benchmarks/sir/sir.data.R">here</a>.</p>
522-
<p>The full model with the old interface is <a href="https://github.com/stan-dev/stat_comp_benchmarks/blob/64b6597cd17de77e91dcfb1a9977758407017d20/benchmarks/sir/sir.stan">here</a>, and the data is <a href="https://github.com/stan-dev/stat_comp_benchmarks/blob/64b6597cd17de77e91dcfb1a9977758407017d20/benchmarks/sir/sir.data.R">here</a>.</p>
521+
<p>The full model with the new interface is <a href="https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/sir.stan">here</a> and the data is <a href="https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/sir.data.R">here</a>.</p>
522+
<p>The full model with the old interface is <a href="https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/sir.old.stan">here</a> and the data is <a href="https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/sir.old.data.R">here</a>.</p>
523523
</div>
524524
</div>
525525
<div id="pkpd-model" class="section level2">
@@ -602,8 +602,8 @@ <h3>Calling the ODE Solver</h3>
602602
</div>
603603
<div id="full-model-1" class="section level3">
604604
<h3>Full Model</h3>
605-
<p>The full model with the new interface is <a href="https://github.com/stan-dev/stat_comp_benchmarks/blob/master/benchmarks/one_comp_mm_elim_abs/one_comp_mm_elim_abs.stan">here</a> and the data is <a href="https://github.com/stan-dev/stat_comp_benchmarks/blob/master/benchmarks/one_comp_mm_elim_abs/one_comp_mm_elim_abs.data.R">here</a>.</p>
606-
<p>The full model with the old interface is <a href="https://github.com/stan-dev/stat_comp_benchmarks/blob/64b6597cd17de77e91dcfb1a9977758407017d20/benchmarks/pkpd/one_comp_mm_elim_abs.stan">here</a> and the data is <a href="https://github.com/stan-dev/stat_comp_benchmarks/blob/64b6597cd17de77e91dcfb1a9977758407017d20/benchmarks/pkpd/one_comp_mm_elim_abs.data.R">here</a>.</p>
605+
<p>The full model with the new interface is <a href="https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/one_comp_mm_elim_abs.stan">here</a> and the data is <a href="https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/one_comp_mm_elim_abs.data.R">here</a>.</p>
606+
<p>The full model with the old interface is <a href="https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/one_comp_mm_elim_abs.old.stan">here</a> and the data is <a href="https://github.com/stan-dev/example-models/blob/master/knitr/convert-odes/one_comp_mm_elim_abs.old.data.R">here</a>.</p>
607607
</div>
608608
</div>
609609
</div>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
t0 <- 0
2+
C0 <-
3+
structure(c(0),
4+
.Dim = c(1))
5+
D <- 30
6+
V <- 2
7+
times <-
8+
c(0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6,
9+
6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10)
10+
N_t <- 20
11+
C <-
12+
c(5.70812264865215, 7.10126075072086,
13+
8.38520678426651, 9.79008249883381, 13.4390409239245,
14+
11.4478987597702, 11.2124282696837, 11.4269217682577,
15+
12.2432859438401, 13.8201804108938, 13.8408670746042,
16+
11.422291744251, 10.5031943081843, 11.9121452242965,
17+
14.0849980781312, 10.5505145523917, 10.1905539351877,
18+
12.2232272590821, 11.7290653047821, 12.2719396535996)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
t0 <- 0
2+
C0 <-
3+
structure(c(0),
4+
.Dim = c(1))
5+
D <- 30
6+
V <- 2
7+
times <-
8+
c(0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6,
9+
6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10)
10+
N_t <- 20
11+
C_hat <-
12+
c(5.70812264865215, 7.10126075072086,
13+
8.38520678426651, 9.79008249883381, 13.4390409239245,
14+
11.4478987597702, 11.2124282696837, 11.4269217682577,
15+
12.2432859438401, 13.8201804108938, 13.8408670746042,
16+
11.422291744251, 10.5031943081843, 11.9121452242965,
17+
14.0849980781312, 10.5505145523917, 10.1905539351877,
18+
12.2232272590821, 11.7290653047821, 12.2719396535996)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
functions {
2+
real[] one_comp_mm_elim_abs(real t,
3+
real[] y,
4+
real[] theta,
5+
real[] x_r,
6+
int[] x_i) {
7+
real dydt[1];
8+
real k_a = theta[1]; // Dosing rate in 1/day
9+
real K_m = theta[2]; // Michaelis-Menten constant in mg/L
10+
real V_m = theta[3]; // Maximum elimination rate in 1/day
11+
real D = x_r[1];
12+
real V = x_r[2];
13+
real dose = 0;
14+
real elim = (V_m / V) * y[1] / (K_m + y[1]);
15+
16+
if (t > 0)
17+
dose = exp(- k_a * t) * D * k_a / V;
18+
19+
dydt[1] = dose - elim;
20+
21+
return dydt;
22+
}
23+
}
24+
25+
data {
26+
real t0; // Initial time in days;
27+
real C0[1]; // Initial concentration at t0 in mg/L
28+
29+
real D; // Total dosage in mg
30+
real V; // Compartment volume in L
31+
32+
int<lower=1> N_t;
33+
real times[N_t]; // Measurement times in days
34+
35+
// Measured concentrations in effect compartment in mg/L
36+
real C_hat[N_t];
37+
}
38+
39+
transformed data {
40+
real x_r[2] = {D, V};
41+
int x_i[0];
42+
}
43+
44+
parameters {
45+
real<lower=0> k_a; // Dosing rate in 1/day
46+
real<lower=0> K_m; // Michaelis-Menten constant in mg/L
47+
real<lower=0> V_m; // Maximum elimination rate in 1/day
48+
real<lower=0> sigma;
49+
}
50+
51+
transformed parameters {
52+
real C[N_t, 1];
53+
{
54+
real theta[3] = {k_a, K_m, V_m};
55+
C = integrate_ode_bdf(one_comp_mm_elim_abs, C0, t0, times, theta, x_r, x_i);
56+
}
57+
}
58+
59+
model {
60+
// Priors
61+
k_a ~ cauchy(0, 1);
62+
K_m ~ cauchy(0, 1);
63+
V_m ~ cauchy(0, 1);
64+
sigma ~ cauchy(0, 1);
65+
66+
// Likelihood
67+
for (n in 1:N_t)
68+
C_hat[n] ~ lognormal(log(C[n, 1]), sigma);
69+
}
70+
71+
generated quantities {
72+
real C_ppc[N_t];
73+
for (n in 1:N_t)
74+
C_ppc[n] = lognormal_rng(log(C[n, 1]), sigma);
75+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
functions {
2+
vector one_comp_mm_elim_abs(real t,
3+
vector y,
4+
real k_a, // Dosing rate in 1/day
5+
real K_m, // Michaelis-Menten constant in mg/L
6+
real V_m, // Maximum elimination rate in 1/day
7+
real D,
8+
real V) {
9+
vector[1] dydt;
10+
11+
real dose = 0;
12+
real elim = (V_m / V) * y[1] / (K_m + y[1]);
13+
14+
if (t > 0)
15+
dose = exp(- k_a * t) * D * k_a / V;
16+
17+
dydt[1] = dose - elim;
18+
19+
return dydt;
20+
}
21+
}
22+
23+
data {
24+
real t0; // Initial time in days;
25+
vector[1] C0; // Initial concentration at t0 in mg/L
26+
27+
real D; // Total dosage in mg
28+
real V; // Compartment volume in L
29+
30+
int<lower=1> N_t;
31+
real times[N_t]; // Measurement times in days
32+
33+
// Measured concentrations in effect compartment in mg/L
34+
real C[N_t];
35+
}
36+
37+
parameters {
38+
real<lower=0> k_a; // Dosing rate in 1/day
39+
real<lower=0> K_m; // Michaelis-Menten constant in mg/L
40+
real<lower=0> V_m; // Maximum elimination rate in 1/day
41+
real<lower=0> sigma;
42+
}
43+
44+
transformed parameters {
45+
vector[1] mu_C[N_t] = ode_bdf_tol(one_comp_mm_elim_abs, C0, t0, times,
46+
1e-8, 1e-8, 1000,
47+
k_a, K_m, V_m, D, V);
48+
}
49+
50+
model {
51+
// Priors
52+
k_a ~ cauchy(0, 1);
53+
K_m ~ cauchy(0, 1);
54+
V_m ~ cauchy(0, 1);
55+
sigma ~ cauchy(0, 1);
56+
57+
// Likelihood
58+
C ~ lognormal(log(mu_C[, 1]), sigma);
59+
}
60+
61+
generated quantities {
62+
real C_ppc[N_t];
63+
for (n in 1:N_t)
64+
C_ppc[n] = lognormal_rng(log(mu_C[n, 1]), sigma);
65+
}

knitr/convert-odes/sir.data.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
N_t <- 20
2+
3+
t <- c(7,14,21,28,35,42,49,56,63,70,77,84,91,98,105,112,119,126,133,140)
4+
5+
y0 <- c(10000,0,0,10000)
6+
7+
stoi <- c(441,419,553,666,753,692,644,509,371,286,200,147,101,68,49,35,19,12,7,10)
8+
9+
B <- c(6141.73,5801.72,9439.14,13742,11975,16387,15144,14705.6,8736.39,7553.29,6007.09,5979.96,3048.17,1566.77,1275.21,824.85,604.605,516.25,258.046,188.591)

0 commit comments

Comments
 (0)