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 randomisation
data: X[[i]]
weights: mat2listw(gal.mat)
Moran I statistic standard deviate = 2.3291,
p-value = 0.009928
alternative hypothesis: greater
sample estimates:
Moran I statistic Expectation
0.22887903 -0.03333333
Variance
0.01267477
$assets
Moran I test under randomisation
data: X[[i]]
weights: mat2listw(gal.mat)
Moran I statistic standard deviate = 2.3024,
p-value = 0.01066
alternative hypothesis: greater
sample estimates:
Moran I statistic Expectation
0.23222902 -0.03333333
Variance
0.01330330
$labors
Moran I test under randomisation
data: X[[i]]
weights: mat2listw(gal.mat)
Moran I statistic standard deviate = 1.4397,
p-value = 0.07498
alternative hypothesis: greater
sample 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 I
data: mydata$product
weights: mat2listw(gal.mat)
number of simulations + 1: 1000
statistic = 0.22888, observed rank = 983, p-value = 0.017
alternative 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-01
2 0.061274825 -0.03333333 0.19471139 0.21440401 4.151160e-01
3 -0.072696996 -0.03333333 0.08520782 -0.13485150 5.536354e-01
4 0.106720593 -0.03333333 0.41371853 0.21774230 4.138150e-01
5 0.099568620 -0.03333333 0.26771377 0.25685972 3.986435e-01
6 -0.313556199 -0.03333333 0.10085119 -0.88239505 8.112184e-01
7 0.104591260 -0.03333333 0.41371853 0.21443182 4.151052e-01
8 0.088169423 -0.03333333 0.26771377 0.23482849 4.071709e-01
9 0.410821690 -0.03333333 0.12170901 1.27313136 1.014857e-01
10 1.924724136 -0.03333333 0.19471139 4.43741204 4.552345e-06
11 -0.249562987 -0.03333333 0.19471139 -0.49002651 6.879424e-01
12 0.060841444 -0.03333333 0.15090996 0.24242402 4.042258e-01
13 0.022436213 -0.03333333 0.19471139 0.12638672 4.497129e-01
14 0.193035354 -0.03333333 0.26771377 0.43750296 3.308733e-01
15 3.663150857 -0.03333333 0.19471139 8.37708989 2.712620e-17
16 0.046699584 -0.03333333 0.12170901 0.22940733 4.092762e-01
17 -0.043668169 -0.03333333 0.10085119 -0.03254341 5.129807e-01
18 -0.089914742 -0.03333333 0.12170901 -0.16218564 5.644202e-01
19 1.283136603 -0.03333333 0.15090996 3.38884723 3.509355e-04
20 0.122279554 -0.03333333 0.85173280 0.16861409 4.330501e-01
21 0.209267893 -0.03333333 0.12170901 0.69539510 2.434039e-01
22 0.204469655 -0.03333333 0.12170901 0.68164137 2.477329e-01
23 -0.005677445 -0.03333333 0.12170901 0.07927317 4.684077e-01
24 -0.691333698 -0.03333333 0.26771377 -1.27171789 8.982633e-01
25 0.147522747 -0.03333333 0.19471139 0.40986179 3.409537e-01
26 0.181482071 -0.03333333 0.15090996 0.55297623 2.901398e-01
27 0.021717245 -0.03333333 0.26771377 0.10639630 4.576340e-01
28 0.369445450 -0.03333333 0.15090996 1.03683018 1.499075e-01
29 0.024374970 -0.03333333 0.08520782 0.19769632 4.216413e-01
30 0.100125941 -0.03333333 0.19471139 0.30244965 3.811547e-01
31 0.010844072 -0.03333333 0.41371853 0.06868276 4.726211e-01