|
| 1 | +#' Likelihood-Free Markhov Chain Monte Carlo (LFMCMC) |
| 2 | +#' |
| 3 | +#' |
| 4 | +#' @aliases epiworld_lfmcmc |
| 5 | +#' @details |
| 6 | +#' Performs a Likelihood-Free Markhov Chain Monte Carlo simulation |
| 7 | +#' @param model A model of class [epiworld_model] |
| 8 | +#' @returns |
| 9 | +#' The `LFMCMC` function returns a model of class [epiworld_lfmcmc]. |
| 10 | +#' @examples |
| 11 | +#' ## Setup an SIR model to use in the simulation |
| 12 | +#' model_seed <- 122 |
| 13 | +#' model_sir <- ModelSIR(name = "COVID-19", prevalence = .1, |
| 14 | +#' transmission_rate = .9, recovery_rate = .3) |
| 15 | +#' agents_smallworld( |
| 16 | +#' model_sir, |
| 17 | +#' n = 1000, |
| 18 | +#' k = 5, |
| 19 | +#' d = FALSE, |
| 20 | +#' p = 0.01 |
| 21 | +#' ) |
| 22 | +#' verbose_off(model_sir) |
| 23 | +#' run(model_sir, ndays = 50, seed = model_seed) |
| 24 | +#' |
| 25 | +#' ## Setup LFMCMC |
| 26 | +#' # Extract the observed data from the model |
| 27 | +#' obs_data <- unname(as.integer(get_today_total(model_sir))) |
| 28 | +#' |
| 29 | +#' # Define the simulation function |
| 30 | +#' simfun <- function(params) { |
| 31 | +#' set_param(model_sir, "Recovery rate", params[1]) |
| 32 | +#' set_param(model_sir, "Transmission rate", params[2]) |
| 33 | +#' run(model_sir, ndays = 50) |
| 34 | +#' res <- unname(as.integer(get_today_total(model_sir))) |
| 35 | +#' return(res) |
| 36 | +#' } |
| 37 | +#' |
| 38 | +#' # Define the summary function |
| 39 | +#' sumfun <- function(dat) { |
| 40 | +#' return(dat) |
| 41 | +#' } |
| 42 | +#' |
| 43 | +#' # Create the LFMCMC model |
| 44 | +#' lfmcmc_model <- LFMCMC(model_sir) |> |
| 45 | +#' set_simulation_fun(simfun) |> |
| 46 | +#' set_summary_fun(sumfun) |> |
| 47 | +#' use_proposal_norm_reflective() |> |
| 48 | +#' use_kernel_fun_gaussian() |> |
| 49 | +#' set_observed_data(obs_data) |
| 50 | +#' |
| 51 | +#' ## Run LFMCMC simulation |
| 52 | +#' # Set initial parameters |
| 53 | +#' par0 <- as.double(c(0.1, 0.5)) |
| 54 | +#' n_samp <- 2000 |
| 55 | +#' epsil <- as.double(1.0) |
| 56 | +#' |
| 57 | +#' # Run the LFMCMC simulation |
| 58 | +#' run_lfmcmc( |
| 59 | +#' lfmcmc = lfmcmc_model, |
| 60 | +#' params_init_ = par0, |
| 61 | +#' n_samples_ = n_samp, |
| 62 | +#' epsilon_ = epsil, |
| 63 | +#' seed = model_seed |
| 64 | +#' ) |
| 65 | +#' |
| 66 | +#' # Print the results |
| 67 | +#' set_stats_names(lfmcmc_model, get_states(model_sir)) |
| 68 | +#' set_par_names(lfmcmc_model, c("Immune recovery", "Infectiousness")) |
| 69 | +#' |
| 70 | +#' print(lfmcmc_model) |
| 71 | +#' @export |
| 72 | +LFMCMC <- function(model) { |
| 73 | + if (!inherits(model, "epiworld_model")) |
| 74 | + stop("model should be of class 'epiworld_model'. It is of class ", class(model)) |
| 75 | + |
| 76 | + structure( |
| 77 | + LFMCMC_cpp(model), |
| 78 | + class = c("epiworld_lfmcmc") |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +#' @rdname LFMCMC |
| 83 | +#' @param lfmcmc LFMCMC model |
| 84 | +#' @param params_init_ Initial model parameters |
| 85 | +#' @param n_samples_ Number of samples |
| 86 | +#' @param epsilon_ Epsilon parameter |
| 87 | +#' @param seed Random engine seed |
| 88 | +#' @returns The simulated model of class [epiworld_lfmcmc]. |
| 89 | +#' @export |
| 90 | +run_lfmcmc <- function(lfmcmc, params_init_, n_samples_, epsilon_, seed = NULL) UseMethod("run_lfmcmc") |
| 91 | + |
| 92 | +#' @export |
| 93 | +run_lfmcmc.epiworld_lfmcmc <- function(lfmcmc, params_init_, n_samples_, epsilon_, seed = NULL) { |
| 94 | + if (length(seed)) set.seed(seed) |
| 95 | + run_lfmcmc_cpp(lfmcmc, params_init_, n_samples_, epsilon_, sample.int(1e4, 1)) |
| 96 | + invisible(lfmcmc) |
| 97 | +} |
| 98 | + |
| 99 | +#' @rdname LFMCMC |
| 100 | +#' @param lfmcmc LFMCMC model |
| 101 | +#' @param observed_data_ Observed data |
| 102 | +#' @returns The lfmcmc model with the observed data added |
| 103 | +#' @export |
| 104 | +set_observed_data <- function(lfmcmc, observed_data_) UseMethod("set_observed_data") |
| 105 | + |
| 106 | +#' @export |
| 107 | +set_observed_data.epiworld_lfmcmc <- function(lfmcmc, observed_data_) { |
| 108 | + set_observed_data_cpp(lfmcmc, observed_data_) |
| 109 | + invisible(lfmcmc) |
| 110 | +} |
| 111 | + |
| 112 | +#' @rdname LFMCMC |
| 113 | +#' @param lfmcmc LFMCMC model |
| 114 | +#' @param fun The LFMCMC proposal function |
| 115 | +#' @returns The lfmcmc model with the proposal function added |
| 116 | +#' @export |
| 117 | +set_proposal_fun <- function(lfmcmc, fun) UseMethod("set_proposal_fun") |
| 118 | + |
| 119 | +#' @export |
| 120 | +set_proposal_fun.epiworld_lfmcmc <- function(lfmcmc, fun) { |
| 121 | + set_proposal_fun_cpp(lfmcmc, fun) |
| 122 | + invisible(lfmcmc) |
| 123 | +} |
| 124 | + |
| 125 | +#' @rdname LFMCMC |
| 126 | +#' @param lfmcmc The LFMCMC model |
| 127 | +#' @returns The LFMCMC model with proposal function set to norm reflective |
| 128 | +#' @export |
| 129 | +use_proposal_norm_reflective <- function(lfmcmc) { |
| 130 | + use_proposal_norm_reflective_cpp(lfmcmc) |
| 131 | + invisible(lfmcmc) |
| 132 | +} |
| 133 | + |
| 134 | +#' @rdname LFMCMC |
| 135 | +#' @param lfmcmc LFMCMC model |
| 136 | +#' @param fun The LFMCMC simulation function |
| 137 | +#' @returns The lfmcmc model with the simulation function added |
| 138 | +#' @export |
| 139 | +set_simulation_fun <- function(lfmcmc, fun) UseMethod("set_simulation_fun") |
| 140 | + |
| 141 | +#' @export |
| 142 | +set_simulation_fun.epiworld_lfmcmc <- function(lfmcmc, fun) { |
| 143 | + set_simulation_fun_cpp(lfmcmc, fun) |
| 144 | + invisible(lfmcmc) |
| 145 | +} |
| 146 | + |
| 147 | +#' @rdname LFMCMC |
| 148 | +#' @param lfmcmc LFMCMC model |
| 149 | +#' @param fun The LFMCMC sumamry function |
| 150 | +#' @returns The lfmcmc model with the summary function added |
| 151 | +#' @export |
| 152 | +set_summary_fun <- function(lfmcmc, fun) UseMethod("set_summary_fun") |
| 153 | + |
| 154 | +#' @export |
| 155 | +set_summary_fun.epiworld_lfmcmc <- function(lfmcmc, fun) { |
| 156 | + set_summary_fun_cpp(lfmcmc, fun) |
| 157 | + invisible(lfmcmc) |
| 158 | +} |
| 159 | + |
| 160 | +#' @rdname LFMCMC |
| 161 | +#' @param lfmcmc LFMCMC model |
| 162 | +#' @param fun The LFMCMC kernel function |
| 163 | +#' @returns The lfmcmc model with the kernel function added |
| 164 | +#' @export |
| 165 | +set_kernel_fun <- function(lfmcmc, fun) UseMethod("set_kernel_fun") |
| 166 | + |
| 167 | +#' @export |
| 168 | +set_kernel_fun.epiworld_lfmcmc <- function(lfmcmc, fun) { |
| 169 | + set_kernel_fun_cpp(lfmcmc, fun) |
| 170 | + invisible(lfmcmc) |
| 171 | +} |
| 172 | + |
| 173 | +#' @rdname LFMCMC |
| 174 | +#' @param lfmcmc The LFMCMC model |
| 175 | +#' @returns The LFMCMC model with kernel function set to gaussian |
| 176 | +#' @export |
| 177 | +use_kernel_fun_gaussian <- function(lfmcmc) { |
| 178 | + use_kernel_fun_gaussian_cpp(lfmcmc) |
| 179 | + invisible(lfmcmc) |
| 180 | +} |
| 181 | + |
| 182 | +#' @rdname LFMCMC |
| 183 | +#' @param lfmcmc LFMCMC model |
| 184 | +#' @param names The model parameter names |
| 185 | +#' @returns The lfmcmc model with the parameter names added |
| 186 | +#' @export |
| 187 | +set_par_names <- function(lfmcmc, names) UseMethod("set_par_names") |
| 188 | + |
| 189 | +#' @export |
| 190 | +set_par_names.epiworld_lfmcmc <- function(lfmcmc, names) { |
| 191 | + set_par_names_cpp(lfmcmc, names) |
| 192 | + invisible(lfmcmc) |
| 193 | +} |
| 194 | + |
| 195 | +#' @rdname LFMCMC |
| 196 | +#' @param lfmcmc LFMCMC model |
| 197 | +#' @param names The model stats names |
| 198 | +#' @returns The lfmcmc model with the stats names added |
| 199 | +#' @export |
| 200 | +set_stats_names <- function(lfmcmc, names) UseMethod("set_stats_names") |
| 201 | + |
| 202 | +#' @export |
| 203 | +set_stats_names.epiworld_lfmcmc <- function(lfmcmc, names) { |
| 204 | + set_stats_names_cpp(lfmcmc, names) |
| 205 | + invisible(lfmcmc) |
| 206 | +} |
| 207 | + |
| 208 | +#' @rdname LFMCMC |
| 209 | +#' @param x LFMCMC model to print |
| 210 | +#' @param ... Ignored |
| 211 | +#' @returns The lfmcmc model |
| 212 | +#' @export |
| 213 | +print.epiworld_lfmcmc <- function(x, ...) { |
| 214 | + print_lfmcmc_cpp(x) |
| 215 | + invisible(x) |
| 216 | +} |
0 commit comments