天鹰(中南财大——博士研究生)
E-mail: [yanbinglh@163.com]
空间计量经济学其本质内涵依据的是地理学第一定律,也即两个体之间在地理距离上越相近,那么它们之间的相关性就越强。然而,学习空间计量经济学的第一步就是要构造空间权重矩阵,根据空间权重矩阵去进行后续的检验以及模型回归,因此,空间权重矩阵的构造显得越发基础和重要,它直接关乎模型结果的成败。很多同学习惯拿来主义,反而忽视了自己练手的机会,接下来本文重点演示常用的几种的空间权重矩阵的构建方法和注意事项:
- 邻接矩阵,也即0-1矩阵
- 反距离矩阵
- 地理距离的平方
- 经济距离矩阵
- 逆经济距离矩阵
上述目标矩阵的构建,多数基于个体的经纬度来实现,关于经纬度的数据,不管是省级层面还是地市级市经纬度数据均可以通过多种方法得到,本文不在赘述(具体数据来源见文末)。
本文主要基于省级层面数据来构造上述各种矩阵
1.邻接矩阵
其本质思想是基于两个个体在地理位置上是否相邻进行判断并进行赋值,如果相邻,则赋值1,不相邻则赋值0。
具体操作命令如下:
use shengji_jingwei.dta,clear / /导入经纬度数据
spwmatrix gecon y x, wn(w01) wtype(bin) dband(0 12) cart xport(w01,txt) row replace / /生成01矩阵
结果显示:
N.B.: Elements of one row of the weights matrix sum up to zero; weights matrix was not row-standardized
This row is:
30
You might want to rethink your weights structure criteria.
Use nearstat to obtain distance information and a neighbor count
for your distance-cutoff or distance band.
An alternative is to specify the noisland option to generate the spatial weights without the island observation:
Binary distance spatial weights matrix (30 x 30) calculated successfully and the following action(s) taken:
- Spatial weights matrix created as Stata object(s): w01.
- Spatial weights matrix saved to .txt file, C:\Users\天鹰\Desktop\12.23空间计量学习/w01.txt, for use with other Stata packages.
在结果呈现的时候,可以有不同的形式,包括:文本格式、excel、word以及spmat(注意:stata15该格式没法直接打开)
具体命令如下:
spmat import w01 using w01.txt,replace / /导出为txt格式
dataout using w01.txt, word excel replace / /矩阵转换为word和excel文件
spmat save w01 using w01.spmat,replace / /导出为spmat格式
spmat use w01 using w01.spmat,replace / /导入w01矩阵
同时,需要注意的是,后续空间计量模型的各种检验以及回归,需要的是dta格式文件,为满足后续要求,本文也进行了txt格式转dta格式的操作。
clear all
insheet using w01.txt, delimiter(" ")
*list, table clean noheader
save w01.dta, replace / /把txt文本文件保存为dta文件,留做后续回归使用
最终0-1矩阵呈现
v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31
1 0 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 1 0
2 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 1 0
3 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 1 0
4 1 1 1 0 1 1 0 0 1 1 1 1 0 1 1 1 1 1 0 0 0 1 1 0 0 1 1 0 1 0
5 1 1 1 1 0 1 0 0 0 1 0 1 0 0 1 1 1 0 0 0 0 1 0 0 0 1 1 0 1 0
6 1 1 1 1 1 0 1 1 1 1 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 1 1 1 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
10 1 1 1 1 1 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0
11 1 1 1 1 0 0 0 0 1 1 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
12 1 1 1 1 1 1 0 0 1 1 1 0 1 1 1 1 1 1 1 0 0 1 0 1 0 1 0 0 0 0
13 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2.反距离矩阵和地理矩阵平方的构建方法
其中(alpha = 1) 表示反距离,(alpha = 2) 表示反距离平方。
- ①反距离矩阵
spwmatrix gecon y x, wn(winv) wtype(inv) cart alpha(1) xport(winv,txt) row replace / / 生成反距离矩阵
结果输出在屏幕上
matlist winv / / *输出建立的矩阵* /
spmat import winv using winv.txt,replace / /导出为txt格式
spmat save winv using winv.spmat,replace / /导出为spmat格式
spmat use winv using winv.spmat,replace / /导入winv矩阵
clear all
insheet using winv.txt, delimiter(" ")
*list, table clean noheader
save winv.dta, replace / /把txt文本文件保存为dta文件,留做后续回归使用
- ②反距离平方矩阵
具体命令如下
spwmatrix gecon y x, wn(winvsq) wtype(inv) cart alpha(2) xport(winv,txt) row replace / /生成反距离平方矩阵
3.经济距离矩阵和逆经济距离矩阵平方的构建方法
- ①经济距离矩阵
具体命令如下:
spwmatrix gecon x y, wn(weco) wtype(econ) cart econvar(pgdp) xport(weco,txt) rowstand replace / /生成经济距离矩阵
注意:经济距离矩阵构造过程中由于蕴含了经济指标,而使得两个体之间的相似程度不在仅仅取决于地理位置,同时还取决于经济相近。具体在命令中体现为
econvar(pgdp) 本文在经纬度基础上添加的经济变量为人均GDP
wtype(econ)
- ②经济反距离矩阵
具体命令如下:
spwmatrix gecon x y, wn(winveco) wtype(invecon) cart econvar(pgdp) xport(winveco,txt) rowstand replace / /生成经济距离矩阵
和经济距离矩阵区别体现为:
wtype(invecon)
矩阵结果为:
v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31
1 0 .8754078 .1138751 .001618 .0031358 .0005528 7.77e-06 2.48e-07 .0001006 .0002292 4.36e-06 .000046 2.33e-07 7.01e-07 .0047585 .0002189 1.00e-05 3.24e-07 1.36e-08 9.62e-10 6.94e-12 4.32e-07 1.13e-08 1.36e-08 1.05e-10 .0000277 3.26e-07 1.86e-10 5.28e-06 5.99e-15
2 .9022794 0 .0754405 .0011504 .0012481 .0012037 .0000143 3.38e-07 .0001753 .0009076 .000016 .0001268 6.94e-07 1.70e-06 .0170579 .0003354 .000016 5.65e-07 3.11e-08 1.54e-09 1.48e-11 4.95e-07 9.37e-09 1.71e-08 1.10e-10 .0000216 1.89e-07 9.99e-11 2.95e-06 2.58e-15
3 .4291732 .2760048 0 .1505959 .0278411 .0043987 .0001911 .0000186 .0000404 .0009104 .000025 .0029752 3.30e-06 .0000511 .0671402 .0303705 .0011684 .0000661 1.67e-07 6.03e-08 1.04e-09 .0000475 8.04e-07 5.17e-07 4.26e-09 .007526 9.88e-06 1.73e-08 .001441 7.52e-13
4 .0045312 .0031275 .1119634 0 .0590568 .0000351 1.01e-06 1.14e-07 8.05e-06 .0001915 .0000114 .0077641 4.28e-06 .0005088 .0044057 .6756738 .0063517 .0007788 8.87e-07 2.43e-06 1.85e-08 .0007542 .0001533 .0000225 2.84e-07 .0986088 .0007046 4.21e-06 .0253357 2.40e-11
5 .0822245 .0317692 .1936757 .5528853 0 .0012774 .0000102 4.29e-07 9.59e-06 .0003773 .0000191 .0003615 .0002222 .0000141 .0650706 .0118382 .0011742 .0000299 2.97e-06 1.41e-07 4.36e-10 .0002421 .0000164 3.79e-06 6.92e-08 .035219 .0010001 9.91e-07 .022555 4.66e-11
6 .0188998 .0399519 .0398997 .0004282 .0016665 0 .6996732 .0069401 .0003542 .0029361 .000116 .0004072 .0000111 5.00e-06 .1884488 .0002289 .0000199 7.10e-07 9.82e-08 1.50e-09 4.41e-11 3.99e-07 3.50e-09 8.39e-09 4.40e-11 .0000108 5.52e-08 4.14e-11 1.33e-06 1.58e-15
7 .0001797 .0003215 .0011725 8.38e-06 8.96e-06 .4735466 0 .5242176 8.43e-06 .0000333 1.80e-06 9.20e-06 8.22e-08 1.29e-07 .0004855 4.44e-06 1.41e-06 1.73e-08 6.85e-10 2.83e-11 1.54e-12 2.84e-08 5.90e-11 1.19e-10 6.18e-13 3.64e-07 8.86e-10 9.99e-13 3.65e-08 6.07e-17
8 .0000108 .0000143 .0002158 1.77e-06 7.15e-07 .0088744 .9908704 0 1.62e-07 6.24e-07 3.06e-08 5.14e-07 1.20e-09 7.21e-09 9.49e-06 7.88e-07 1.24e-08 5.96e-09 1.14e-11 1.57e-12 1.99e-13 2.80e-10 7.87e-12 6.15e-12 3.92e-14 2.51e-08 1.19e-10 4.80e-13 5.27e-08 1.60e-16
9 .0013443 .0022746 .0001433 .0000385 4.89e-06 .0001385 4.87e-06 4.95e-08 0 .4474513 .4902973 .0367533 .0086312 .003086 .0086573 .0006984 .0003185 .0000876 .0000595 4.49e-07 5.67e-08 3.77e-06 1.89e-08 5.86e-07 2.02e-09 5.52e-06 2.73e-08 1.71e-11 2.05e-07 1.33e-16
10 .0015855 .0060948 .0016707 .000473 .0000995 .0005939 9.95e-06 9.87e-08 .2314713 0 .4223532 .1806388 .0050015 .0032532 .1381152 .0065718 .0017046 .0002012 .0000822 6.17e-07 2.86e-08 .0000193 1.01e-07 1.56e-06 5.61e-09 .0000557 2.28e-07 1.39e-10 2.20e-06 1.26e-15
11 .0000316 .0001127 .0000482 .0000295 5.29e-06 .0000246 5.65e-07 5.08e-09 .2663619 .4437749 0 .0528403 .2023475 .0256768 .004309 .0009656 .0011927 .0005937 .0016491 3.97e-06 6.80e-07 .0000176 7.01e-08 3.24e-06 1.19e-08 9.97e-06 4.69e-08 4.34e-11 3.12e-07 2.86e-16
12 .0001112 .0002974 .0019077 .0067037 .0000333 .0000288 9.61e-07 2.84e-08 .0066399 .0630919 .0175737 0 .0037758 .7169408 .0134439 .1417291 .020109 .0066252 .0001019 .000094 6.71e-07 .0002241 .0000208 .0000448 1.99e-07 .0004727 3.48e-06 4.53e-09 .0000252 2.30e-14
13 2.62e-06 7.59e-06 9.85e-06 .0000172 .0000968 3.64e-06 4.00e-08 3.11e-10 .0072739 .0081493 .3139634 .0176114 0 .3843463 .002566 .000878 .0052462 .0110798 .2481955 .0002083 .0000673 .0001874 7.03e-07 .0000661 3.14e-07 .0000216 1.36e-07 2.62e-10 6.32e-07 1.51e-15
14 1.69e-06 3.98e-06 .0000327 .000439 1.30e-06 3.53e-07 1.34e-08 3.98e-10 .0005571 .0011353 .0085328 .7130878 .0823365 0 .0001603 .0141788 .0260826 .1396944 .0088532 .0029945 .0000677 .000862 .0002419 .0005734 2.68e-06 .0001503 1.90e-06 7.05e-09 7.86e-06 2.52e-14
15 .0594635 .2069278 .2225844 .0196564 .0310439 .0688202 .0002622 2.71e-06 .0080907 .2495556 .007414 .0695365 .0028489 .0008296 0 .0473371 .0043531 .0001684 .0000327 3.68e-07 7.01e-09 .000074 5.05e-07 2.09e-06 9.85e-09 .0009369 3.23e-06 2.00e-09 .0000552 2.99e-14
16 .0004987 .0007419 .0183743 .5485615 .0010288 .0000153 4.38e-07 4.11e-08 .000119 .002165 .0003029 .1335935 .0001776 .0133742 .0086324 0 .1785498 .0385835 .0000315 .0000308 2.39e-06 .0043795 .0001203 .0001609 9.60e-07 .0463862 .0001236 1.60e-06 .0040433 5.52e-12
17 5.94e-06 9.19e-06 .0001837 .0013445 .0000266 3.46e-07 3.62e-08 1.69e-10 .0000141 .0001461 .0000974 .0049349 .0002761 .0064059 .0002066 .0464494 0 .0448057 .0001895 .0000793 6.15e-07 .8776565 .0000558 .0004897 2.14e-06 .016342 .0000233 4.96e-08 .0002545 3.70e-13
18 1.62e-06 2.73e-06 .0000875 .0013846 5.68e-06 1.04e-07 3.73e-09 6.82e-10 .0000327 .000145 .0004074 .0136637 .0049017 .2883187 .0000672 .0842291 .3768047 0 .0275055 .0327418 .0012635 .1189903 .0012464 .0414695 .0001672 .005522 .0000531 9.70e-07 .0009873 7.16e-11
19 3.86e-07 8.57e-07 1.25e-06 8.98e-06 3.21e-06 8.14e-08 8.39e-10 7.36e-12 .0001263 .000337 .0064418 .0011964 .624315 .1040371 .0000741 .0003924 .0090622 .1565176 0 .0749371 .0127442 .0033775 .0000311 .006272 .0000638 .000056 1.25e-06 8.10e-09 2.44e-06 5.01e-14
20 2.05e-08 3.18e-08 3.41e-07 .0000185 1.15e-07 9.33e-10 2.60e-11 7.63e-13 7.15e-07 1.90e-06 .0000116 .0008301 .0003937 .0264557 6.27e-07 .0002883 .0028475 .139926 .0562554 0 .0478589 .0171497 .0103011 .650237 .0472388 .0001254 .0000384 1.26e-06 .0000189 7.74e-12
21 2.31e-09 4.77e-09 9.21e-08 2.19e-06 5.54e-09 4.29e-10 2.22e-11 1.52e-12 1.41e-06 1.37e-06 .0000312 .0000923 .0019863 .0093203 1.87e-07 .000346 .0003451 .0845884 .1494931 .7474629 0 .0005286 .0000805 .0048182 .000894 6.32e-06 2.94e-07 1.19e-07 1.21e-06 1.41e-12
22 2.81e-07 3.12e-07 8.17e-06 .0001748 5.99e-06 7.58e-09 7.97e-10 4.15e-12 1.83e-07 1.81e-06 1.57e-06 .0000602 .0000108 .0002318 3.85e-06 .0012473 .9389737 .0154897 .0000773 .0005227 1.03e-06 0 .0047775 .016265 .0001516 .0205837 .0003477 2.38e-06 .0010607 1.58e-11
23 3.69e-07 2.96e-07 6.94e-06 .0017835 .0000203 3.33e-09 8.29e-11 5.86e-12 4.59e-08 4.74e-07 3.14e-07 .0002786 2.03e-06 .0032236 1.31e-06 .0017195 .0030653 .0081406 .0000356 .0157198 7.88e-06 .2395215 0 .1890864 .2089912 .0263222 .2109824 .0421246 .0489651 1.55e-07
24 1.04e-07 1.27e-07 1.05e-06 .0000615 1.11e-06 1.87e-09 3.92e-11 1.08e-12 3.35e-07 1.72e-06 3.41e-06 .0001418 .0000449 .0018162 1.28e-06 .0005399 .0063126 .0635977 .00169 .2334779 .0001107 .1915284 .0444291 0 .2168594 .0011868 .2379644 8.17e-06 .0002215 4.51e-11
25 2.81e-09 2.85e-09 3.03e-08 2.72e-06 7.07e-08 3.44e-11 7.15e-13 2.40e-14 4.05e-09 2.17e-08 4.38e-08 2.20e-06 7.45e-07 .0000297 2.11e-08 .0000113 .0000967 .0008979 .0000602 .0593968 .0000719 .0062491 .1719492 .7581707 0 .0000853 .0026671 .0002472 .000061 3.80e-09
26 .0000956 .0000725 .0068747 .1213731 .0046324 1.09e-06 5.43e-08 1.97e-09 1.42e-06 .0000277 4.73e-06 .0006746 6.60e-06 .0002147 .0002586 .0701672 .0951422 .0038177 6.81e-06 .0000203 6.55e-08 .1094605 .0027888 .0005354 .000011 0 .0176183 .0000235 .5661704 3.06e-10
27 2.68e-06 1.51e-06 .0000216 .0020712 .0003139 1.33e-08 3.15e-10 2.24e-11 1.68e-08 2.71e-07 5.31e-08 .0000119 9.92e-08 6.49e-06 2.12e-06 .0004466 .0003239 .0000877 3.63e-07 .0000149 7.27e-09 .004409 .0533853 .2824539 .0008215 .0420573 0 .004309 .6092586 3.80e-08
28 8.95e-08 4.66e-08 2.21e-06 .0007207 .0000182 5.82e-10 2.08e-11 5.29e-12 6.16e-10 9.67e-09 2.87e-09 9.01e-07 1.12e-08 1.40e-06 7.68e-08 .0003398 .0000402 .0000937 1.37e-07 .0000285 1.73e-07 .0017664 .6219616 .0005138 .0044404 .0032835 .2516995 0 .0907256 .0243631
29 .0000208 .0000113 .0015057 .035534 .0033816 1.53e-07 6.21e-09 4.71e-09 6.02e-08 1.25e-06 1.69e-07 .0000409 2.20e-07 .0000128 .0000174 .0069672 .0016882 .0007767 3.38e-07 3.49e-06 1.43e-08 .006426 .0059124 .0001139 8.96e-06 .6459229 .2909125 .0007409 0 2.65e-08
30 1.18e-10 4.95e-11 3.95e-09 1.69e-07 3.51e-08 9.12e-13 5.19e-14 7.27e-14 1.96e-13 3.61e-12 7.78e-13 1.88e-10 2.65e-12 2.06e-10 4.73e-11 4.77e-08 1.24e-08 2.90e-07 3.49e-11 7.19e-09 8.32e-11 4.81e-07 .0000942 1.17e-07 2.81e-06 1.76e-06 .0000913 .9996753 .0001335 0