Skip to content
Prev 27716 / 29559 Next

poly2nb neighbour itself should be considered a neighbour

Can you manipulate the adjacency list structure to add `i` to each list
element vector?

eg using sample data from spdep:

make a neighbour structure:

 > colnn = poly2nb(columbus)

this is a list - so for example polygon 4 is next to:

 > colnn[[4]]
 [1] 2 3 5 8

2, 3, 5, and 8. It seems you want to include `4` in that vector. So run
this loop:

 > for(i in 1:length(colnn)){colnn[[i]]=as.integer(c(i,colnn[[i]]))}

 which produces a nb structure:

 > colnn
Neighbour list object:
Number of regions: 49
Number of nonzero links: 285
Percentage nonzero weights: 11.87005
Average number of links: 5.816327

compared with the original without self-links:
Neighbour list object:
Number of regions: 49
Number of nonzero links: 236
Percentage nonzero weights: 9.829238
Average number of links: 4.816327

If you want to go on to make a weights list object:
the neighbours are preserved:
List of 3
 $ style     : chr "W"
 $ neighbours:List of 49
  ..$ : int [1:3] 1 2 3
  ..$ : int [1:4] 2 1 3 4
  ..$ : int [1:5] 3 1 2 4 5
  ..$ : int [1:5] 4 2 3 5 8
...
$ weights   :List of 49
  ..$ : num [1:3] 0.333 0.333 0.333
  ..$ : num [1:4] 0.25 0.25 0.25 0.25
  ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
  ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2

So I think that's what you want...

Note carefully the use of `as.integer` here:

  for(i in 1:length(colnn)){colnn[[i]]=as.integer(c(i,colnn[[i]]))}

because there's code in `spdep` that expects these things to be stored as
integer and passes them to C code. Get this wrong when manipulating

 > xx = poly2nb(columbus)
 > xx
Neighbour list object:
Number of regions: 49
Number of nonzero links: 236
Percentage nonzero weights: 9.829238
Average number of links: 4.816327
 > xx[[1]] = c(1, xx[[1]])
 > xx
 Error in card(nb) :
  INTEGER() can only be applied to a 'integer', not a 'double'

so instead:
Neighbour list object:
Number of regions: 49
Number of nonzero links: 237
Percentage nonzero weights: 9.870887
Average number of links: 4.836735

works - I don't think its strictly necessary in the `for` loop because `i`
is an integer but belt and braces....

Barry
On Sun, Nov 3, 2019 at 7:12 PM Robert R <usercatch at outlook.com> wrote:

            

  
  
Message-ID: <CANVKczN6xn+USgu_KzhYpcFctnMbQYXkzraEoDQnS6MXpc1_-w@mail.gmail.com>
In-Reply-To: <VI1P190MB07688C3278CB943D1BE4C7E7B07C0@VI1P190MB0768.EURP190.PROD.OUTLOOK.COM>