0

I am a beginner in R. I have a row standardized matrix (1542x1542) that I created in excel and saved as a .csv file. I am trying to use the matrix in R to calculate Moran's I. -using the following command:

# Weights Matrix Based on Connectivity
sw <- read.csv(file = "20210929_Weights_Matrix.csv")
sw.2.mat <- as.matrix(sw)

## mat to listw
mat2listw(sw.2.mat)
dnn.2.listw = nb2listw(sw.2.mat, zero.policy=T)

However, when I run the command I get the following errors

sw <- read.csv(file = "20210929_Weights_Matrix.csv")
sw.2.mat <- as.matrix(sw)
mat2listw(sw.2.mat) Error in mat2listw(sw.2.mat) : x must be a square matrix

dnn.2.listw = nb2listw(sw.2.mat, zero.policy=T) 
Error in nb2listw(sw.2.mat, zero.policy = T) : Not a neighbours list

When I try to add an additional row in excel, I get the following error in R

sw <- read.csv(file = "20210929_Weights_Matrix.csv")
sw.2.mat <- as.matrix(sw)
## mat to listw
mat2listw(sw.2.mat)

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

dnn.2.listw = nb2listw(sw.2.mat, zero.policy=T)

Error in nb2listw(sw.2.mat, zero.policy = T) : Not a neighbours list

Could someone please help? Is there a possibility I can share my excel?

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Grace
  • 1
  • 1
  • Check `dim(sw.2.mat)` to see how many rows and columns you have. The error is suggesting you don't have a 'square' matrix, with the same number of rows and columns. – thelatemail Dec 20 '21 at 23:13
  • Hi Andrew, Thank you for your help. The command returns [1] 1541 1542. Although in excel it is showing 1542 rows and 1542 columns. Why is R not reading the full matrix? even when I add a row full of zeros, I receive the second error "Error in if (any(x < 0)) stop("values in x cannot be negative") : missing value where TRUE/FALSE needed". Could you please help? – Grace Dec 20 '21 at 23:17
  • I'm not Andrew, but no worries. Try adding `header=FALSE` to your `read.csv()` call. Normally the first row of a csv is for column names, which I'm guessing you don't have. – thelatemail Dec 20 '21 at 23:21
  • My bad! I apologize @thelatemail. I added 'header=FALSE' but still this message shows 'Error in if (any(x < 0)) stop("values in x cannot be negative") : missing value where TRUE/FALSE needed'. I looked it up but I truly caanot find a solution. Any suggestions? – Grace Dec 20 '21 at 23:26
  • The error messages in R are helpful, even without knowing/seeing your data. The error is saying that you have a negative value in your matrix, which the function can't cope with. You've fixed one problem, now you need to eliminate or fix the negative values. – thelatemail Dec 20 '21 at 23:28
  • Thank you very much for your help. I checked but there are no negative values. I will check again. But is it possible that there is another problem? May I please share my data and code with you? – Grace Dec 20 '21 at 23:37
  • 1
    Are you absolutely sure? Try `any(sw.2.mat < 0)` and `which(sw2.mat < 0)` to check for sure in R. I can't help any further re: receiving data etc. You can always add example data to the question using `dput` or possibly a link to an external file. – thelatemail Dec 20 '21 at 23:40
  • Thank you very much. I tried "any(sw.2.mat<0)" and got the following[1] NA. Then "which(sw.2.mat<0)"and got integer(0) – Grace Dec 20 '21 at 23:48
  • `any(sw.2.mat < 0, na.rm=TRUE)` to remove `NA` – thelatemail Dec 20 '21 at 23:49
  • I got [1] FALSE and then tried to run the whole thing again the same error. – Grace Dec 20 '21 at 23:52
  • @thelatemail I even tried to look up further and tried the following command 'nona <- sw.2.mat[!is.na(sw.2.mat)]' still I receive the same error – Grace Dec 21 '21 at 00:09
  • 1
    [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with. – camille Dec 21 '21 at 01:51

0 Answers0