I’m working on a spatial cross section model with R-programming-language. I’m running these codes which refers to anselin data (spatial_columbus) in spdep package. I found them from these links:

Main Page for more information


library(spdep)# Loading required package: sp &Loading required package: Matrix
xy<-cbind(mydata$X, mydata$Y)

When I used these codes for data (columbus) in spdep package, everything is fine, but when I want apply them to my data (human capital index) I’m not able to define neighborhood for my data . My problem is in this line ( neighbor<-col.gal.nb). it’s the neighbors list from an original GAL-format file and it’s a pre-defined file . I don’t know any codes for creating neighbors to my data and I think my data framework is different from spatial Columbus data framework (There is AREA, PERIMETER,ID and polyID columns that I don’t have these columns in my data framework).

The help link for col.gal.nb is here (you can open this link in R environment after above codes)

My questions are here:

  1. How can I use ArcGIS to create AREA and PERIMETER to my data file? I have some counties as samples. Are any easier solution for finding these values or I should only use ArcGIS for this purpose? (In sample codes the author used ArcGIS to calculating these values).
  2. Which code pack can I use to creating neighbors based on contiguity and distance in R? My dependent variable is HCIand Y is latitude, X is longitude and other variables are independent variables.

My data set framework : http://www.mediafire.com/view/lf43qm1h5ptztt6/spatial_columbus.csv

Pre-defined framework : http://www.mediafire.com/view/ml76tbrf6p69vkf/human_capital_index.csv


  • A gal file is the neighbor file defined from GeoDa. It is freeware - so if you want you can open your areas in that software and create various spatial weights matrices, and then export them as gal files. I'm sure you can do it directly in R as well, but the GeoDa program is quite nice. (It is the same set of people writing spdep and GeoDa - hence why spdep can take gal files as the spatial weights.) – Andy W Sep 18 '14 at 16:09
  • 1
    @Andy W, It is a huge leap to assume that just because the R object name has "gal" in it that is was produced in GeoDa. There is no import statement in the example code that supports this assumption. And you are quite incorrect, GeoDa was developed by Luc Anselin, now at Arizona State whereas spdep is out of Rodger Bivand's lab at the NHH Norwegian school of Econometrics. – Jeffrey Evans Sep 18 '14 at 17:14
  • Thank you andy and Jeffrey for your comments. What is your idea Jeffrey about my problem? – user2991243 Sep 18 '14 at 17:17
  • 1
    @JeffreyEvans - sorry, I did not mean to insinuate Luc Anselin was the only author of spdep. For a tutorial on importing gal files see the workbook written by Luc Anselin, specifically read.gal. – Andy W Sep 18 '14 at 18:29
  • No, really Anselin is not an author of spdep. In the linked workshop material he uses R, sp and spdep as an augmentation to GeoDa, which has pretty been superseded by PySal. However, he did not develop the package. If you type citation("spdep") in R you will see the citation as: Roger Bivand (2014). spdep: Spatial dependence: weighting schemes, statistics and models. R package version 0.5-74 – Jeffrey Evans Sep 18 '14 at 18:42

It would be very helpful for you to read up on some R basics, particularity pertaining to sp class objects. A very good starting point would be Bivand's ASDAR book and the sp vignette. Here are some other related R spatial analysis introductory material.

As to your problem at hand. For one, you can easily create a variety of spatial weights matrices in R using the spdep package. If you are working with polygons then you want to use n-neighbor contingency and not distance. It is also quite unnecessary to use ArcGIS to calculate area and perimeter. I would however, highly recommend projecting your data into a "distance based" projection which can be accomplished using "spTransform" (requiring both sp and rgdal). You can use the "readOGR" function in rgdal to read a polygon shapefile. Here are examples that demonstrate some solutions to your questions:

    # Add packages and example data

    eire <- readOGR(system.file("etc/shapes", package="spdep")[1], "eire")
          plot( eire )

    # Create spatial weights matrix from polygon object
    knn <- knn2nb(knearneigh(coordinates(eire), k=4))
      all.linked <- max(unlist(nbdists(knn, coordinates(eire))))
        nb <- dnearneigh(coordinates(eire), 0, all.linked)
          colW <- nb2listw(nb, style="W")

    # Plot neighbor contingency 
      plot(eire, border="grey")
        plot(nb, coordinates(eire), add=TRUE)
            title("All neighbors (kNN=n-1)")
      plot(eire, border="grey")
        plot(knn, coordinates(eire), add=TRUE)
            title("Neighbor contingency (kNN=4)")       

    # Calculate polygon(s) area   
    sapply(slot(eire, "polygons"), slot, "area")

    # Calculate polygon(s) perimeter 
    perimeter <- function(x) {
      p <- vector()
          for(i in 1:length(x)) {
           px <- as(x[i,], "SpatialLines")
           p <- append(p, LineLength(as.matrix(coordinates(px)[[1]][[1]])))
        return( p )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.