next up previous
Next: Space-time smoothing in R Up: Spatial and spatio-temporal data Previous: Image plots for displaying

Working with shape files and neighborhood structures in R

One can import and plot shapefiles in R using the spdep package. 
library(spdep) 
ccShape=readShapePoly('cbg00barncnty') # here cbg00barncnty is an ArcGIS shapefile 
plot(ccShape,add=T,lwd=lwd,border='grey') # ,col='lightgrey') # this assumes you've already plotted some data based on coordinates in the projection of the shapefile 
Or, to plot the regions in a new plot: 
plot(ccShape) # plot the regions
One can then manipulate the polygon information to extract neighborhood structure for use in CAR and SAR models 
ccNb=poly2nb(ccShape) # produces neighbor structure based on shared borders 
plot(ccNb,centroidCoords,add=TRUE) # add neighbor structure; for this you would need the centroid coordinates (not necessarily in the shapefile) 
ccListw=nb2listw(ccNb,style='B') # convert neighbor structure to neighbor list for use in spautolm() (be careful to make sure to specify the weights argument (the heteroscedasticity in the variances) if needed for legitimate CAR model  
plot(ccListw,centroidCoords,add=TRUE) # add neighbor structure; for this you would need the centroid coordinates (not necessarily in the shapefile) 
ccMat=nb2mat(ccNb,style='B') # can examine to see what the weight matrix looks like  
ccNumNeighbors=rowSums(ccMat) # calculate number of neighbors for each region

Keywords: shapefile, GIS, CAR, SAR, neighbor

Last modified: 12/20/09.


next up previous
Next: Space-time smoothing in R Up: Spatial and spatio-temporal data Previous: Image plots for displaying
Chris Paciorek 2012-01-21