Skip to content

Commit

Permalink
Adding example using cl cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Sep 18, 2023
1 parent 95949bf commit 64249ad
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: fmcmc
Title: A friendly MCMC framework
Version: 0.5-2
Version: 0.5-2.9000
Date: 2023-08-29
Authors@R: c(person("George", "Vega Yon", email = "[email protected]", role =
c("aut", "cre"), comment = c(ORCID = "0000-0002-3171-0844")),
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# fmcmc 0.5-2.9000

* Added an example using cluster objects.



# fmcmc 0.5-2

* Adressing [`roxygen2` issue #1491](https://github.com/r-lib/roxygen2/issues/1491)
Expand Down
74 changes: 74 additions & 0 deletions R/mcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,80 @@
#' summary(ans)
#' }
#'
#' # Example using a user-defined cl object -------------------------------------
#'
#' # A silly function that gets the first two parameters of the
#' # vector. Using the default multicore example will cause and error
#' get_m <- function(pars) {
#' pars[1:2]
#' }
#'
#' fun <- function(pars, D) {
#' # Putting the parameters in a sensible way
#' m <- get_m(pars)
#' s <- cbind( c(pars[3], pars[4]), c(pars[4], pars[5]) )
#'
#' # Computing the unnormalized log likelihood
#' sum(log(mvtnorm::dmvnorm(D, m, s)))
#' }
#'
#' if (FALSE) {
#'
#' # Thi will fail with the error
#' # Error in checkForRemoteErrors(val) :
#' # 4 nodes produced errors; first error: could not find function "get_m"
#' ans <- MCMC(
#' initial = c(mu0=5, mu1=5, s0=5, s01=0, s2=5),
#' fun,
#' kernel = kernel_normal_reflective(
#' lb = c(-10, -10, .01, -5, .01),
#' ub = 5,
#' scale = 0.01
#' ),
#' nsteps = 1e3,
#' thin = 10,
#' burnin = 5e2,
#' D = D,
#' # Multiple chains using parallel computing
#' multicore = TRUE,
#' nchains = 4
#' )
#'
#' summary(ans)
#'
#' }
#'
#' # To solve the error, we need to build the cluster object
#' library(parallel)
#' cl <- makePSOCKcluster(4)
#'
#' # Export the function `get_m` to the cluster. The function `fun` and data `D`
#' # are automatically exported by the `MCMC` function.
#' clusterExport(cl, "get_m")
#'
#' # Run the MCMC
#' ans <- MCMC(
#' initial = c(mu0=5, mu1=5, s0=5, s01=0, s2=5),
#' fun,
#' kernel = kernel_normal_reflective(
#' lb = c(-10, -10, .01, -5, .01),
#' ub = 5,
#' scale = 0.01
#' ),
#' nsteps = 1e3,
#' thin = 10,
#' burnin = 5e2,
#' D = D,
#' # Multiple chains using parallel computing
#' multicore = TRUE,
#' nchains = 4,
#' # Use the cluster object
#' cl = cl
#' )
#'
#' summary(ans)
#'
#'
#' @aliases Metropolis-Hastings
#' @seealso [get_logpost()], [get_logpost()] ([mcmc-output]) for post execution of `MCMC`, and
#' [ith_step()] for accessing objects within an `MCMC` call.
Expand Down
4 changes: 4 additions & 0 deletions inst/NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
fmcmc 0.5-2.9000

- Added an example using cluster objects.

fmcmc 0.5-2

- Adressing roxygen2 issue #1491
Expand Down
74 changes: 74 additions & 0 deletions man/MCMC.Rd

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

0 comments on commit 64249ad

Please sign in to comment.