Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/freguglia/mrf2dbayes
An extension of mrf2d for Bayesian analysis.
https://github.com/freguglia/mrf2dbayes
Last synced: 18 days ago
JSON representation
An extension of mrf2d for Bayesian analysis.
- Host: GitHub
- URL: https://github.com/freguglia/mrf2dbayes
- Owner: Freguglia
- Created: 2020-12-19T23:34:43.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-19T18:10:03.000Z (11 months ago)
- Last Synced: 2024-01-19T19:27:58.172Z (11 months ago)
- Language: R
- Homepage:
- Size: 289 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.Rmd
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%"
)
set.seed(1)
```# mrf2dbayes
[![R build status](https://github.com/Freguglia/mrf2dbayes/workflows/R-CMD-check/badge.svg)](https://github.com/Freguglia/mrf2dbayes/actions)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)The goal of `mrf2dbayes` is to provide out-of-the-box implementations of Bayesian inference algorithms for Markov Random Fields on 2-dimensional lattices with pairwise interactions. It can be viewed as a Bayesian extension of the [`mrf2d`](https://github.com/Freguglia/mrf2k) package.
## Installation
The development version from [GitHub](https://github.com/) can be installed with:
``` r
# install.packages("devtools")
devtools::install_github("Freguglia/mrf2dbayes")
```## Functionalities
### Likelihood approximations
Because the likelihood function for Markov Random Fields is unavailable due to an intractable normalizing constant, `mrf2dbayes` uses an approximate-posterior distribution approach, which substitutes the likelihood function for an approximation in the acceptance ratio of the Metropolis-Hastings algorithm steps.
log-Likelihood approximations are represented by `llapprox` object. These can be created with the homonym function `llapprox()` and passing a reference field, an interaction structure (`mrfi` object from `mrf2d`), an interaction family (also introduced in `mrf2d`), a method and its related additional arguments.
Approximation methods currently implemented are:
* `"pseudo"`: Pseudolikelihood approximation.
* `"gt"`: Monte-Carlo Likelihood approximation from Geyer \& Thompson (1992).
* TODO: `"wl"`: Wang-Landau algorithm.
* TODO: `"cpseudo"`: Corrected Pseudolikelihood approach from ...```{r}
library(mrf2dbayes)
# Example MRF from mrf2d
z <- mrf2d::Z_pottslla <- llapprox(z, mrfi(1), "oneeach", method = "pseudo")
```### MRF parameters posterior sampling
`mrfbayes()` runs a Metropolis-Hastings algorithm to sample from the posterior distribution of the MRF parameters considering centered Gaussian priors and Gaussian transition kernels. It takes an observed (discrete) random field, `llapprox` object and the size of the chain (`nsamples`) as arguments, as well as other arguments to control parameters such as the prior distribution variance and the transition kernel variance.
```{r, cache = TRUE}
metrop <- mrfbayes(z, lla, nsamples = 10000)
```A `mrfbayes_out` object is returned. `plot()` and `summary()` methods are available.
```{r, out.width = "70%", fig.align = 'center'}
plot(metrop)
summary(metrop)
```### Hidden MRF Bayesian analysis
Considering a Gaussian mixture driven by a Hidden MRF, a Metropolis-Within-Gibbs approach is used to sample the hidden (latent) field, the parameters of the hidden MRF and the parameters of the emission distribution simultaneously.
A Gaussian prior is used for the means of the emission distribution and an Inverse-Gamma for the variances.
```{r, cache = TRUE}
bold5000 <- mrf2d::bold5000
# Dummy discrete field as reference
dummy <- matrix(sample(0:4,
prod(dim(bold5000)), replace = TRUE),
nrow = nrow(bold5000), ncol = ncol(bold5000))lla_bold <- llapprox(dummy, mrfi(1), "onepar", method = "pseudo")
``````{r, cache = TRUE}
hmrf_metrop <- hmrfbayes(bold5000, lla_bold, nsamples = 20000)
``````{r}
mrf2d::cplot(bold5000)
``````{r}
plot(hmrf_metrop, "pars")
plot(hmrf_metrop, "theta")
plot(hmrf_metrop, "zprobs")
```