################################################################
#### Load the libraries required to do an extended analysis
################################################################
library(MASS)
library(lattice)
library(coda)
library(MCMCpack)
library(spam)
library(evd)
library(stats4)
library(truncdist)
library(foreign)
library(shapefiles)
library(sp)
library(boot)
library(Matrix)
library(nlme)
library(maptools)
library(grid)
library(deldir)
library(splines)
library(spdep)
library(CARBayes)
#############################
#### Example 1 - house prices
#############################
#### Read in the data
data(housedata)
data(shp)
data(dbf)
#### Combine the data and shapefile and create the neighbourhood matrix W
data.combined <- combine.data.shapefile(housedata, shp, dbf)
W.nb <- poly2nb(data.combined, row.names = rownames(housedata))
W.mat <- nb2mat(W.nb, style="B")
#### Run a regression model with spatially correlated random effects
housedata$crime.inv <- 1 / housedata$crime
form <- housedata$price~housedata$crime.inv+housedata$rooms+housedata$sales+factor(housedata$type)
model.spatial <- gaussian.properCAR(formula=form, W=W.mat, burnin=20000, n.sample=100000,
thin=10, prior.max.nu2=1000000, prior.max.tau2=1000000, prior.var.beta=rep(10000000, 7))
#### Plot the fitted values on a map
fitted.housedata <- model.spatial$fitted.values[ ,3]
data.combined@data <- data.frame(data.combined@data, fitted.housedata)
northarrow <- list("SpatialPolygonsRescale", layout.north.arrow(), offset = c(220000,647000),
scale = 4000)
scalebar <- list("SpatialPolygonsRescale", layout.scale.bar(), offset = c(225000,647000),
scale = 10000, fill=c("transparent","black"))
text1 <- list("sp.text", c(225000,649000), "0")
text2 <- list("sp.text", c(230000,649000), "5000 m")
spplot(data.combined, c("fitted.housedata"), sp.layout=list(northarrow, scalebar, text1, text2),
at=seq(min(fitted.housedata)-1, max(fitted.housedata)+1, length.out=51),
col.regions=terrain.colors(n=50))
################################
#### Example 2 - disease mapping
################################
#### Read in the data
data(respdata)
data(shp)
data(dbf)
#### Combine the data and shapefile and create the neighbourhood matrix W
data.combined <- combine.data.shapefile(respdata, shp, dbf)
W.nb <- poly2nb(data.combined, row.names = rownames(respdata))
W.mat <- nb2mat(W.nb, style="B")
#### Create the dissimilrity metric
Z.incomedep <- as.matrix(dist(cbind(respdata$incomedep2010, respdata$incomedep2010),
method="manhattan", diag=TRUE, upper=TRUE)) * W.mat
#### Run the local CAR model
formula <- respdata$observed2010 ~ offset(log(respdata$expected2010))
model.dissimilarity <- poisson.dissimilarityCAR(formula=formula, W=W.mat,
Z=list(Z.incomedep=Z.incomedep), rho=0.99, fix.rho=TRUE, burnin=20000,
n.sample=100000, thin=10)
#### Plot a map with the boundaries overlayed
border.locations <- model.dissimilarity$W.posterior
risk.estimates <- model.dissimilarity$fitted.values[ ,3] / respdata$expected2010
data.combined@data <- data.frame(data.combined@data, risk.estimates)
boundary.final <- highlight.borders(border.locations=border.locations, ID=rownames(respdata),
shp=shp, dbf=dbf)
boundaries = list("sp.points", boundary.final, col="white", pch=19, cex=0.2)
northarrow <- list("SpatialPolygonsRescale", layout.north.arrow(), offset = c(220000,647000),
scale = 4000)
scalebar <- list("SpatialPolygonsRescale", layout.scale.bar(), offset = c(225000,647000),
scale = 10000, fill=c("transparent","black"))
text1 <- list("sp.text", c(225000,649000), "0")
text2 <- list("sp.text", c(230000,649000), "5000 m")
spplot(data.combined, c("risk.estimates"), sp.layout=list(northarrow, scalebar, text1, text2, boundaries),
at=seq(min(risk.estimates)-0.1, max(risk.estimates)+0.1, length.out=51),
col.regions=terrain.colors(n=50))
Run the code above in your browser using DataLab