Skip to content

Commit

Permalink
Updating news file and correcting how seeds are set
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Nov 20, 2024
1 parent b26ac55 commit 423e5f0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
18 changes: 18 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# epiworldR 0.4-3 (development version)

## New features

* The package now includes the `LFMCMC` module that implements
the likelihood-free Markov Chain Monte Carlo algorithm. This
module is used to estimate the parameters of the models.

* The new function `add_param()` allows the user to add parameters
to the model.

* The new function `rm_globalevent()` allows the user to remove
global events from the model.

* The function `today()` returns the current day (step) of the
simulation.


# epiworldR 0.3-2

* Starting version 0.3-0, `epiworldR` is versioned using the same version as the C++ library, `epiworld`.
Expand Down
48 changes: 37 additions & 11 deletions R/LFMCMC.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#' Likelihood-Free Markhov Chain Monte Carlo (LFMCMC)
#'
#'
#' @aliases epiworld_lfmcmc
#' @param model A model of class [epiworld_model] or `NULL` (see details).
#' @details
#' Performs a Likelihood-Free Markhov Chain Monte Carlo simulation
#' @param model A model of class [epiworld_model]
#' Performs a Likelihood-Free Markhov Chain Monte Carlo simulation. When
#' `model` is not `NULL`, the model uses the same random-number generator
#' engine as the model. Otherwise, when `model` is `NULL`, a new random-number
#' generator engine is created.
#' @returns
#' The `LFMCMC` function returns a model of class [epiworld_lfmcmc].
#' @examples
Expand Down Expand Up @@ -73,14 +75,19 @@
#' get_params_mean(lfmcmc_model)
#'
#' @export
LFMCMC <- function(model) {
if (!inherits(model, "epiworld_model"))
stop("model should be of class 'epiworld_model'. It is of class ", class(model))
LFMCMC <- function(model = NULL) {

if ((length(model) > 0) && !inherits(model, "epiworld_model"))
stop(
"model should be of class 'epiworld_model'. It is of class ",
paste(class(model), collapse = "\", ")
)

structure(
LFMCMC_cpp(model),
class = c("epiworld_lfmcmc")
)

}

#' @rdname LFMCMC
Expand All @@ -91,21 +98,40 @@ LFMCMC <- function(model) {
#' @param seed Random engine seed
#' @returns The simulated model of class [epiworld_lfmcmc].
#' @export
run_lfmcmc <- function(lfmcmc, params_init_, n_samples_, epsilon_, seed = NULL) UseMethod("run_lfmcmc")
run_lfmcmc <- function(
lfmcmc, params_init_, n_samples_, epsilon_,
seed = NULL
) {
UseMethod("run_lfmcmc")
}

#' @export
run_lfmcmc.epiworld_lfmcmc <- function(lfmcmc, params_init_, n_samples_, epsilon_, seed = NULL) {
if (length(seed)) set.seed(seed)
run_lfmcmc_cpp(lfmcmc, params_init_, n_samples_, epsilon_, sample.int(1e4, 1))
run_lfmcmc.epiworld_lfmcmc <- function(
lfmcmc, params_init_, n_samples_, epsilon_,
seed = NULL
) {

if (length(seed))
set.seed(seed)

run_lfmcmc_cpp(
lfmcmc, params_init_, n_samples_, epsilon_, sample.int(1e4, 1)
)

invisible(lfmcmc)

}

#' @rdname LFMCMC
#' @param lfmcmc LFMCMC model
#' @param observed_data_ Observed data
#' @returns The lfmcmc model with the observed data added
#' @export
set_observed_data <- function(lfmcmc, observed_data_) UseMethod("set_observed_data")
set_observed_data <- function(
lfmcmc, observed_data_
) {
UseMethod("set_observed_data")
}

#' @export
set_observed_data.epiworld_lfmcmc <- function(lfmcmc, observed_data_) {
Expand Down
9 changes: 6 additions & 3 deletions man/LFMCMC.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/lfmcmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ SEXP LFMCMC_cpp(
new LFMCMC<TData_default>()
);

cpp11::external_pointer<Model<int>> modelptr(model);
lfmcmc_ptr->set_rand_engine(modelptr->get_rand_endgine());
if (Rf_inherits(model, "epiworld_model")) {
cpp11::external_pointer<Model<int>> modelptr(model);
lfmcmc_ptr->set_rand_engine(modelptr->get_rand_endgine());
} else {
auto new_ptr = std::make_shared<std::mt19937>(std::mt19937());
lfmcmc_ptr->set_rand_engine(new_ptr);
}

return lfmcmc_ptr;
}
Expand Down

0 comments on commit 423e5f0

Please sign in to comment.