[R-sig-Geo] creating a neighbour list

Roger Bivand Roger.Bivand at nhh.no
Thu Mar 22 19:41:08 CET 2007

On Thu, 22 Mar 2007, Elizabeth Boakes wrote:

> I think there is an easy answer to my questions, I'm just not very
> familiar with R.  Ultimately I want to perform a Moran's I test on a
> grid of binary data.  I'm having problems simply making a neighbour list
> though.  I've used cell2nb to make a neighbour list.  
> I have two questions.
> Firstly, if I use type="queen", I am assuming that each cell has 8
> neighbouring cells?  Is this correct and is there anyway of setting it
> to include the next layer or two layers of neighbouring cells, so that
> each cell would have 24 neighbouring cells, or 48 neighbouring cells?
> Secondly, once I have my neighbour list, I realise I have to convert it
> to a listw object in order to use it in moran.test.  Do I do this using
> nb2listw()?  I don't want to weight anything though so would it be
> possible for someone to tell me exactly what to type?!

The reasons for the many choices you have to make is that there simply are 
many choices, and the researcher, not the software, has to take them.

So, yes, cell2nb() will give you the neighbour list for the four nearest 
neighbour for type="rook", eight nearest for type="queen". For a grid with 
a cell size of 1, you get the same neighbour list with dnearneigh(), with 
d1=0 and d2=1.01, as type="rook". So to get further out than cell2nb(), 
use dnearneigh() with the appropriate d2= for your grid.

Please read the help page of nb2listw() carefully, you need to understand 
the style= argument, and possibly also the glist= argument, where 
different researchers have different needs.

But finally, you say that you have binary data, probably 0/1, for which
Moran's I is inappropriate. Please consider using a join count test, which
suits this kind of data. joincount.test() or joincount.multi() ought to be
OK, and reading the underlying Hopkins paper for the example background
and the section of Upton and Fingleton will help; there is a short
discussion in Fortin and Dale pp.  118-120. The results of join count
statistics typically vary for styles "B" and "W", so you may want to take
that into account ("B" privileges cells within the grid, "W" cells on the

> Sorry for asking such basic questions but I've been struggling with this
> all day....

Hope this helps,


> Thanks very much,
> Elizabeth Boakes
> Dr Elizabeth Boakes
> Postdoctoral Research Assistant
> Institute of Zoology
> Regent's Park
> London NW1 4RY
> tel: 020 7449 6621
> http://www.zoo.cam.ac.uk/ioz 
> Registered Charity no. 208728
> The Zoological Society of London is incorporated by Royal Charter
> Principal Office England. Company Number RC000749
> Registered address: 
> Regent?s Park, London, England NW1 4RY
> Registered Charity in England and Wales no. 208728
> _________________________________________________________________________
> This e-mail has been sent in confidence to the named address...{{dropped}}
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> 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