1

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!

rafa.pereira
  • 13,251
  • 6
  • 71
  • 109
user3050574
  • 23
  • 1
  • 5
  • Are you sure you don't have missing values? what is the value of `sum(is.na(matrix))`? – agstudy Nov 29 '13 at 20:59
  • Another thing to check is whether or not additional rows where created during the importing process (it has happened to me before). – user1738753 Nov 29 '13 at 21:03
  • Thank you both for your responses. The value of sum(is.na(matrix)) is indeed a positive number (25634). I'm confused as to how this would have happened though since all I did was duplicate and horizontally stack vectors in order to get my full spatial weights matrix. Any hints on how to quickly get a weights matrix for such a large sample? I essentially have 22725 "items", and am manually classifying them as neighbors based on being part of a region. I had created individual vectors and used the Matlab repmat function to duplicate and stack. Any good ideas here? – user3050574 Nov 29 '13 at 23:13
  • There are two ideas that are coming to my mind of why you are experiencing this problem: 1. something goes wrong when you are importing the dataset i.e. the spatial weigth is imported not as numeric values but possibly as a boolean type (true/false). If that is the case you can convert values to 1 and 0 based on as.numeric() 2. There might be NA value because some of the points/grids/polygons (whatever it is) don't have neighbors. In other words, the way you created the spatial weight matrix created units with no neighbors. Not knowing your dataset these are two possible solutions. – user1738753 Nov 29 '13 at 23:45
  • Those are good points. What I did was a true/false type statement in excel to give each element a 0 or 1. I then resaved the spreadsheet so that they were all hard numbers so there would be no equation issue, and then saved it again as a text file for importing. When I open the text file, there seem to be only 0's and 1's. I'm now checking my matrix part by part for NA's, but no luck so far. All just 0s and 1s as expected. – user3050574 Nov 29 '13 at 23:50
  • I should also add then that it might be the chance that some items do not have neighbors, except as a neighbor with itself. Would this cause issues with the weights matrix function? – user3050574 Nov 29 '13 at 23:52

1 Answers1

2

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.

user1738753
  • 626
  • 4
  • 12
  • 19
  • 1
    Thank you so much! I have located my problem. Now, it is my understanding that mat2listw creates a row standardized weight matrix from a matrix that is currently just in binary form. Can you confirm that this is correct? And also, do I need to make my diagonal all zeros first or will the mat2listw function take care of that as well? – user3050574 Nov 30 '13 at 00:40
  • 1
    Yes that is correct. I would also convert all diagonals to zero since they are not neighbors of each other. – user1738753 Nov 30 '13 at 00:46
  • Perfect, that was my thought but I wasn't sure if the function would automatically do that for me? – user3050574 Nov 30 '13 at 00:49