1、载入相应的包文件
建议安装R3.3.3此包适用
install.packages("spdep")
library(spdep)
install.packages("spDataLarge")
library(spDataLarge)
2、读取需要处理的原始文件
文件放在什么位置就如何读
mydata=https://www.it610.com/article/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))
$productMoran I test under randomisationdata:X[[i]]
weights: mat2listw(gal.mat)Moran I statistic standard deviate = 2.3291,
p-value = 0.009928
alternative hypothesis: greater
sample estimates:
Moran I statisticExpectation
0.22887903-0.03333333
Variance
0.01267477 $assetsMoran I test under randomisationdata:X[[i]]
weights: mat2listw(gal.mat)Moran I statistic standard deviate = 2.3024,
p-value = 0.01066
alternative hypothesis: greater
sample estimates:
Moran I statisticExpectation
0.23222902-0.03333333
Variance
0.01330330 $laborsMoran I test under randomisationdata:X[[i]]
weights: mat2listw(gal.mat)Moran I statistic standard deviate = 1.4397,
p-value = 0.07498
alternative hypothesis: greater
sample estimates:
Moran I statisticExpectation
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 = https://www.it610.com/article/0.017
alternative hypothesis: greater> moran.plot(mydata$product,list=mat2listw(gal.mat),xlab="product",ylab="product.slag")
>
计算其空间滞后值
product.lag <- gal.mat %*% mydata$product【r语言|R中处理空间面板模型的包spdep的用法】5、计算局域空间自相关
product.lag
lisa = localmoran(mydata$product,mat2listw(gal.mat))
> lisa
IiE.IiVar.IiZ.IiPr(z > 0)
1-0.895039931 -0.03333333 0.41371853 -1.33969808 9.098282e-01
20.061274825 -0.03333333 0.194711390.21440401 4.151160e-01
3-0.072696996 -0.03333333 0.08520782 -0.13485150 5.536354e-01
40.106720593 -0.03333333 0.413718530.21774230 4.138150e-01
50.099568620 -0.03333333 0.267713770.25685972 3.986435e-01
6-0.313556199 -0.03333333 0.10085119 -0.88239505 8.112184e-01
70.104591260 -0.03333333 0.413718530.21443182 4.151052e-01
80.088169423 -0.03333333 0.267713770.23482849 4.071709e-01
90.410821690 -0.03333333 0.121709011.27313136 1.014857e-01
101.924724136 -0.03333333 0.194711394.43741204 4.552345e-06
11 -0.249562987 -0.03333333 0.19471139 -0.49002651 6.879424e-01
120.060841444 -0.03333333 0.150909960.24242402 4.042258e-01
130.022436213 -0.03333333 0.194711390.12638672 4.497129e-01
140.193035354 -0.03333333 0.267713770.43750296 3.308733e-01
153.663150857 -0.03333333 0.194711398.37708989 2.712620e-17
160.046699584 -0.03333333 0.121709010.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
191.283136603 -0.03333333 0.150909963.38884723 3.509355e-04
200.122279554 -0.03333333 0.851732800.16861409 4.330501e-01
210.209267893 -0.03333333 0.121709010.69539510 2.434039e-01
220.204469655 -0.03333333 0.121709010.68164137 2.477329e-01
23 -0.005677445 -0.03333333 0.121709010.07927317 4.684077e-01
24 -0.691333698 -0.03333333 0.26771377 -1.27171789 8.982633e-01
250.147522747 -0.03333333 0.194711390.40986179 3.409537e-01
260.181482071 -0.03333333 0.150909960.55297623 2.901398e-01
270.021717245 -0.03333333 0.267713770.10639630 4.576340e-01
280.369445450 -0.03333333 0.150909961.03683018 1.499075e-01
290.024374970 -0.03333333 0.085207820.19769632 4.216413e-01
300.100125941 -0.03333333 0.194711390.30244965 3.811547e-01
310.010844072 -0.03333333 0.413718530.06868276 4.726211e-01
推荐阅读
- r语言|手把手(R语言文本挖掘和词云可视化实践)
- R语言从入门到机器学习|R语言rename重命名dataframe的列名实战:rename重命名dataframe的列名(写错的列名不会被重命名)
- R下载安装,Linux版
- R|不同方法的正态性检验及R语言实现
- jiebaR - 中文分词
- 模型评估
- 信息增益率
- R - dplyr 包
- 《区域经济研究实用方法》---GeoDa在空间数据分析中的应用