clear
set more off

* Merge of CEPII gravity data with Penn World Tables for year 2002

use dist_cepii.dta, clear 
rename iso_d isocode
merge n:1 isocode using pwt_2002.dta
keep if _merge==3
keep isocode iso_o dis* rgdpe pop
keep if rgdpe~=. & pop~=.


* Construction of Centrality (GDP and Population Weighted)
egen sumgdp=sum(rgdpe), by(iso_o)
egen sumpop=sum(pop), by(iso_o)
gen centrality_gdp=(1/10000)*(rgdpe/dist)
gen centrality_pop=(pop/dist)

collapse (sum) centrality_gdp centrality_pop, by(iso_o)

rename iso_o iso_code

* Merge with data in Antras, Chor, Fally and Hillberry, AER P&P

merge iso_code using country_upstreamness.dta
rename iso_code isocode
sort isocode

drop _merge
sort isocode
merge isocode using country_characteristics_file.dta

rename exp_upstreamness_all export_upstreamness

* REGRESSIONS IN ACFH
keep if lrgdppc9605~=. & lK_L9605~=. & pc9605~=. &  rule9605~=. &  yr_sch~=.

reg  export_upstreamness lrgdppc9605 lK_L9605 pc9605 rule9605 yr_sch, robust
outreg2 using  RemotenessEvidence, dec(3) replace

reg  export_upstreamness lrgdppc9605 lK_L9605 pc9605 rule9605 yr_sch if centrality_gdp ~=., robust
outreg2 using  RemotenessEvidence, dec(3)

* ADDING CENTRALITY

reg  export_upstreamness  centrality_gdp lrgdppc9605 rule9605 pc9605 lK_L9605  yr_sch, robust
outreg2 using  RemotenessEvidence, dec(3)

reg  export_upstreamness  centrality_pop lrgdppc9605 rule9605 pc9605 lK_L9605  yr_sch, robust
outreg2 using  RemotenessEvidence, dec(3)


* ROBUSTNESS

reg  export_upstreamness  centrality_gdp lrgdppc9605, robust
outreg2 using  RemotenessEvidence, dec(3)

reg  export_upstreamness  centrality_pop lrgdppc9605, robust
outreg2 using  RemotenessEvidence, dec(3)

reg  export_upstreamness centrality_gdp, robust
outreg2 using  RemotenessEvidence, dec(3)

reg  export_upstreamness centrality_pop, robust
outreg2 using  RemotenessEvidence, dec(3)

* OUTPUT SHOULD APPEAR ON FILE RemotenessEvidence.txt
