Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/freguglia/mrf2d

An R package for analysis of Markov Random Fields on 2-dimensional lattices.
https://github.com/freguglia/mrf2d

cran gibbs-energy hidden-markov-model hidden-markov-models markov-random-field mrf mrf-models neighborhood r rpackage

Last synced: 18 days ago
JSON representation

An R package for analysis of Markov Random Fields on 2-dimensional lattices.

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
echo = FALSE
)
```
# mrf2d

[![R-CMD-check](https://github.com/Freguglia/mrf2d/actions/workflows/check-release.yaml/badge.svg)](https://github.com/Freguglia/mrf2d/actions/workflows/check-release.yaml)
[![CRAN status](https://www.r-pkg.org/badges/version/mrf2d)](https://cran.r-project.org/package=mrf2d)
[![Codecov test coverage](https://codecov.io/gh/Freguglia/mrf2d/branch/main/graph/badge.svg)](https://app.codecov.io/gh/Freguglia/mrf2d?branch=main)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)

Markov Random Fields are probabilistic models capable of describing sets of random variables with a local dependence property (the Markov property) defined on a neighborhood system. Particularly on the context of image processing, pixels can be seen as vertices of a graph defined on a finite 2-dimensional lattice, and a neighborhood system can be defined based on their relative positions to construct a MRF.

drawing

The goal of `mrf2d` is to provide a framework for the analysis of Markov Random Fields with pairwise interactions on 2-dimensional lattices, including Hidden Markov Random Fields. It introduces the S4 class `mrfi` to describe interaction structures in a very general way, being able to adapt from very simple cases like the Ising Model to complex anisotropic models with different types of interaction.

```{r example_interacions, fig.show="hold", warning = FALSE, message = FALSE, fig.width=1.8, fig.height=1.6}
library(mrf2d)

plot(mrfi())
plot(mrfi(max_norm = 2, norm_type = "m"))
plot(mrfi(max_norm = 2, positions = list(c(3,3),c(4,4), c(-2,4))))
```

A complete paper describing the details of the package and examples is published in the Journal of Statistical Software and can be found [here](https://doi.org/10.18637/jss.v101.i08).

---

## Installation

You can install the stable version of `mrf2d` from [CRAN](https://CRAN.R-project.org) with:

```{r, echo = TRUE, eval = FALSE}
install.packages("mrf2d")
```

The development version is available on the package's [Github page](https://github.com/Freguglia/mrf2d). It can be installed with the `devtools` package by using

```{r, echo = TRUE, eval=FALSE}
devtools::install_github("Freguglia/mrf2d")
```

---

## Usage

`mrf2d` introduces a programming interface for the general Markov Random Field model in *Freguglia, Victor, Nancy L. Garcia, and Juliano L. Bicas. "Hidden Markov random field models applied to color homogeneity evaluation in dyed textile images." Environmetrics (2019): e2613.* Using specific interaction structures and parameter restrictions can lead to important models as particular cases, such as the Potts model.

It introduces the S4 class `mrfi` to represent interaction structures. The `mrfi()` function can be used to create these objects representing interaction structures with relative positions included based on the norm of the relative position (distance) or explicitly specified

```{r plot_interaction, echo = TRUE, fig.width=2, fig.height=2}
interact <- mrfi(max_norm = 1, positions = list(c(4,2)))
interact
plot(interact)
```

Potentials (parameters) are represented by three-dimensional arrays, where rows and columns represent pixel label values and slices represent interacting positions.

```{r, echo = TRUE}
potentials <- expand_array(c(-0.9, -0.9, 0.2), family = "oneeach", C = 1, mrfi = interact)
potentials
```

The negative values out of diagonal means different "colors" are less likely in that relative position.

The package has many built-in functions for sampling, potentials estimation and hidden MRF model fitting (used for image segmentation), but it also provides all the basic stack of computations used to implement algorithms for MRF models, making it suitable for development of research in Markov Random Field models.

```{r Z_example_plot, echo = TRUE, fig.width=5.5, fig.height=4, fig.align='center'}
set.seed(1)
img_dim <- c(200,200)
Z <- rmrf2d(img_dim, mrfi = interact, theta = potentials, cycles = 60)
dplot(Z, legend = TRUE)
```

---

## Contributing and Bug Reports

If you're interested in contributing or found a bug or error, please file an [issue](https://github.com/Freguglia/mrf2d/issues). Contributions can be done in form of code optimization, new ideas, discussing new structures, etc.