简体   繁体   中英

Issues with calculating moran I test for spatial autocorrelation

I have an issue with using R to calculate the Moran I test for spatial autocorrelation.

I did the following:

#I download all the appropriate libraries 

    library(maptools) 
    library(spdep) 
    library(splancs) 
    library(spatstat)
    library(pwt)

#i import my shapefile and I calculate the coordinates
serbia<-readShapePoly("SRB_adm1")
coords<-coordinates(serbia)


#i created a weigthed matrix using the above definition of neigbour(dnb60 object)
dnb60.listw<-nb2listw(dnb60,style="W", zero.policy=F)

#i import my dataset which contains around 500 variables and is a firm level dataset containing 2373 firms.
library(foreign)
statafile<-read.dta("path", missing.type = T, warn.missing.labels = T) 

#i combine the shapefile(serbia) with the imported dataset(statafile) and created file with coordiantes (new) using common variable ID_1(region code). My final dataset is data_total.
new<- cbind(coordinates(serbia), serbia$ID_1)
newdata <- data.frame(new)
names(newdata)<-c("X", "Y", "ID_1")
cis_08_10 <- merge(statafile, serbia, by="ID_1", all.x=T)
data_total<-merge(cis_08_10, newdata, by="ID_1",all.x=T)

I am interested in the calculated Moran I test for the specific variable prod_ser in the final dataset data_total .

I did the following:

#calculating Moran I test
moran.test(data_total$prod_ser, dnb60.listw, randomisation=F)
I get the following error: Error in moran.test(data_total$prod_ser, dnb60.listw, randomisation = F) : 
  objects of different length

Now, data_total$prod_ser has length 2373 and dnb60.listw has length 3. I think the main issue is that W matrix was created using the serbia shapefile containing 25 regions, whereas prod_ser variable is a firm-level variable from data_total having 2373 firms (which I guess should correspond to point-data, with firms being points).

Why didn't merging datasets help? What else do I need to do to calculate Moran I without this error?

Well!You have identified the problem yourself. That is exactly the reason why you are getting such error. The 'dnb60.listw' is based upon the data set serbia and since you are using 'data total' in the moran.test formula you are getting such error. You should first merge the data and then estimate the 'dnb60.listw' based on the merged data and then calculate the Moran I . You should be fine.

PS I am not an expert in R and english is also not my first language and therefore, I apologize in advance if there is any misunderstanding :)

The issue is that both datasets are different. What worked for me was:

  1. First, creating the W matrix.
nbk <- knn2nb(knearneigh(st_centroid(dnb60)))
  1. Second, converting this to a list:
dnb60.listw<-nb2listw(dnb60,style="W", zero.policy=F)
  1. Checking if all lengths are the same
length(dnb60) 
length(nbk)
length(dnb60.listw) 

Since I don't have your data, I can't reproduce the exact example, but if you run this part, you should get that all of these 3 lenghts are different. And they should be the same so you can run the Moran test

Solution? What I would do is run the merges first. Make sure all of your datasets have the same number of observations. Then, check this with the lengths I provided in step 3, and then, run the Moran test.

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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