[R-sig-Geo] Producing a conditional adjacency matrix

Rhys Dubin rhy@@dub|n @end|ng |rom @@nt@ox@@c@uk
Wed Oct 2 08:20:56 CEST 2019


Thank you so much Roger! Works like a charm.

Rhys

On Oct 1, 2019, at 3:38 PM, Roger Bivand <Roger.Bivand using nhh.no<mailto:Roger.Bivand using 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

From there use nb2listw() to generate weights if that is what you need. lapply() is always worth learning - do not use View() on objects that are not data.frames or matrices, it either tells nothing or is misleading.

Hope this clarifies,

Roger


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 Oct 1, 2019, at 1:22 PM, Roger Bivand <Roger.Bivand using nhh.no<mailto:Roger.Bivand using nhh.no>> wrote:

On Mon, 30 Sep 2019, Rhys Dubin wrote:

Hi all,

I have a question regarding matrices (produced using poy2nb in spdep).

I’m trying to produce an adjacency matrix from a GIS Polygon shapefile with about 500 individual polygons (queen or rook — it doesn’t really matter for now), but conditional on the value of the parent polygon.

For instance, if a polygon had an arbitrary value of 1, I want to return a list of all neighboring polygons that have a value of 2 or 3 (ie. ≠ 1). The same would go for a polygon with a value of 2 (ie. return all neighbors with a value of ≠ 2).

Is there a solution to this issue built into spdep, do I need to use a different package, or is it possible to accomplish this with a subsetting command?

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


Thanks!

Rhys


_______________________________________________
R-sig-Geo mailing list
R-sig-Geo using r-project.org<mailto:R-sig-Geo using 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; e-mail: Roger.Bivand using nhh.no<mailto:Roger.Bivand using nhh.no>
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en



--
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: Roger.Bivand using nhh.no<mailto:Roger.Bivand using nhh.no>
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list