Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bisaloo/simplemh
Simple R implementation of the Metropolis-Hastings MCMC Algorithm
https://github.com/bisaloo/simplemh
Last synced: 11 days ago
JSON representation
Simple R implementation of the Metropolis-Hastings MCMC Algorithm
- Host: GitHub
- URL: https://github.com/bisaloo/simplemh
- Owner: Bisaloo
- Created: 2021-01-12T14:10:16.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-02-09T09:15:06.000Z (11 months ago)
- Last Synced: 2024-11-24T09:11:46.509Z (29 days ago)
- Language: R
- Homepage: http://hugogruson.fr/simpleMH/
- Size: 1.5 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# simpleMH
[![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
[![R-CMD-check](https://github.com/Bisaloo/simpleMH/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/Bisaloo/simpleMH/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/Bisaloo/simpleMH/branch/main/graph/badge.svg)](https://app.codecov.io/gh/Bisaloo/simpleMH?branch=main)
[![CRAN status](https://www.r-pkg.org/badges/version-ago/simpleMH)](https://CRAN.R-project.org/package=simpleMH)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](https://hugogruson.fr/mcmcensemble/) or
[adaptMCMC](https://github.com/scheidan/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:
```{r, eval = FALSE}
install.packages("simpleMH")
```or from my [r-universe](https://bisaloo.r-universe.dev/) (development version):
```{r, eval = FALSE}
install.packages("simpleMH", repos = "https://bisaloo.r-universe.dev")
```## Example
```{r 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()`:
```{r sampling-landscape, dpi = 150}
plot(as.data.frame(res$samples))
```We can then use the [coda package](https://cran.r-project.org/package=coda) to
post-process the chain (burn-in, thinning, etc.), plot the trace and density,
or compute convergence diagnostics:```{r coda-plots, dpi = 150}
plot(res$samples)
```