其他
一文读懂空间计量及stata应用(附lr检验、空间可视化、权重矩阵、检验等)
version 14.0
*ssc install spmap
*ssc install shp2dta
*net install sg162, from(http://www.stata.com/stb/stb60)
*net install st0292, from(http://www.stata-journal.com/software/sj13-2)
*net install spwmatrix, from(http://fmwww.bc.edu/RePEc/bocode/s)
*net install splagvar, from(http://fmwww.bc.edu/RePEc/bocode/s)
*ssc install xsmle.pkg
*ssc install xtcsd
shp2dta using nuts2_164, database(data_shp) coordinates(coord) ///
genid(id) genc(c) replaceuse data_shp, clear
describe
spmap using coord, id(id) note("Europe, EU15")
drop if id==3|id==5|id==6|id==164|id==7|id==8|id==12|id==4|id==2|id==1|id==11|id==12
spmap using coord, id(id) note("Europe without Finland and Sweden, EU15")
clear all
import excel migr_unemp07_12.xls, firstrow
save migr_unemp.dta, replace
use migr_unemp
describe
use data_shp, clear
merge 1:1 POLY_ID using migr_unemp, gen(union) force
assert union==3
drop union
save migr_unemp_shp.dta, replace
use migr_unemp_shp.dta
format U2012 %12.1f
spmap U2012 using coord, id(id) clmethod(q) title("Unemployment rate") ///
legend(size(medium) position(5)) fcolor(Blues2) note("Europe, 2012" "Source: Eurostat")
format NM2012 %12.1f
spmap NM2012 using coord, id(id) clmethod(q) title("Net migration rate") ///
legend(size(medium) position(5)) fcolor(BuRd) note("Europe, 2012" "Source: Eurostat")
spmap U2012 using coord, id(id) clmethod(e) title("Unemployment rate") ///
legend(size(medium) position(5)) fcolor(Blues2) note("Europe, 2012" "Source: Eurostat")
spmap NM2012 using coord, id(id) clmethod(e) title("Net migration rate") ///
legend(size(medium) position(5)) fcolor(BuRd) note("Europe, 2012" "Source: Eurostat")
spmap U2012 using coord, id(id) clmethod(boxplot) title("Unemployment rate") ///
legend(size(medium) position(5)) fcolor(Heat) note("Europe, 2012" "Source: Eurostat")
spmap NM2012 using coord, id(id) clmethod(boxplot) title("Net migration rate") ///
legend(size(medium) position(5)) fcolor(Rainbow) note("Europe, 2012" "Source: Eurostat")
graph hbox U2012, asyvars ytitle("")
graph hbox NM2012, asyvars ytitle("")
spmap U2012 using coord, id(id) clmethod(s) title("Unemployment rate") ///
legend(size(medium) position(5)) fcolor(Blues2) note("Europe, 2012" "Source: Eurostat")
spmap NM2012 using coord, id(id) clmethod(s) title("Net migration rate") ///
legend(size(medium) position(5)) fcolor(BuRd) note("Europe, 2012" "Source: Eurostat")
spmap U2012 using coord, id(id) fcolor(RdYlBu) cln(8) point(data(migr_unemp_shp) xcoord(x_c) ///
ycoord(y_c) deviation(NM2012) sh(T) fcolor(dknavy) size(*0.3)) legend(size(medium) position(5)) legt(Unemployment) ///
note("Solid triangles indicate values over the mean of net-migration." "Europa, 2012. Source: Eurostat")
spmap NM2012 using coord, id(id) fcolor(RdYlBu) cln(8) diagram(var(U2012) xcoord(x_c) ycoord(y_c) ///
fcolor(gs2) size(1)) legend(size(medium) position(5)) legstyle(3) legt(Net migration) ///
note(" " "Boxes indicate values of unemployment." "Europe, 2012. Source: Eurostat")
spmat contiguity Wcontig using "coord.dta", id(id)
* Problem with conguity criterion: 5 islands.
* We choose k-nn: 5 nearest neighbours row-standardized
spwmatrix gecon y_c x_c, wn(W5st) knn(5) row con
* We need the spatial W as a SPMAT object:
* First, we generate W 5nn binary and then we export as txt
spwmatrix gecon y_c x_c, wn(W5bin) knn(5) xport(W5bin,txt) replace
* Read the txt file and to adapt format for SPMAT
insheet using "W5bin.txt", delim(" ") clear
drop in 1
rename v1 id
save "W5bin.dta", replace
* Generate SPMAT object: W5 row-standardize
spmat dta W5_st v*, id(id) norm(row)
spmat summarize W5_st, links
spmat graph W5_st
1. From .GAL file to Stata format //gal格式导入stata中
* spwmatrix import using matrix.gal, wname(W_geoda)
* 2. From .GWT file to Stata format //gwt格式导入stata中
* spmat import W_knn using knn.gwt, geoda
* 3. From SPMAT object to SPATWMAT object
* spmat export Wknn using "Wknn_noid.txt", noid replace
* insheet using "Wknn_noid.txt", delim(" ") clear
* drop in 1
* save "Wknn_noid.dta", replace
* spatwmat using "Wcont_noid.dta", name(Wks) standardize
Moran I test, Geary's c test and Getis-Ord G test.
use migr_unemp_shp.dta, clear
spatgsa U2012, w(W5st) moran geary two
spatgsa NM2012, w(W5st) moran geary two
* For Getis-Ord test we need a binary matrix:
* spatwmat using "W5bin_noid.dta", name(W5b)
* This binary matrix has been created previously by spwmatrix
spatgsa U2012, w(W5bin) go two
spatgsa NM2012, w(W5bin) go two
* Moran I scatterplot
splagvar U2012, wname(W5st) wfrom(Stata) ind(U2012) order(1) plot(U2012) moran(U2012)
splagvar NM2012, wname(W5st) wfrom(Stata) ind(NM2012) order(1) plot(NM2012) moran(NM2012)
* Local Moran I (LISA)
genmsp_v0 U2012, w(W5st)
graph twoway (scatter Wstd_U2012 std_U2012 if pval_U2012>=0.05, msymbol(i) mlabel ///
(id) mlabsize(*0.6) mlabpos(c)) (scatter Wstd_U2012 std_U2012 if pval_U2012<0.05, ///
msymbol(i) mlabel (id) mlabsize(*0.6) mlabpos(c) mlabcol(red)) (lfit Wstd_U2012 ///
std_U2012), yline(0, lpattern(--)) xline(0, lpattern(--)) xlabel(-1.5(1)4.5, ///
labsize(*0.8)) xtitle("{it:z}") ylabel(-1.5(1)3.5, angle(0) labsize(*0.8)) ///
ytitle("{it:Wz}") legend(off) scheme(s1color) title("Local Moran I of Unemployment rate")
spmap msp_U2012 using coord, id(id) clmethod(unique) title("Unemployment rate") ///
legend(size(medium) position(4)) ndl("No signif.") fcolor(blue red) ///
note("Europe, 2012" "Source: Eurostat")
OLS estimation
************************************************************************************
use migr_unemp_shp, clear
reg U2012 NM2012
* Spatial tests
spwmatrix gecon y_c x_c, wn(W5st) knn(5) row
spatdiag, weights(W5st)
************************************************************************************
* Spatial models using Maximum Likelihood (ML)
************************************************************************************
* Spatial Lag Model (SLM) with W5_st spmat object
spreg ml U2012 NM2012, id(id) dlmat(W5_st)
estimates store SLM_ml
* Spatial Error Model (SEM)
spreg ml U2012 NM2012, id(id) elmat(W5_st)
estimates store SEM_ml
* Spatial autoregressive SARAR model: combine SLM-SEM
spreg ml U2012 NM2012, id(id) dlmat(W5_st) elmat(W5_st)
estimates store SARAR_ml
* Spatial Durbin model (SDM)
spmat lag wx_NM2012 W5_st NM2012
spreg ml U2012 NM2012 wx_NM2012, id(id) dlmat(W5_st)
estimates store SDM_ml
* Selecting between SDM and SEM: LR_comfac
lrtest SDM_ml SEM_ml
estimates table SLM_ml SEM_ml SARAR_ml SDM_ml CLIFFORD_ml, b(%7.2f) star(0.1 0.05 0.01)
* Others alternative commands: "spmlreg" de Jeanty o "spatreg" de Pisati
************************************************************************************
* Spatial model using Instrumental Variables / Generalized method of moments(IV-GMM)
************************************************************************************
* Spatial Lag Model (SLM)
spivreg U2012 NM2012, dl(W5_st) id(id)
* SLM could be estimated using habitual commands in Stata
spmat lag wx_U2012 W5_st U2012
spmat lag wx2_NM2012 W5_st wx_NM2012
ivregress 2sls U2012 NM2012 (wx_U2012 = wx_NM2012 wx2_NM2012)
* Spatial Error Model (SEM)
spivreg U2012 NM2012, el(W5_st) id(id)
* SARAR Model
spivreg U2012 NM2012, dl(W5_st) el(W5_st) id(id)
* Spatial Durbin Model (SDM)
spivreg U2012 NM2012 wx_NM2012, dl(W5_st) id(id)
ereturn list
* Same result of SDM using ivregress:
spmat lag wx3_NM2012 W5_st wx2_NM2012
ivregress 2sls U2012 NM2012 wx_NM2012 (wx_U2012 = wx2_NM2012 wx3_NM2012)
* Cliff-Ord Model
spivreg U2012 NM2012 wx_NM2012, dl(W5_st) el(W5_st) id(id)
************************************************************************************
* Corrections for heteroskedasticity and endogeneity
* SLM assuming endogeneity in net migration and heteroskedasticity
spivreg U2012 NM2012 (NM2012 = NM2009), dl(W5_st) id(id) het
* SEM assuming endogeneity in net migration and heteroskedasticity
spivreg U2012 NM2012 (NM2012 = NM2009), el(W5_st) id(id) het
* SARAR with heteroskedasticity correction
spivreg U2012 NM2012, el(W5_st) dl(W5_st) id(id) het
* Alternative command: "spreg gs2sls"
************************************************************************************
* Interpretation of spatial estimation
************************************************************************************
* Remembering the SLM estimated under ML
use migr_unemp_shp, clear
spreg ml U2012 NM2012, dl(W5_st) id(id)
* Read W and betas in MATA language
spmat getmatrix W5_st W
mata:
b = st_matrix("e(b)")
b
lambda = b[1,3]
lambda
S = luinv(I(rows(W))-lambda*W)
end
* Using formulas of effects
* Total effects
mata: (b[1,1]/rows(W))*sum(S)
* Direct effects
mata: (b[1,1]/rows(W))*trace(S)
* Indirect effects (spatial spillovers)
mata: (b[1,1]/rows(W))*sum(S) - (b[1,1]/rows(W))*trace(S)
* Reshaping format: from wide to long format
reshape long NM U, i(id) j(year)
tsset id year
xtset id year
xtdes
************************************************************************************
* STATIC MODELS
************************************************************************************
************************************************************************************
* Spatial deteccion
xtreg U NM, fe
xtcsd, pes abs
xtreg U NM, re
xtcsd, pes abs
* Fixed Effects
* SLM model
xsmle U NM, fe wmat(W5_st) mod(sar) hausman
xsmle U NM, fe type(ind, leeyu) wmat(W5_st) mod(sar) effects
estimates store SLM_fe
* SEM model
xsmle U NM, fe emat(W5_st) mod(sem) hausman
estimates store SEM_fe
* SARAR model
xsmle U NM, fe wmat(W5_st) emat(W5_st) mod(sac) effects
estimates store SARAR_fe
* SDM model
xsmle U NM, fe type(ind) wmat(W5_st) mod(sdm) hausman effects
estimates store SDM_fe
* Common factor Test
testnl ([Wx]NM = -[Spatial]rho*[Main]NM)
estimates table SLM_fe SEM_fe SARAR_fe SDM_fe, b(%7.2f) star(0.1 0.05 0.01) statistics(aic)
* Random Effects
* SLM model
xsmle U NM, wmat(W5_st) mod(sar)
* SEM model
xsmle U NM, emat(W5_st) mod(sem)
* SDM model
xsmle U NM, wmat(W5_st) mod(sdm)
************************************************************************************
* DYNAMIC MODELS
************************************************************************************
************************************************************************************
* Detecting serial dependence
xtserial U NM
* dynSLM model (named as SAR)
xsmle U NM, dlag(1) fe wmat(W5_st) type(ind) mod(sar) effects nsim(499)
estimates store dynSLM_1
xsmle U NM, dlag(2) fe wmat(W5_st) type(ind) mod(sar) effects nsim(499)
estimates store dynSLM_2
xsmle U NM, dlag(3) fe wmat(W5_st) type(ind) mod(sar) effects nsim(499)
estimates store dynSLM_3
estimates table dynSLM_1 dynSLM_2 dynSLM_3, b(%7.2f) star(0.1 0.05 0.01) statistics(aic)
* dynSDM model
*xsmle U NM, dlag(1) fe wmat(W5_st) type(ind) mod(sdm) effects nsim(499)
*estimates store dynSDM_1
*xsmle U NM, dlag(2) fe wmat(W5_st) type(ind) mod(sdm) effects
*estimates store dynSDM_2
*xsmle U NM, dlag(3) fe wmat(W5_st) type(ind) mod(sdm) effects
*estimates store dynSDM_3
科研数据:
001 中国高速铁路线路&城市高铁开通数据002 地级市面板数据1990-2019003 上市公司数据集-慈善、股权、研发、审计、高管004 地级市高新技术企业统计情况2000-2019005 碳交易、碳排放(分行业、国家、省、市、县)006 2008-2018中国上市公司政治关联原始数据007 1936-2018年全国县级以上干部数据008 地级市市长市委书记数据库009 上市公司2006-2018年资产负债收益010 各县接收上山下乡知青数量
011 832国家级贫困县摘帽数据
学习资料:
计量统计:7种主流数据分析软件及经典教材推荐Stata数据清洗方法回归结果不显著可采取方法与思路面板数据汇总实证模型三步走:数据、模型、结果检验调节变量、中介变量、控制变量七种经典回归方法六种定量方法解决内生性问题(stata代码)Stata双重差分操作流程及代码交互项与异质性分析面板交互固定效应模型详解5种安慰剂检验方法详解DIDM:多期多个体倍分法案例及代码
中介效应检验程序、操作应用政策评估反事实框架及匹配方法开展政策效应评估传统PSM-DID模型改进与应用广义DID超强的政策评估工具中介效应分析的四种方式、原则、方法和应用Stata17中DID、DDD方法及使用策略DID的平行趋势检验步骤和程序
文本相似度计算及政策量化分析政策效应评估的四种主流方法详解
科研论文:经管类CSSCI南大核心来源期刊投稿方式综合社科高校学报CSSCI南大核心来源期刊投稿方式因果推断——现代统计的思想飞跃2020年中国经济学研究热点分析空间计量经济学文献综述陆铭的13个实证研究锦囊碳达峰和碳中和管理研究:进展与综述国内几篇A刊的发表经验陈强:计量经济学实证论文写作全解析刘修岩:城市经济学模型与实证方法进展与趋势刘俏:”碳中和“给经济学提出那些新问题洪永淼:大数据革命和中国经济学研究范式博士如何接受完整、全面的科研训练顶级经济学期刊青睐何种计量方法管理世界投稿经验:如何回应审稿人意见基于195篇实证论文发现期刊编辑的喜好CSSCI期刊主编:论文写作用词八条建议论文参考文献怎么引用才能通过查重给博士生论文投稿实用建议常任轨教职经济学学术刊物目录
洪永淼等:中国经济科学的研究现状与发展趋