# MRF: Markov Random Fields Fitting Functions In gamlss.spatial: Spatial Terms in Generalized Additive Models for Location Scale and Shape Models

## Description

The functions `MRF()` and `MRFA()` are used to fit a Gaussian Markov Random Fields (MRF) model. They are used by the functions `mrf()` and `mrfa()` respectively to fit a MRF additive term within GAMLSS

## Usage

 ```1 2 3 4 5 6 7 8 9``` ```MRF(y, x, precision = NULL, neighbour = NULL, polys = NULL, area = NULL, weights = rep(1, length(y)), sig2e = 1, sig2b = 1, sig2e.fix = FALSE, sig2b.fix = FALSE, penalty = FALSE, delta = c(0.01, 0.01), shift = c(0, 0)) MRFA(y, x, precision = NULL, neighbour = NULL, polys = NULL, area = NULL, weights = rep(1, length(y)), lambda = NULL, df = NULL, start = 10) ```

## Arguments

 `y` response variable `x` a factor containing the areas `precision` the precision matrix if set `neighbour` an object containing the neighbour information for the area if set `polys` the polygon information if set `area` this argument is here to allow more areas than the levels of the factor `x`, see example below. `weights` prior weights `sig2e` starting values for the error variance `sig2b` starting values for the random field variance `sig2e.fix` whether sig2e is fixed in the fitting, default equals FALSE `sig2b.fix` whether sig2B is fixed in the fitting, default equals FALSE `penalty` whether quadratic penalty is required to help convergence in for flat likelihoods, this is equivalent of putting a normal prior distribution for the log-sigmas e.g. `logsig2e-N(shift, 1/delta)` `delta` the precision of the prior `shift` the mean of the prior `lambda` smoothing parameter for `MRFA` function `start` starting value for the smoothing parameter `lambda` for `MRFA` function `df` for fixing the degrees of freedom (only in `MRFA()`)

## Details

There are two functions for fitting Markov random fields: i) `MRF())` which uses the Q-function (marginal likelihood) for estimating the `sig2e` and `sig2b` parameters and ii) `MRFA()` which estimates the smoothing parameter `lambda=sig2e/sig2b` using the "alternating" method.

## Value

a fitted MRF object

## Author(s)

Fernanda De Bastiani, Mikis Stasinopoulos, Robert Rigby and Vlasios Voudouris.

## References

Stasinopoulos, D. M., Rigby, R. A., Heller, G. Z., Voudouris, V. and De Bastiani, F. (2017) Flexible Regression and Smoothing: Using GAMLSS in R. Chapman and Hall, Boca Raton. (see also http://www.gamlss.org/)

De Bastiani, F. Rigby, R. A., Stasinopoulos, D. M., Cysneiros, A. H. M. A. and Uribe-Opazo, M. A. (2016) Gaussian Markov random eld spatial models in GAMLSS. Journal of Applied Statistics, pp 1-19.

Rigby, R. A. and Stasinopoulos D. M. (2005). Generalized additive models for location, scale and shape,(with discussion), Appl. Statist., 54, part 3, pp 507-554.

Rue and Held (2005) Gaussian markov random fields: theory and applications, Chapman & Hall, USA.

Stasinopoulos D. M. Rigby R.A. (2007) Generalized additive models for location scale and shape (GAMLSS) in R. Journal of Statistical Software, Vol. 23, Issue 7, Dec 2007, http://www.jstatsoft.org/v23/i07.

`mrf`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46``` ```library(mgcv) data(columb) data(columb.polys) vizinhos=polys2nb(columb.polys) precisionC <- nb2prec(vizinhos,x=columb\$district) # MRFA m1<-MRFA(columb\$crime, columb\$district, polys=columb.polys) m11<-MRFA(columb\$crime, columb\$district, precision=precisionC) m12<-MRFA(columb\$crime, columb\$district, neighbour=vizinhos) draw.polys(columb.polys, m12, scheme="heat",swapcolors=TRUE) ## Not run: # MRF m2<-MRF(columb\$crime, columb\$district, polys=columb.polys) m21<-MRF(columb\$crime, columb\$district, precision=precisionC) m22<-MRF(columb\$crime, columb\$district, neighbour=vizinhos) AIC(m1, m11,m12,m2, m21, m22, k=0) draw.polys(columb.polys, m12, scheme="heat",swapcolors=TRUE) # removing one area columb2 <- columb[-5,] # creating new precision matrix precisionC2 <- nb2prec(vizinhos,x=columb\$district,area=columb\$district) # MRFA # new data but declaring area m11<-MRFA(columb2\$crime, columb2\$district, polys=columb.polys, area=columb\$district) # new data old polys m112<-MRFA(columb2\$crime, columb2\$district, polys=columb.polys) # new data old precision old area m111<-MRFA(columb2\$crime, columb2\$district, precision=precisionC,area=columb\$district) # new data old neighbour old area m121<-MRFA(columb2\$crime, columb2\$district, neighbour=vizinhos,area=columb\$district) # new data new precision old area m113<-MRFA(columb2\$crime, columb2\$district, precision=precisionC2,area=columb\$district) AIC(m11,m112,m111,m121,m113, k=0) m11<-MRFA(columb2\$crime, columb2\$district, polys=columb.polys, area=columb\$district) # new data old polys m112<-MRFA(columb2\$crime, columb2\$district, polys=columb.polys) # new data old precision old area m111<-MRFA(columb2\$crime, columb2\$district, precision=precisionC,area=columb\$district) # new data old neighbour old area m121<-MRFA(columb2\$crime, columb2\$district, neighbour=vizinhos,area=columb\$district) # new data new precision old area m113<-MRFA(columb2\$crime, columb2\$district, precision=precisionC2,area=columb\$district) AIC(m11,m112,m111,m121,m113, k=0) draw.polys(columb.polys, fitted(m11)) ## End(Not run) ```

### Example output

```Loading required package: gamlss.dist

Attaching package: 'gamlss.data'

The following object is masked from 'package:datasets':

sleep

**********   GAMLSS Version 5.1-3  **********
For more on GAMLSS look at http://www.gamlss.org/
Type gamlssNews() to see new features/changes/bug fixes.

This is mgcv 1.8-28. For overview type 'help("mgcv-package")'.

Attaching package: 'nnet'

The following object is masked from 'package:mgcv':

multinom

Spam version 2.2-2 (2019-03-07) is loaded.
Type 'help( Spam)' or 'demo( spam)' for a short introduction
and overview of this package.
Help for individual functions is also obtained by adding the
suffix '.spam' to the function name, e.g. 'help( chol.spam)'.

Attaching package: 'spam'

The following objects are masked from 'package:base':

backsolve, forwardsolve

df      AIC
m2  24.46858 335.9114
m21 24.46858 335.9114
m22 24.46858 335.9114
m1  24.46856 335.9115
m11 24.46856 335.9115
m12 24.46856 335.9115
df      AIC
m11  23.56706 330.9478
m112 23.56706 330.9478
m111 23.56706 330.9478
m121 23.56706 330.9478
m113 23.56706 330.9478
df      AIC
m11  23.56706 330.9478
m112 23.56706 330.9478
m111 23.56706 330.9478
m121 23.56706 330.9478
m113 23.56706 330.9478
```

gamlss.spatial documentation built on May 2, 2019, 2:16 a.m.