空间权重矩阵与相关性检验(Stata)
1 空间相关性检验
cd "D:\Allcode\Stata\Spatial-Econometrics\chapter01"
*将权重数据转化为矩阵
spatwmat using data\testwm.dta,name(W) // 或者使用 matlist W
* 矩阵查看,这里为0-1矩阵
由于窗口限制,不能很好显示完整。
1.1 全局空间相关性检验
use data\test01.dta,clear
*变量描述
describe
/*
Contains data from data\test01.dta
Observations: 49
Variables: 6 21 Feb 2020 13:14
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Variable Storage Display Value
name type format label Variable label
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
id byte %8.0g Neighborhood id value
hoval float %9.0g Housing value (in $1,000)
income float %9.0g Household income (in $1,000)
crime float %9.0g Residential burglaries & vehicle thefts per 1,000 households
x float %9.0g x coordinate of centroid (in arbitrary digitizing units)
y float %9.0g y coordinate of centroid (in arbitrary digitizing units)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sorted by:
end of do-file
*/
数据集test01是关于美国俄亥俄州哥伦布市的截面数据,hoval是房价,income表示收入,crime为犯罪率。下面进行全局空间自相关检验
* 全局空间自相关检验(双边检验)_莫兰指数
spatgsa crime,weights(W) moran twotail
1.2 局部空间自相关检验
* 局部空间自相关检验(双边检验)_莫兰指数
spatlsa crime,weights(W) moran twotail
1.3 散点图
* 行标准化权重矩阵
spatwmat using data\testwm.dta,name(W1) standardize
* 可视化
spatlsa crime,weights(W1) moran graph(moran) symbol(n)
2 权重矩阵
2.1 截断距离权重矩阵
* 导入数据
use data\test02.dta,clear
部分数据结构
* 生产截断二进制距离矩阵(标准化)
spatwmat,name(W2) xcoord(x) ycoord(y) band(0 12) binary standardize
* 查看矩阵
matrix list W2
* 将矩阵W2转化为文本txt
* 可能无法识别mat2txt命令,需要 ssc install mat2txt
mat2txt,matrix(W2) saving(out\weight) replace
* 将weight.txt转化为excel格式
dataout using out\weight.txt,excel replace //ssc install dataout
转化的矩阵后缀不是xlsx,而是xml,此时用excel打开即可
2.2 反距离权重矩阵
**# 5 反距离权重矩阵
spwmatrix gecon y x,wname(weight1) wtype(inv)
* 查看反距离权重矩阵
matlist weight1
* 保存为txt文件
mat2txt,matrix(weight1) saving(out\weight1) replace
* 转化为excel数据
dataout using out\weight1.txt,excel
反二次方权重矩阵可以通过反距离权重相乘得到文章来源:https://www.toymoban.com/news/detail-454468.html
* 反二次距离矩阵
mat weight2 = hadamard(weight1,weight1)
matlist weight2
mat2txt,matrix(weight2) saving(out\weight2) replace
dataout using out\weight2.txt,excel
生成新的权重矩阵可以用来检验空间相关性和空间计量建模。文章来源地址https://www.toymoban.com/news/detail-454468.html
到了这里,关于空间权重矩阵与相关性检验(Stata)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!