我在使用R计算莫兰指数来检验空间自相关时遇到了问题。
我执行了以下步骤:
#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)
我对特定变量
prod_ser
在最终数据集data_total
中进行Moran I检验很感兴趣。我做了以下事情:
#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
现在,
data_total$prod_ser
的长度为2373,dnb60.listw
的长度为3。我认为主要问题是W
矩阵是使用包含25个区域的serbia
形状文件创建的,而prod_ser
变量是来自data_total
的企业级变量,有2373家企业(我猜应该对应于点数据,企业是点)。为什么合并数据集没有帮助?我需要做什么才能计算Moran I而不出现此错误?