Skip to content
Prev 5875 / 29559 Next

nearest neighbour list from distance matrix, etc.

On Tue, 26 May 2009, Wakefield, Ewan wrote:

            
No, you cannot pass it to for example dnearneigh() in spdep. You would 
need to construct an "nb" neighbours object manually, something like:

library(spdep)
example(columbus)
col_d <- as.matrix(dist(coordinates(columbus)))
# create a distance matrix
nb_d062 <- dnearneigh(coordinates(columbus), 0, 0.62)
# to set a benchmark example using the same coordinates
col_d062 <- apply(col_d, 1, function(x) which(x <= 0.62 & x > 0))
# extract the column indices for the same criteria, dropping self
any(sapply(col_d052, length) < 1)
which(sapply(col_d052, length) < 1)
# check that every entity included (case not treated here, but empty
# vector needs replacing with integer value of 0)
col_d062a <- lapply(col_d062, function(x) {names(x) <- NULL; x})
names(col_d062a) <- NULL
# remove index names
class(col_d062a) <- "nb"
attr(col_d062a, "region.id") <- as.character(1:nrow(col_d))
col_d062a
n.comp.nb(col_d062a)$nc
nb_d062
n.comp.nb(nb_d062)$nc

The neighbour objects are the same, both with two components. This case is 
for a single distance band, you could use different intervals in the 
selection command for alternative bands.
All observations ought to be included, but can form multiple graph 
components, so you most likely do not have to worry about this problem.

With regard to Moran correlograms, it could well be that including a 
couple of pairs of observations will make the estimated variance of the 
statistic very large, so it will not be significant. Note that 
correlograms also suffer from multiple comparisons, so adjustments may be 
required anyway. If only one or two bands have many pairs, why not go with 
those (or a single band and avoid adjustment)?

Hope this helps,

Roger

  
    
Message-ID: <alpine.LRH.2.00.0906061832030.8949@reclus.nhh.no>
In-Reply-To: <28C15D104A882F47954DEE56148152CC015A20AB99@nerckwmb1.ad.nerc.ac.uk>