空间杜宾模型stata代码 | 您所在的位置:网站首页 › 空间制作模型 › 空间杜宾模型stata代码 |
cd "C:\Users\Desktop\空间杜宾模型和检验以及结果解释\stata数据1"
空间杜宾模型和检验以及结果解释\
stata数据 //设为自己的工作路径 use data,clear ***一、变量的描述性统计 sum y x1 x2 x3 a1 a2 a3 a4 ***二、空间计量模型准备工作 gen lnx1 = ln(x1) gen lnx2 = ln(x2) gen lnx3 = ln(x3) replace lnx1 =0 if missing(lnx1) ***三、空间权重矩阵制作 spatwmat using W.dta, n(W) standardize matrix list W ***四、空间相关性检验 **Moran’ s I指数 *(1)计算全局莫兰指数 xtset id year spatwmat using W.dta, n(W) standardize
=============================!!!!!从preserve到restore务必视为一个整体,选中一起执行!!============ 1.方法1!!!! preserve keep if year==2004 //改变年份2004 spatgsa y,weights(W) moran geary twotail restore
forvalue i = 2004/2016{ preserve keep if year==`i' spatgsa y,weights(W) moran twotail //计算全局莫兰指数 restore }
2.方法2!!!(因为扩大了矩阵,所以可以同时对所有年份求全局莫兰指数) clear all use W,clear //W为矩阵名称 spcs2xt a1-a30,matrix(aaa)time(13) //扩大权重矩阵数量,因为W是30x30而样本有390组观测值,需要扩大13倍,此时会生成aaaxt.dta的390x390的权重矩阵 spatwmat using aaaxt,name(W) standardize use data, clear spatgsa y,weights(W) moran geary twotail
*(2)计算局部莫兰指数 use data,clear xtset id year spatwmat using W.dta, n(W) standardize preserve keep if year==2006 //改变年份2004 spatlsa y,weights(W) moran twotail //计算局部莫兰指数 restore
**moran散点图 *第一种方法 preserve keep if year==2004 //改变年份2004 splagvar y , wname(W) wfrom(Stata) moran(y) plot(y) restore *第二种方法 1.方法一 preserve keep if year==2016 //改变年份2019 spatlsa y,weights(W)moran graph(moran) symbol(id) id(pro) //显示地名 restore 2.方法二 clear all use W,clear //W为矩阵名称 spcs2xt a1-a30,matrix(aaa)time(13) //扩大权重矩阵数量,因为W是30x30而样本有390组观测值,需要扩大13倍,此时会生成aaaxt.dta的390x390的权重矩阵 spatwmat using aaaxt,name(W) standardize use data, clear spatlsa y,weights(W)moran graph(moran) symbol(id) id(pro)
***五、空间计量模型 **空间杜宾模型(SDM) use data,clear 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 **空间误差模型(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 **空间滞后模型(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
***六、相关检验 **6.1LM检验(只有LM检验需要扩大矩阵) clear all use data, clear use W //W为矩阵名称 spcs2xt a1-a30,matrix(aaa)time(13) //扩大权重矩阵数量,因为W是30x30而样本有390组观测值,需要扩大13倍,此时会生成aaaxt.dta的390x390的权重矩阵 spatwmat using aaaxt,name(W) clear use data //调用论文数据data xtset id year reg y x1 x2 x3 a1 a2 a3 a4 spatdiag,weights(W)
**6.2效应检验 use data,clear xtset id year spatwmat using W.dta, n(W) standardize xsmle y x1 x2 x3 a1 a2 a3 a4 , fe model(sdm) wmat(W) nolog noeffects type(ind) est store ind xsmle y x1 x2 x3 a1 a2 a3 a4 , fe model(sdm) wmat(W) nolog noeffects type(time) est store time xsmle y x1 x2 x3 a1 a2 a3 a4 , fe model(sdm) wmat(W) nolog noeffects type(both) est store both
lrtest both ind,df(10) //比较“双向”和“个体”效应 LR检验 lrtest both time,df(10) //比较“时间”和“双向”效应 LR检验 drop _est_ind _est_time _est_both
**6.3Hausman检验 xsmle y x1 x2 x3 a1 a2 a3 a4 , fe model(sdm) wmat(W) nolog noeffects type(both) est store fe xsmle y x1 x2 x3 a1 a2 a3 a4 , re model(sdm) wmat(W) nolog noeffects type(both) est store re hausman fe re
**6.4Wald检验 clear all use data spatwmat using W.dta,name(W) standardize xtset id year xsmle y x* a*, fe model(sdm) wmat(W) type(both) nolog noeffects //Wald Test for SAR test [Wx]x1 = [Wx]x2 = [Wx]x3 = [Wx]a1= [Wx]a2= [Wx]a3= [Wx]a4=0 //Wald Test for SEM testnl ([Wx]x1 = -[Spatial]rho*[Main]x1)([Wx]x2 = -[Spatial]rho*[Main]x2)([Wx]x3= -[Spatial]rho*[Main]x3) ([Wx]a1 = -[Spatial]rho*[Main]a1)([Wx]a2= -[Spatial]rho*[Main]a2) ([Wx]a3 = -[Spatial]rho*[Main]a3)([Wx]a4= -[Spatial]rho*[Main]a4) **6.5 LR检验 xsmle y x* a*, fe model(sdm) wmat(W) type(both) nolog noeffects est store sdm xsmle y x* a*, fe model(sar) wmat(W) type(both) nolog noeffects est store sar xsmle y x* a*, fe model(sem) emat(W) type(both) nolog noeffects est store sem lrtest sdm sar //H0:空间杜宾模型可以简化为空间滞后模型(SAR) lrtest sdm sem //H0:空间杜宾模型可以简化为空间误差模型(SEM)
**七、空间杜宾模型结果解释 xsmle y x1 x2 x3 a1 a2 a3 a4 , fe model(sdm) wmat(W) nolog noeffects type(both) xsmle y x1 x2 x3 a1 a2 a3 a4 , fe model(sdm) wmat(W) nolog effects type(both) //效应分解了!
xsmle y x1 x2 x3 a1 a2 a3 a4 , fe model(sdm) wmat(W) nolog noeffects type(ind) est store ind xsmle y x1 x2 x3 a1 a2 a3 a4 , fe model(sdm) wmat(W) nolog noeffects type(time) est store time xsmle y x1 x2 x3 a1 a2 a3 a4 , fe model(sdm) wmat(W) nolog noeffects type(both) est store both **以下三行代码一起执行!!! local m " ind time both" esttab `m', mtitle(`m') nogap s(r2 N ) logout, save(Descriptive2) word replace: esttab `m', mtitle(`m') nogap s(r2 N ) //输出到word
**八、绘制地图(包含南海九段线的中国地图) cd C:\Users\52811\Desktop\空间杜宾模型和检验以及结果解释\中国地图 //根据自己情况调整,调整至工作目录中国地图 use china_province_data,clear
**从excel表格中,把变量y复制过来!!!! spmap y using "china_province_co",id(area_id) clnumber(3) title("中国地图") label(label(name) xcoord(xxx) ycoord(yyy) size(*.76)) fcolor(Reds) |
CopyRight 2018-2019 实验室设备网 版权所有 |