【问题标题】:Issues with calculating moran I test for spatial autocorrelation计算 Moran I 测试空间自相关的问题
【发布时间】:2015-06-28 14:41:55
【问题描述】:

我在使用 R 计算空间自相关的 Moran I 检验时遇到问题。

我做了以下事情:

#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)

我对最终数据集data_total 中特定变量prod_ser 的计算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 shapefile 创建的,而 prod_ser 变量是一个公司-level 变量来自data_total,有 2373 家公司(我猜应该对应点数据,公司是点)。

为什么合并数据集没有帮助?如果没有此错误,我还需要做什么才能计算 Moran I?

【问题讨论】:

    标签: r


    【解决方案1】:

    嗯!您自己已经确定了问题所在。这正是您收到此类错误的原因。 'dnb60.listw' 基于数据集 serbia,并且由于您在 moran.test 公式中使用“数据总计”,因此您会遇到此类错误。您应该首先合并数据,然后根据合并的数据估计“dnb60.listw”,然后计算 Moran I 。你应该没事。

    附:我不是R专家,英语也不是我的第一语言,因此,如果有任何误解,我提前道歉:)

    【讨论】:

    • 我确实尝试过,但出现以下错误:knn1IT
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    相关资源
    最近更新 更多