################################################################
#### Load the libraries required to do an extended analysis
################################################################
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)
#### Remove the outlying observation
housedata <- housedata[!rownames(housedata)=="S02000655", ]
#### Combine the data and shapefile
data.combined <- combine.data.shapefile(housedata, shp, dbf)
#### Transform the price and crime variables
housedata$logprice <- log(housedata$price)
housedata$logcrime <- log(housedata$crime)
housedata$logdriveshop <- log(housedata$driveshop)
#### Compute the neighbourhood matrix
W.nb <- poly2nb(data.combined, row.names = rownames(housedata))
W.list <- nb2listw(W.nb, style="B")
W.mat <- nb2mat(W.nb, style="B")
form <- "logprice~logcrime+rooms+sales+factor(type)+logdriveshop"
model.spatial <- gaussian.properCAR(formula=form, data=housedata, W=W.mat,
burnin=20000, n.sample=100000, thin=10)
################################
#### 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
respdata$SIR2010 <- respdata$observed2010 / respdata$expected2010
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 / 2
#### Run the local CAR model
formula <- "observed2010 ~ offset(log(expected2010))"
model.dissimilarity <- poisson.dissimilarityCAR(formula=formula, data=respdata,
W=W.mat, Z=list(Z.incomedep=Z.incomedep), rho=0.99,
burnin=20000, n.sample=100000, thin=10)
#### Plot a map with the boundaries overlayed
border.locations <- model.dissimilarity$W.summary$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),scales=list(draw = TRUE), at=seq(min(risk.estimates)-0.1,
max(risk.estimates)+0.1, length.out=8),
col.regions=c("#FFFFB2", "#FED976", "#FEB24C", "#FD8D3C", "#FC4E2A", "#E31A1C", "#B10026"))
Run the code above in your browser using DataLab