[R-sig-Geo] How do I calculate neighborhood contiguity in R?

Roger Bivand Roger.Bivand at nhh.no
Fri Aug 29 18:48:37 CEST 2014


On Tue, 26 Aug 2014, Shanley Thompson wrote:

> Hello,
> My end goal is to calculate join counts, using the function
> joincounts.multi() from the R package spdep. I understand that first I need
> to calculate the nb object, then the spatial weights matrix, then I can do
> the join counts.
>
> I have a very large raster file (nrows = 19663, ncols = 34073), with cell
> size of 30 m, in integer format. I read it in using the raster() function
> from the Raster package.
>
> For the first part (creating a "nb" object), I have tried using the cell2nb
> file but it keeps crashing - a quick search tells me others have had the
> same problem due to large files.

Please note that "crash" means that R error-exits. I believe you mean that 
R ran out of memory, and it does this in different ways on different 
platforms. You didn't provide the output of sessionInfo(), so we don't 
know your platform. Obviously a neighbour object with almost 640 million 
observations, say with <=8 neighbours each, is itself a very large object. 
But this isn't the main problem.

>
> I read that one can use "dnearneigh" function in the spdep package instead,
> but I do not understand how to do so. Can someone provide a detailed
> example?  Alternatively, would it be better to convert my raster layer to a
> polygon layer then use the function poly2nb?
>
> I am relatively new to R so the simpler the better!
>

Step back and ask yourself whether the entities represented by 30x30m 
raster cells are actually entities (like botanical field plots) or are an 
artificial result of the arbitrary gridding of the scene by the 
instrument. I would argue that a:

RRRGG
RRGGG
RGGBB
GGBBB
GBBBB

grid is three entities, not 25, with the R G and B entities gridded 
inflating the degrees of freedom - and giving completely different sets of 
join counts. The only reason to believe the 30x30 grid is if the scale or 
footprint of the land cover classes (or whatever) that you are studying 
matches the grid resolution.

So the simple answer is: don't do join count on this kind of data unless 
the entities are suitable - even if there was a way (making the counts as 
a focal procedure in raster perhaps), the results would be spurious. You 
could also sample your scene in say 500x500 blocks, choosing a random SW 
cell to start. With a number of samples, you'd get a picture of the join 
counts that would not be far from the whole scene.

Hope this helps,

Roger

> Thank you so much!
>
>
> Shanley Thompson, MSc, PhD Candidate
> Department of Geography, University of Victoria, Canada
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list