knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) set.seed(1)
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
package.
The development version from GitHub can be installed with:
# install.packages("devtools") devtools::install_github("Freguglia/mrf2dbayes")
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)."wl"
: Wang-Landau algorithm."cpseudo"
: Corrected Pseudolikelihood approach from ...library(mrf2dbayes) # Example MRF from mrf2d z <- mrf2d::Z_potts lla <- llapprox(z, mrfi(1), "oneeach", method = "pseudo")
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.
metrop <- mrfbayes(z, lla, nsamples = 10000)
A mrfbayes_out
object is returned. plot()
and summary()
methods are available.
plot(metrop) summary(metrop)
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.
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")
hmrf_metrop <- hmrfbayes(bold5000, lla_bold, nsamples = 20000)
mrf2d::cplot(bold5000)
plot(hmrf_metrop, "pars") plot(hmrf_metrop, "theta") plot(hmrf_metrop, "zprobs")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.