Source code for CAR model:
#Extended BYM CAR Model:
#col: rat sighting counts
#subdist: the average distance in meters from each rat sighting to the nearest subway line
#greendist: the average distance in meters from each rat sighting to public space
#prcntl: proportion of the population in each census tract with less than a high school education
#DP0010001: total population in each census tract
#PcntVacantUnits: proportion of housing units that are vacant in each census tract
#TotHouseUnits: total housing units in each census tract
#oldhouse: proportion of housing structures built before 1950 in each census tract
#largehouses: the proportion of housing structures with five or more family units in each census tract
#PercentLack: the proportion of housing structures lacking complete plumbing facilities in each census tract
#lnland: natural log of census tract land area; this is the offset
#distances3: spatial dataframe object
library(spdep)
library(CARBayes)
dist3.nb <- poly2nb(distances3)
dist3.lw <- nb2listw(dist3.nb, style = "B", zero.policy = TRUE)
dist3.mat <- nb2mat(dist3.nb, style = "B", zero.policy = TRUE)
form2 <- "col ~ subdist + greendist + prcntl + DP0010001 + PcntVacantUnits + TotHouseUnits + oldhouse +largehouses + PercentLack + lnland"
model.spatial2 <- bymCAR.re(formula = form2, family = "poisson", data = distances3@data, W = dist3.mat)