|
a |
|
b/R/outbreaker.R |
|
|
1 |
#' outbreaker2: main function for reconstructing disease outbreaks |
|
|
2 |
#' |
|
|
3 |
#' The function \code{outbreaker} is the main function of the package. It runs |
|
|
4 |
#' processes various inputs (data, configuration settings, custom priors, |
|
|
5 |
#' likelihoods and movement functions) and explores the space of plausible |
|
|
6 |
#' transmission trees of a densely sampled outbreaks.\cr |
|
|
7 |
#' |
|
|
8 |
#' The emphasis of 'outbreaker2' is on modularity, which enables customisation |
|
|
9 |
#' of priors, likelihoods and even movements of parameters and augmented data by |
|
|
10 |
#' the user. This the dedicated vignette on this topic |
|
|
11 |
#' \code{vignette("outbreaker2_custom")}.\cr |
|
|
12 |
#' |
|
|
13 |
#' |
|
|
14 |
#' |
|
|
15 |
#' @export |
|
|
16 |
#' |
|
|
17 |
#' @aliases outbreaker |
|
|
18 |
#' |
|
|
19 |
#' @rdname outbreaker |
|
|
20 |
#' |
|
|
21 |
#' @author Thibaut Jombart (\email{thibautjombart@@gmail.com}). |
|
|
22 |
#' |
|
|
23 |
#' @references Jombart T, Cori A, Didelot X, Cauchemez S, Fraser C and Ferguson |
|
|
24 |
#' N (2014). Bayesian reconstruction of disease outbreaks by combining |
|
|
25 |
#' epidemiologic and genomic data. PLoS Computational Biology. |
|
|
26 |
#' |
|
|
27 |
#' @seealso \code{\link{outbreaker_data}} to process input data, and |
|
|
28 |
#' \code{\link{create_config}} to process/set up parameters |
|
|
29 |
#' |
|
|
30 |
#' @param data a list of named items containing input data as returned by |
|
|
31 |
#' \code{\link{outbreaker_data}}. |
|
|
32 |
#' |
|
|
33 |
#' @param config a set of settings as returned by \code{\link{create_config}}. |
|
|
34 |
#' |
|
|
35 |
#' @param likelihoods a set of log-likelihood functions as returned by |
|
|
36 |
#' \code{\link{custom_likelihoods}}. |
|
|
37 |
#' |
|
|
38 |
#' @param priors a set of log-prior functions as returned by |
|
|
39 |
#' \code{\link{custom_priors}}. |
|
|
40 |
#' |
|
|
41 |
#' @param moves a set of movement functions as returned by |
|
|
42 |
#' \code{\link{custom_moves}}. |
|
|
43 |
|
|
|
44 |
#' @seealso |
|
|
45 |
#' |
|
|
46 |
#' \itemize{ |
|
|
47 |
#' |
|
|
48 |
#' \item \code{\link{outbreaker_data}}: function to process input data. |
|
|
49 |
#' |
|
|
50 |
#' \item \code{\link{create_config}}: function to create default and customise |
|
|
51 |
#' configuration settings. |
|
|
52 |
#' |
|
|
53 |
#' \item \code{\link{custom_priors}}: function to specify customised prior |
|
|
54 |
#' functions. |
|
|
55 |
#' |
|
|
56 |
#' \item \code{\link{custom_likelihoods}}: function to specify customised likelihoods |
|
|
57 |
#' functions. |
|
|
58 |
#' |
|
|
59 |
#' \item \code{\link{custom_moves}}: function to create default and customise movement |
|
|
60 |
#' functions. |
|
|
61 |
#' |
|
|
62 |
#' } |
|
|
63 |
#' |
|
|
64 |
#' @examples |
|
|
65 |
#' |
|
|
66 |
#' ## get data |
|
|
67 |
#' data(fake_outbreak) |
|
|
68 |
#' dat <- fake_outbreak |
|
|
69 |
#' |
|
|
70 |
#' \dontrun{ |
|
|
71 |
#' ## run outbreaker |
|
|
72 |
#' out <- outbreaker(data = list(dna = dat$dna, dates = dat$onset, w_dens = dat$w), |
|
|
73 |
#' config = list(n_iter = 2e4, sample_every = 200)) |
|
|
74 |
#' plot(out) |
|
|
75 |
#' as.data.frame(out) |
|
|
76 |
#' |
|
|
77 |
#' ## run outbreaker, no DNA sequences |
|
|
78 |
#' out2 <- outbreaker(data = list(dates = dat$onset, w_dens = w), |
|
|
79 |
#' config = list(n_iter = 2e4, sample_every = 200)) |
|
|
80 |
#' plot(out2) |
|
|
81 |
#' as.data.frame(out2) |
|
|
82 |
#' |
|
|
83 |
#' } |
|
|
84 |
outbreaker <- function(data = outbreaker_data(), |
|
|
85 |
config = create_config(), |
|
|
86 |
priors = custom_priors(), |
|
|
87 |
likelihoods = custom_likelihoods(), |
|
|
88 |
moves = custom_moves() |
|
|
89 |
) { |
|
|
90 |
|
|
|
91 |
## CHECKS / PROCESS DATA ## |
|
|
92 |
data <- outbreaker_data(data = data) |
|
|
93 |
|
|
|
94 |
## CHECK / PROCESS CONFIG ## |
|
|
95 |
config <- create_config(config, data = data) |
|
|
96 |
|
|
|
97 |
## ADD CONVOLUTIONS TO DATA ## |
|
|
98 |
data <- add_convolutions(data = data, config = config) |
|
|
99 |
|
|
|
100 |
## PROCESS CUSTOM FUNCTIONS FOR PRIORS AND LIKELIHOOD ## |
|
|
101 |
priors <- custom_priors(priors) |
|
|
102 |
loglike <- custom_likelihoods(likelihoods) |
|
|
103 |
|
|
|
104 |
|
|
|
105 |
## CREATE AND INITIALIZE MCMC CHAIN ## |
|
|
106 |
temp <- create_param(data = data, config = config) |
|
|
107 |
param_store <- temp$store |
|
|
108 |
param_current <- temp$current |
|
|
109 |
param_store <- outbreaker_init_mcmc(data, param_current, param_store, |
|
|
110 |
loglike, priors, config) |
|
|
111 |
|
|
|
112 |
|
|
|
113 |
## here we create a list of function for moving parameters |
|
|
114 |
moves <- bind_moves(moves = moves, |
|
|
115 |
config = config, |
|
|
116 |
data = data, |
|
|
117 |
likelihoods = loglike, |
|
|
118 |
priors = priors) |
|
|
119 |
|
|
|
120 |
|
|
|
121 |
## IMPORTS |
|
|
122 |
|
|
|
123 |
## preliminary run to detect imported cases this relies on a shorter run of |
|
|
124 |
## the MCMC, then computing the average 'global influence' (-loglike) of |
|
|
125 |
## each data point, identifying outliers (based on fixed threshold) and |
|
|
126 |
## marking outliers down as 'imported cases'. |
|
|
127 |
|
|
|
128 |
temp <- outbreaker_find_imports(moves = moves, |
|
|
129 |
data = data, |
|
|
130 |
param_current = param_current, |
|
|
131 |
param_store = param_store, |
|
|
132 |
config = config, |
|
|
133 |
likelihoods = loglike) |
|
|
134 |
param_current <- temp$param_current |
|
|
135 |
param_store <- temp$param_store |
|
|
136 |
config <- temp$config |
|
|
137 |
|
|
|
138 |
|
|
|
139 |
## PERFORM MCMC |
|
|
140 |
|
|
|
141 |
## procedure is the same as before, with some cases fixed as 'imported' |
|
|
142 |
|
|
|
143 |
param_store <- outbreaker_move(moves = moves, |
|
|
144 |
data = data, |
|
|
145 |
param_current = param_current, |
|
|
146 |
param_store = param_store, |
|
|
147 |
config = config, |
|
|
148 |
likelihoods = loglike, |
|
|
149 |
priors = priors) |
|
|
150 |
|
|
|
151 |
|
|
|
152 |
## SHAPE RESULTS |
|
|
153 |
|
|
|
154 |
## this takes the chains generated by 'outbreaker_move', stored as a list, |
|
|
155 |
## and puts everything back together as a single data.frame; augmented data |
|
|
156 |
## stored as vectors (e_g. 'alpha') become numbered columns of the |
|
|
157 |
## data.frame (e_g. 'alpha_1', 'alpha_2' etc.) |
|
|
158 |
|
|
|
159 |
out <- outbreaker_mcmc_shape(param_store, data) |
|
|
160 |
|
|
|
161 |
return(out) |
|
|
162 |
} |
|
|
163 |
|