set more off
clear all
set mem 4M
set matsize 11000
set maxvar 30000


* BRING IN EORA AND MERGE WITH CEPII GRAVITY DATA

use EORA_ALL_YEARS.dta, clear

merge iso_o iso_d using dist_cepii.dta
gen ldist=ln(dist)
keep if flow~=.
gen own=0
replace own=1 if iso_o==iso_d
replace contig=1 if own==1
replace comlang_off=1 if own==1
sort iso_o iso_d year
collapse (mean) flow contig comlang_off comlang_ethno colony comcol col45 curcol distcap distw distwces ldist own, by(iso_o iso_d year)
gen lflow=ln(2*flow)
*Here we multiply by 2 because we have collapsed by mean rather than sum
egen country_pair = group(iso_o iso_d)
egen iso_o_year=group(iso_o year)
egen iso_d_year=group(iso_d year)

tab iso_o_year, gen(d_source_year)
tab iso_d_year, gen(d_dest_year)


*First Column (standard gravity)
qui regress lflow ldist d_source_year* d_dest_year*, noconstant cluster(country_pair)
outreg2 using  ElasticityEvidenceAllYears, keep(ldist) dec(3) replace

*Second Column (standard gravity with more controls)
qui regress lflow ldist contig comlang_off own d_source_year* d_dest_year* , noconstant cluster(country_pair)
outreg2 using  ElasticityEvidenceAllYears, keep(ldist contig comlang_off own) dec(3)

* BRING IN EORA AND MERGE WITH CEPII GRAVITY DATA (WILL NOW DO FINAL GOODS AND INPUTS SEPARATELY)

use EORA_ALL_YEARS.dta, clear

merge iso_o iso_d using dist_cepii.dta
gen ldist=ln(dist)
keep if flow~=.
gen own=0
replace own=1 if iso_o==iso_d
replace contig=1 if own==1
replace comlang_off=1 if own==1
egen country_pair = group(iso_o iso_d)

egen iso_o_year=group(iso_o year)
egen iso_d_year=group(iso_d year)

tab iso_o_year, gen(d_source_year)
tab iso_d_year, gen(d_dest_year)


*Third Column (standard gravity for final goods)
qui regress lflow ldist d_source_year* d_dest_year* if input==0, noconstant cluster(country_pair)
outreg2 using  ElasticityEvidenceAllYears, keep(ldist) dec(3)

*Fourth Column (standard gravity for final goods with more controls)
qui regress lflow ldist contig comlang_off own d_source_year* d_dest_year* if input==0, noconstant cluster(country_pair)
outreg2 using  ElasticityEvidenceAllYears, keep(ldist contig comlang_off own) dec(3)


*Fifth Column (standard gravity for inputs)
qui regress lflow ldist d_source_year* d_dest_year* if input==1, noconstant cluster(country_pair)
outreg2 using  ElasticityEvidenceAllYears, keep(ldist) dec(3)

*Sixth Column (standard gravity for inputs with more controls)
qui regress lflow ldist contig comlang_off own d_source_year* d_dest_year*if input==1, noconstant cluster(country_pair)
outreg2 using  ElasticityEvidenceAllYears, keep(ldist contig comlang_off own) dec(3)
