Skip to content

Commit bf401cf

Browse files
committed
2 parents b6ac05a + f0341fe commit bf401cf

File tree

7 files changed

+1362
-0
lines changed

7 files changed

+1362
-0
lines changed

knitr/disease_transmission/boarding_school_case_study.Rmd

Lines changed: 1179 additions & 0 deletions
Large diffs are not rendered by default.
11.9 KB
Loading
246 KB
Loading
7.36 KB
Loading
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
functions {
2+
//theta[1] = beta
3+
//theta[2] = gamma
4+
//x_i[1] = N, total population
5+
// y = S, I, R
6+
real[] sir(real t,
7+
real[] y,
8+
real[] theta,
9+
real[] x_r,
10+
int[] x_i) {
11+
real dydt[3];
12+
dydt[1] = - theta[1] * y[1] * y[2] / x_i[1];
13+
dydt[2] = theta[1] * y[1] * y[2] / x_i[1] - theta[2] * y[2];
14+
dydt[3] = theta[2] * y[2];
15+
return dydt;
16+
17+
}
18+
}
19+
20+
data {
21+
int<lower=1> T;
22+
real y0[3];
23+
real t0;
24+
real ts[T];
25+
int N;
26+
int cases[T];
27+
}
28+
29+
transformed data {
30+
real x_r[0];
31+
int x_i[1];
32+
x_i[1]=N;
33+
}
34+
35+
parameters {
36+
real<lower=0> gamma;
37+
real<lower=0> beta;
38+
real<lower=0> phi_inv;
39+
}
40+
41+
transformed parameters{
42+
real y[T,3];
43+
real phi = 1. / phi_inv;
44+
{
45+
real theta[2];
46+
theta[1] = beta;
47+
theta[2] = gamma;
48+
49+
y = integrate_ode_rk45(sir, y0, t0, ts, theta, x_r, x_i);
50+
}
51+
}
52+
53+
54+
model {
55+
//priors
56+
beta ~ normal(2, 1);
57+
gamma ~ normal(0.4, 0.5);
58+
phi_inv ~ exponential(5);
59+
60+
//sampling distribution
61+
//col(matrix x, int n) - The n-th column of matrix x. Here the number of infected people
62+
cases ~ neg_binomial_2(col(to_matrix(y),2), phi);
63+
}
64+
65+
generated quantities {
66+
real R0 = beta/gamma;
67+
real recovery_time = 1/gamma;
68+
real pred_cases[T];
69+
pred_cases = neg_binomial_2_rng(col(to_matrix(y),2), phi);
70+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
functions {
2+
//theta[1] = beta
3+
//theta[2] = gamma
4+
//x_i[1] = N, total population
5+
// y = S, I, R
6+
real[] sir(real t,
7+
real[] y,
8+
real[] theta,
9+
real[] x_r,
10+
int[] x_i) {
11+
real dydt[3];
12+
dydt[1] = - theta[1] * y[1] * y[2] / x_i[1];
13+
dydt[2] = theta[1] * y[1] * y[2] / x_i[1] - theta[2] * y[2];
14+
dydt[3] = theta[2] * y[2];
15+
return dydt;
16+
17+
}
18+
}
19+
20+
data {
21+
int<lower=1> T;
22+
real y0[3];
23+
real t0;
24+
real ts[T];
25+
int N;
26+
int cases[T];
27+
}
28+
29+
transformed data {
30+
real x_r[0];
31+
int x_i[1];
32+
x_i[1]=N;
33+
}
34+
35+
parameters {
36+
real<lower=0> gamma;
37+
real<lower=0> beta;
38+
real<lower=0> phi_inv;
39+
}
40+
41+
transformed parameters{
42+
real y[T,3];
43+
real phi = 1. / phi_inv;
44+
{
45+
real theta[2];
46+
theta[1] = beta;
47+
theta[2] = gamma;
48+
49+
y = integrate_ode_rk45(sir, y0, t0, ts, theta, x_r, x_i);
50+
}
51+
}
52+
53+
54+
model {
55+
beta ~ normal(2, 1);
56+
gamma ~ normal(0.4, 0.5);
57+
58+
//col(matrix x, int n) - The n-th column of matrix x
59+
phi_inv ~ exponential(5);
60+
61+
}
62+
63+
generated quantities {
64+
real R0 = beta/gamma;
65+
real recovery_time = 1/gamma;
66+
real pred_cases[T];
67+
pred_cases = neg_binomial_2_rng(col(to_matrix(y),2) + 1e-5, phi);
68+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
functions {
2+
//theta[1] = beta
3+
//theta[2] = gamma
4+
//x_i[1] = N, total population
5+
// y = S, I, R
6+
real[] sir(real t,
7+
real[] y,
8+
real[] theta,
9+
real[] x_r,
10+
int[] x_i) {
11+
real dydt[3];
12+
dydt[1] = - theta[1] * y[1] * y[2] / x_i[1];
13+
dydt[2] = theta[1] * y[1] * y[2] / x_i[1] - theta[2] * y[2];
14+
dydt[3] = theta[2] * y[2];
15+
return dydt;
16+
17+
}
18+
}
19+
20+
data {
21+
int<lower=1> T;
22+
real y0[3];
23+
real t0;
24+
real ts[T];
25+
int N;
26+
real<lower=0> beta;
27+
real<lower=0> gamma;
28+
real<lower=0> phi;
29+
}
30+
31+
transformed data {
32+
real x_r[0];
33+
int x_i[1];
34+
x_i[1]=N;
35+
}
36+
37+
generated quantities {
38+
real theta[2];
39+
real pred_cases[T];
40+
real y[T,3];
41+
theta[1] = beta;
42+
theta[2] = gamma;
43+
y = integrate_ode_rk45(sir, y0, t0, ts, theta, x_r, x_i);
44+
pred_cases = neg_binomial_2_rng(col(to_matrix(y),2) + 1e-9, phi);
45+
}

0 commit comments

Comments
 (0)