|
a |
|
b/R/outbreaker_move.R |
|
|
1 |
## #' Movements of augmented data and parameters for outbreaker2 |
|
|
2 |
## #' |
|
|
3 |
## #' This function moves all parameters for outbreaker2. |
|
|
4 |
## #' |
|
|
5 |
## #' @author Thibaut Jombart \email{thibautjombart@@gmail.com} |
|
|
6 |
## #' |
|
|
7 |
## #' |
|
|
8 |
## #' @param moves a list of movement functions as returned by \code{bind_moves} |
|
|
9 |
## #' (internal function) |
|
|
10 |
## #' |
|
|
11 |
## #' @param param a list of parameters as returned by |
|
|
12 |
## #' \code{create_param} |
|
|
13 |
## #' |
|
|
14 |
## #' @param densities a list containing lists of functions computing densities, |
|
|
15 |
## #' named: 'loglike' (log-likelihoods), 'priors' and 'posteriors' |
|
|
16 |
## #' |
|
|
17 |
## #' @return a potentially modified list of parameters as returned by |
|
|
18 |
## #' \code{create_param} |
|
|
19 |
## #' |
|
|
20 |
outbreaker_move <- function(moves, data, param_current, |
|
|
21 |
param_store, config, |
|
|
22 |
likelihoods, priors) { |
|
|
23 |
## get number of moves ## |
|
|
24 |
J <- length(moves) |
|
|
25 |
|
|
|
26 |
## Set up progress bar |
|
|
27 |
if(config$pb) { |
|
|
28 |
pb <- utils::txtProgressBar(min = 1, max = config$n_iter, style = 3) |
|
|
29 |
} |
|
|
30 |
|
|
|
31 |
## RUN MCMC ## |
|
|
32 |
for (i in seq.int(2, config$n_iter, 1)) { |
|
|
33 |
## move parameters / augmented data |
|
|
34 |
for (j in seq_len(J)) { |
|
|
35 |
## move parameters |
|
|
36 |
param_current <- moves[[j]](param_current) |
|
|
37 |
|
|
|
38 |
## safemode |
|
|
39 |
if (config$paranoid) { |
|
|
40 |
diagnostic <- look_for_trouble(param_current, param_store, data) |
|
|
41 |
if (!diagnostic$pass) { |
|
|
42 |
stop(paste0( |
|
|
43 |
"\n\n PARANOID MODE DETECTED AN ERROR WHILE FINDING IMPORTS:\n", |
|
|
44 |
sprintf("at iteration %d, movement %d (%s) with the following diagnostics:\n%s\n", |
|
|
45 |
i, j, names(moves)[j], diagnostic$msg))) |
|
|
46 |
} |
|
|
47 |
} |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
## store outputs if needed |
|
|
51 |
if ((i %% config$sample_every) == 0) { |
|
|
52 |
if(config$pb) { |
|
|
53 |
utils::setTxtProgressBar(pb, i) |
|
|
54 |
} |
|
|
55 |
param_store <- outbreaker_mcmc_store(param_current, param_store, data, |
|
|
56 |
config, likelihoods, priors, i) |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
} # end of the chain |
|
|
60 |
|
|
|
61 |
if(config$pb) { |
|
|
62 |
cat("\n") |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
## output is a list of saved chain states |
|
|
66 |
return(param_store) |
|
|
67 |
} |