利用STATA创建空间权重矩阵及空间杜宾模型计算命令Word下载.docx
《利用STATA创建空间权重矩阵及空间杜宾模型计算命令Word下载.docx》由会员分享,可在线阅读,更多相关《利用STATA创建空间权重矩阵及空间杜宾模型计算命令Word下载.docx(7页珍藏版)》请在冰点文库上搜索。
Createscentroidvariables
*genid(newvarname):
Createsuniqueidvariablefordatabase.dta
shp2dtausingCHN_adm1,database(chinaprovince)coordinates(coord)genid(id)gencentroids(c)
**绘制2016年中國GDP分布圖
*spmap:
Visualizationofspatialdata
*clnumber(#):
numberofclasses
*id(idvar):
basemappolygonidentifier(识别符,声明变量名,一般以字母或下划线开头,包含数字、字母、下划线)
*_2016GDP:
变量
*coord:
之前创建的坐标系数据集
spmap_2016GDPusingcoord,id(id)clnumber(5)
*更改变量名
renamex_clongitude
renamey_clatitude
**生成距离矩阵
*spmat:
用于定义与管理空间权重矩阵
*Spatial-weightingmatricesarestoredinspatial-weightingmatrixobjects(spmatobjects).
*spmatobjectscontainadditionalinformationaboutthedatausedinconstructingspatial-weightingmatrices.
*spmatobjectsareusedinfittingspatialmodels;
seespreg(ifinstalled)andspivreg(ifinstalled).
*idistance:
(产生距离矩阵)createanspmatobjectcontaininganinverse-distancematrixW
*或contiguity:
createanspmatobjectcontainingacontiguitymatrixW
*idistance_jingdu:
命名名称为“idistance_jingdu”的距離矩陣
*longitude:
使用经度
*latitude:
使用纬度
*id(id):
使用id
*dfunction(function[,miles]):
(设置计算距离方法)specifythedistancefunction.
*functionmaybeoneofeuclidean(default),dhaversine,rhaversine,ortheMinkowskidistanceoforderp,wherepisanintegergreaterthanorequalto1.
*normalize(row):
(行标准化)specifiesoneofthethreeavailablenormalizationtechniques:
row,minmax,andspectral.
*Inarow-normalizedmatrix,eachelementinrowiisdividedbythesumofrowi'
selements.
*Inaminmax-normalizedmatrix,eachelementisdividedbytheminimumofthelargestrowsumandcolumnsumofthematrix.
*Inaspectral-normalizedmatrix,eachelementisdividedbythemodulusofthelargesteigenvalueofthematrix.
spmatidistanceidistance_jingdulongitudelatitude,id(id)dfunction(euclidean)normalize(row)
**保存stata可读文件idistance_jingdu.spmat
spmatsaveidistance_jingduusingidistance_jingdu.spmat
**将刚刚保存的idistance_jingdu.spmat文件转化为txt文件
spmatexportidistance_jingduusingidistance_jingdu.txt
**生成相邻矩阵
spmatcontiguitycontiguity_jingduusingcoord,id(id)normalize(row)
spmatsavecontiguity_jingduusingcontiguity_jingdu.spmat
spmatexportcontiguity_jingduusingcontiguity_jingdu.txt
**计算Moran’sI
*安装spatwmat
*spatwmat:
用于定义空间权重矩阵
importsorgeneratesthespatialweightsmatricesrequiredbyspatgsa,spatlsa,spatdiag,andspatreg.
*Asanoption,spatwmatalsogeneratestheeigenvaluesmatrixrequiredbyspatreg.
*name(W):
读取空间权重矩阵W
使用生成的空间权重矩阵W
*xcoord:
x坐标
*ycoord:
y坐标
*band(08):
宽窗介绍
*band(numlist)isrequiredifoptionusingfilenameisnotspecified.
*Itspecifiesthelowerandupperboundsofthedistancebandwithinwhichlocationpairsmustbeconsidered"
neighbors"
(i.e.,spatiallycontiguous)
*and,therefore,assignedanonzerospatialweight.
*binary:
requeststhatabinaryweightsmatrixbegenerated.Tothisaim,allnonzerospatialweightsaresetto1.
spatwmat,name(W)xcoord(longitude)ycoord(latitude)band(08)
*安装绘制Moran’sI工具:
splagvar
*splagvar---Generatesspatiallylaggedvariables,constructstheMoranscatterplot,
*andcalculatesglobalMoran'
sIstatistics.
使用变量_2016GDP
*wname(W):
使用空间权重矩阵W
*indicatethenameofthespatialweightsmatrixtobeused
*wfrom(Stata):
indicatesourceofthespatialweightsmatrix
*wfrom(Stata|Mata)indicateswhetherthespatialweightsmatrixisaStatamatrixloadedinmemoryoraMatafilelocatedintheworkingdirectory.
*IfthespatialweightsmatrixhadbeencreatedusingspwmatrixitshouldexistasaStatamatrixorasaMatafile.
*moran(_2016GDP):
计算变量_2016GDP的Moran'
sI值
*plot(_2016GDP):
构建变量_2016GDPMoran散点图
splagvar_2016GDP,wname(W)wfrom(Stata)moran(_2016GDP)plot(_2016GDP)
**使用距离矩阵计算空间计量模型
cdD:
\软件学习软件资料\stata\stata指导书籍命令\陈强高级计量经济学及stata应用(第二版)全部数据
*使用product.dta数据集(陈强的高级计量经济学及其stata应用P594)
*将数据集product.dta存入当前工作路径
useproduct.dta,clear
*创建新变量,对原有部分变量取对数
genlngsp=log(gsp)
genlnpcap=log(pcap)
genlnpc=log(pc)
genlnemp=log(emp)
*将空间权重矩阵usaww.spat存入当前工作路径
spmatuseusawwusingusaww.spmat
*使用聚类稳健的标准误估计随机效应的SDM模型
xsmlelngsplnpcaplnpclnempunemp,wmat(usaww)model(sdm)robustnolog
*使用选择项durbin(lnemp),不选择不显著的变量,使用聚类稳健的标准误估计随机效应的SDM模型
xsmlelngsplnpcaplnpclnempunemp,wmat(usaww)model(sdm)durbin(lnemp)robustnolognoeffects
*使用选择项durbin(lnemp),不选择不显著的变量,使用聚类稳健的标准误估计固定效应的SDM模型
xsmlelngsplnpcaplnpclnempunemp,wmat(usaww)model(sdm)durbin(lnemp)robustnolognoeffectsfe
*存储随机效应和固定效应结果
quixsmlelngsplnpcaplnpclnempunemp,wmat(usaww)model(sdm)durbin(lnemp)r2nolognoeffectsre
eststore
quixsmlelngsplnpcaplnpclnempunemp,wmat(usaww)model(sdm)durbin(lnemp)r2nolognoeffectsfe
eststofe
*esttab:
将保存的结果汇总到一张表格中
*b(fmt):
specifyformatforpointestimates
*beta[(fmt)]:
displaybetacoefficientsinsteadofpointest'
s
*se[(fmt)]:
displaystandarderrorsinsteadoftstatistics
*star(*0.1**0.05***0.01):
标记不同显著性水平对应的P值
*r2|ar2|pr2[(fmt)]:
display(adjusted,pseudo)R-squared
*p[(fmt)]:
displayp-valuesinsteadoftstatistics
*label:
makeuseofvariablelabels
*title(string):
specifyatitleforthetable
esttabfere,bser2star(*0.1**0.05***0.01)
*hausman检验
*进行hausman检验前,回归中没有使用稳健标准误(没用“r”),
*是因为传统的豪斯曼检验建立在同方差的前提下
*constant:
includeestimatedinterceptsincomparison;
defaultistoexclude
*df(#):
use#degreesoffreedom
*sigmamore:
baseboth(co)variancematricesondisturbancevarianceestimatefromefficientestimator
*sigmaless:
baseboth(co)variancematricesondisturbancevarianceestimatefromconsistentestimator
hausmanfere
**有时我们还会得到负的chi2值,即chi2<
0,表明模型不能满足Hausman检验的渐近假设。
产生这些情况的原因可能有多种,
*但我认为一个主要的原因是我们的模型设定有问题,导致Hausman检验的基本假设得不到满足。
*这时,我们最好先对模型的设定进行分析,看看是否有遗漏变量的问题,或者某些变量是非平稳的等等。
*在确定模型的设定没有问题的情况下再进行Hausman检验,如果仍然拒绝原假设或是出现上面的问题,
*那么我们就认为随机效应模型的基本假设(个体效应与解释变量不相关)得不到满足。
*此时,需要采用工具变量法或是使用固定效应模型。
*连玉君(论文(2014):
Hausman检验统计量有效性的MonteCarlo模拟分析)
*研究了hausman检验统计量的小样本性质,结果表明,
*内生性问题(解释变量与个体效应相关)是导致hausman统计量出现负值的主要原因,
*进一步分析表明,修正后的hausman统计量,以及过度识别检验方法能够很好地克服上述缺陷,
*且具有很好的有限样本性质。
*陈强(高级计量经济学及其stata应用P153)介绍工具变量法与豪斯曼的stata命令及实例
quireglwiqsexprtenurernssmsa
eststools
quiivregress2slslwsexprtenurernssmsa(iq=medkww)
eststoiv
hausmanivols,constantsigmamore
*由于传统的豪斯曼检验在异方差的情形下不成立,故进行异方差稳健的DWH检验,
estatendogenous
*使用ivreg2检验选择的工具变量是否为内生解释变量
ivreg2lwsexprtenurernssmsa(iq=medkww),rendog(iq)
*endog(iq)表示检验变量iq是否为内生变量
*若果存在异方差,则GMM比2SLS更有效率,故进行最优GMM估计
ivregressgmmlwsexprtenurernssmsa(iq=medkww)
*进行过度识别检验
estatoverid
*若P值不显著,则认为所有工具变量均为外生。
*接下来考虑迭代GMM
ivregressgmmlwsexprtenurernssmsa(iq=medkww),igmm
*(迭代GMM与两步GMM的系数估计值相差无几)