【发布时间】: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