감염병 수리 모형을 개발하는 데 있어 가장 근본적인 질문 중 하나는 주어진 관찰값 (시계열)하에서 어떤 모형을 선택하고 그 모수의 값을 어떻게 결정하는가이다. 모형을 선택하는 과정은 따로 다루기로 하고 여기서는 일반적으로 사용되는 감염병 수리 모형 (i.e., SIR)을 사용할 때 모수를 추정하는 과정에 대해서 이야기해보자. 최대 가능도 (maximum likelihood) 방법에 대해서는 전에 언급하였다. 모수를 추정하는 여러 방법 중에 마르코프 연쇄 몬테카를로 (Markov Chain Monte Carlo; MCMC) 방법이 적절한 모수의 값을 찾아내고 그 값의 불확실성 (uncertainty)를 나타내는 데 가장 널리 쓰이는 방법 중의 하나이다. MCMC 알고리듬을 직접 작성해서 사용한는 것도 원리를 이해하는 데에는 도움이 되지만 이미 다양한 통계 패키지에서 MCMC가 사용되고 있으므로 기존 패키지를 사용하는 것도 합리적인 방법이 될 수 있다. 회귀 분석 등 통계모형의 경우BUGS (Bayesian Inference Using Gibbs Sampling) 혹은 JAGS (Just Another Gibbs Sampler), Stan, 그리고 NIMBLE 등에 구현된 MCMC를 사용하는 것이 많이 보편화 되어 있다.
이 중 NIMBLE 은 R 패키지 nimble을 이용해서 사용할 수 있고 패키지에서 제공하는 함수 기능을 이용해서 감염병 수리 모형을 구현하고 MCMC 까지 할 수 있다 (1). syntax 또한 R과 유사해서 R를 사용하는 사람에게는 Stan (2) 보다 더 접근이 용이한 것 같다. 아래에는 nimble 함수 기능을 이용하여 Euler 방법에 기반한 SIR 모형을 구현한 예이다.
Code
library(nimble)sir_incidence <-nimbleFunction(run =function(beta =double(0)) { tend <-100# 100 days of simulation dt <-0.1# time step of 0.1 day# initial condition St <-999 It <-1 Rt <-0 CIt <-0# create vectors for the state variables S <-rep(0, tend) I <-rep(0, tend) R <-rep(0, tend) CI <-rep(0, tend) # cumulative incidence# first elements of the vectors are initial conditions S[1] <- St I[1] <- It R[1] <- Rt CI[1] <- CIt gamma <-0.2# 1/gamma = duration of infectiousnessfor (i in2:tend) { # each dayfor (j in1:ceiling(1/dt)) { # time steps per day Nt <- St + It + Rt # total population size rate_StoI <- St * beta * It / Nt * dt # transition rate from S to I rate_ItoR <- gamma * It * dt # transition rate from I to R dS <-- rate_StoI # rate of change for S dI <- rate_StoI - rate_ItoR # rate of change for I dR <- rate_ItoR # rate of change for R dCI <- rate_StoI # rate of change for cumulative incidence St <- St + dS # update the St It <- It + dI # update the It Rt <- Rt + dR # update the Rt CIt <- CIt + dCI # update the CIt } S[i] <- St # put St in the vector I[i] <- It # put It in the vector R[i] <- Rt # put Rt in the vector CI[i] <- CIt # put CIt in the vector }# daily incidence from cumulative incidence inc <- CI[2:tend] - CI[1:(tend-1)] return(inc) returnType(double(1)) # return type })
Valpine P de, Turek D, Paciorek CJ, Anderson-Bergman C, Lang DT, Bodik R. Programming with models: Writing statistical algorithms for general model structures with NIMBLE. Journal of Computational and Graphical Statistics. 2017;26(2):403–13. doi:10.1080/10618600.2016.1172487
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;80(1):1–32. doi:10.18637/jss.v080.i01