Skip to content

Producing a conditional adjacency matrix

4 messages · Roger Bivand, Rhys Dubin

#
On Mon, 30 Sep 2019, Rhys Dubin wrote:

            
No built-in solution, but to make progress a clear, reproducible example 
is essential, otherwise we're guessing what you want. For example, add a 
doctored variable to the eire or columbus data sets to show your needs. Do 
you want a glist including (1) all neighbours j with a value of x not 
equal to x[i], and excluding (0) all with x[j] == x[i]?

Roger

  
    
#
Hi Roger,

Including a bit more detail below (my apologies if this is just complete gibberish ? first time posting to a forum like this):

First, I add a random ID variable to the columbus dataset indicating the variable I?d like to base my exclusions on in the final neighbor list:
In my case, that produced the following IDs for the first 4 polyids:

POLYID 1: 3
POLYID 2: 2
POLYID 3: 3
POLYID 4: 1

Second, col.ga.nb indicates that each of the previous POLYID?s has the following neighbors
POLYID 1 (w new assigned ID of 3): 2, 3
POLYID 2 (? ? 2): 1, 3, 4
POLYID 3 (? ? 3): 1, 2, 4, 5
POLYID 4 (? ? 1): 2, 3, 5, 8

Third (or brevity?s sake) looking only at POLYID 1 (with new ID = 3):

Its neighbors from col.ga.nb (POLYID 2 and 3) have ID values of 3 and 1 respectively

My end goal would be to produce a new list in which POLYID 1?s only neighbor is POLYID 3 (with an ID of 1). POLYID 2 would be excluded, as its ID value matches its parent. 

The same would be true of each subsequent polygon. 

In essence, the same format you mentioned at the end of your reply: a glist including all neighbors j with a value of x not equal to x[i], and excluding all with x[j] == x[i].

Best,

Rhys
#
On Tue, 1 Oct 2019, Rhys Dubin wrote:

            
Leave col.gal.nb as it is, and generate a list of general weights:
Loading required package: sp
Loading required package: spData
Loading required package: sf
Linking to GEOS 3.7.2, GDAL 3.0.1, PROJ 6.2.0
[1] "bbs"        "col.gal.nb" "columbus"   "coords"     "polys"
[1] 2 3
[1] 1
[1] 3 1 # second entry equal
as.integer(columbus$ID[i] != columbus$ID[col.gal.nb[[i]]]))
[1] 1 0 # second weight zero
#
Thank you so much Roger! Works like a charm.

Rhys
On Oct 1, 2019, at 3:38 PM, Roger Bivand <Roger.Bivand at nhh.no<mailto:Roger.Bivand at nhh.no>> wrote:

        
On Tue, 1 Oct 2019, Rhys Dubin wrote:
Hi Roger,

Including a bit more detail below (my apologies if this is just complete gibberish ? first time posting to a forum like this):

First, I add a random ID variable to the columbus dataset indicating the variable I?d like to base my exclusions on in the final neighbor list:
columbus$ID <- sample(1:3, nrow(columbus), replace=T)

In my case, that produced the following IDs for the first 4 polyids:

POLYID 1: 3
POLYID 2: 2
POLYID 3: 3
POLYID 4: 1

Second, col.ga.nb indicates that each of the previous POLYID?s has the following neighbors

View(col.gal.nb)

POLYID 1 (w new assigned ID of 3): 2, 3
POLYID 2 (? ? 2): 1, 3, 4
POLYID 3 (? ? 3): 1, 2, 4, 5
POLYID 4 (? ? 1): 2, 3, 5, 8

Third (or brevity?s sake) looking only at POLYID 1 (with new ID = 3):

Its neighbors from col.ga.nb (POLYID 2 and 3) have ID values of 3 and 1 respectively

My end goal would be to produce a new list in which POLYID 1?s only neighbor is POLYID 3 (with an ID of 1). POLYID 2 would be excluded, as its ID value matches its parent.

Leave col.gal.nb as it is, and generate a list of general weights:

library(spdep)
Loading required package: sp
Loading required package: spData
Loading required package: sf
Linking to GEOS 3.7.2, GDAL 3.0.1, PROJ 6.2.0
data(columbus, package="spData")
ls()
[1] "bbs"        "col.gal.nb" "columbus"   "coords"     "polys"
col.gal.nb[[1]]
[1] 2 3
set.seed(1)
columbus$ID <- sample(1:3, nrow(columbus), replace=T)
columbus$ID[1]
[1] 1
columbus$ID[col.gal.nb[[1]]]
[1] 3 1 # second entry equal
glist <- lapply(seq(along=col.gal.nb), function(i)
 as.integer(columbus$ID[i] != columbus$ID[col.gal.nb[[i]]]))
glist[[1]]
[1] 1 0 # second weight zero