Using mat2listw function in R to create spatial we

2019-02-20 16:09发布

问题:

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.



标签: r spatial spdep