Simple Metropolis-Hastings MCMC Algorithm

A very bare-bones interface to use the Metropolis-Hastings Monte Carlo Markov Chain algorithm. It is suitable for teaching and testing purposes.


simpleMH

Lifecycle:maturing R-CMD-check Codecov testcoverage CRANstatus

This package offers a very bare-bones interface to use the Metropolis-Hastings Monte Carlo Markov Chain algorithm. It is suitable for teaching and testing purposes. For more advanced uses, you can check out the mcmcensemble or adaptMCMC packages, which are designed with a very similar interface, but often allow better convergence, especially for badly scaled problems or highly correlated set of parameters.

Installation

You can install this package from CRAN:

install.packages("simpleMH")

or from my r-universe (development version):

install.packages("simpleMH", repos = "https://bisaloo.r-universe.dev")

Example

library(simpleMH)

## a log-pdf to sample from
p.log <- function(x) {
  B <- 0.03                              # controls 'bananacity'
  -x[1]^2/200 - 1/2*(x[2]+B*x[1]^2-100*B)^2
}

res <- simpleMH(
  p.log,
  inits = c(0, 0),
  theta.cov = diag(2),
  max.iter = 5000,
  coda = TRUE # to be able to have nice plots and diagnostics with the coda pkg
)

Here is the resulting sampling landscape of p.log():

plot(as.data.frame(res$samples))

We can then use the coda package to post-process the chain (burn-in, thinning, etc.), plot the trace and density, or compute convergence diagnostics:

plot(res$samples)

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("simpleMH")

0.1.1 by Hugo Gruson, 3 years ago


https://github.com/Bisaloo/simpleMH


Report a bug at https://github.com/Bisaloo/simpleMH/issues


Browse source code at https://github.com/cran/simpleMH


Authors: Hugo Gruson [aut, cre, cph]


Documentation:   PDF Manual  


GPL-3 license


Imports mvtnorm

Suggests coda, mockery, testthat, knitr, rmarkdown


See at CRAN