orcounty <- readShapePoly("orcounty.shp",proj4string=CRS("+proj=longlat"))
plot(orcounty) #save image now: "plot.orcounty.pdf"

---------------
summary(orcounty)
coordinates(orcounty)
centers=coordinates(orcounty)
centers=data.frame(centers) points(centers,col="blue",cex=1.2) text(centers,labels=rownames(centers),cex=1.5)
orcounty.centers = coordinates(orcounty)
#save image now: "orcounty.labels.pdf"

---------------
k=1
knn1 = knearneigh(orcounty.centers,k,longlat=T)
orcounty.knn1=knn2nb(knn1)
plot(orcounty)
plot(orcounty.knn1, orcounty.centers, col="blue",add=T)
#save image now: "knn1.pdf"

---------------
plot(orcounty)
k=2
knn2 = knearneigh(orcounty.centers,k,longlat=T)
orcounty.knn2=knn2nb(knn2)
plot(orcounty.knn2, orcounty.centers, col="blue",add=T)
#save image now: "knn2.pdf"

---------------
plot(orcounty)
k=3
knn3 = knearneigh(orcounty.centers,k,longlat=T)
orcounty.knn3=knn2nb(knn3)
plot(orcounty.knn3, orcounty.centers, col="blue",add=T)
#save image now: "knn3.pdf"

---------------
plot(orcounty)
k=4
knn4 = knearneigh(orcounty.centers,k,longlat=T)
orcounty.knn4=knn2nb(knn4)
plot(orcounty.knn4, orcounty.centers, col="blue",add=T)
#save image now: "knn4.pdf"

---------------
plot(orcounty)
k=5
knn5 = knearneigh(orcounty.centers,k,longlat=T)
orcounty.knn5=knn2nb(knn5)
plot(orcounty.knn4, orcounty.centers, col="blue",add=T)
#save image now: "knn5.pdf"

---------------
d=100
orcounty.dist.100 = dnearneigh(orcounty.centers,0,d,longlat=T)
plot(orcounty)
plot(orcounty.dist.100, orcounty.centers,add=T,lwd=2,col="red")
#save image now: "d100.pdf"

---------------
d=200
orcounty.dist.200 = dnearneigh(orcounty.centers,0,d,longlat=T)
plot(orcounty)
plot(orcounty.dist.200, orcounty.centers,add=T,lwd=2,col="red")
#save image now: "d200.pdf"

---------------
d=150
orcounty.dist.150 = dnearneigh(orcounty.centers,0,d,longlat=T)
plot(orcounty)
plot(orcounty.dist.150, orcounty.centers,add=T,lwd=2,col="red")
#save image now: "d150.pdf"

---------------
d=15
orcounty.dist.15 = dnearneigh(orcounty.centers,0,d,longlat=T)
plot(orcounty)
plot(orcounty.dist.15, orcounty.centers,add=T,lwd=2,col="red")
#save image now: "d15.pdf"

---------------
d=1000
orcounty.dist.1000 = dnearneigh(orcounty.centers,0,d,longlat=T)
plot(orcounty)
plot(orcounty.dist.1000, orcounty.centers,add=T,lwd=2,col="red")
#save image now: "d1000.pdf"

---------------
orcounty.lags=nblag(orcounty.knn2,2)
plot(orcounty)
plot(orcounty.lags[[2]],orcounty.centers, add=T,lwd=3,col="green",lty=2)
#save image now: "orcounty.lag2.pdf"

---------------
w.cols = 1:36
w.rows = 1:36
w.mat.knn = nb2mat(orcounty.knn1, zero.policy=TRUE)
w.mat.knn
image(w.cols,w.rows,w.mat.knn,col=brewer.pal(3,"BuPu"))
#save image now: "knn1.matrix.pdf"

---------------
w.mat.dist = nb2mat(orcounty.dist.100, zero.policy=TRUE) image(w.cols,w.rows,w.mat.dist,col=brewer.pal(9,"PuRd"))
#save image now: "d100.matrix.pdf"

---------------
breaks = round(quantile(orcounty$MEDIANRENT))
colors = c("red","orange","yellow","green") plot(orcounty,col=colors[findInterval(orcounty$MEDIANRENT,breaks,all.inside=TRUE)])
#save image now: "orcounty.medianrent.pdf"

---------------
display.brewer.all() nclr = 4 plotclr = brewer.pal(nclr,"PuRd")
class = classIntervals(orcounty$MEDIANRENT,nclr,style="quantile")
colcode = findColours(class,plotclr)
plot(orcounty,col=colcode)
title(main="Median Rent in Oregon",sub="Quantiles")
#legend code not working:
##legend(71.5,35,legend=names(attr(colcode, "table")),fill=attr(colcode, "palette"), cex=0.75,bty="n")
#tried to alter parameters (guessing this is what I should do)
##legend(321.2,242.2,legend=names(attr(colcode, "table")),fill=attr(colcode, "palette"), cex=0.75,bty="n")
#didn't work, so just save image as is: "orcounty.medianrent.PuRd"

---------------
moran.plot(orcounty$MEDIANRENT,nb2listw(orcounty.dist.200),labels=orcounty$NAME)
#save image now: "orcounty.moran.pdf"

---------------
moran.test(orcounty$MEDIANRENT,nb2listw(orcounty.dist.200, style="W"))
