我已经用spmat导出了空间逆距离(dstate)与空间邻近关系矩阵(cstate),但不知道怎么保存为dta格式,有用mata尝试但不成功,语句如下:
use usdb
. shp2dta using "D:\data\s_10nv15\s_10nv15", database(state) coordinates(statexy) genid(id) gencentroids(c)
. use state
spmat contiguity cstate using statexy, id (id) normalize (minmax)
warning: spatial-weighting matrix contains 4 islands
. spmat summarize cstate, links
Summary of spatial-weighting object cstate
-------------------------------------------
Matrix | Description
---------------+---------------------------
Dimensions | 53 x 53
Stored as | 53 x 53
Links |
total | 208
min | 0
mean | 3.924528
max | 8
-------------------------------------------
warning: spatial-weighting matrix contains 4 islands
. spmat idistance dstate longitude latitude, id (id) dfunction (dhaversine) normalize (minmax)
. spmat summarize dstate
Summary of spatial-weighting object dstate
-------------------------------------------
Matrix | Description
---------------+---------------------------
Dimensions | 53 x 53
Stored as | 53 x 53
Values |
min | 0
min>0 | .0007981
mean | .0104402
max | .1942416
-------------------------------------------
spmat getmat cstate matcstate, id(id)
. spmat getmat dstate matdstate, id(id)
mata describe的结果似乎显示两个权重矩阵已经存在:。
. mata
------------------------------------------------- mata (type end to exit) -------------------------------------------------------: mata describe
# bytes type name and extent
-------------------------------------------------------------------------------
8 struct scalar cstate
8 struct scalar dstate
8 struct scalar dstateUstd
424 real colvector id[53]
22,472 real matrix matcstate[53,53]
22,472 real matrix matdstate[53,53]
-------------------------------------------------------------------------------
Q1: 我尝试将这两个空间权重矩阵,以及相乘之后的新矩阵分别保存。但不知道是哪里出错,一直不成功。
: mate=matcstate*matdstate
: mata describe
# bytes type name and extent
-------------------------------------------------------------------------------
8 struct scalar cstate
8 struct scalar dstate
8 struct scalar dstateUstd
424 real colvector id[53]
22,472 real matrix matcstate[53,53]
22,472 real matrix matdstate[53,53]
22,472 real matrix mate[53,53]
-------------------------------------------------------------------------------
Q2: 想要显示已生成的三个矩阵,不知道应该用什么命令,尝试了st_view不对。
: st_view
<istmt>: 3499 st_view not found
r(3499);
保存不成功:
: mata matsave mate
matrix name required
r(100);
: mata matsave mate*
matrix name required
r(100);
: mata matdescribe mate
file mate.mmat not found
r(601);
: mata matuse mate
file mate.mmat not found
Q3: 如何将矩阵根据一个变量而融合进现有数据库?
由于我的目标是将两个矩阵相乘后的得到的矩阵导入我的数据库,对应上100个公司在10年间在50个州的发展数据(比如:分店数)。我想用分店数这个有100*10*50个observation的向量 乘以 空间邻近与逆距离矩阵的乘积。在这之前我需要将空间邻近与逆距离矩阵的乘积矩阵(mate)复制到每一个公司每一年对应的1*50的开店数向量旁边。而我并不知道这一步怎么做?我感觉类似merge。 因为这个乘积矩阵只因50个州(空间)而变,而不因年份和公司变化,所以想要一个类似merge 1:m stateid using ...这样的命令把矩阵merge上去,但不知道怎么实现?