简体   繁体   English

在R中使用mat2listw函数创建空间权重矩阵

[英]Using mat2listw function in R to create spatial weights matrix

I am attempting to create a weights object in R with the mat2listw function. 我正在尝试使用mat2listw函数在R中创建权重对象。 I have a very large spatial weights matrix (roughly 22,000x22,000) that was created in Excel and read into R, and I'm now trying to implement: 我有一个很大的空间权重矩阵(大约22,000x22,000),该矩阵是在Excel中创建并读入R的,现在我正在尝试实现:

library(spdep) 
SW=mat2listw(matrix) 

I am getting the following error: 我收到以下错误:

Error in if (any(x<0)) stop ("values in x cannot be negative"): missing 
value where TRUE/FALSE needed. 

What's going wrong here? 这是怎么了 My current matrix is all 0's and 1's, with no missing values and no negative elements. 我当前的矩阵是全0和1,没有缺失值和负元素。 What am I missing? 我想念什么?

I'd appreciate any advice. 我将不胜感激任何建议。 Thanks in advance for your help! 在此先感谢您的帮助!

Here is a simple test to your previous comment: 这是对您之前的评论的简单测试:

library(spdep)
m1 <-matrix(rbinom(100, 1, 0.5), ncol =10, nrow = 10) #create a random 10 * 10 matrix
m2 <- m1 # create a duplicate of the first matrix
m2[5,4] <- NA # assign an NA value in the second matrix
SW <- mat2listw(m1) # create weight list matrix
SW2 <- mat2listw(m2) # create weight list matrix

The first matrix one does not fail, but the second matrix does. 第一个矩阵不会失败,但是第二个矩阵会失败。 The real question is now why your weight matrix is created containing NAs. 现在真正的问题是,为什么要创建包含NA的权重矩阵。 Have you considered creating spatial weight matrix in r? 您是否考虑过在r中创建空间权重矩阵? Using dnearneigh or other function. 使用dnearneigh或其他功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM