[R-sig-Geo] creating an adjacency matrix in spdep

Roger Bivand Roger.Bivand at nhh.no
Tue Feb 1 20:06:40 CET 2011


On Tue, 1 Feb 2011, Stratford, Jeffrey wrote:

> Thanks Roger,  I did read through I just wanted to make sure. When I
> read "nearest neighbor" I think of the point closest to another point
> and I think I am looking for a matrix of distances between all points -
> so the two ideas (nearest neighbor and adjacency matrix) didn't jive in
> my head.  Do I have it right though - is the adjacency matrix a
> rectangular matrix of distances (or inverse distances) between points?

No, it is not a matrix. An nb object is a list of vectors of neighbour IDs 
(taken as integer IDs in j=1,N, j!=i); a listw object pairs an nb object 
with the matching spatial weights in the same configuration, and is very 
much like a sparse matrix representation. listw2mat() converts a listw 
object into a dense matrix with mostly zero entries; sparse matrix 
representations are also used in spdep.

By definition, an adjacency matrix is a sparse matrix representing the 
graph of edges representing adjacency relationships between nodes, and 
only in very odd circumstances would it be dense (all adjacent to all). 
You are setting the distance threshold too large. The example in 
?dnearneigh shows how you can use the nearest neighbour to set the 
threshold so that all observations are linked, which is most likely needed 
in WB.

> I was able to run the functions you mentioned but I don't get the matrix 
> I'm looking for.  Here's what I have so far:
>
> # creating and exporting an adjacency matrix
> library(spdep)
> sosp09 <- read.csv("g:\\sosp\\2009\\sosp2009c.csv", header=T)
> n <- nrow(sosp09)
> plot(sosp09$x, sosp09$y)
> coords <-cbind(sosp09$x, sosp09$y)
> coordinates(sosp09) <- c("x","y")
> # these are the USGS coordinates
> distan <- dnearneigh(coords, 0, 100000)

Distance-based neighbours are necessarily symmetric by definition, so the 
next step is superfluous.

Hope this helps,

Roger

> distan2 <- make.sym.nb(distan)
> plot(distan, coords)
>
> # When I pull up distan and distan2 I get a summary
>
> Neighbour list object:
> Number of regions: 53
> Number of nonzero links: 2740
> Percentage nonzero weights: 97.54361
> Average number of links: 51.69811
>
> Thanks the help!  I'm going through your spatial book and it's really
> good - lots of tips!
>
> -----Original Message-----
> From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
> Sent: Monday, January 31, 2011 3:30 AM
> To: Stratford, Jeffrey
> Cc: r-sig-geo at r-project.org
> Subject: Re: [R-sig-Geo] creating an adjacency matrix in spdep
>
> On Sun, 30 Jan 2011, Stratford, Jeffrey wrote:
>
>> Hi everyone,
>>
>>
>>
>> Is it possible to create an adjacency matrix required for
>> WinBUGS/GeoBUGS in spdep from a csv file containing UTM coordinates?
>>
>>
>>
>> I've gone through the spdep documentation and it looks like the
>> equivalent there is col.gal.nb?  Is that right?  I see where that
> object
>> is used but not how it was created.
>>
>
> There is plenty of documentation in spdep on the creation of nb objects
> if
> you look for it. For point support (your case), you can choose between
> dnearneigh() for distance-based neighbours, knn2nb(knearneigh()) for k
> nearest neighbours (will need making summetric with make.sym.nb() for
> WinBUGS), or the various graph2nb() variants (which may also need making
>
> symmetric). The case you refer to from ?nb2WB refers to ?columbus, which
>
> explains that col.gal.nb is an nb object imported from an original
> GAL-format file for compatibility with GeoDa - the example shows how,
> using read.gal(). You should also be able to read the *.gal or *.gwt
> files
> you made in GeoDa with read.gal() or read.gwt2nb(), then out again with
> nb2WB().
>
> Hope this helps,
>
> Roger
>
>>
>>
>> I also realize that I can get the same through GeoDa but I haven't
> been
>> able to figure out how to export the adj matrix from there.
>>
>>
>>
>> Many thanks,
>>
>>
>>
>> Jeff
>>
>>
>>
>> *****************************************
>>
>> Jeffrey A. Stratford, Ph.D.
>>
>> Department of Health and Biological Sciences
>>
>> 84 W. South St.
>>
>> Wilkes Univertsity, PA 18766
>>
>> 570-332-2942
>>
>> http://web.wilkes.edu/jeffrey.stratford/
>>
>> *****************************************
>>
>>
>>
>>
>> 	[[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list