美文网首页
【Stata】多期空间DID(原创)

【Stata】多期空间DID(原创)

作者: 一颗小柚子lyc | 来源:发表于2022-04-22 15:36 被阅读0次

所需要的数据:面板数据

DID的设置可以参照以前的文章,反正无论是两期DID还是多期DID,本质上就是时间节点的变化,这里主要介绍如何进行空间DID。

首先,为啥要进行空间DID,这肯定和我的被解释变量有关。我的被解释变量,Polyc,也就是每个城市的形态,在空间上存在正向自相关,所以我才用空间DID。要是因变量根本不存在空间关联,那也不需要空间计量。

注意:由于是空间计量,所以我们需要空间权重矩阵。我们面板数据只需要一年的空间权重矩阵就可以,把这个一年的空间权重矩阵用到所有年份。(因为地理距离,在短期内也不会改变。如果是经济距离可能需要每年不一样,这个我还没研究出来咋搞。)

所以等于说数据需要两个,一个是274个县级市×6年的面板数据(data.dta)

一个是其中一年的截面数据(2013.dta)

cd E:\stata\data(cd 后面是文件的位置,先设置工作环境)

use 2013.dta(用截面数据获取空间权重矩阵,这里的pointX和pointY是经纬度数据。空间权重矩阵的方法有很多,我用的是经纬度数据获取,也可以直接Stata导入shp文件获取)

spatwmat, name(W01) xcoord( POINT_X ) ycoord( POINT_Y ) band(0 12) binary standardize

band(0 12)指的是搜索范围,大家可以根据研究需求自己设置

binary standardize 是进行标准化设置

name(W01)是给权重矩阵取名W01

matlist W01 (进行权重矩阵的计算)

mat2txt,matrix(W01) saving(W2013)replace  //矩阵保存为txt文件,并取名W2013

dataout using W2013.txt,word excel  //矩阵转换为word和excel文件

用excel打开W2013.xml,并保存成xlsx格式。接着,把数据设置成矩阵,删除多余的东西。其中,我们需要在第一行加上变量的名称,这里我取名是a1~a274(因为我研究对象是273个地区),并删除掉最右边那一列的多余数据

处理前 处理后

然后就可以把处理好的W2013.xlsx导入Stata,并转换成W2013.dta格式,进行权重矩阵的计算啦

use W2013.dta

spmat dta W2013new a1-a274(然后,在stata中打开,a1-a274是所有的变量的变量名,注意W2013nwe是新的名字,可以换成任何的名字)

接着,储存W2013new(引号里是存储位置,新的spmat的名字叫W2013new,可以换成任何名字)

spmat save W2013new using "F:\Desktop\空间计量的论文\W2013new.spmat"

***建立截面空间权重矩阵                 

spatwmat using W.dta,name(W)

***构造空间面板矩阵               

list a1-a273

spcs2xt a*, matrix(W) time(8)                      8指的是你用了几年的数据

spatwmat using Wxt.dta,name(Wp)

spmat dta usaww12xt v1-v1644

spmat save usaww12xt using "F:\Desktop\空间计量的论文\Wp.spmat"

matrix list Wp

然后就可以打开面板数据进行空间DID的操作了

use data.dta(面板数据)

xtset FID time(使用xtset设置region和year(格式为xtset region year))

spmat use usaww11 using W2013new.spmat(生成空间权重矩阵,这是面板数据根据截面的spmat生成的空间权重矩阵,用的是刚刚的W2013new.spmat。usaww11是我们面板数据要用的空间权重矩阵的名字,可以自己设置成啥都行)

然后我们就可以进行各种空间计量的计算了

(1)空间杜宾模型(SDM)

xtset id year

spatwmat using W.dta,name(W) standardize

随机效应模型

xsmle y x a, model(sdm) wmat(W) type(both) nolog effects re

时间固定效应

xsmle y x a, model(sdm) wmat(W) type(time) nolog effects fe

个体固定效应

xsmle y x a, model(sdm) wmat(W) type(ind) nolog effects fe

双固定效应xsmle y x a, model(sdm) wmat(W) type(both) nolog effects fe

(2)空间误差模型(SEM)

xtset id year

spatwmat using W.dta,name(W) standardize

随机效应模型

xsmle y x a, model(sem) emat(W) type(both) nolog effects re

时间固定效应

xsmle y x a, model(sem) emat(W) type(time) nolog effects fe

个体固定效应

xsmle y x a, model(sem) emat(W) type(ind) nolog effects fe

双固定效应

xsmle y x a, model(sem) emat(W) type(both) nolog effects fe

(3)空间滞后模型(SLM)

xtset id year

spatwmat using W.dta,name(W) standardize

随机效应模型

xsmle y x a, model(sar) wmat(W) type(both) nolog effects re

时间固定效应

xsmle y x a, model(sar) wmat(W) type(time) nolog effects fe

个体固定效应

xsmle y x a, model(sar) wmat(W) type(ind) nolog effects fe

双固定效应

xsmle y x a, model(sar) wmat(W) type(both) nolog effects fe

xsmle允许估计随机效应空间自回归、空间durbin和空间误差模型、固定效应空间自相关模型和广义空间随机效应模型

看看空间DID:

xsmle y sdid covariate,wmat(usaww) model(sdm) durbin(sdid ).set store SDMDID

xsmle y sdid covariate,wmat(usaww) model(sar) .set store SARDID

xsmle y sdid covariate,emat(usaww) model(sem) .set store SEMDID

xsmle y sdid covariate,wmat(usaww) emat(usaww) model(sac) .set store SACDID

相关文章

网友评论

      本文标题:【Stata】多期空间DID(原创)

      本文链接:https://www.haomeiwen.com/subject/axpwertx.html