I developed a SIR model (1) and solved it using an Euler method, then generated fake data as a sequence of noisy observations of daily incidence. Stan (2) is used for Bayesian parameter estimation. I could have used the ODE solving routine available in Stan as in my previous post. However, an SIR model solved via the Euler method can be extended mor easily (e.g., stochatic model). I also suspect it would be easier to combine with the other statistical modeling techinques (e.g., hierarchical model), which I am going to post later.
Simulate the data
Generate \(y_{1:N}\) as a sequence of noisy observations of a daily incidence. Almost the same Stan model is used twice: once to create fake data and the second time to estimate parameters via HMC.
Stan model to create fake data
Code
stan_code_data <-"data { int<lower=0> N; // length of the data int<lower=0> iter_per_day; real dt; real<lower=0> S0; real<lower=0> I0; real<lower=0> R0; real<lower=0> CI0; real<lower=0> phi; real<lower=0> gamma; real<lower=0> beta;}parameters {}transformed parameters { vector<lower=0>[N] daily_inf; vector<lower=0>[N+1] S; vector<lower=0>[N+1] I; vector<lower=0>[N+1] R; vector<lower=0>[N+1] CI; real<lower=0> st; // susceptible at time t real<lower=0> it; real<lower=0> rt; real<lower=0> cit; // cumulative infections at time t real<lower=0> n; // total population size real<lower=0> n_si; // number moving from S to I real<lower=0> n_ir; // number moving from I to R S[1] = S0; I[1] = I0; R[1] = R0; CI[1] = CI0; for (i in 2:(N+1)) { st = S[i-1]; it = I[i-1]; rt = R[i-1]; cit = CI[i-1]; for (j in 1:iter_per_day) { n = st + it + rt; n_si = dt * beta * st * it / n; n_ir = dt * gamma * it; st = st - n_si; it = it + n_si - n_ir; rt = rt + n_ir; cit = cit + n_si; } S[i] = st; I[i] = it; R[i] = rt; CI[i] = cit; } for (i in 2:(N+1)) { daily_inf[i-1] = CI[i] - CI[i-1]; }}model {}generated quantities { array[N] int y_sim; for (i in 1:N) { y_sim[i] = neg_binomial_2_rng(daily_inf[i] + 1e-6, phi); //y_sim[i] = poisson_rng(daily_inf[i] + 1e-6); }}"
Hethcote HW. The mathematics of infectious diseases. SIAM Review. 2000;42(4):599–653. doi:10.1137/S0036144500371907
2.
Carpenter B, Gelman A, Hoffman MD, Lee D, Goodrich B, Betancourt M, et al. Stan: A probabilistic programming language. Journal of Statistical Software. 2017;76(1):1–32. doi:10.18637/jss.v076.i01