Using mat2listw function in R to create spatial weights matrix

拈花ヽ惹草 提交于 2019-12-01 11:43:42

问题


I am attempting to create a weights object in R with the mat2listw function. 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:

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. What am I missing?

I'd appreciate any advice. Thanks in advance for your help!


回答1:


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. Have you considered creating spatial weight matrix in r? Using dnearneigh or other function.



来源:https://stackoverflow.com/questions/20292761/using-mat2listw-function-in-r-to-create-spatial-weights-matrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!