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. 
ccShape=read.shape('cbg00barncnty')  
ccPoly=Map2poly(ccShape) # produces polygons  
plot(ccPoly) # plot the regions

One can then manipulate the polygon information to extract neighborhood structure for use in CAR and SAR models 
ccNb=poly2nb(ccPoly) # 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: 4/7/07.



Chris Paciorek 2008-12-14