Spatial weights: asymmetric adjacency matrix?

I am creating an adjacency matrix for spatial analysis in R. The data is all counties in the continental US. I have spatial polygon counties from Census Tiger files from Census.

I can create a list of neighbors and it is symmetrical. But when I convert this to adjacency matrix, it is not symmetric. This is a problem because my goal is to run a spatial autologous model with ngspatial::autologistic

and I get an error that I must provide a symmetric binary adjacency matrix.

Here is my R code for creating an adjacency matrix:

us<-readShapeSpatial("County_2010Census_DP1.shp")
#Trim out counties outside of continental US
us2<-us[!substr(us$GEOID10,1,2)%in%c('02','60','66','78','15','72'),]
us2.nb = poly2nb(us2)
is.symmetric.nb(us2.nb) #Comes out true
us2.adj = nb2mat(us2.nb, style="B",zero.policy=F)
isSymmetric(us2.adj) #comes out false

      

As an aside, I can use splogit

with this adjacency matrix without issue. I am not an expert in spatial analysis, so I cannot say that I know what is happening inside these teams.

+3


source to share


1 answer


The matrix is us2.adj

symmetrical. The problem is testing. It turns out that

isSymmetric(us2.adj)

      

uses all.equal(...)

matrices with transposition to check equality, and all.equal(...)

checks attributes as well as values. nb2mat(...)

creates a matrix with row names specified for polygon IDs and no column names specified. Thus, it all.equal(...)

returns FALSE

, and therefore matters isSymmetric(...)

. The function autologistic(...)

is obviously using the same test.

us2.adj <- nb2mat(us2.nb, style="B",zero.policy=F)
isSymmetric(us2.adj)
# [1] FALSE
isSymmetric(us2.adj,check.attributes=FALSE)
# [1] TRUE

      



A simple solution is to either set column names for row names, or set row names in NULL

.

x <- us2.adj
colnames(x) <- rownames(x)
isSymmetric(x)
# [1] TRUE
y <- us2.adj
rownames(y) <- NULL
isSymmetric(y)
# [1] TRUE

      

BTW, I think the reason this question went unanswered for 18 hours is because you didn't provide a link to your shapefile. If you do not provide a reproducible example, then for members the question should be ignored or dropped. See this link for instructions

+2


source







All Articles