$$\newcommand{\bm}[1]{\boldsymbol{\mathbf{#1}}}$$

# 1 Introduction

This is a short tutorial giving a flavour of how the package Stan may be used in R to calibrate spatial regression models, and be used (in conjunction with standard R) as a Bayesian inferential tool. The data used is based on the Irish data distributed with the spdep package, but with a more detailed set of polygons defining the counties of the Republic of Ireland, and using the Irish National Grid as a map projection. To work through this tutorial you will need to have R installed, and also the following R packages and their dependencies: spdep,rstan,ggplot2 and tmap. Note that you may need to install RStan from github - note that there are a few extra steps here - you can’t just install via CRAN in the usual way. However full instruction, for Mac, Windows and linux, are here: https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started

Once these are in place, you’ll need to load them into R:

library(spdep)
library(rstan)
library(ggplot2)
library(tmap)

and also, set up the options for stan:

rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

Next, you will need to load the data. This takes the form of a SpatialPolygonsDataFrame called ireland which is in the binary file ireland.RData. The file can be downloaded from this link: https://www.dropbox.com/s/vzzo2jqqiaqputo/ireland.RData?dl=0 - download this into your working folder.

Once this is done, read in the data

load('ireland.RData')
ireland.nb <- poly2nb(ireland,snap=0.0002)

The second line produces a nb object - this contains information about which counties in Ireland are coterminous - that is, they share a common boundary. The objected can be drawn, as below:

plot(ireland)
plot(ireland.nb,coords=coordinates(ireland),add=TRUE,pch=16,col='darkred')