温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh
作者: 袁子晴 (香港大学)
邮箱: yzq0612@foxmail.com
目录
地理学第一定律 (Tobler's first law of geography) 认为 “所有事物都与其他事物相关,并且近处的事物比远处的事物更相关”。空间计量模型通过引入空间权重矩阵来研究单元之间的关联方式和关联程度,以此反映地理学第一定律。
但是大量的经验事实和实证研究表明时间维度也是一个很重要的因素。我们可以随着时间的推移在不同时点上收集空间横截面数据,从而得到两种类型的时空数据:空间面板数据和混合空间截面数据。
空间面板数据是在一段时间内对空间单元 (Spatial Units) 进行追踪调查所得到的,即在不同的时间点上观测到的空间单元都是一样的,这意味着空间单元必须可以被重复观测。例如,在研究环境污染的影响因素时,我们可以收集每个国家在每年的
但是,有些情况下一个具体的空间单位在一段时间内仅能被观察一次,比如房屋交易、事故、犯罪、公司的成立/关闭等。这种情况下不同的时点上观测到的空间单元是不同的,从而得到混合空间截面数据。
空间自相关的特殊性在于其多向作用 (Multidirectional Effect),某一空间单元受到周围其它空间单元的影响,同时它也对周围其它空间单元产生影响,下图中的红色双向箭头描述了「空间维度的多向效应」。时间的自相关是单向性的,昨天的观测值可以影响到今天的观测值,但反之则不可,也就是时间惯性在方向上的单一性,下图中灰色单向箭头描述了「时间维度的单向效应」。
如果研究的数据集是混合截面数据,或者是由于缺失值导致的非平衡空间面板数据,这种情况下可以把上述数据集看做成一个混合的大截面,此时时空权重矩阵的维度不再是空间单元的个数,而是观测值的数目。
因此,在运用混合空间截面数据研究时空自回归模型 ( Spatio-Temporal Autoregressive Regression, STAR ) 时,既要考虑到空间维度的多向效应,也要兼顾时间维度的单向效应。
其中,空间滞后项为:
时间滞后项为:
矩阵形式可以写成:
其中,
一般地,将空间权重
上面之所以列出几种特殊情况下退化的时空权重矩阵,是为了可以将一般化的时空权重矩阵拆分如下:
该案例是基于 1991 年至 1996 年在一个虚构城市发生的房屋交易数据,该数据集中包含每笔交易的房屋坐标 (X, Y) 和交易时间。在利用该数据估计时空自回归模型 (STAR, spatio-temporal autoregressive model) 时,Stata 的外部命令 geo2xy
可以通过地图投影变换将地理经纬度转换为坐标。
第一步:进行数据预处理,包括主要解释变量和被解释变量的对数化处理、以及生成年份和组别的虚拟变量;
*Loading the data base
use "Transaction-STExample.dta"
*Generating temporal (continuous) variable
quietly generate date = 365*(year - 1990) + 31*(month - 1) + day
sort date /*Chronologically order data*/
*Generating coordinates in km (instead of meters)
*(eliminating scale problem for building weights)
quietly generate xc = XMTM/1000
quietly generate yc = YMTM/1000
*Generating dependent variable
quietly generate log_price = log(saleprice)
/*Generating independent variables*/
**Generating group of independent dummy variables
tab quality, gen(QIndex)
tab location, gen(Sfixed)
*Generating time fixed effects (annual dummy variables)
quietly generate d91 = (year==1991)
quietly generate d92 = (year==1992)
quietly generate d93 = (year==1993)
quietly generate d94 = (year==1994)
quietly generate d95 = (year==1995)
*Applying mathematical transformation on independent variables
quietly generate log_livearea = log(livearea)
quietly generate log_age = log(age)
虽然 Stata 的 spmatrix create
或 spmatrix
命令可以直接根据地理坐标创建空间权重矩阵,但是这些模块通常依赖于单一的空间关系范式,即距离的倒数。此外,该命令是基于循环函数的,较为耗费时间和内存。因此,Dubé 和 Legros 利用 Stata 中的 Mata 模块计算。该模块允许研究者在 Stata 中自定义地创建并导出生成的矩阵,从而用于空间自相关指数的计算或空间和时空自回归模型的估计。事实上,在时空建模中能够灵活地生成自己所需要的空间权重矩阵特别有用。
*Build spatio-temporally lagged variables
quietly generate WTprice = .
mata
/*1.导入用于计算的列向量 Import the main vectors used for calculations*/
N = st_nobs()
yn = st_data(.,"log_price") /*Vector of dependent variables*/
XC = st_data(.,"xc") /*Vector of X coordinates*/
YC = st_data(.,"yc") /*Vector of Y coordinates*/
TC = st_data(.,"date") /*Vector of temporal coordinates*/
/*2.生成空间和时间距离矩阵 Generate the spatial and temporal matrices*/
XX = (XC*J(N,1,1)') - (J(N,1,1)*XC') /*Distance between X coordinates*/
YY = (YC*J(N,1,1)') - (J(N,1,1)*YC') /*Distance between Y coordinates*/
ZZ = (TC*J(N,1,1)') - (J(N,1,1)*TC') /*Eucledian distance between transactions*/
DD = sqrt((XX:*XX) + (YY:*YY)) /*Distance matrix*/
SO = exp(-DD)-I(N) /*Spatial weight matrix (negative exponential transformation)*/
TT = exp(-abs(ZZ)) /*Temporal weight matrix (negative exponential transformation)*/
/*3.对于时空关系加以限制 Impose restrictions on spatial and temporal relations*/
DK = ((DD*J(N,1,1)):/N)*J(N,1,1)' /*Estimate & attribute to each observtion the mean distance of all observations*/
T1 = (ZZ:<=65):*(ZZ:>25) /*Define the temporal "past" window (in this case between 25 and 65 days)*/
/*4.生成空间和时空权重矩阵 Generating spatial and spatio-temporal weights*/
ST = (DD:<=DK):*SO /*Truncated spatial weights matrix*/
TU = T1:*TT /*Temporal weights within temporal "past" window*/
Wb = ST:*TU /*Lower triangular spatio-temporal weight matrix*/
/*5.行标准化 Standardizing spatio-temporal weight matrix*/
SL = (Wb*J(N,1,1))*J(N,1,1)' /*Row sums*/
WSb = Wb:/(SL + (SL:==0)) /*Row standardization*/
/*6.生成时空滞后项 Generating the spatiotemporally dynamic depedent variable*/
Wyn = WSb*yn /*Creating spatio-temporally lagged dependent variable (exogenous variable)*/
st_store(.,("WTprice"),(Wyn))
end
*Eliminating observations having no spatio-temporal neighbours
drop if WTprice==0
clear mata
代码解析:
:
) 的时候,包括加 (+
)、减 (-
)、乘 (*
)、除(/
) 在内的代数运算符可以被用于「元素对元素」的运算。根据二维平面上两点 *Building spatial block-diagonal weights matrix
mata
/*1. 导入用于计算的列向量 Import the main vectors used for calculations*/
N = st_nobs()
yn = st_data(.,"log_price") /*Vector of dependent variables*/
XC = st_data(.,"xc") /*Vector of X coordinates*/
YC = st_data(.,"yc") /*Vector of Y coordinates*/
TC = st_data(.,"date") /*Vector of temporal coordinates*/
/* 2. 生成空间和时间距离矩阵 Generate the spatial and temporal matrices*/
XX = (XC*J(N,1,1)') - (J(N,1,1)*XC') /*Distance between X coordinates*/
YY = (YC*J(N,1,1)') - (J(N,1,1)*YC') /*Distance abetween Y coordinates*/
ZZ = (TC*J(N,1,1)') - (J(N,1,1)*TC') /*Eucledian distance between transactions*/
DD = sqrt((XX:*XX) + (YY:*YY)) /*Distance matrix*/
SO = exp(-DD)-I(N) /*Spatial weight matrix (negative exponential transformation)*/
TT = exp(-abs(ZZ)) /*Temporal weight matrix (negative exponential transformation)*/
/* 3. 对于时空关系加以限制 Impose restrictions on spatial and temporal relations*/
DK = ((DD*J(N,1,1)):/N)*J(N,1,1)' /*Estimate & attribute to each observtion the mean distance of all observations*/
TO = (ZZ:<=25):*(ZZ:>=-15) /*Define the "present" temporal window or contemporaneous period*/
/*4. 构建当期的空间权重矩阵 Creating contemporaneous spatial weight matrix*/
TO = TO:*TT
Sb = SO:*TO
/*5. 行标准化 Row-standardizing the contemporaneous weight matrix*/
SL = (Sb*J(N,1,1))*J(N,1,1)' /*Row sums*/
SSb = Sb:/SL /*Row standardization*/
/*6. 导出 Exporting the contemporaneous weight matrix*/
st_matrix("Sb",Sb)
end
代码解析:
将上述生成的时空权重矩阵
*Importing spatial weights matrix in Stata
spmat putmatrix WST Sb, normalize(row) id(ID) replace
spmat note WST : "Spatial - block diagonal - row-standardized weights matrix"
spmat graph WST, blocks(10) /*Graphing the spatial relations of the weights matrix*/
*Generating macro for independent variables
global varX log_livearea log_age basement garage panoramicview QIndex2 QIndex3 bathroom
global varT d91-d95
global varS Sfixed1 Sfixed2 Sfixed4 Sfixed5
Stata 官方 spregress
命令可以估计 STAR 模型。具体地,采用广义空间最小二乘法对 STAR 模型进行估计 (generalized spatial two-stage least squares, GS2SLS),具体结果如下:
. spreg gs2sls log_price $varX $varT $varS WTprice, id(ID) dlmat(WST)
Spatial autoregressive model Number of obs = 7084
(GS2SLS estimates)
-------------------------------------------------------------------------------
log_price | Coef. Std. Err. z P>|z| [95% Conf. Interval]
--------------+----------------------------------------------------------------
log_price |
log_livearea | 1.128009 .008789 128.34 0.000 1.110783 1.145236
log_age | -.0540687 .0057479 -9.41 0.000 -.0653344 -.042803
basement | .0789563 .0087618 9.01 0.000 .0617834 .0961292
garage | .0314771 .0148944 2.11 0.035 .0022846 .0606696
panoramicview | .1619325 .023933 6.77 0.000 .1150248 .2088403
QIndex2 | .0472097 .0105297 4.48 0.000 .0265718 .0678476
QIndex3 | .0508522 .0097422 5.22 0.000 .0317578 .0699466
bathroom | .1025071 .008493 12.07 0.000 .0858611 .1191531
d91 | .2186493 .0134815 16.22 0.000 .1922261 .2450725
d92 | .2192898 .012974 16.90 0.000 .1938613 .2447184
d93 | .1438101 .0113244 12.70 0.000 .1216147 .1660055
d94 | .0668429 .010911 6.13 0.000 .0454577 .0882282
d95 | .0516173 .0104344 4.95 0.000 .0311662 .0720684
Sfixed1 | .0867594 .0255836 3.39 0.001 .0366164 .1369023
Sfixed2 | -.0756727 .0258302 -2.93 0.003 -.1262989 -.0250466
Sfixed4 | -.1624837 .00875 -18.57 0.000 -.1796334 -.1453339
Sfixed5 | -.2889483 .0118157 -24.45 0.000 -.3121067 -.26579
WTprice | .0773753 .0091718 8.44 0.000 .059399 .0953516
_cons | 5.293063 .1625815 32.56 0.000 4.974409 5.611717
--------------+----------------------------------------------------------------
lambda |
_cons | .1023655 .0121711 8.41 0.000 .0785105 .1262205
-------------------------------------------------------------------------------
可以看出,时空滞后项 WTprice 前面的系数估计值为 0.0773753,且统计上高度显著,表明动态空间溢出效应显著为正。空间自相关系数 lambda 为 0.1023655,且显著为正,表明空间溢出效应显著为正。
Dubé, J., & Legros, D. (2013). Dealing with spatial data pooled over time in statistical models. Letters in Spatial and Resource Sciences, 6(1), 1-18. -Link-
Dubé, J., & Legros, D. (2014). Spatial econometrics using microdata. John Wiley & Sons. -Link-
Dubé, J., & Legros, D. (2018). Decomposing and Interpreting Spatial Effects in Spatio-Temporal Analysis: Evidences for Spatial Data Pooled Over Time. In GeoComputational Analysis and Modeling of Regional Systems (pp. 373-394). Springer, Cham. -Link-
Thanos, S., Dubé, J., & Legros, D. (2016). Putting time into space: the temporal coherence of spatial applications in the housing market. Regional Science and Urban Economics, 58, 78-88.-Link-
Note: 产生如下推文列表的命令为:
lianxh 空间 mata, m
安装最新版lianxh
命令:
ssc install lianxh, replace
连享会-直播课 上线了!
http://lianxh.duanshu.com
免费公开课:
直击面板数据模型 - 连玉君,时长:1小时40分钟,课程主页 Stata 33 讲 - 连玉君, 每讲 15 分钟. Stata 小白的取经之路 - 龙志能,时长:2 小时,课程主页 部分直播课 课程资料下载 (PPT,dofiles等)
支持回看
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 | 因果推断, 空间计量,寒暑假班等 | |
⭕ 数据清洗系列 | 游万海 | 直播, 88 元,已上线 |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会学习群-常见问题解答汇总:
✨ https://gitee.com/arlionn/WD
New!
lianxh
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh