|
library(R2OpenBUGS)
############################################################
########### Load data ############
############################################################
dat<-read.csv("F:\\ST733\\Data\\Obama2012.csv")
W<-as.matrix(read.csv("F:\\ST733\\Data\\NCADJ.csv",row.names=1))
Y<-dat[,2] #Percent that voted for Obama
X<-dat[,8] #Percent black
############################################################
########### Set up for BUGS ############
############################################################
n<-ncol(W)
num<-colSums(W)
adj<-NULL
for(j in 1:n){
adj<-c(adj,which(W[j,]==1))
}
adj<-as.vector(adj)
num<-as.vector(num)
weights<-1+0*adj
# num[i] = number of regions that adjacent to region i
# The first num[1] elements of adj are region 1's neighbors,
# the next num[2] elements of adj are region 2's neighbors, etc.
# weights being all one specify that all neighbors are weighted equally
############################################################
### CAR model with spatially-varying coefficients ###
############################################################
sink("svc_car_model.txt")
cat("
model{
for(i in 1:n){ #Likelihood
Y[i] ~ dnorm(mu[i],tau[1])
mu[i] <- a[i] + b[i]*X[i]
a[i] <- int[1] + R1[i] #Add in an intercept to get the unrestricted CAR.
b[i] <- c*a[i] + (int[2] + R2[i])
}
#CAR model (in BUGS, the R[j,] are forced to sum to zero)
R1[1:n] ~ car.normal(adj[], weights[], num[], tau[2])
R2[1:n] ~ car.normal(adj[], weights[], num[], tau[3])
#Priors
int[1]~dflat() # Must be flat to get the ICAR
int[2]~dflat()
c~dnorm(0,0.1)
tau[1] ~ dgamma(0.1,0.1)
tau[2] ~ dgamma(0.1,0.1)
tau[3] ~ dgamma(0.1,0.1)
}
}", fill = TRUE)
sink()
############################################################
######### Call BUGS ###########
############################################################
data<-list(X=X,Y=Y,n=n,num=num,adj=adj,weights=weights)
inits<-function(){
list(int=c(.25,1),c=0,tau=rep(10,3))
}
keepers <- c("a","b","c")
out <- bugs(
data=data,
inits=inits,
parameters.to.save=keepers,
model.file="svc_car_model.txt",
n.iter=10000,
n.chains=3,
n.burnin=1000,
n.thin=1,
debug=FALSE,
#Change this to the location of the openBUGS executable on your computer
OpenBUGS.pgm="F:\\OpenBUGS322\\OpenBUGS.exe",
)
############################################################
######### Summarize the results ###########
############################################################
plot.nc<-function(data,main="",breaks=5,sig_digits=3){
library(maps)
d<-data[c(1:26,27,27,27:100)] #county 27 has three polygons!
if(length(breaks)==1){
breaks<-quantile(d,seq(0,1,length=breaks+1),na.rm=T)
}
nbreaks<-length(breaks)-1
low<-breaks[-(nbreaks+1)]
high<-breaks[-1]
col<-rep(1,nbreaks)
for(j in 2:nbreaks){
col<-ifelse(d>low[j],j,col)
}
shade<-1-col/nbreaks
shades<-1-1:nbreaks/nbreaks
low<-round(low,sig_digits)
high<-round(high,sig_digits)
map("county","north carolina",fill=T,col=gray(shade))
title(main)
legend("bottomleft",paste(low,"-",high),lwd=5,ncol=2,col=gray(shades))
}
plot.nc(X,main="Pct black")
plot.nc(Y,main="Pct Obama")
plot.nc(out$mean$a,main="Posterior mean a")
plot.nc(out$mean$b,main="Posterior mean b")
b<-out$sims.list$b
b_mean<-colMeans(b)
boxplot(b[,order(b_mean)],outline=F)
|