Skip to content
Prev 1368 / 29559 Next

distribution of distance for lags (spdep package)

On Tue, 3 Oct 2006, Horacio wrote:

            
You are on the right track. Here is an example:

data(columbus)
col.lags <- nblag(col.gal.nb, 4)
# to get a list of neighbour lists at increasing lags
ldists <- lapply(col.lags, function(x) nbdists(x, coords))
# to get a list of lists of distances
ldists1 <- lapply(ldists, unlist)
# to flatten the list to contain vectors of distances, as shown by:
str(ldists1)
# but note that each link is included, both i-j and j-i
sapply(ldists1, summary)
# for example will give a summary or more complicated:
dens <- lapply(ldists1, function(x) density(x, bw=0.1, from=0, to=3.1))
plot(c(0,3.1), c(0,max(sapply(dens, function(x) max(x$y)))), type="n", 
  xlab="distance", ylab="density")
lapply(1:4, function(i) lines(dens[[i]]$x, dens[[i]]$y, lty=i))

for a density plot.

Thanks for the question, I can see that summary.nb() needs a rewrite which 
would have made this a bit easier.

Roger