本文介绍了空间权重:非对称邻接矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个邻接矩阵,以在R中进行空间分析.数据均为美国大陆上的县.我从美国人口普查虎文件中得到了县的空间多边形.

我能够创建邻居列表,并且它是对称的.但是,当我将其转换为邻接矩阵时,它不是对称的.这是一个问题,因为我的目标是使用ngspatial::autologistic运行空间自动物流模型,并且出现一个错误,我必须提供对称的二进制邻接矩阵.

这是我的R代码,用于创建邻接矩阵:

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

顺便说一句,我可以将splogit与该邻接矩阵一起使用而不会出现问题.我不是空间分析专家,所以我不能说我知道这些命令中正在发生什么.

解决方案

矩阵us2.adj是对称的.问题出在测试上.事实证明

isSymmetric(us2.adj)

使用all.equal(...)来测试矩阵与转置矩阵是否相等,并且all.equal(...)检查属性和值. nb2mat(...)创建一个行名称设置为多边形ID且未设置列名称的矩阵.因此,all.equal(...)返回FALSE,因此isSymmetric(...)也是如此.显然,autologistic(...)函数使用相同的测试.

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

简单的解决方案是将列名设置为行名,或将行名设置为NULL.

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

顺便说一句,我认为这个问题18个小时没有得到回答的原因是您没有提供指向shapefile的链接.如果您没有提供可复制的示例,则倾向于让成员忽略或否决该问题.有关说明,请参见此链接. >

I am creating an adjacency matrix to do spatial analysis in R. The data are all counties in the continental US. I've got the counties spatial polygons from US Census Tiger files.

I am able to create the neighbors list, and it is symmetric. But when I convert that to an adjacency matrix it is not symmetric. This is a problem because my goal is to run a spatial autologistic model using ngspatial::autologistic, and I get an error that I must supply a symmetric binary adjacency matrix.

Here is my R code to create the 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 am able to use splogit with this adjacency matrix without a problem. I'm no expert on spatial analysis, so I can't say I know what is going on within these commands.

解决方案

The matrix us2.adj is symmetric. The problem is with the test. It turns out that

isSymmetric(us2.adj)

uses all.equal(...) to test for equality of the matrix with it's transpose, and all.equal(...) checks the attributes as well as the values. nb2mat(...) creates a matrix with the row names set to the polygon IDs and the column names unset. So all.equal(...) returns FALSE and therefore so does isSymmetric(...). Evidently, the autologistic(...) function uses this 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

The simple solution is to either set the columns names to the row names, or set the row names to 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 that you did not provide a link to your shapefile. If you do not provide a reproducible example, the tendency is for members to ignore or downvote the question. See this link for instructions

这篇关于空间权重:非对称邻接矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 02:15