From d9a2f48fc582682b01632a5718c739e1a26b98be Mon Sep 17 00:00:00 2001 From: Andrew Pulsipher Date: Mon, 4 Nov 2024 13:20:12 -0700 Subject: [PATCH] Fill out fields for roxygen docs of LFMCMC --- R/LFMCMC.R | 69 ++++++++++++++++++++++++++++++++++++++++++++----- man/LFMCMC.Rd | 71 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 126 insertions(+), 14 deletions(-) diff --git a/R/LFMCMC.R b/R/LFMCMC.R index 20a64e5c..efcc8a8c 100644 --- a/R/LFMCMC.R +++ b/R/LFMCMC.R @@ -3,14 +3,71 @@ #' #' @aliases epiworld_lfmcmc #' @details -#' TODO: Detail LFMCMC +#' Performs a Likelihood-Free Markhov Chain Monte Carlo simulation #' @param model A model of class [epiworld_model] #' @returns -#' - The `LFMCMC`function returns a model of class [epiworld_lfmcmc]. +#' The `LFMCMC` function returns a model of class [epiworld_lfmcmc]. #' @examples -#' model_sir <- ModelSIR(name = "COVID-19", prevalence = 0.01, -#' transmission_rate = 0.9, recovery_rate = 0.1) -#' model_lfmcmc <- LFMCMC(model_sir) +#' ## Setup an SIR model to use in the simulation +#' model_seed <- 122 +#' model_sir <- ModelSIR(name = "COVID-19", prevalence = .1, +#' transmission_rate = .9, recovery_rate = .3) +#' agents_smallworld( +#' model_sir, +#' n = 1000, +#' k = 5, +#' d = FALSE, +#' p = 0.01 +#' ) +#' verbose_off(model_sir) +#' run(model_sir, ndays = 50, seed = model_seed) +#' +#' ## Setup LFMCMC +#' # Extract the observed data from the model +#' obs_data <- unname(as.integer(get_today_total(model_sir))) +#' +#' # Define the simulation function +#' simfun <- function(params) { +#' set_param(model_sir, "Recovery rate", params[1]) +#' set_param(model_sir, "Transmission rate", params[2]) +#' run(model_sir, ndays = 50) +#' res <- unname(as.integer(get_today_total(model_sir))) +#' return(res) +#' } +#' +#' # Define the summary function +#' sumfun <- function(dat) { +#' return(dat) +#' } +#' +#' # Create the LFMCMC model +#' lfmcmc_model <- LFMCMC(model_sir) |> +#' set_simulation_fun(simfun) |> +#' set_summary_fun(sumfun) |> +#' use_proposal_norm_reflective() |> +#' use_kernel_fun_gaussian() |> +#' set_observed_data(obs_data) +#' +#' ## Run LFMCMC simulation +#' # Set initial parameters +#' par0 <- as.double(c(0.1, 0.5)) +#' n_samp <- 2000 +#' epsil <- as.double(1.0) +#' +#' # Run the LFMCMC simulation +#' run_lfmcmc( +#' lfmcmc = lfmcmc_model, +#' params_init_ = par0, +#' n_samples_ = n_samp, +#' epsilon_ = epsil, +#' seed = model_seed +#' ) +#' +#' # Print the results +#' set_stats_names(lfmcmc_model, get_states(model_sir)) +#' set_par_names(lfmcmc_model, c("Immune recovery", "Infectiousness")) +#' +#' print(lfmcmc_model) #' @export LFMCMC <- function(model) { if (!inherits(model, "epiworld_model")) @@ -28,7 +85,7 @@ LFMCMC <- function(model) { #' @param n_samples_ Number of samples #' @param epsilon_ Epsilon parameter #' @param seed Random engine seed -#' @returns The simulated model of class `epiworld_lfmcmc`. +#' @returns The simulated model of class [epiworld_lfmcmc]. #' @export run_lfmcmc <- function(lfmcmc, params_init_, n_samples_, epsilon_, seed = NULL) UseMethod("run_lfmcmc") diff --git a/man/LFMCMC.Rd b/man/LFMCMC.Rd index eaa6d427..3081b364 100644 --- a/man/LFMCMC.Rd +++ b/man/LFMCMC.Rd @@ -64,11 +64,9 @@ set_stats_names(lfmcmc, names) \item{...}{Ignored} } \value{ -\itemize{ -\item The \code{LFMCMC}function returns a model of class \link{epiworld_lfmcmc}. -} +The \code{LFMCMC} function returns a model of class \link{epiworld_lfmcmc}. -The simulated model of class \code{epiworld_lfmcmc}. +The simulated model of class \link{epiworld_lfmcmc}. The lfmcmc model with the observed data added @@ -94,10 +92,67 @@ The lfmcmc model Likelihood-Free Markhov Chain Monte Carlo (LFMCMC) } \details{ -TODO: Detail LFMCMC +Performs a Likelihood-Free Markhov Chain Monte Carlo simulation } \examples{ -model_sir <- ModelSIR(name = "COVID-19", prevalence = 0.01, - transmission_rate = 0.9, recovery_rate = 0.1) -model_lfmcmc <- LFMCMC(model_sir) +## Setup an SIR model to use in the simulation +model_seed <- 122 +model_sir <- ModelSIR(name = "COVID-19", prevalence = .1, + transmission_rate = .9, recovery_rate = .3) +agents_smallworld( + model_sir, + n = 1000, + k = 5, + d = FALSE, + p = 0.01 +) +verbose_off(model_sir) +run(model_sir, ndays = 50, seed = model_seed) + +## Setup LFMCMC +# Extract the observed data from the model +obs_data <- unname(as.integer(get_today_total(model_sir))) + +# Define the simulation function +simfun <- function(params) { + set_param(model_sir, "Recovery rate", params[1]) + set_param(model_sir, "Transmission rate", params[2]) + run(model_sir, ndays = 50) + res <- unname(as.integer(get_today_total(model_sir))) + return(res) +} + +# Define the summary function +sumfun <- function(dat) { + return(dat) +} + +# Create the LFMCMC model +lfmcmc_model <- LFMCMC(model_sir) |> + set_simulation_fun(simfun) |> + set_summary_fun(sumfun) |> + use_proposal_norm_reflective() |> + use_kernel_fun_gaussian() |> + set_observed_data(obs_data) + +## Run LFMCMC simulation +# Set initial parameters +par0 <- as.double(c(0.1, 0.5)) +n_samp <- 2000 +epsil <- as.double(1.0) + +# Run the LFMCMC simulation +run_lfmcmc( + lfmcmc = lfmcmc_model, + params_init_ = par0, + n_samples_ = n_samp, + epsilon_ = epsil, + seed = model_seed +) + +# Print the results +set_stats_names(lfmcmc_model, get_states(model_sir)) +set_par_names(lfmcmc_model, c("Immune recovery", "Infectiousness")) + +print(lfmcmc_model) }