Diff of /R/create_param.R [000000] .. [dfe06d]

Switch to unified view

a b/R/create_param.R
1
#' Initializes outputs for outbreaker
2
#'
3
#' This function creates initial outputs and parameter states for outbreaker.
4
#'
5
#' @author Thibaut Jombart (\email{thibautjombart@@gmail.com}).
6
#'
7
#' @param data A list of data items as returned by \code{outbreaker_data}, or
8
#' arguments passed to this function.
9
#'
10
#' @param config A list of settings as returned by \code{create_config}, or
11
#' arguments passed to this function.
12
#'
13
#' @export
14
#'
15
#' @aliases outbreaker_store
16
#'
17
#' @aliases outbreaker_param
18
#'
19
#' @return
20
#'
21
#' A list containing two components \code{$store} and
22
#' \code{$current}. \code{store} is a list with the class
23
#' \code{outbreaker_store}, used for storing 'saved' states of the
24
#' MCMC. \code{current} is a list with the class \code{outbreaker_param}, used
25
#' for storing 'current' states of the MCMC. \cr \cr
26
#'
27
#' \code{outbreaker_store} class content:
28
#' \itemize{
29
#'
30
#'  \item \code{size}: The length of the list, corresponding to the number of
31
#' samples saved from the MCMC.
32
#'
33
#'  \item \code{step}: A vector of integers of length \code{size}, storing the
34
#' steps of the MCMC corresponding to the saved samples.
35
#'
36
#'  \item \code{post}: A numeric vector of length \code{size}, storing
37
#' log-posterior values.
38
#'
39
#'  \item \code{like}: A numeric vector of length \code{size}, storing
40
#' log-likelihood values.
41
#'
42
#'  \item \code{prior}: A numeric vector of length \code{size},
43
#' storing log-prior values.
44
#'
45
#'  \item \code{alpha}: A list of length \code{size}. Each item of the list is
46
#' an integer vector of length \code{data$N}, storing indices (from 1 to N) of
47
#' infectors for each case.
48
#'
49
#'  \item \code{t_inf}: A list of length \code{size}. Each item of the list is
50
#' an integer vector of length \code{data$N}, storing dates of infections for
51
#' each case.
52
#'
53
#'  \item \code{mu}: A numeric vector of length \code{size}, storing values of
54
#' the mutation rate.
55
#'
56
#'  \item \code{kappa}: A list of length \code{size}. Each item of the list is
57
#' an integer vector of length \code{data$N}, storing the number of generations
58
#' before the last sampled ancestor for each case.
59
#'
60
#'  \item \code{pi}: A numeric vector of length \code{size}, storing values of
61
#' the reporting probability.
62
#'
63
#'  \item \code{eps}: A numeric vector of length \code{size}, storing values of
64
#' the contact reporting coverage.
65
#'
66
#'  \item \code{lambda}: A numeric vector of length \code{size}, storing values of
67
#' the non-infectious contact rate.
68
#'
69
#'  \item \code{counter}: A counter used to keep track of the current iteration
70
#' of the MCMC (used internally).
71
#'
72
#' }
73
#'
74
#'
75
#' \code{outbreaker_param} class content:
76
#' \itemize{
77
#'
78
#'  \item \code{alpha}: An integer vector of length \code{data$N}, storing
79
#' indices (from 1 to N) of infectors for each case.
80
#'
81
#'  \item \code{t_inf}: An integer vector of length \code{data$N}, storing dates
82
#' of infections for each case.
83
#'
84
#'  \item \code{mu}: The value of the mutation rate.
85
#'
86
#'  \item \code{kappa}: An integer vector of length \code{data$N}, storing the
87
#' number of generations before the last sampled ancestor for each case.
88
#'
89
#'  \item \code{pi}: The value of the reporting probability.
90
#'
91
#'  \item \code{eps}: The value of the contact reporting coverage.
92
#'
93
#'  \item \code{lambda}: The value of the non-infectious contact rate.
94
#'
95
#' }
96
#'
97
#' @examples
98
#'
99
#' ## load data
100
#' x <- fake_outbreak
101
#' data <- outbreaker_data(dates = x$sample, dna = x$dna, w_dens = x$w)
102
#'
103
#' ## modify config settings
104
#' config <- create_config(move_alpha = FALSE, n_iter = 2e5, sample_every = 1000)
105
#'
106
#' ## create param object
107
#' param <- create_param(data = data, config = config)
108
#'
109
create_param <- function(data = outbreaker_data(),
110
                         config = create_config()) {
111
    ## CREATE EMPTY OUTPUT VECTORS ##
112
    size <- round(config$n_iter/config$sample_every)
113
    step <- integer(size)
114
    post <- prior <- like <- mu <- pi <- eps <- lambda <- double(size)
115
    alpha <- as.list(integer(size))
116
    t_inf <- as.list(integer(size))
117
    kappa <- as.list(integer(size))
118
119
    ## SET CURRENT VALUES AND COUNTER ##
120
    step[1] <- 1L
121
    current_mu <- mu[1] <- config$init_mu
122
    current_alpha <- alpha[[1]] <- config$init_alpha
123
    current_kappa <- kappa[[1]] <- config$init_kappa
124
    current_pi <- pi[1] <- config$init_pi
125
    current_eps <- eps[1] <- config$init_eps
126
    current_lambda <- lambda[1] <- config$init_lambda
127
    if (is.null(config$init_t_inf)) {
128
        current_t_inf <- t_inf[[1]] <- data$dates - which.max(data$f_dens) + 1L
129
    } else {
130
        current_t_inf <- t_inf[[1]] <- config$init_t_inf
131
    }
132
    counter <- 1L
133
134
135
    store <- list(
136
                size = size, step = step,
137
                post = post, like = like, prior = prior,
138
                alpha = alpha, t_inf = t_inf, mu = mu, kappa = kappa, pi = pi,
139
                eps = eps, lambda = lambda,
140
                counter = counter
141
                )
142
    class(store) <- c("outbreaker_store", "list")
143
144
145
    current  <- list(
146
        alpha = current_alpha, t_inf = current_t_inf, mu = current_mu,
147
        kappa = current_kappa, pi = current_pi, eps = current_eps,
148
        lambda = current_lambda
149
    )
150
    class(current) <- c("outbreaker_param", "list")
151
152
153
154
    ## SHAPE CHAIN ##
155
    out <- list(store = store,
156
                current = current)
157
    return(out)
158
}
159