R中处理空间面板模型的包spdep的用法

来源:互联网 发布:人肉软件是什么 编辑:程序博客网 时间:2024/06/08 10:10

1、载入相应的包文件
建议安装R3.3.3此包适用

install.packages("spdep")library(spdep)install.packages("spDataLarge")library(spDataLarge)2、读取需要处理的原始文件文件放在什么位置就如何读

mydata=read.csv(“D:/regionResearchMethods/Ch4_R/industrialProduction.csv”,header = T)
3、如何使用空间权重矩阵
这是在geoda这个软件中已经做好的

setwd("D:/regionResearchMethods/Ch4_R")gal<-read.gal("Province31.gal")gwt<-read.gwt2nb("Province31.gwt")gal.mat<-nb2mat(gal)

4、计算全域空间自相关

 mydata <-read.csv("industrialProduction.csv")lapply(mydata[2:4],moran.test,listw = mat2listw(gal.mat))$product        Moran I test under randomisationdata:  X[[i]]  weights: mat2listw(gal.mat)  Moran I statistic standard deviate = 2.3291,p-value = 0.009928alternative hypothesis: greatersample estimates:Moran I statistic       Expectation        0.22887903       -0.03333333          Variance        0.01267477 $assets        Moran I test under randomisationdata:  X[[i]]  weights: mat2listw(gal.mat)  Moran I statistic standard deviate = 2.3024,p-value = 0.01066alternative hypothesis: greatersample estimates:Moran I statistic       Expectation        0.23222902       -0.03333333          Variance        0.01330330 $labors        Moran I test under randomisationdata:  X[[i]]  weights: mat2listw(gal.mat)  Moran I statistic standard deviate = 1.4397,p-value = 0.07498alternative hypothesis: greatersample estimates:Moran I statistic       Expectation        0.12428349       -0.03333333          Variance        0.01198569 > moran.mc(mydata$product,listw = mat2listw(gal.mat),nsim = 999)        Monte-Carlo simulation of Moran Idata:  mydata$product weights: mat2listw(gal.mat)  number of simulations + 1: 1000 statistic = 0.22888, observed rank = 983, p-value = 0.017alternative hypothesis: greater> moran.plot(mydata$product,list=mat2listw(gal.mat),xlab="product",ylab="product.slag")> 计算其空间滞后值

product.lag <- gal.mat %*% mydata$product
product.lag

5、计算局域空间自相关

 lisa = localmoran(mydata$product,mat2listw(gal.mat))> lisa             Ii        E.Ii     Var.Ii        Z.Ii    Pr(z > 0)1  -0.895039931 -0.03333333 0.41371853 -1.33969808 9.098282e-012   0.061274825 -0.03333333 0.19471139  0.21440401 4.151160e-013  -0.072696996 -0.03333333 0.08520782 -0.13485150 5.536354e-014   0.106720593 -0.03333333 0.41371853  0.21774230 4.138150e-015   0.099568620 -0.03333333 0.26771377  0.25685972 3.986435e-016  -0.313556199 -0.03333333 0.10085119 -0.88239505 8.112184e-017   0.104591260 -0.03333333 0.41371853  0.21443182 4.151052e-018   0.088169423 -0.03333333 0.26771377  0.23482849 4.071709e-019   0.410821690 -0.03333333 0.12170901  1.27313136 1.014857e-0110  1.924724136 -0.03333333 0.19471139  4.43741204 4.552345e-0611 -0.249562987 -0.03333333 0.19471139 -0.49002651 6.879424e-0112  0.060841444 -0.03333333 0.15090996  0.24242402 4.042258e-0113  0.022436213 -0.03333333 0.19471139  0.12638672 4.497129e-0114  0.193035354 -0.03333333 0.26771377  0.43750296 3.308733e-0115  3.663150857 -0.03333333 0.19471139  8.37708989 2.712620e-1716  0.046699584 -0.03333333 0.12170901  0.22940733 4.092762e-0117 -0.043668169 -0.03333333 0.10085119 -0.03254341 5.129807e-0118 -0.089914742 -0.03333333 0.12170901 -0.16218564 5.644202e-0119  1.283136603 -0.03333333 0.15090996  3.38884723 3.509355e-0420  0.122279554 -0.03333333 0.85173280  0.16861409 4.330501e-0121  0.209267893 -0.03333333 0.12170901  0.69539510 2.434039e-0122  0.204469655 -0.03333333 0.12170901  0.68164137 2.477329e-0123 -0.005677445 -0.03333333 0.12170901  0.07927317 4.684077e-0124 -0.691333698 -0.03333333 0.26771377 -1.27171789 8.982633e-0125  0.147522747 -0.03333333 0.19471139  0.40986179 3.409537e-0126  0.181482071 -0.03333333 0.15090996  0.55297623 2.901398e-0127  0.021717245 -0.03333333 0.26771377  0.10639630 4.576340e-0128  0.369445450 -0.03333333 0.15090996  1.03683018 1.499075e-0129  0.024374970 -0.03333333 0.08520782  0.19769632 4.216413e-0130  0.100125941 -0.03333333 0.19471139  0.30244965 3.811547e-0131  0.010844072 -0.03333333 0.41371853  0.06868276 4.726211e-01